编写标签相关接口;完善用户管理服务层;完善权限管理服务层;完善错误管理子系统;
This commit is contained in:
parent
f4d00f349f
commit
e09f373fc0
@ -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态)
|
||||
|
@ -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<Integer> 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());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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<Integer> users;
|
||||
|
||||
|
||||
public JsonableTagUserList(Tag tag){
|
||||
for(User user : tag.getUsers()){
|
||||
users.add(user.getId());
|
||||
}
|
||||
}
|
||||
}
|
@ -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();
|
||||
}
|
||||
}
|
@ -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<Object> 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<Object> 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<Object> handleNotAcceptable(Exception ex) {
|
||||
return getResponse(HttpStatus.NOT_ACCEPTABLE, ex);
|
||||
}
|
||||
|
||||
@ExceptionHandler(value = {RelatedObjectsExistException.class})
|
||||
public ResponseEntity<Object> handleConflict(Exception ex) {
|
||||
return getResponse(HttpStatus.CONFLICT, ex);
|
||||
}
|
||||
|
||||
private ResponseEntity<Object> 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){
|
||||
|
@ -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<Tag> 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<JSONObject> 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<JsonableTag> listTag(){
|
||||
Iterable<Tag> tagIterable = permissionService.findAllTag();
|
||||
Set<JsonableTag> 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> 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> user = userService.findUserById(userId);
|
||||
if(!user.isPresent()) return jsonRespond.getRespond406("User Not Exist");
|
||||
permissionService.delete(tag.get());
|
||||
}
|
||||
|
||||
Optional<Tag> 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> 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> tag = permissionService.findTag(name);
|
||||
if(!tag.isPresent()) throw new NotFoundException(name);
|
||||
|
||||
Set<Integer> 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> tag = permissionService.findTag(name);
|
||||
if(!tag.isPresent()) throw new NotFoundException(name);
|
||||
Set<User> newUserSet = userService.findUsersById(new HashSet<>(userList.getUsers()));
|
||||
|
||||
Set<User> 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> tag = permissionService.findTag(name);
|
||||
if(!tag.isPresent()) throw new NotFoundException(name);
|
||||
Set<User> userSet = tag.get().getUsers();
|
||||
Set<User> 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<JsonableUser> getUserTags(@RequestParam(value = "name") List<String> names){
|
||||
Set<Tag> tagSet = permissionService.findTags(names);
|
||||
Set<User> userSet = new HashSet<>();
|
||||
Set<JsonableUser> jsonableUsers = new HashSet<>();
|
||||
for(Tag tag : tagSet){
|
||||
userSet.addAll(tag.getUsers());
|
||||
}
|
||||
for(User user : userSet){
|
||||
jsonableUsers.add(new JsonableUser(user));
|
||||
}
|
||||
return jsonableUsers;
|
||||
}
|
||||
|
||||
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
@ -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;
|
||||
}
|
@ -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;
|
||||
}
|
||||
}
|
@ -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);
|
||||
}
|
||||
}
|
@ -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;
|
||||
}
|
@ -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<BaseCollege> 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<BaseEthnic> 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<BaseMajor> 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<BasePoliticalStatus> 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<BaseCandidateCategory> 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<BaseStudentInfo> 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);
|
||||
}
|
||||
}
|
||||
|
@ -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<Tag> findTag(Integer id);
|
||||
|
||||
// 列出所有的标签
|
||||
Iterable<Tag> findAllTag();
|
||||
|
||||
Set<Tag> findTags(List<String> names);
|
||||
|
||||
// 查找功能性权限容器
|
||||
Optional<FunctionalPermissionContainer> 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);
|
||||
|
@ -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<User> findUsersById(Set<Integer> usersId);
|
||||
|
||||
// 随机生成一个用户名
|
||||
void generateRandomUsername(User user);
|
||||
|
||||
|
@ -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<Tag> findAllTag() {
|
||||
return tagRepository.findAll();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<Tag> findTags(List<String> names) {
|
||||
Set<Tag> tagSet = new HashSet<>();
|
||||
for(String name : names){
|
||||
Optional<Tag> tag = findTag(name);
|
||||
if(!tag.isPresent()) throw new NotFoundException(name);
|
||||
tagSet.add(tag.get());
|
||||
}
|
||||
return tagSet;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<FunctionalPermissionContainer> 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())
|
||||
|
@ -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<User> findUsersById(Set<Integer> usersId) {
|
||||
Set<User> userSet = new HashSet<>();
|
||||
for(Integer id : usersId){
|
||||
Optional<User> 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()));
|
||||
|
Loading…
Reference in New Issue
Block a user