diff --git a/src/main/java/com/codesdream/ase/component/activity/ActivityConverter.java b/src/main/java/com/codesdream/ase/component/activity/ActivityConverter.java new file mode 100644 index 0000000..e3f0d18 --- /dev/null +++ b/src/main/java/com/codesdream/ase/component/activity/ActivityConverter.java @@ -0,0 +1,168 @@ +package com.codesdream.ase.component.activity; + +import com.alibaba.fastjson.JSONObject; +import com.codesdream.ase.exception.DataInvalidFormatException; +import com.codesdream.ase.model.activity.Activity; +import com.codesdream.ase.model.activity.Attendance; +import com.codesdream.ase.model.activity.Period; +import com.codesdream.ase.model.permission.User; +import com.codesdream.ase.repository.activity.ActivityRepository; +import com.codesdream.ase.service.ActivityService; +import com.codesdream.ase.service.AttendanceService; +import com.codesdream.ase.service.PeriodService; +import com.codesdream.ase.service.UserService; +import javafx.util.converter.LocalDateTimeStringConverter; +import org.springframework.stereotype.Component; + +import javax.annotation.Resource; +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; +import java.util.*; + +//将合法的JSON对象转化为Activity对象 +@Component +public class ActivityConverter { + + @Resource + ActivityService activityService; + + @Resource + UserService userService; + + @Resource + PeriodService periodService; + + @Resource + AttendanceService attendanceService; + + public Activity convertToActivity(Optional json) { + if (!json.isPresent()) { + throw new NullPointerException(); + } + Activity activity = new Activity(); + JSONObject jsonObject = json.get(); + + String username = (String) jsonObject.get("creator"); + User creator = userService.findUserByUsername(username); + activity.setCreator(creator); + + List participateGroupFromJson = (List) jsonObject.get("participate-group"); + Set participateGroup = new HashSet<>(); + for (String name : participateGroupFromJson) { + User user = userService.findUserByUsername(name); + participateGroup.add(user); + } + activity.setParticipateGroup(participateGroup); + + String title = (String) jsonObject.get("title"); + activity.setTitle(title); + + String chiefManagerName = (String) jsonObject.get("chief-manager"); + User chiefManager = userService.findUserByUsername(chiefManagerName); + activity.setChiefManager(chiefManager); + + List assistManagerFromJSON = (List) jsonObject.get("assist-manager"); + Set assistManager = new HashSet<>(); + for (String name : assistManagerFromJSON) { + User user = userService.findUserByUsername(name); + assistManager.add(user); + } + activity.setAssistManagers(assistManager); + + String type = (String) jsonObject.get("type"); + activity.setType(type); + + String startTimeFromJSON = (String) jsonObject.get("start-time"); + String endTimeFromJSON = (String) jsonObject.get("end-time"); + LocalDateTime startTime = LocalDateTime.parse(startTimeFromJSON, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")); + LocalDateTime endTime = LocalDateTime.parse(endTimeFromJSON, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")); + Period period = new Period(startTime, endTime); + period.setEnabled(false); + period = periodService.save(period); + activity.setPlanPeriod(period); + + String cycle = (String) jsonObject.get("cycle"); + activity.setCycle(cycle); + + String description = (String) jsonObject.get("description"); + activity.setDescription(description); + + List signGroupFromJSON = (List) jsonObject.get("sign-group"); + Set signGroup = new HashSet<>(); + for (String name : signGroupFromJSON) { + User user = userService.findUserByUsername(name); + signGroup.add(user); + } + activity.setSignGroup(signGroup); + + List informGroupFromJSON = (List) jsonObject.get("inform-group"); + Set informGroup = new HashSet<>(); + for (String name : informGroupFromJSON) { + User user = userService.findUserByUsername(name); + informGroup.add(user); + } + activity.setInformGroup(informGroup); + + List visibleGroupFromJSON = (List) jsonObject.get("visible-group"); + Set visibleGroup = new HashSet<>(); + for (String name : visibleGroupFromJSON) { + User user = userService.findUserByUsername(name); + visibleGroup.add(user); + } + activity.setVisibleGroup(informGroup); + + String remindTimeFromJSON = (String) jsonObject.get("remind-time"); + String numStr = remindTimeFromJSON.substring(0, remindTimeFromJSON.length() - 1); + int num = Integer.parseInt(numStr); + char unit = remindTimeFromJSON.charAt(remindTimeFromJSON.length() - 1); + switch (unit) { + case 'w': { + activity.setRemindTime(activity.getPlanPeriod().getStartTime().minusWeeks(num)); + break; + } + case 'd': { + activity.setRemindTime(activity.getPlanPeriod().getStartTime().minusDays(num)); + break; + } + case 'm': { + activity.setRemindTime(activity.getPlanPeriod().getStartTime().minusMinutes(num)); + break; + } + case 'h': { + activity.setRemindTime(activity.getPlanPeriod().getStartTime().minusHours(num)); + break; + } + case 's': { + activity.setRemindTime(activity.getPlanPeriod().getStartTime().minusSeconds(num)); + } + } + + Set periods = new HashSet<>(); + String[] attendanceTimes = (String[]) jsonObject.get("attendance"); + boolean attendanceOnLine = (boolean) jsonObject.get("attendance-online"); + if ((attendanceTimes.length & 1) == 1) { + throw new DataInvalidFormatException(); + } + for (int i = 0; i < attendanceTimes.length; i += 2) { + LocalDateTime start = LocalDateTime.parse(attendanceTimes[i], DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")); + LocalDateTime end = LocalDateTime.parse(attendanceTimes[i + 1], DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")); + Period period1 = new Period(start, end); + periods.add(period1); + } + Attendance attendance = new Attendance(); + attendance.setClockInPeriods(periods); + attendance.setOnline(attendanceOnLine); + /** + * 二维码模块未完成 + */ + attendance = attendanceService.save(attendance); + activity.setAttendance(attendance); + + activity.setOn(false); + activity.setOff(false); + + return activity; + + } +} diff --git a/src/main/java/com/codesdream/ase/controller/activity/ActivityCreatorController.java b/src/main/java/com/codesdream/ase/controller/activity/ActivityCreatorController.java index 25f1d4b..ad6e384 100644 --- a/src/main/java/com/codesdream/ase/controller/activity/ActivityCreatorController.java +++ b/src/main/java/com/codesdream/ase/controller/activity/ActivityCreatorController.java @@ -1,14 +1,13 @@ package com.codesdream.ase.controller.activity; -import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.codesdream.ase.component.ASESpringUtil; +import com.codesdream.ase.component.activity.ActivityConverter; import com.codesdream.ase.component.activity.NullValueAttributes; import com.codesdream.ase.component.datamanager.JSONParameter; -import com.codesdream.ase.component.json.respond.JSONBaseRespondObject; +import com.codesdream.ase.component.json.respond.JSONStandardFailedRespond; import com.codesdream.ase.configure.ActivityFormConfigure; import com.codesdream.ase.exception.InvalidFormFormatException; -import com.codesdream.ase.exception.UserNotFoundException; import com.codesdream.ase.model.activity.Activity; import com.codesdream.ase.model.activity.UserActivity; import com.codesdream.ase.model.permission.User; @@ -17,7 +16,7 @@ import com.codesdream.ase.repository.permission.UserRepository; import com.codesdream.ase.service.ActivityService; import com.codesdream.ase.validator.ActivityValidator; import com.codesdream.ase.validator.NullValueValidator; -import com.codesdream.ase.validator.WebFormValidator; +import com.codesdream.ase.validator.JSONFormValidator; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.PostMapping; @@ -48,7 +47,7 @@ public class ActivityCreatorController { ActivityFormConfigure activityFormConfigure; @Resource - WebFormValidator webFormValidator; + JSONFormValidator jsonFormValidator; @Resource NullValueValidator nullValueValidator; @@ -65,6 +64,9 @@ public class ActivityCreatorController { @Resource UserActivityRepository userActivityRepository; + @Resource + ActivityConverter activityConverter; + private final String url = "/forget/activity"; @RequestMapping(value = url + "/activity_creator") @@ -73,24 +75,22 @@ public class ActivityCreatorController { @PostMapping(value = url + "/activity_creator") @ResponseBody - String activityCreator(@RequestBody JSONObject jsonParam, Model model, HttpServletRequest request) throws InvalidFormFormatException, NoSuchMethodException, IllegalAccessException, InvocationTargetException { + String activityCreator(HttpServletRequest request) throws InvalidFormFormatException, NoSuchMethodException, IllegalAccessException, InvocationTargetException { JSONObject error = new JSONObject(); - - Map parameterMap = request.getParameterMap(); aseSpringUtil = new ASESpringUtil(); + Optional json = jsonParameter.getJSONByRequest(request); + if (!json.isPresent()) return jsonParameter.getJSONString(new JSONStandardFailedRespond()); + //WebFormValidator webFormValidator = aseSpringUtil.getBean(WebFormValidator.class); - if(!webFormValidator.check(activityFormConfigure.getStdActivityForm(), parameterMap)) { - error.put("error", "Invalid form format"); + List formatCheckResult = jsonFormValidator.check(activityFormConfigure.getStdActivityForm(), json.get()); + if (!formatCheckResult.isEmpty()) { + error.put("error", formatCheckResult); return error.toJSONString(); } // 需要检查JSON是否合法 - Optional jsonObject = jsonParameter.getJSONByRequest(request); - if(!jsonObject.isPresent()){ - error.put("error", "Invalid type."); - return error.toJSONString(); - } - Activity activity = jsonObject.get().toJavaObject(Activity.class); + + Activity activity = activityConverter.convertToActivity(json); //NullValueValidator nullValueValidator = aseSpringUtil.getBean(NullValueValidator.class); List nullValues = nullValueValidator.checkNullValues(activity); @@ -116,7 +116,7 @@ public class ActivityCreatorController { if(nullValueAttributes.getNullValueAttributes().isEmpty()){ //ActivityValidator activityValidator = aseSpringUtil.getBean(ActivityValidator.class); - String[] errorParameters = activityValidator.check(parameterMap); + String[] errorParameters = activityValidator.check(json); if(errorParameters != null){ JSONObject invalidParameters = new JSONObject(); invalidParameters.put("invalid_parameters", errorParameters); @@ -127,7 +127,7 @@ public class ActivityCreatorController { //UserRepository userRepository = aseSpringUtil.getBean(UserRepository.class); //activityService = aseSpringUtil.getBean(ActivityService.class); activity = activityService.createActivity(activity); - String username = parameterMap.get("creator")[0]; + String username = json.get().get("creator").toString(); Optional user = userRepository.findByUsername(username); //UserActivityRepository userActivityRepository = aseSpringUtil.getBean(UserActivityRepository.class); UserActivity userActivity = userActivityRepository.findByUser(user.get()); @@ -135,7 +135,7 @@ public class ActivityCreatorController { userActivityRepository.save(userActivity); } } - //否则返回一个JSON对象给前端,此处应该是JSP页面,动态标红 + //否则返回一个JSON对象给前端 else{ JSONObject nullParameters = new JSONObject(); nullParameters.put("null_values",nullValueAttributes.getNullValueAttributes()); diff --git a/src/main/java/com/codesdream/ase/controller/activity/ActivityViewerController.java b/src/main/java/com/codesdream/ase/controller/activity/ActivityViewerController.java index e6df04e..7068097 100644 --- a/src/main/java/com/codesdream/ase/controller/activity/ActivityViewerController.java +++ b/src/main/java/com/codesdream/ase/controller/activity/ActivityViewerController.java @@ -17,13 +17,15 @@ import java.security.Principal; @Controller public class ActivityViewerController { + private final String url = "/forget/activity"; + @Resource ActivityService activityService; @Resource ActivityRepository activityRepository; - @RequestMapping(value = "/my/participated", method = RequestMethod.GET) + @RequestMapping(value = url + "/my/participated", method = RequestMethod.GET) String showParticipated(Model model, HttpServletRequest request){ Principal principal = request.getUserPrincipal(); String username = principal.getName(); diff --git a/src/main/java/com/codesdream/ase/exception/InvalidFormFormatException.java b/src/main/java/com/codesdream/ase/exception/InvalidFormFormatException.java index f688f5e..be25439 100644 --- a/src/main/java/com/codesdream/ase/exception/InvalidFormFormatException.java +++ b/src/main/java/com/codesdream/ase/exception/InvalidFormFormatException.java @@ -9,7 +9,7 @@ import javax.persistence.criteria.CriteriaBuilder; @Data public class InvalidFormFormatException extends Throwable { - private String message = ""; + private String message = "Invalid form format"; public InvalidFormFormatException(){ super(); diff --git a/src/main/java/com/codesdream/ase/model/activity/Period.java b/src/main/java/com/codesdream/ase/model/activity/Period.java index 38c12ec..8064ffc 100644 --- a/src/main/java/com/codesdream/ase/model/activity/Period.java +++ b/src/main/java/com/codesdream/ase/model/activity/Period.java @@ -29,4 +29,9 @@ public class Period { @Column(name = "enabled")//, nullable = false) private boolean enabled; + public Period(LocalDateTime startTime, LocalDateTime endTime) { + this.startTime = startTime; + this.endTime = endTime; + } + } diff --git a/src/main/java/com/codesdream/ase/repository/activity/AttendanceRepository.java b/src/main/java/com/codesdream/ase/repository/activity/AttendanceRepository.java new file mode 100644 index 0000000..31776a3 --- /dev/null +++ b/src/main/java/com/codesdream/ase/repository/activity/AttendanceRepository.java @@ -0,0 +1,9 @@ +package com.codesdream.ase.repository.activity; + +import com.codesdream.ase.model.activity.Attendance; +import org.springframework.data.repository.CrudRepository; +import org.springframework.stereotype.Repository; + +@Repository +public interface AttendanceRepository extends CrudRepository { +} diff --git a/src/main/java/com/codesdream/ase/repository/activity/PeriodRepository.java b/src/main/java/com/codesdream/ase/repository/activity/PeriodRepository.java new file mode 100644 index 0000000..24a45ad --- /dev/null +++ b/src/main/java/com/codesdream/ase/repository/activity/PeriodRepository.java @@ -0,0 +1,9 @@ +package com.codesdream.ase.repository.activity; + +import com.codesdream.ase.model.activity.Period; +import org.springframework.data.repository.CrudRepository; +import org.springframework.stereotype.Repository; + +@Repository +public interface PeriodRepository extends CrudRepository { +} diff --git a/src/main/java/com/codesdream/ase/service/AttendanceService.java b/src/main/java/com/codesdream/ase/service/AttendanceService.java new file mode 100644 index 0000000..5484358 --- /dev/null +++ b/src/main/java/com/codesdream/ase/service/AttendanceService.java @@ -0,0 +1,18 @@ +package com.codesdream.ase.service; + +import com.codesdream.ase.model.activity.Attendance; +import com.codesdream.ase.repository.activity.AttendanceRepository; +import org.springframework.stereotype.Service; + +import javax.annotation.Resource; + +@Service +public class AttendanceService { + + @Resource + AttendanceRepository attendanceRepository; + + public Attendance save(Attendance attendance) { + return attendanceRepository.save(attendance); + } +} diff --git a/src/main/java/com/codesdream/ase/service/PeriodService.java b/src/main/java/com/codesdream/ase/service/PeriodService.java new file mode 100644 index 0000000..879de10 --- /dev/null +++ b/src/main/java/com/codesdream/ase/service/PeriodService.java @@ -0,0 +1,22 @@ +package com.codesdream.ase.service; + +import com.codesdream.ase.model.activity.Period; +import com.codesdream.ase.repository.activity.PeriodRepository; +import org.springframework.stereotype.Service; + +import javax.annotation.Resource; + +@Service +public class PeriodService { + + @Resource + PeriodRepository periodRepository; + + public Period save(Period period) { + return periodRepository.save(period); + } + + public void delete(Period period) { + periodRepository.delete(period); + } +} diff --git a/src/main/java/com/codesdream/ase/validator/ActivityValidator.java b/src/main/java/com/codesdream/ase/validator/ActivityValidator.java index 2d8b4bf..3f6488f 100644 --- a/src/main/java/com/codesdream/ase/validator/ActivityValidator.java +++ b/src/main/java/com/codesdream/ase/validator/ActivityValidator.java @@ -1,17 +1,20 @@ package com.codesdream.ase.validator; +import com.alibaba.fastjson.JSONObject; import com.codesdream.ase.model.activity.Activity; import org.springframework.stereotype.Component; +import java.nio.file.OpenOption; import java.util.ArrayList; import java.util.List; import java.util.Map; +import java.util.Optional; //检查当前活动各属性值是否合法(存在) @Component public class ActivityValidator { - public String[] check(Map parameterMap){ + public String[] check(Optional json) { return null; } } diff --git a/src/main/java/com/codesdream/ase/validator/JSONFormValidator.java b/src/main/java/com/codesdream/ase/validator/JSONFormValidator.java new file mode 100644 index 0000000..79a3c83 --- /dev/null +++ b/src/main/java/com/codesdream/ase/validator/JSONFormValidator.java @@ -0,0 +1,30 @@ +package com.codesdream.ase.validator; + +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONObject; +import com.codesdream.ase.exception.DataIllegalTableFormatException; +import org.springframework.stereotype.Component; + +import java.util.*; + +//用于检查JSON格式是否合法 +@Component +public class JSONFormValidator { + + /** + * @param stdForm 标准表单格式,根据需求自定义 + * @param json 待检验的json对象 + * @return 空列表或者缺失的表单信息列表 + */ + public List check(Set stdForm, JSONObject json) { + + List res = new ArrayList<>(); + Set jsonForm = json.keySet(); + for (String str : stdForm) { + if (!jsonForm.contains(str)) { + res.add(str); + } + } + return res; + } +} diff --git a/src/main/java/com/codesdream/ase/validator/WebFormValidator.java b/src/main/java/com/codesdream/ase/validator/WebFormValidator.java deleted file mode 100644 index 6ffaac0..0000000 --- a/src/main/java/com/codesdream/ase/validator/WebFormValidator.java +++ /dev/null @@ -1,19 +0,0 @@ -package com.codesdream.ase.validator; - -import org.springframework.stereotype.Component; - -import java.util.*; - -//用于检查网页表单格式是否合法 -@Component -public class WebFormValidator { - - public boolean check(Collection stdForm, Map webFormMap){ - - Collection webForm = webFormMap.keySet(); - if(webForm.containsAll(stdForm)){ - return true; - } - return false; - } -} diff --git a/src/test/java/com/codesdream/ase/test/JSONFormValidatorTest.java b/src/test/java/com/codesdream/ase/test/JSONFormValidatorTest.java new file mode 100644 index 0000000..11ef842 --- /dev/null +++ b/src/test/java/com/codesdream/ase/test/JSONFormValidatorTest.java @@ -0,0 +1,61 @@ +package com.codesdream.ase.test; + +import com.alibaba.fastjson.JSONObject; +import com.codesdream.ase.component.ASESpringUtil; +import com.codesdream.ase.component.json.respond.JSONBaseRespondObject; +import com.codesdream.ase.configure.ActivityFormConfigure; +import com.codesdream.ase.exception.InvalidFormFormatException; +import com.codesdream.ase.validator.JSONFormValidator; +import com.fasterxml.jackson.databind.util.JSONPObject; +import lombok.extern.slf4j.Slf4j; +import org.apache.poi.openxml4j.exceptions.InvalidFormatException; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.ActiveProfiles; +import org.springframework.test.context.junit4.SpringRunner; + +import javax.annotation.Resource; +import java.util.HashMap; +import java.util.Map; +import java.util.Optional; + +@Slf4j +@RunWith(SpringRunner.class) +@SpringBootTest +@ActiveProfiles("test") +public class JSONFormValidatorTest { + @Resource + ASESpringUtil aseSpringUtil; + + @Test + public void checkTest() throws InvalidFormFormatException { + Map map = new HashMap() {{ + put("creator", "tom"); + put("title", "haha"); + put("description", "h"); + put("cycle", "h"); + put("volunteers", "tom"); + put("participate", "tom"); + put("sign", "s"); + put("visible", "s"); + put("start-time", "1"); + put("remind", "1"); + put("enclosure", "1"); + put("chief-manager", "tom"); + put("assist-manager", "1"); + put("attendance", "1"); + put("type", "h"); + put("attendance", "aa"); + put("inform", "aaa"); + }}; + JSONObject jsonObject = new JSONObject(); + jsonObject.putAll(map); + Optional json = Optional.of(jsonObject); + ActivityFormConfigure activityFormConfigure = aseSpringUtil.getBean(ActivityFormConfigure.class); + JSONFormValidator jsonFormValidator = aseSpringUtil.getBean(JSONFormValidator.class); + if (jsonFormValidator.check(activityFormConfigure.getStdActivityForm(), json.get()).isEmpty()) { + System.out.println("error"); + } else System.out.println("ok"); + } +}