Merge remote-tracking branch 'origin/master'

This commit is contained in:
Saturneric 2020-03-25 02:01:49 +08:00
commit 2cf0ff666f
14 changed files with 362 additions and 41 deletions

13
pom.xml
View File

@ -150,6 +150,19 @@
<scope>runtime</scope>
</dependency>
<!-- 二维码支持包 -->
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>3.2.0</version>
</dependency>
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>javase</artifactId>
<version>3.2.0</version>
</dependency>
</dependencies>

View File

@ -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<JSONObject> 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<String> participateGroupFromJson = (List) jsonObject.get("participate-group");
Set<User> 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<String> assistManagerFromJSON = (List) jsonObject.get("assist-manager");
Set<User> 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<String> signGroupFromJSON = (List) jsonObject.get("sign-group");
Set<User> signGroup = new HashSet<>();
for (String name : signGroupFromJSON) {
User user = userService.findUserByUsername(name);
signGroup.add(user);
}
activity.setSignGroup(signGroup);
List<String> informGroupFromJSON = (List) jsonObject.get("inform-group");
Set<User> informGroup = new HashSet<>();
for (String name : informGroupFromJSON) {
User user = userService.findUserByUsername(name);
informGroup.add(user);
}
activity.setInformGroup(informGroup);
List<String> visibleGroupFromJSON = (List) jsonObject.get("visible-group");
Set<User> 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<Period> 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;
}
}

View File

@ -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<String, String[]> parameterMap = request.getParameterMap();
aseSpringUtil = new ASESpringUtil();
Optional<JSONObject> 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<String> formatCheckResult = jsonFormValidator.check(activityFormConfigure.getStdActivityForm(), json.get());
if (!formatCheckResult.isEmpty()) {
error.put("error", formatCheckResult);
return error.toJSONString();
}
// 需要检查JSON是否合法
Optional<JSONObject> 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<String> 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> 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());

View File

@ -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();

View File

@ -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();

View File

@ -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;
}
}

View File

@ -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<Attendance, Integer> {
}

View File

@ -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<Period, Integer> {
}

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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<String, String[]> parameterMap){
public String[] check(Optional<JSONObject> json) {
return null;
}
}

View File

@ -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<String> check(Set<String> stdForm, JSONObject json) {
List<String> res = new ArrayList<>();
Set<String> jsonForm = json.keySet();
for (String str : stdForm) {
if (!jsonForm.contains(str)) {
res.add(str);
}
}
return res;
}
}

View File

@ -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<String> stdForm, Map<String, String[]> webFormMap){
Collection<String> webForm = webFormMap.keySet();
if(webForm.containsAll(stdForm)){
return true;
}
return false;
}
}

View File

@ -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<String, String> map = new HashMap<String, String>() {{
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<JSONObject> 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");
}
}