diff --git a/src/main/java/com/codesdream/ase/component/api/QuickJSONRespond.java b/src/main/java/com/codesdream/ase/component/api/QuickJSONRespond.java index dfe654d..be125bf 100644 --- a/src/main/java/com/codesdream/ase/component/api/QuickJSONRespond.java +++ b/src/main/java/com/codesdream/ase/component/api/QuickJSONRespond.java @@ -3,6 +3,8 @@ package com.codesdream.ase.component.api; import com.codesdream.ase.component.datamanager.JSONParameter; import com.codesdream.ase.component.json.respond.EmptyDataObjectRespond; import com.codesdream.ase.component.json.respond.JSONBaseRespondObject; +import com.sun.deploy.net.HttpResponse; +import org.springframework.http.HttpStatus; import org.springframework.stereotype.Component; import javax.annotation.Resource; @@ -23,6 +25,33 @@ public class QuickJSONRespond { return jsonParameter.getJSONString(respondObject); } + // 根据对象构造获得标准的JSON响应字符串返回 + public String getJSONStandardRespond(HttpStatus status, Object dataObject){ + JSONBaseRespondObject respondObject = new JSONBaseRespondObject(status.value(), status.getReasonPhrase()); + + respondObject.setData(dataObject); + return jsonParameter.getJSONString(respondObject); + } + + // 根据对象构造获得标准的JSON响应字符串返回 + public String getJSONStandardRespond(HttpStatus status, String info, Object dataObject){ + JSONBaseRespondObject respondObject = new JSONBaseRespondObject(status.value(), status.getReasonPhrase()); + if(info != null) respondObject.setInfo(info); + else respondObject.setInfo(null); + + respondObject.setData(dataObject); + return jsonParameter.getJSONString(respondObject); + } + + // 根据对象构造获得标准的JSON响应字符串返回 + public String getJSONStandardRespond(HttpStatus status, String info){ + JSONBaseRespondObject respondObject = new JSONBaseRespondObject(status.value(), status.getReasonPhrase()); + if(info != null) respondObject.setInfo(info); + else respondObject.setInfo(null); + + return jsonParameter.getJSONString(respondObject); + } + // 获得标准的JSON响应字符串返回特定状态码的和解释息 public String getJSONStandardRespond(Integer code, String msg, String info){ JSONBaseRespondObject respondObject = new JSONBaseRespondObject(code, msg); @@ -34,37 +63,42 @@ public class QuickJSONRespond { // 获得标准的JSON响应字符串返回(404状态) public String getRespond404(String info){ - return getJSONStandardRespond(404, "Not Found", info); + return getJSONStandardRespond(HttpStatus.NOT_FOUND, info); } // 获得标准的JSON响应字符串返回(404状态) public String getRespond404(String info, Object object){ - return getJSONStandardRespond(404, "Not Found", info, object); + return getJSONStandardRespond(HttpStatus.NOT_FOUND, info, object); } // 获得标准的JSON响应字符串返回(500状态) public String getRespond500(String info){ - return getJSONStandardRespond(500, "Internal Server Error", info); + return getJSONStandardRespond(HttpStatus.INTERNAL_SERVER_ERROR, info); } // 获得标准的JSON响应字符串返回(200状态) public String getRespond200(String info){ - return getJSONStandardRespond(200, "Ok", info); + return getJSONStandardRespond(HttpStatus.OK, info); } // 获得标准的JSON响应字符串返回(200状态) public String getRespond200(String info, Object object){ - return getJSONStandardRespond(200, "Ok", info, object); + return getJSONStandardRespond(HttpStatus.OK, info, object); } // 获得标准的JSON响应字符串返回(403状态) public String getRespond403(String info){ - return getJSONStandardRespond(403, "Forbidden", info); + return getJSONStandardRespond(HttpStatus.FORBIDDEN, info); } - // 获得标准的JSON响应字符串返回(403状态) + // 获得标准的JSON响应字符串返回(406状态) public String getRespond406(String info){ - return getJSONStandardRespond(406, "Not Acceptable", info); + return getJSONStandardRespond(HttpStatus.NOT_ACCEPTABLE, info); + } + + // 获得标准的JSON响应字符串返回(406状态) + public String getRespond406(String info, Object object){ + return getJSONStandardRespond(HttpStatus.NOT_ACCEPTABLE, info, object); } // 获得标准的JSON响应字符串返回(501态) diff --git a/src/main/java/com/codesdream/ase/component/json/model/JsonableTag.java b/src/main/java/com/codesdream/ase/component/json/model/JsonableTag.java index b5397cd..8db125b 100644 --- a/src/main/java/com/codesdream/ase/component/json/model/JsonableTag.java +++ b/src/main/java/com/codesdream/ase/component/json/model/JsonableTag.java @@ -5,6 +5,7 @@ import com.codesdream.ase.model.permission.User; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; import lombok.Data; +import lombok.NoArgsConstructor; import net.bytebuddy.implementation.bind.annotation.DefaultMethod; import java.util.ArrayList; @@ -13,6 +14,7 @@ import java.util.List; import java.util.Set; @Data +@NoArgsConstructor @ApiModel("标签") public class JsonableTag { @ApiModelProperty(value = "标签id") @@ -21,21 +23,12 @@ public class JsonableTag { private String name; @ApiModelProperty(value = "标签说明", example = "该系统的管理员") private String description; - @ApiModelProperty(value = "用户列表", hidden = true) - private Set users = new HashSet<>(); - public JsonableTag(){ - - } public JsonableTag(Tag tag){ this.id = tag.getId(); this.name = tag.getName(); this.description = tag.getDescription(); - // 构建用户的ID列表 - for(User user : tag.getUsers()) { - users.add(user.getId()); - } } } diff --git a/src/main/java/com/codesdream/ase/component/json/model/JsonableTagUserList.java b/src/main/java/com/codesdream/ase/component/json/model/JsonableTagUserList.java new file mode 100644 index 0000000..523c61e --- /dev/null +++ b/src/main/java/com/codesdream/ase/component/json/model/JsonableTagUserList.java @@ -0,0 +1,26 @@ +package com.codesdream.ase.component.json.model; + +import com.codesdream.ase.model.permission.Tag; +import com.codesdream.ase.model.permission.User; +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.util.List; + +@Data +@NoArgsConstructor +@ApiModel("标签所属用户集合") +public class JsonableTagUserList { + + @ApiModelProperty(name = "用户列表") + private List users; + + + public JsonableTagUserList(Tag tag){ + for(User user : tag.getUsers()){ + users.add(user.getId()); + } + } +} diff --git a/src/main/java/com/codesdream/ase/component/json/model/JsonableUser.java b/src/main/java/com/codesdream/ase/component/json/model/JsonableUser.java new file mode 100644 index 0000000..74f6467 --- /dev/null +++ b/src/main/java/com/codesdream/ase/component/json/model/JsonableUser.java @@ -0,0 +1,19 @@ +package com.codesdream.ase.component.json.model; + +import com.codesdream.ase.model.permission.User; +import io.swagger.annotations.ApiModel; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Data +@ApiModel("用户") +@NoArgsConstructor +public class JsonableUser { + private Integer id; + private String username; + + public JsonableUser(User user){ + this.id = user.getId(); + this.username = user.getUsername(); + } +} diff --git a/src/main/java/com/codesdream/ase/controller/ASEControllerAdvice.java b/src/main/java/com/codesdream/ase/controller/ASEControllerAdvice.java index 755d930..bdddf30 100644 --- a/src/main/java/com/codesdream/ase/controller/ASEControllerAdvice.java +++ b/src/main/java/com/codesdream/ase/controller/ASEControllerAdvice.java @@ -1,26 +1,17 @@ package com.codesdream.ase.controller; import com.codesdream.ase.component.api.QuickJSONRespond; -import com.codesdream.ase.component.error.ErrorResponse; import com.codesdream.ase.component.json.respond.ErrorInfoJSONRespond; +import com.codesdream.ase.exception.badrequest.AlreadyExistException; +import com.codesdream.ase.exception.conflict.RelatedObjectsExistException; import com.codesdream.ase.exception.notfound.NotFoundException; -import com.sun.xml.bind.v2.model.annotation.Quick; -import org.apache.poi.openxml4j.opc.internal.ContentType; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; -import org.springframework.security.authentication.AuthenticationServiceException; -import org.springframework.security.core.AuthenticationException; -import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.RestControllerAdvice; -import org.springframework.web.context.request.WebRequest; import javax.annotation.Resource; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; -import java.util.ArrayList; import java.util.Date; -import java.util.List; @RestControllerAdvice public class ASEControllerAdvice { @@ -28,17 +19,37 @@ public class ASEControllerAdvice { @Resource private QuickJSONRespond quickJSONRespond; - @ExceptionHandler(value = {NullPointerException.class}) + @ExceptionHandler(value = { + NullPointerException.class, + AlreadyExistException.class + }) public ResponseEntity handleBadRequest(Exception ex) { - - String json = quickJSONRespond.getRespond400(null, getJSONRespondObject(ex)); - return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(json); + return getResponse(HttpStatus.BAD_REQUEST, ex); } @ExceptionHandler(value = {NotFoundException.class}) public ResponseEntity handleNotFound(Exception ex) { - String json = quickJSONRespond.getRespond404(null, getJSONRespondObject(ex)); - return ResponseEntity.status(HttpStatus.NOT_FOUND).body(json); + + return getResponse(HttpStatus.NOT_FOUND, ex); + } + + @ExceptionHandler(value = {}) + public ResponseEntity handleNotAcceptable(Exception ex) { + return getResponse(HttpStatus.NOT_ACCEPTABLE, ex); + } + + @ExceptionHandler(value = {RelatedObjectsExistException.class}) + public ResponseEntity handleConflict(Exception ex) { + return getResponse(HttpStatus.CONFLICT, ex); + } + + private ResponseEntity getResponse(HttpStatus status, Exception ex){ + return ResponseEntity.status(status).body(getJSON(status, ex)); + + } + + private String getJSON(HttpStatus status, Exception ex){ + return quickJSONRespond.getJSONStandardRespond(status, getJSONRespondObject(ex)); } private Object getJSONRespondObject(Exception ex){ diff --git a/src/main/java/com/codesdream/ase/controller/PermissionController.java b/src/main/java/com/codesdream/ase/controller/PermissionController.java index d702bee..766d619 100644 --- a/src/main/java/com/codesdream/ase/controller/PermissionController.java +++ b/src/main/java/com/codesdream/ase/controller/PermissionController.java @@ -4,7 +4,12 @@ import com.alibaba.fastjson.JSONObject; import com.codesdream.ase.component.datamanager.JSONParameter; import com.codesdream.ase.component.api.QuickJSONRespond; import com.codesdream.ase.component.json.model.JsonableTag; +import com.codesdream.ase.component.json.model.JsonableTagUserList; +import com.codesdream.ase.component.json.model.JsonableUser; import com.codesdream.ase.component.json.respond.PermissionJSONRespond; +import com.codesdream.ase.exception.badrequest.AlreadyExistException; +import com.codesdream.ase.exception.conflict.RelatedObjectsExistException; +import com.codesdream.ase.exception.notfound.NotFoundException; import com.codesdream.ase.exception.notfound.TagNotFoundException; import com.codesdream.ase.model.permission.Tag; import com.codesdream.ase.model.permission.User; @@ -14,11 +19,14 @@ import com.codesdream.ase.service.PermissionService; import io.swagger.annotations.Api; import io.swagger.annotations.ApiImplicitParam; import io.swagger.annotations.ApiOperation; +import org.apache.poi.ss.formula.functions.T; +import org.springframework.http.HttpStatus; import org.springframework.web.bind.annotation.*; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import java.util.HashSet; +import java.util.List; import java.util.Optional; import java.util.Set; @@ -33,23 +41,14 @@ public class PermissionController { @Resource private IUserService userService; - @Resource - private JSONParameter jsonParameter; - - @Resource - private QuickJSONRespond jsonRespond; - // 根据名字创建新的标签 @PostMapping("tag") + @ResponseStatus(HttpStatus.CREATED) @ApiOperation(value = "创建新的标签", notes = "创建标签时其ID自动分配,指定ID无效") public JsonableTag createTag(@RequestBody JsonableTag tag){ - String tagName = tag.getName(); -/* if(tagName == null) return jsonRespond.getRespond406("Missing Tag Name");*/ - Optional tagOptional = permissionService.findTag(tagName); -/* if(tagOptional.isPresent()) return jsonRespond.getRespond409("Tag Name Already Exist");*/ - + if(tagOptional.isPresent()) throw new AlreadyExistException(tagName); Tag newTag = permissionService.getDefaultTag(tagName); if(tag.getDescription() != null) { newTag.setDescription(tag.getDescription()); @@ -57,9 +56,9 @@ public class PermissionController { return new JsonableTag(permissionService.save(newTag)); } - // 根据名字搜索标签的简要信息 @GetMapping("tag") + @ResponseStatus(HttpStatus.OK) @ApiOperation("搜索标签信息") @ApiImplicitParam(name = "name", value = "标签名") public JsonableTag checkTag(@RequestParam(value = "name") String name){ @@ -67,33 +66,105 @@ public class PermissionController { if(tagOptional.isPresent()){ return new JsonableTag(tagOptional.get()); } - else throw new TagNotFoundException(name); + else throw new NotFoundException(name); } - // 将用户添加到Tag中 - @PostMapping("tag/add/user") - public String addUserTag(HttpServletRequest request){ - Optional jsonObjectOptional = jsonParameter.getJSONByRequest(request); - if(!jsonObjectOptional.isPresent()) return jsonRespond.getRespond400("Illegal JSON Format"); + // 根据名字搜索标签的简要信息 + @GetMapping("tags") + @ResponseStatus(HttpStatus.OK) + @ApiOperation("列出所有的标签信息") + @ApiImplicitParam(name = "name", value = "标签名") + public Set listTag(){ + Iterable tagIterable = permissionService.findAllTag(); + Set jsonableTagSet = new HashSet<>(); + for(Tag tag : tagIterable){ + jsonableTagSet.add(new JsonableTag(tag)); + } + return jsonableTagSet; + } - JSONObject jsonObject = jsonObjectOptional.get(); - Integer tagId = jsonObject.getInteger("tagId"); - Integer userId = jsonObject.getInteger("userId"); + // 根据名字搜索标签的简要信息 + @DeleteMapping("tag") + @ResponseStatus(HttpStatus.NO_CONTENT) + @ApiOperation("删除标签") + @ApiImplicitParam(name = "name", value = "标签名") + public void deleteTag(@RequestParam(value = "name") String name){ + Optional tag = permissionService.findTag(name); + if(!tag.isPresent()) throw new NotFoundException(name); - if(userId == null || tagId == null) - return jsonRespond.getRespond406("Request Violates The Interface Protocol"); + // 检查外键关联 + if(tag.get().getUsers().size() > 0) throw new RelatedObjectsExistException(); + if(tag.get().getPermissionContainersCollections().size() > 0) throw new RelatedObjectsExistException(); - Optional user = userService.findUserById(userId); - if(!user.isPresent()) return jsonRespond.getRespond406("User Not Exist"); + permissionService.delete(tag.get()); + } - Optional tag = permissionService.findTag(tagId); - if(!tag.isPresent()) return jsonRespond.getRespond406("Tag Not Exist"); - // 检查用户是否已经在标签中 - if(tag.get().getUsers().contains(user.get())) return jsonRespond.getRespond409("User Already In The Tag"); - permissionService.addUserToTag(tag.get(), user.get()); + @GetMapping("tag/users") + @ResponseStatus(HttpStatus.OK) + @ApiOperation("搜索单个标签所属用户集合信息") + public JsonableTagUserList getUserTag(@RequestParam(value = "name") String name){ + Optional tag = permissionService.findTag(name); + if(!tag.isPresent()) throw new NotFoundException(name); + return new JsonableTagUserList(tag.get()); + } - return jsonRespond.getRespond200("Add User TO Tag Successful"); + @PutMapping("tag/users") + @ApiOperation("更新索单个标签所属用户集合信息") + public JsonableTagUserList setUserTag(@RequestParam String name, @RequestBody JsonableTagUserList userList){ + Optional tag = permissionService.findTag(name); + if(!tag.isPresent()) throw new NotFoundException(name); + + Set userSet = new HashSet<>(userList.getUsers()); + tag.get().setUsers(userService.findUsersById(userSet)); + + return new JsonableTagUserList(permissionService.save(tag.get())); + } + + @PostMapping("tag/users") + @ApiOperation("更新单个标签所属用户集合中添加一个或多个用户") + public JsonableTagUserList addUserTag(@RequestParam String name, @RequestBody JsonableTagUserList userList){ + Optional tag = permissionService.findTag(name); + if(!tag.isPresent()) throw new NotFoundException(name); + Set newUserSet = userService.findUsersById(new HashSet<>(userList.getUsers())); + + Set userSet = tag.get().getUsers(); + + userSet.addAll(newUserSet); + tag.get().setUsers(userSet); + + return new JsonableTagUserList(permissionService.save(tag.get())); + } + + @DeleteMapping("tag/users") + @ApiOperation("从单个标签所属用户集合中删除一个或多个用户") + @ApiImplicitParam(name = "name", value = "标签名") + public JsonableTagUserList deleteUserTag(@RequestParam String name, @RequestBody JsonableTagUserList userList){ + Optional tag = permissionService.findTag(name); + if(!tag.isPresent()) throw new NotFoundException(name); + Set userSet = tag.get().getUsers(); + Set deleteUserSet = userService.findUsersById(new HashSet<>(userList.getUsers())); + + userSet.removeAll(deleteUserSet); + tag.get().setUsers(userSet); + + return new JsonableTagUserList(permissionService.save(tag.get())); + } + + @GetMapping("tags/users") + @ResponseStatus(HttpStatus.OK) + @ApiOperation("搜索多个标签所属用户集合信息") + public Set getUserTags(@RequestParam(value = "name") List names){ + Set tagSet = permissionService.findTags(names); + Set userSet = new HashSet<>(); + Set jsonableUsers = new HashSet<>(); + for(Tag tag : tagSet){ + userSet.addAll(tag.getUsers()); + } + for(User user : userSet){ + jsonableUsers.add(new JsonableUser(user)); + } + return jsonableUsers; } diff --git a/src/main/java/com/codesdream/ase/exception/badrequest/AlreadyExistException.java b/src/main/java/com/codesdream/ase/exception/badrequest/AlreadyExistException.java new file mode 100644 index 0000000..81fa88b --- /dev/null +++ b/src/main/java/com/codesdream/ase/exception/badrequest/AlreadyExistException.java @@ -0,0 +1,14 @@ +package com.codesdream.ase.exception.badrequest; + +import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.NoArgsConstructor; + +@EqualsAndHashCode(callSuper = true) +@Data +@NoArgsConstructor +public class AlreadyExistException extends RuntimeException { + public AlreadyExistException(String msg){ + super(msg); + } +} diff --git a/src/main/java/com/codesdream/ase/exception/BaseInformationAlreadyExistException.java b/src/main/java/com/codesdream/ase/exception/badrequest/BaseInformationAlreadyExistException.java similarity index 61% rename from src/main/java/com/codesdream/ase/exception/BaseInformationAlreadyExistException.java rename to src/main/java/com/codesdream/ase/exception/badrequest/BaseInformationAlreadyExistException.java index 54a564b..47fda13 100644 --- a/src/main/java/com/codesdream/ase/exception/BaseInformationAlreadyExistException.java +++ b/src/main/java/com/codesdream/ase/exception/badrequest/BaseInformationAlreadyExistException.java @@ -1,16 +1,16 @@ -package com.codesdream.ase.exception; +package com.codesdream.ase.exception.badrequest; import lombok.Data; import lombok.EqualsAndHashCode; @EqualsAndHashCode(callSuper = true) @Data -public class BaseInformationAlreadyExistException extends RuntimeException { +public class BaseInformationAlreadyExistException extends AlreadyExistException { private String className; private String value; public BaseInformationAlreadyExistException(Class aClass, String value){ - super(); + super(String.format("%s: %s", aClass.getName(), value)); this.className = aClass.getName(); this.value = value; } diff --git a/src/main/java/com/codesdream/ase/exception/UsernameAlreadyExistException.java b/src/main/java/com/codesdream/ase/exception/badrequest/UsernameAlreadyExistException.java similarity index 59% rename from src/main/java/com/codesdream/ase/exception/UsernameAlreadyExistException.java rename to src/main/java/com/codesdream/ase/exception/badrequest/UsernameAlreadyExistException.java index 634d2e8..c08d0e0 100644 --- a/src/main/java/com/codesdream/ase/exception/UsernameAlreadyExistException.java +++ b/src/main/java/com/codesdream/ase/exception/badrequest/UsernameAlreadyExistException.java @@ -1,16 +1,16 @@ -package com.codesdream.ase.exception; +package com.codesdream.ase.exception.badrequest; import lombok.Data; import lombok.EqualsAndHashCode; @EqualsAndHashCode(callSuper = true) @Data -public class UsernameAlreadyExistException extends RuntimeException { +public class UsernameAlreadyExistException extends AlreadyExistException { String username; public UsernameAlreadyExistException(String username){ - super(); + super(username); this.username = username; } } diff --git a/src/main/java/com/codesdream/ase/exception/conflict/RelatedObjectsExistException.java b/src/main/java/com/codesdream/ase/exception/conflict/RelatedObjectsExistException.java new file mode 100644 index 0000000..48fb482 --- /dev/null +++ b/src/main/java/com/codesdream/ase/exception/conflict/RelatedObjectsExistException.java @@ -0,0 +1,13 @@ +package com.codesdream.ase.exception.conflict; + +import lombok.NoArgsConstructor; + +/** + * 存在与之相关联的对象 + */ +@NoArgsConstructor +public class RelatedObjectsExistException extends RuntimeException { + public RelatedObjectsExistException(String msg){ + super(msg); + } +} diff --git a/src/main/java/com/codesdream/ase/exception/BaseInformationNotExistException.java b/src/main/java/com/codesdream/ase/exception/notfound/BaseInformationNotFoundException.java similarity index 50% rename from src/main/java/com/codesdream/ase/exception/BaseInformationNotExistException.java rename to src/main/java/com/codesdream/ase/exception/notfound/BaseInformationNotFoundException.java index 65da967..961d435 100644 --- a/src/main/java/com/codesdream/ase/exception/BaseInformationNotExistException.java +++ b/src/main/java/com/codesdream/ase/exception/notfound/BaseInformationNotFoundException.java @@ -1,16 +1,16 @@ -package com.codesdream.ase.exception; +package com.codesdream.ase.exception.notfound; import lombok.Data; import lombok.EqualsAndHashCode; @EqualsAndHashCode(callSuper = true) @Data -public class BaseInformationNotExistException extends RuntimeException { +public class BaseInformationNotFoundException extends NotFoundException { private String className; private String value; - public BaseInformationNotExistException(Class baseInformationClass, String value){ - super(); + public BaseInformationNotFoundException(Class baseInformationClass, String value){ + super(String.format("%s: %s", baseInformationClass.getName(), value)); this.className = baseInformationClass.getName(); this.value = value; } diff --git a/src/main/java/com/codesdream/ase/service/BaseInformationService.java b/src/main/java/com/codesdream/ase/service/BaseInformationService.java index 072ec94..ec287a5 100644 --- a/src/main/java/com/codesdream/ase/service/BaseInformationService.java +++ b/src/main/java/com/codesdream/ase/service/BaseInformationService.java @@ -1,9 +1,9 @@ package com.codesdream.ase.service; import com.codesdream.ase.component.datamanager.DataTable; -import com.codesdream.ase.exception.BaseInformationAlreadyExistException; +import com.codesdream.ase.exception.badrequest.BaseInformationAlreadyExistException; import com.codesdream.ase.exception.BaseInformationIllegalException; -import com.codesdream.ase.exception.BaseInformationNotExistException; +import com.codesdream.ase.exception.notfound.BaseInformationNotFoundException; import com.codesdream.ase.model.information.*; import com.codesdream.ase.repository.information.*; import lombok.extern.slf4j.Slf4j; @@ -103,7 +103,7 @@ public class BaseInformationService implements IBaseInformationService { if(administrativeDivision.isPresent()) { return administrativeDivision.get(); } - else throw new BaseInformationNotExistException(BaseAdministrativeDivision.class, name); + else throw new BaseInformationNotFoundException(BaseAdministrativeDivision.class, name); } return administrativeDivision.get(); @@ -114,7 +114,7 @@ public class BaseInformationService implements IBaseInformationService { Optional college = collegeRepository.findByName(name); // 检查 - if(!college.isPresent()) throw new BaseInformationNotExistException(BaseCollege.class, name); + if(!college.isPresent()) throw new BaseInformationNotFoundException(BaseCollege.class, name); return college.get(); } @@ -122,7 +122,7 @@ public class BaseInformationService implements IBaseInformationService { public BaseEthnic findEthnicByName(String name) { Optional ethnic = ethnicRepository.findByName(name); - if(!ethnic.isPresent()) throw new BaseInformationNotExistException(BaseEthnic.class, name); + if(!ethnic.isPresent()) throw new BaseInformationNotFoundException(BaseEthnic.class, name); return ethnic.get(); } @@ -130,7 +130,7 @@ public class BaseInformationService implements IBaseInformationService { public BaseMajor findMajorByName(String name) { Optional major = majorRepository.findByName(name); - if(!major.isPresent()) throw new BaseInformationNotExistException(BaseMajor.class, name); + if(!major.isPresent()) throw new BaseInformationNotFoundException(BaseMajor.class, name); return major.get(); } @@ -139,7 +139,7 @@ public class BaseInformationService implements IBaseInformationService { Optional politicalStatus = politicalStatusRepository.findByName(name); if(!politicalStatus.isPresent()) - throw new BaseInformationNotExistException(BasePoliticalStatus.class, name); + throw new BaseInformationNotFoundException(BasePoliticalStatus.class, name); return politicalStatus.get(); } @@ -148,7 +148,7 @@ public class BaseInformationService implements IBaseInformationService { Optional candidateCategory = candidateCategoryRepository.findByName(name); if(!candidateCategory.isPresent()) - throw new BaseInformationNotExistException(BaseCandidateCategory.class, name); + throw new BaseInformationNotFoundException(BaseCandidateCategory.class, name); return candidateCategory.get(); } @@ -157,7 +157,7 @@ public class BaseInformationService implements IBaseInformationService { Optional studentInfo = studentInfoRepository.findByStudentId(studentId); if(!studentInfo.isPresent()) - throw new BaseInformationNotExistException(BaseStudentInfo.class, studentId); + throw new BaseInformationNotFoundException(BaseStudentInfo.class, studentId); return studentInfo.get(); } @@ -202,7 +202,7 @@ public class BaseInformationService implements IBaseInformationService { row.elementAt(infoIndex.elementAt(7)), row.elementAt(infoIndex.elementAt(8))); save(studentInfo); - } catch (BaseInformationNotExistException e){ + } catch (BaseInformationNotFoundException e){ String log_info = String.format("一项学生信息的某项基本信息未在数据库找到, 该项数据无效." + " %s: %s",e.getClassName(), e.getValue()); log.warn(log_info); @@ -281,7 +281,7 @@ public class BaseInformationService implements IBaseInformationService { public BaseStudentInfo update(BaseStudentInfo baseStudentInfo) { // 更新前检查 if(!checkStudentInfo(baseStudentInfo.getStudentId())) - throw new BaseInformationNotExistException(BaseStudentInfo.class, baseStudentInfo.getStudentId()); + throw new BaseInformationNotFoundException(BaseStudentInfo.class, baseStudentInfo.getStudentId()); return studentInfoRepository.save(baseStudentInfo); } } diff --git a/src/main/java/com/codesdream/ase/service/IPermissionService.java b/src/main/java/com/codesdream/ase/service/IPermissionService.java index fc1cb70..ca8ec6e 100644 --- a/src/main/java/com/codesdream/ase/service/IPermissionService.java +++ b/src/main/java/com/codesdream/ase/service/IPermissionService.java @@ -4,6 +4,7 @@ import com.codesdream.ase.model.permission.*; import javafx.util.Pair; import java.util.Collection; +import java.util.List; import java.util.Optional; import java.util.Set; @@ -23,6 +24,11 @@ public interface IPermissionService { // 查找用户标签 Optional findTag(Integer id); + // 列出所有的标签 + Iterable findAllTag(); + + Set findTags(List names); + // 查找功能性权限容器 Optional findFPC(String name); @@ -95,6 +101,8 @@ public interface IPermissionService { Tag save(Tag tag); + void delete(Tag tag); + FunctionalPermissionContainer save(FunctionalPermissionContainer fpc); ScopePermissionContainer save(ScopePermissionContainer spc); diff --git a/src/main/java/com/codesdream/ase/service/IUserService.java b/src/main/java/com/codesdream/ase/service/IUserService.java index fbd20db..8426fd2 100644 --- a/src/main/java/com/codesdream/ase/service/IUserService.java +++ b/src/main/java/com/codesdream/ase/service/IUserService.java @@ -8,6 +8,7 @@ import org.springframework.security.core.GrantedAuthority; import java.util.Collection; import java.util.List; import java.util.Optional; +import java.util.Set; public interface IUserService { @@ -42,6 +43,8 @@ public interface IUserService { // 更具学号获得对应的用户名 String getUsernameByStudentId(String studentId); + Set findUsersById(Set usersId); + // 随机生成一个用户名 void generateRandomUsername(User user); diff --git a/src/main/java/com/codesdream/ase/service/PermissionService.java b/src/main/java/com/codesdream/ase/service/PermissionService.java index 73ad410..918c7f2 100644 --- a/src/main/java/com/codesdream/ase/service/PermissionService.java +++ b/src/main/java/com/codesdream/ase/service/PermissionService.java @@ -2,6 +2,7 @@ package com.codesdream.ase.service; import com.codesdream.ase.component.permission.UserFPCListGenerator; import com.codesdream.ase.component.permission.UserFSRGenerator; +import com.codesdream.ase.exception.notfound.NotFoundException; import com.codesdream.ase.model.permission.*; import com.codesdream.ase.repository.permission.FunctionalPermissionContainerRepository; import com.codesdream.ase.repository.permission.PermissionContainersCollectionRepository; @@ -68,6 +69,22 @@ public class PermissionService implements IPermissionService { return tagRepository.findById(id); } + @Override + public Iterable findAllTag() { + return tagRepository.findAll(); + } + + @Override + public Set findTags(List names) { + Set tagSet = new HashSet<>(); + for(String name : names){ + Optional tag = findTag(name); + if(!tag.isPresent()) throw new NotFoundException(name); + tagSet.add(tag.get()); + } + return tagSet; + } + @Override public Optional findFPC(String name) { return fpcRepository.findByName(name); @@ -223,6 +240,11 @@ public class PermissionService implements IPermissionService { return tagRepository.save(tag); } + @Override + public void delete(Tag tag) { + tagRepository.delete(tag); + } + @Override public FunctionalPermissionContainer save(FunctionalPermissionContainer fpc) { if(fpcRepository.findByName(fpc.getName()).isPresent()) diff --git a/src/main/java/com/codesdream/ase/service/UserService.java b/src/main/java/com/codesdream/ase/service/UserService.java index 3fd26b1..f4710e4 100644 --- a/src/main/java/com/codesdream/ase/service/UserService.java +++ b/src/main/java/com/codesdream/ase/service/UserService.java @@ -5,7 +5,7 @@ import com.codesdream.ase.component.auth.ASEUsernameEncoder; import com.codesdream.ase.component.permission.UserRolesListGenerator; import com.codesdream.ase.exception.UserInformationIllegalException; import com.codesdream.ase.exception.notfound.UserNotFoundException; -import com.codesdream.ase.exception.UsernameAlreadyExistException; +import com.codesdream.ase.exception.badrequest.UsernameAlreadyExistException; import com.codesdream.ase.model.information.BaseStudentInfo; import com.codesdream.ase.model.permission.User; import com.codesdream.ase.repository.permission.UserRepository; @@ -15,10 +15,7 @@ import org.springframework.security.core.userdetails.UsernameNotFoundException; import org.springframework.stereotype.Service; import javax.annotation.Resource; -import java.util.Collection; -import java.util.List; -import java.util.Optional; -import java.util.UUID; +import java.util.*; @Service public class UserService implements IUserService { @@ -86,6 +83,17 @@ public class UserService implements IUserService { return usernameEncoder.encode(studentId); } + @Override + public Set findUsersById(Set usersId) { + Set userSet = new HashSet<>(); + for(Integer id : usersId){ + Optional user = findUserById(id); + if(!user.isPresent()) throw new UserNotFoundException(String.format("ID: %d", id)); + userSet.add(user.get()); + } + return userSet; + } + @Override public void generateRandomUsername(User user) { user.setUsername(usernameEncoder.encode(UUID.randomUUID().toString()));