Skip to content

Commit

Permalink
fix v2 issue #9
Browse files Browse the repository at this point in the history
  • Loading branch information
odbz committed Feb 20, 2024
1 parent 7bec852 commit 004a5ea
Show file tree
Hide file tree
Showing 2 changed files with 291 additions and 260 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,190 +58,208 @@
@RestController
public class AdminController extends BaseController {

public static final int SUGGEST_LIMIT = 10;
public static final int DEFAULT_PAGE_SIZE = 10;
private final Map<String, Consumer<List<String>>> propertiesMap = new HashMap<>();
@Autowired
private AnalysisToSubmissionDTOConverter analysisToSubmissionDTO;
@Autowired
private GenericConversionService conversionService;
@Autowired
private AnalysisRepository analysisRepository;
@Autowired
private AnalysisService analysisService;
@Autowired
private DataNodeService dataNodeService;
@Autowired
private ExecutionEngineIntegrationService executionEngineIntegrationService;

public AdminController(UserService userService) {
super(userService);
initProps();
}
public static final int SUGGEST_LIMIT = 10;
public static final int DEFAULT_PAGE_SIZE = 10;
private final Map<String, Consumer<List<String>>> propertiesMap = new HashMap<>();
@Autowired
private AnalysisToSubmissionDTOConverter analysisToSubmissionDTO;
@Autowired
private GenericConversionService conversionService;
@Autowired
private AnalysisRepository analysisRepository;
@Autowired
private AnalysisService analysisService;
@Autowired
private DataNodeService dataNodeService;
@Autowired
private ExecutionEngineIntegrationService executionEngineIntegrationService;

public AdminController(UserService userService) {
super(userService);
initProps();
}

@ApiOperation(value = "Get all admins", hidden = true)
@GetMapping("/api/v1/admin/admins")
public JsonResult<List<UserDTO>> getAdmins(
@RequestParam(name = "sortBy", required = false) String sortBy,
@RequestParam(name = "sortAsc", required = false) Boolean sortAsc
) throws PermissionDeniedException {

JsonResult<List<UserDTO>> result;
List<User> users = userService.getAllAdmins(sortBy, sortAsc);
List<UserDTO> dtos = users.stream()
.map(user -> conversionService.convert(user, UserDTO.class))
.collect(Collectors.toList());
result = new JsonResult<>(JsonResult.ErrorCode.NO_ERROR);
result.setResult(dtos);
return result;
}

@ApiOperation("Suggests user according to query to add admin")
@GetMapping("/api/v1/admin/admins/suggest")
public JsonResult<List<UserDTO>> suggestAdmins(
Principal principal,
@RequestParam("query") String query,
@RequestParam(value = "limit", required = false) Integer limit
) {

JsonResult<List<UserDTO>> result = new JsonResult<>(JsonResult.ErrorCode.NO_ERROR);
userService
.findByUsername(principal.getName())
.ifPresent(user -> {
List<User> users = userService.suggestNotAdmin(user, query,
limit == null ? SUGGEST_LIMIT : limit);
result.setResult(users.stream().map(u -> conversionService
.convert(u, UserDTO.class))
.collect(Collectors.toList())
);
});
return result;
}

@ApiOperation(value = "Get all admins", hidden = true)
@GetMapping("/api/v1/admin/admins")
public JsonResult<List<UserDTO>> getAdmins(
@RequestParam(name = "sortBy", required = false) String sortBy,
@RequestParam(name = "sortAsc", required = false) Boolean sortAsc
) throws PermissionDeniedException {

JsonResult<List<UserDTO>> result;
List<User> users = userService.getAllAdmins(sortBy, sortAsc);
List<UserDTO> dtos = users.stream()
.map(user -> conversionService.convert(user, UserDTO.class))
.collect(Collectors.toList());
result = new JsonResult<>(JsonResult.ErrorCode.NO_ERROR);
result.setResult(dtos);
return result;
}
@ApiOperation("Remove admin")
@DeleteMapping("/api/v1/admin/admins/{username:.+}")
public JsonResult<?> removeAdmin(@PathVariable String username) {

@ApiOperation("Suggests user according to query to add admin")
@GetMapping("/api/v1/admin/admins/suggest")
public JsonResult<List<UserDTO>> suggestAdmins(
Principal principal,
@RequestParam("query") String query,
@RequestParam(value = "limit", required = false) Integer limit
) {

JsonResult<List<UserDTO>> result = new JsonResult<>(JsonResult.ErrorCode.NO_ERROR);
userService
.findByUsername(principal.getName())
.ifPresent(user -> {
List<User> users = userService.suggestNotAdmin(user, query, limit == null ? SUGGEST_LIMIT : limit);
result.setResult(users.stream().map(u -> conversionService
.convert(u, UserDTO.class))
.collect(Collectors.toList())
);
});
return result;
}
userService.findByUsername(username).ifPresent(user -> userService.remove(user.getId()));
return new JsonResult<>(JsonResult.ErrorCode.NO_ERROR);
}

@ApiOperation("Remove admin")
@DeleteMapping("/api/v1/admin/admins/{username:.+}")
public JsonResult<?> removeAdmin(@PathVariable String username) {
@ApiOperation(value = "Invalidate all unfinished analyses")
@PostMapping(Constants.Api.Analysis.INVALIDATE_ALL_UNFINISHED)
public Integer invalidateAllUnfinishedAnalyses(final Principal principal)
throws PermissionDeniedException {

userService.findByUsername(username).ifPresent(user -> userService.remove(user.getId()));
return new JsonResult<>(JsonResult.ErrorCode.NO_ERROR);
if (principal == null) {
throw new AuthException("user not found");
}

@ApiOperation(value = "Invalidate all unfinished analyses")
@PostMapping(Constants.Api.Analysis.INVALIDATE_ALL_UNFINISHED)
public Integer invalidateAllUnfinishedAnalyses(final Principal principal) throws PermissionDeniedException {

if (principal == null) {
throw new AuthException("user not found");
return analysisService.invalidateAllUnfinishedAnalyses(getUser(principal));
}

@ApiOperation(value = "list submissions")
@GetMapping("/api/v1/admin/submissions")
public Page<SubmissionDTO> list(@PageableDefault(value = DEFAULT_PAGE_SIZE, sort = "id",
direction = Sort.Direction.DESC) Pageable pageable) {

Page<Analysis> analyses;
String sortField = isFinishedSort(pageable) ? "finished"
: isSubmittedSort(pageable) ? "submitted" : isStudySort(pageable) ? "study"
: (isAnalysisSort(pageable) ? "analysis" : (isStatusSort(pageable) ? "status" : ""));

if (pageable.getSort() == null || "".equals(sortField)) {
Pageable p;
if (!isCustomSort(pageable)) {
p = pageable;
} else {
p = buildPageRequest(pageable);
}
analyses = analysisRepository.findAll(p);
} else {
if (sortField.contains("finished") || sortField.contains("submitted")) {
analyses = analysisRepository.findAllPagedOrderBySubmittedAndFinished(
pageable.getSort().get().findFirst().get().getDirection().name()
, sortField
, PageRequest.of(pageable.getPageNumber(), pageable.getPageSize()));
} else {
analyses = analysisRepository.findAllPagedOrderByAnalysisStudyStatus(
pageable.getSort().get().findFirst().get().getDirection().name()
, sortField
, PageRequest.of(pageable.getPageNumber(), pageable.getPageSize()));
}

return analysisService.invalidateAllUnfinishedAnalyses(getUser(principal));
}
return analyses.map(analysis -> analysisToSubmissionDTO.convert(analysis));
}

@ApiOperation(value = "list submissions")
@GetMapping("/api/v1/admin/submissions")
public Page<SubmissionDTO> list(@PageableDefault(value = DEFAULT_PAGE_SIZE, sort = "id",
direction = Sort.Direction.DESC) Pageable pageable) {
Page<Analysis> analyses;
String sortField= isFinishedSort(pageable) ?"finished":
(isSubmittedSort(pageable) ?"submitted":
(isStatusSort(pageable)?"status":""));
if(pageable.getSort() == null || "".equals(sortField))
{
Pageable p;
if (!isCustomSort(pageable)) {
p = pageable;
} else {
p = buildPageRequest(pageable);
}
analyses = analysisRepository.findAll(p);
}else{
analyses = analysisRepository.findAllPagedOrderByCustomFields( pageable.getSort().get().findFirst().get().getDirection().name()
,sortField
,PageRequest.of(pageable.getPageNumber(), pageable.getPageSize()));
}
return analyses.map(analysis -> analysisToSubmissionDTO.convert(analysis));
}
@ApiOperation(value = "get execution engine status")
@GetMapping("/api/v1/admin/execution-engine/status")
public EngineStatusResponse getExecutionEngineStatus() {
return new EngineStatusResponse(executionEngineIntegrationService.getExecutionEngineStatus());
}

protected Pageable buildPageRequest(Pageable pageable) {

@ApiOperation(value = "get execution engine status")
@GetMapping("/api/v1/admin/execution-engine/status")
public EngineStatusResponse getExecutionEngineStatus() {
return new EngineStatusResponse(executionEngineIntegrationService.getExecutionEngineStatus());
if (pageable.getSort() == null) {
return pageable;
}
PageRequest result;
List<String> properties = new LinkedList<>();
Sort.Direction direction = Sort.Direction.ASC;
for (Sort.Order order : pageable.getSort()) {
direction = order.getDirection();
String property = order.getProperty();
if (propertiesMap.containsKey(property)) {
propertiesMap.get(property).accept(properties);
} else {
properties.add(property);
}
}
result = PageRequest.of(pageable.getPageNumber(), pageable.getPageSize(), direction,
properties.toArray(new String[properties.size()]));

protected Pageable buildPageRequest(Pageable pageable) {
return result;
}

if (pageable.getSort() == null) {
return pageable;
}
PageRequest result;
List<String> properties = new LinkedList<>();
Sort.Direction direction = Sort.Direction.ASC;
for (Sort.Order order : pageable.getSort()) {
direction = order.getDirection();
String property = order.getProperty();
if (propertiesMap.containsKey(property)) {
propertiesMap.get(property).accept(properties);
} else {
properties.add(property);
}
}
result = PageRequest.of(pageable.getPageNumber(), pageable.getPageSize(), direction,
properties.toArray(new String[properties.size()]));
private boolean isCustomSort(final Pageable pageable) {

return result;
}
return isSortOf(pageable, propertiesMap::containsKey);
}

private boolean isCustomSort(final Pageable pageable) {
private boolean isStatusSort(final Pageable pageable) {

return isSortOf(pageable, propertiesMap::containsKey);
}
return isSortOf(pageable, "status"::equals);
}

private boolean isStatusSort(final Pageable pageable) {
private boolean isAnalysisSort(final Pageable pageable) {
return isSortOf(pageable, "analysis"::equals);
}

return isSortOf(pageable, "status"::equals);
}
private boolean isStudySort(final Pageable pageable) {
return isSortOf(pageable, "study"::equals);
}

private boolean isSubmittedSort(final Pageable pageable) {
private boolean isSubmittedSort(final Pageable pageable) {

return isSortOf(pageable, "submitted"::equals);
}
return isSortOf(pageable, "submitted"::equals);
}

private boolean isFinishedSort(final Pageable pageable) {
private boolean isFinishedSort(final Pageable pageable) {

return isSortOf(pageable, "finished"::equals);
}
return isSortOf(pageable, "finished"::equals);
}

private boolean isSortOf(final Pageable pageable, Function<String, Boolean> predicate) {
private boolean isSortOf(final Pageable pageable, Function<String, Boolean> predicate) {

if (pageable.getSort() == null) {
return false;
}
for (Sort.Order order : pageable.getSort()) {
if (predicate.apply(order.getProperty())) {
return true;
}
}
return false;
if (pageable.getSort() == null) {
return false;
}
for (Sort.Order order : pageable.getSort()) {
if (predicate.apply(order.getProperty())) {
return true;
}
}
return false;
}

protected void initProps() {
protected void initProps() {

propertiesMap.put("author.fullName", p -> {
p.add("author.firstName");
p.add("author.lastName");
});
propertiesMap.put("fullName", p -> {
p.add("author.firstName");
p.add("author.lastName");
});
propertiesMap.put("analysis", p -> p.add("title"));
propertiesMap.put("study", p -> p.add("studyTitle"));
propertiesMap.put("status", p -> p.add("journal.state"));
}
propertiesMap.put("author.fullName", p -> {
p.add("author.firstName");
p.add("author.lastName");
});
propertiesMap.put("fullName", p -> {
p.add("author.firstName");
p.add("author.lastName");
});
}

private class EngineStatusResponse {
public EngineStatusResponse(final ExecutionEngineStatus status) {
this.status = status;
}
public ExecutionEngineStatus status;
private class EngineStatusResponse {

public EngineStatusResponse(final ExecutionEngineStatus status) {
this.status = status;
}

public ExecutionEngineStatus status;
}
}
Loading

0 comments on commit 004a5ea

Please sign in to comment.