diff --git a/src/main/java/com/codesdream/ase/controller/ActivityController.java b/src/main/java/com/codesdream/ase/controller/ActivityController.java index e299297..dff7f0f 100644 --- a/src/main/java/com/codesdream/ase/controller/ActivityController.java +++ b/src/main/java/com/codesdream/ase/controller/ActivityController.java @@ -1,9 +1,14 @@ package com.codesdream.ase.controller; +import com.alibaba.fastjson.JSONObject; import com.codesdream.ase.component.ASESpringUtil; +import com.codesdream.ase.component.datamanager.JSONParameter; import com.codesdream.ase.configure.ActivityFormConfigure; import com.codesdream.ase.exception.InvalidFormFormatException; +import com.codesdream.ase.exception.LackOfActivityInformation; +import com.codesdream.ase.model.activity.Activity; import com.codesdream.ase.service.ActivityService; +import com.codesdream.ase.validator.NullValueValidator; import com.codesdream.ase.validator.WebFormValidator; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; @@ -12,7 +17,10 @@ import org.springframework.web.bind.annotation.RequestMapping; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; +import javax.validation.constraints.Null; +import java.lang.reflect.InvocationTargetException; import java.util.HashMap; +import java.util.List; import java.util.Map; @Controller @@ -21,19 +29,26 @@ public class ActivityController { @Resource ActivityService activityService; + @Resource + JSONParameter jsonParameter; + @RequestMapping(value = "/activity_creator") String activityCreatorView(Model model){return "activity_creator";} @PostMapping(value = "/activity_creator") - String activityCreator(Model model, HttpServletRequest request) throws InvalidFormFormatException { + String activityCreator(Model model, HttpServletRequest request) throws InvalidFormFormatException, NoSuchMethodException, IllegalAccessException, InvocationTargetException { Map parameterMap = request.getParameterMap(); ASESpringUtil aseSpringUtil = new ASESpringUtil(); ActivityFormConfigure activityFormConfigure = aseSpringUtil.getBean(ActivityFormConfigure.class); WebFormValidator webFormValidator = aseSpringUtil.getBean(WebFormValidator.class); - if(!webFormValidator.check(activityFormConfigure.getStdActivityForm(), parameterMap)){ + if(!webFormValidator.check(activityFormConfigure.getStdActivityForm(), parameterMap)) { throw new InvalidFormFormatException("Invalid activity form."); } + JSONObject jsonObject = jsonParameter.getJSONByRequest(request); + Activity activity = jsonObject.toJavaObject(Activity.class); + NullValueValidator nullValueValidator = aseSpringUtil.getBean(NullValueValidator.class); + List nullValues = nullValueValidator.checkNullValues(activity); return "act_created"; } diff --git a/src/main/java/com/codesdream/ase/exception/LackOfActivityInformation.java b/src/main/java/com/codesdream/ase/exception/LackOfActivityInformation.java new file mode 100644 index 0000000..9b6425a --- /dev/null +++ b/src/main/java/com/codesdream/ase/exception/LackOfActivityInformation.java @@ -0,0 +1,14 @@ +package com.codesdream.ase.exception; + +import lombok.Data; +import lombok.EqualsAndHashCode; + +@EqualsAndHashCode(callSuper = true) +@Data +public class LackOfActivityInformation extends Throwable { + private String message = ""; + + public LackOfActivityInformation(){super();} + + public LackOfActivityInformation(String message){this.message = message;} +} diff --git a/src/main/java/com/codesdream/ase/model/activity/Activity.java b/src/main/java/com/codesdream/ase/model/activity/Activity.java index 2cf5f7b..2a0b4f2 100644 --- a/src/main/java/com/codesdream/ase/model/activity/Activity.java +++ b/src/main/java/com/codesdream/ase/model/activity/Activity.java @@ -6,6 +6,7 @@ import lombok.EqualsAndHashCode; import org.springframework.security.core.parameters.P; import javax.persistence.*; +import java.time.LocalDateTime; import java.util.*; @Data @@ -137,7 +138,7 @@ public class Activity { //提前提醒时间 @Column(name = "remind_time", nullable = true) @Temporal(TemporalType.TIMESTAMP) - private Date remindTime; + private LocalDateTime remindTime; //附件组(名字) @ElementCollection(targetClass = java.lang.String.class) diff --git a/src/main/java/com/codesdream/ase/service/ActivityService.java b/src/main/java/com/codesdream/ase/service/ActivityService.java index e91941e..1c683b8 100644 --- a/src/main/java/com/codesdream/ase/service/ActivityService.java +++ b/src/main/java/com/codesdream/ase/service/ActivityService.java @@ -2,6 +2,7 @@ package com.codesdream.ase.service; import com.codesdream.ase.model.activity.Activity; import com.codesdream.ase.model.activity.Report; +import com.codesdream.ase.model.permission.User; import com.codesdream.ase.repository.activity.ActivityRepository; import org.springframework.stereotype.Service; @@ -50,4 +51,9 @@ public class ActivityService implements IActivityService { return activityRepository.save(activity); } + /*@Override + public Activity findActivitiesInTheCharge(User user) { + + }*/ + } diff --git a/src/main/java/com/codesdream/ase/service/IActivityService.java b/src/main/java/com/codesdream/ase/service/IActivityService.java index 18a3476..1e9ba3e 100644 --- a/src/main/java/com/codesdream/ase/service/IActivityService.java +++ b/src/main/java/com/codesdream/ase/service/IActivityService.java @@ -2,6 +2,7 @@ package com.codesdream.ase.service; import com.codesdream.ase.model.activity.Activity; import com.codesdream.ase.model.activity.Report; +import com.codesdream.ase.model.permission.User; import java.util.Optional; @@ -27,4 +28,7 @@ public interface IActivityService { //活动创建 Activity createActivity(Activity activity); + //查询主要负责的活动 + //Activity findActivitiesInTheCharge(User user); + } diff --git a/src/main/java/com/codesdream/ase/validator/NullValueValidator.java b/src/main/java/com/codesdream/ase/validator/NullValueValidator.java new file mode 100644 index 0000000..d6249bd --- /dev/null +++ b/src/main/java/com/codesdream/ase/validator/NullValueValidator.java @@ -0,0 +1,76 @@ +package com.codesdream.ase.validator; + +import com.codesdream.ase.model.activity.Attendance; +import com.codesdream.ase.model.activity.Period; +import com.codesdream.ase.model.activity.Report; +import com.codesdream.ase.model.permission.User; +import org.springframework.stereotype.Component; + +import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.time.LocalDateTime; +import java.util.ArrayList; +import java.util.Date; +import java.util.List; +import java.util.Set; + +@Component +public class NullValueValidator { + + /** + * 传入一个对象,利用Java的反射机制判断各个属性的值是否为空,并返回空值列表 + * @param object 传入的对象 + * @return 一个字符串列表,维护值为空的属性的名字 + * @throws NoSuchMethodException + * @throws InvocationTargetException + * @throws IllegalAccessException + */ + public List checkNullValues (Object object) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { + Field[] fields = object.getClass().getDeclaredFields(); + List result = new ArrayList<>(); + for (Field field : fields){ + String name = field.getName(); + String type = field.getGenericType().toString(); + Method method = object.getClass().getMethod("get" + name); + name = field.getName(); + if(type.equals("class java.lang.String")){ + String str = (String) method.invoke(object); + if(str.isEmpty()) result.add(name); + } + else if(type.equals("class com.codesdream.ase.model.permission.User")){ + User user = (User) method.invoke(object); + if(user == null) result.add(name); + } + else if(type.equals("java.util.Set")){ + Set users = (Set) method.invoke(object); + if(users.isEmpty()) result.add(name); + } + else if(type.equals("class com.codesdream.ase.model.activity.Period")){ + Period period = (Period) method.invoke(object); + if(period == null) result.add(name); + } + else if(type.equals(("class java.time.LocalDateTime"))){ + LocalDateTime date = (LocalDateTime) method.invoke(object); + if(date == null) result.add(name); + } + else if(type.equals("java.util.List")){ + List strings = (List) method.invoke(object); + if(strings.isEmpty()) result.add(name); + } + else if(type.equals("boolean")){ + boolean a = (boolean) method.invoke(object); + if(!a) result.add(name); + } + else if(type.equals("class com.codesdream.ase.model.activity.Attendance")){ + Attendance attendance = (Attendance) method.invoke(object); + if(attendance == null) result.add(name); + } + else if(type.equals("class com.codesdream.ase.model.activity.Report")){ + Report report = (Report) method.invoke(object); + if(report == null) result.add(name); + } + } + return result; + } +} diff --git a/src/main/resources/templates/huodong1.html b/src/main/resources/templates/huodong1.html index a66d952..7026964 100644 --- a/src/main/resources/templates/huodong1.html +++ b/src/main/resources/templates/huodong1.html @@ -39,7 +39,7 @@ - +
创建人创建人 二狗子 报名人数 1223