diff --git a/src/main/java/com/codesdream/ase/configure/ActivityFormConfigure.java b/src/main/java/com/codesdream/ase/configure/ActivityFormConfigure.java new file mode 100644 index 0000000..0d988bf --- /dev/null +++ b/src/main/java/com/codesdream/ase/configure/ActivityFormConfigure.java @@ -0,0 +1,39 @@ +package com.codesdream.ase.configure; + +import lombok.Data; +import org.springframework.stereotype.Component; + +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; + +@Component +public class ActivityFormConfigure { + + private static final Set stdActivityForm = new HashSet(){{ + add("title"); + add("creator"); + add("type"); + add("description"); + add("cycle"); + add("volunteers"); + add("participate"); + add("sign"); + add("visible"); + add("inform"); + add("start-time"); + add("remind"); + add("enclosure"); + add("chief-manager"); + add("assist-manager"); + add("attendance"); + }}; + public final static HashMap> stdActivityMap; + + static { + stdActivityMap = new HashMap() {{ + put("activity", stdActivityForm); + }}; + } +} diff --git a/src/main/java/com/codesdream/ase/controller/ActivityController.java b/src/main/java/com/codesdream/ase/controller/ActivityController.java new file mode 100644 index 0000000..6d732e5 --- /dev/null +++ b/src/main/java/com/codesdream/ase/controller/ActivityController.java @@ -0,0 +1,43 @@ +package com.codesdream.ase.controller; + +import com.codesdream.ase.component.ASESpringUtil; +import com.codesdream.ase.configure.ActivityFormConfigure; +import com.codesdream.ase.configure.AppConfigure; +import com.codesdream.ase.exception.InvalidFormFormatException; +import com.codesdream.ase.service.ActivityService; +import com.codesdream.ase.validator.WebFormValidator; +import org.springframework.http.HttpRequest; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestMapping; + +import javax.annotation.Resource; +import javax.servlet.http.HttpServletRequest; +import java.util.HashMap; +import java.util.Map; + +@Controller +public class ActivityController { + + @Resource + ActivityService activityService; + + @RequestMapping(value = "/activityCreator") + String activityCreatorView(Model model){return "activityCreator";} + + @PostMapping(value = "/activityCreator") + String activityCreator(Model model, HttpServletRequest request) throws InvalidFormFormatException { + + Map parameterMap = request.getParameterMap(); + ASESpringUtil aseSpringUtil = new ASESpringUtil(); + ActivityFormConfigure activityFormConfigure = aseSpringUtil.getBean(ActivityFormConfigure.class); + WebFormValidator webFormValidator = aseSpringUtil.getBean(WebFormValidator.class); + HashMap checkResult = webFormValidator.check(activityFormConfigure.stdActivityMap, parameterMap); + if(checkResult.get("activity").equals(false)){ + throw new InvalidFormFormatException("Invalid activity form."); + } + return "act_created"; + } +} + diff --git a/src/main/java/com/codesdream/ase/exception/InvalidFormFormatException.java b/src/main/java/com/codesdream/ase/exception/InvalidFormFormatException.java new file mode 100644 index 0000000..f688f5e --- /dev/null +++ b/src/main/java/com/codesdream/ase/exception/InvalidFormFormatException.java @@ -0,0 +1,21 @@ +package com.codesdream.ase.exception; + +import lombok.Data; +import lombok.EqualsAndHashCode; + +import javax.persistence.criteria.CriteriaBuilder; + +@EqualsAndHashCode(callSuper = true) +@Data +public class InvalidFormFormatException extends Throwable { + + private String message = ""; + + public InvalidFormFormatException(){ + super(); + } + + public InvalidFormFormatException(String message){ + this.message = message; + } +} diff --git a/src/main/java/com/codesdream/ase/model/activity/UserActivity.java b/src/main/java/com/codesdream/ase/model/activity/UserActivity.java index 8a06e1d..b1acab7 100644 --- a/src/main/java/com/codesdream/ase/model/activity/UserActivity.java +++ b/src/main/java/com/codesdream/ase/model/activity/UserActivity.java @@ -17,39 +17,39 @@ public class UserActivity { private int id; //关联的用户 - @OneToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "userActivity") + @OneToOne(cascade = {CascadeType.MERGE, CascadeType.DETACH, CascadeType.PERSIST}, fetch = FetchType.LAZY, mappedBy = "userActivity") @JoinColumn(nullable = false) private User user; //主要负责的活动 - @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY) + @OneToMany(cascade = {CascadeType.MERGE, CascadeType.DETACH, CascadeType.PERSIST}, fetch = FetchType.LAZY) private List manageActivities; //次要负责的活动 - @ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY) + @ManyToMany(cascade = {CascadeType.MERGE, CascadeType.DETACH, CascadeType.PERSIST}, fetch = FetchType.LAZY) private List assistActivities; //可见的活动 - @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY) + @OneToMany(cascade = {CascadeType.MERGE, CascadeType.DETACH, CascadeType.PERSIST}, fetch = FetchType.LAZY) private List visibleActivities; //创建的活动 - @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY) + @OneToMany(cascade = {CascadeType.MERGE, CascadeType.DETACH, CascadeType.PERSIST}, fetch = FetchType.LAZY) private List createdActivities; //可报名的活动 - @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY) + @OneToMany(cascade = {CascadeType.MERGE, CascadeType.DETACH, CascadeType.PERSIST}, fetch = FetchType.LAZY) private List signUpActivities; //已经参与的活动 - @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY) + @OneToMany(cascade = {CascadeType.MERGE, CascadeType.DETACH, CascadeType.PERSIST}, fetch = FetchType.LAZY) private List participatedActivities; //正在(将要)参与的活动 - @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY) + @OneToMany(cascade = {CascadeType.MERGE, CascadeType.DETACH, CascadeType.PERSIST}, fetch = FetchType.LAZY) private List participatingActivities; //打卡签到活动 - @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY) + @OneToMany(cascade = {CascadeType.MERGE, CascadeType.DETACH, CascadeType.PERSIST}, fetch = FetchType.LAZY) private List clockIn; } diff --git a/src/main/java/com/codesdream/ase/service/ActivityService.java b/src/main/java/com/codesdream/ase/service/ActivityService.java index 3166f03..e91941e 100644 --- a/src/main/java/com/codesdream/ase/service/ActivityService.java +++ b/src/main/java/com/codesdream/ase/service/ActivityService.java @@ -45,4 +45,9 @@ public class ActivityService implements IActivityService { return activityRepository.save(activity); } + @Override + public Activity createActivity(Activity activity) { + return activityRepository.save(activity); + } + } diff --git a/src/main/java/com/codesdream/ase/service/IActivityService.java b/src/main/java/com/codesdream/ase/service/IActivityService.java index d35c7c1..18a3476 100644 --- a/src/main/java/com/codesdream/ase/service/IActivityService.java +++ b/src/main/java/com/codesdream/ase/service/IActivityService.java @@ -24,4 +24,7 @@ public interface IActivityService { //活动信息更新 Activity update(Activity activity); + //活动创建 + Activity createActivity(Activity activity); + } diff --git a/src/main/java/com/codesdream/ase/validator/WebFormValidator.java b/src/main/java/com/codesdream/ase/validator/WebFormValidator.java new file mode 100644 index 0000000..59ce3ec --- /dev/null +++ b/src/main/java/com/codesdream/ase/validator/WebFormValidator.java @@ -0,0 +1,31 @@ +package com.codesdream.ase.validator; + +import org.springframework.stereotype.Component; + +import java.util.*; + +@Component +public class WebFormValidator { + + public HashMap check(Map> stdFormMap, Map webFormMap){ + + HashMap result = new HashMap(); + Set stdKey = stdFormMap.keySet(); + Set webKey = webFormMap.keySet(); + + for(String key : webKey){ + if(stdKey.contains(key)){ + Set stdSet = stdFormMap.get(key); + String[] webForm = webFormMap.get(key); + List webList = new ArrayList<>(); + Collections.addAll(webList, webForm); + if(stdSet.containsAll(webList)){ + result.put(key,true); + } + else result.put(key,false); + } + } + + return result; + } +} diff --git a/src/test/java/com/codesdream/ase/ActivityServiceTest.java b/src/test/java/com/codesdream/ase/test/ActivityServiceTest.java similarity index 100% rename from src/test/java/com/codesdream/ase/ActivityServiceTest.java rename to src/test/java/com/codesdream/ase/test/ActivityServiceTest.java