Skip to content

Commit

Permalink
Added: FrontendSiteModule repeatable
Browse files Browse the repository at this point in the history
  • Loading branch information
djuarezgf committed Dec 30, 2023
1 parent e871017 commit cb227ae
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 30 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).

## [0.0.1 - 2023-12-26]
## [0.0.1 - 2023-12-30]
### Added
- First version of the project
- Spring Application
Expand Down Expand Up @@ -50,3 +50,4 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- Thymeleaf Template Engine Configuration
- EmailSender repeatable
- Emails to BRIDGHEAD_ADMINS_WHO_HAVE_NOT_ACCEPTED_NOR_REJECTED_THE_PROJECT
- FrontendSiteModule repeatable
6 changes: 2 additions & 4 deletions src/main/java/de/samply/annotations/FrontendSiteModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@

import jakarta.validation.constraints.NotNull;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.annotation.*;

@Repeatable(FrontendSiteModules.class)
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface FrontendSiteModule {
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/de/samply/annotations/FrontendSiteModules.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package de.samply.annotations;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface FrontendSiteModules {
FrontendSiteModule[] value();
}
4 changes: 0 additions & 4 deletions src/main/java/de/samply/app/ProjectManagerController.java
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,6 @@ public ResponseEntity<String> createProjectQuery(
}

@RoleConstraints(organisationRoles = {OrganisationRole.RESEARCHER})
@FrontendSiteModule(site = ProjectManagerConst.PROJECT_VIEW_SITE, module = ProjectManagerConst.PROJECT_STATE_MODULE)
@FrontendAction(action = ProjectManagerConst.CREATE_QUERY_AND_DESIGN_PROJECT_ACTION)
@PostMapping(value = ProjectManagerConst.CREATE_QUERY_AND_DESIGN_PROJECT)
public ResponseEntity<String> createQueryAndDesignProject(
@NotEmpty @RequestBody() String query,
Expand All @@ -185,8 +183,6 @@ public ResponseEntity<String> createQueryAndDesignProject(
}

@RoleConstraints(organisationRoles = {OrganisationRole.RESEARCHER})
@FrontendSiteModule(site = ProjectManagerConst.PROJECT_VIEW_SITE, module = ProjectManagerConst.PROJECT_STATE_MODULE)
@FrontendAction(action = ProjectManagerConst.DESIGN_PROJECT_ACTION)
@PostMapping(value = ProjectManagerConst.DESIGN_PROJECT)
public ResponseEntity<String> designProject(
@RequestParam(name = ProjectManagerConst.BRIDGEHEADS) String[] bridgeheads,
Expand Down
61 changes: 40 additions & 21 deletions src/main/java/de/samply/frontend/FrontendService.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
package de.samply.frontend;

import de.samply.annotations.FrontendAction;
import de.samply.annotations.FrontendSiteModule;
import de.samply.annotations.RoleConstraints;
import de.samply.annotations.StateConstraints;
import de.samply.annotations.*;
import de.samply.aop.ConstraintsService;
import de.samply.app.ProjectManagerController;
import de.samply.user.roles.RolesExtractor;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.util.UriComponentsBuilder;

import java.lang.reflect.Method;
import java.util.*;

@Service
Expand All @@ -30,31 +28,52 @@ public Collection<ModuleActionsPackage> fetchModuleActionPackage(String site, Op
Map<String, ModuleActionsPackage> moduleModuleActionsPackageMap = new HashMap<>();
String rootPath = RolesExtractor.getRootPath();
Arrays.stream(ProjectManagerController.class.getDeclaredMethods()).forEach(method -> {
FrontendSiteModules frontendSiteModules = method.getAnnotation(FrontendSiteModules.class);
FrontendSiteModule frontendSiteModule = method.getAnnotation(FrontendSiteModule.class);
FrontendAction frontendAction = method.getAnnotation((FrontendAction.class));
Optional<String> path = RolesExtractor.fetchPath(method);

if (frontendSiteModule != null && site.equals(frontendSiteModule.site()) && frontendAction != null && path.isPresent()) {
Optional<RoleConstraints> roleConstraints = Optional.ofNullable(method.getAnnotation(RoleConstraints.class));
Optional<ResponseEntity> responseEntity = this.constraintsService.checkRoleConstraints(roleConstraints, projectCode, bridgehead);
if (responseEntity.isEmpty()) {
Optional<StateConstraints> stateConstraints = Optional.ofNullable(method.getAnnotation(StateConstraints.class));
responseEntity = this.constraintsService.checkStateConstraints(stateConstraints, projectCode, bridgehead);
}
if (responseEntity.isEmpty()) { // If there are no restrictions
ModuleActionsPackage moduleActionsPackage = moduleModuleActionsPackageMap.get(frontendSiteModule.module());
if (moduleActionsPackage == null) {
moduleActionsPackage = new ModuleActionsPackage();
moduleActionsPackage.setModule(frontendSiteModule.module());
moduleModuleActionsPackageMap.put(frontendSiteModule.module(), moduleActionsPackage);
}
moduleActionsPackage.addAction(new Action(frontendAction.action(), rootPath + path.get()));
}
List<FrontendSiteModule> frontendSiteModuleList = new ArrayList<>();
if (frontendSiteModule != null) {
frontendSiteModuleList.add(frontendSiteModule);
}
if (frontendSiteModules != null && frontendSiteModules.value() != null && frontendSiteModules.value().length > 0) {
frontendSiteModuleList.addAll(List.of(frontendSiteModules.value()));
}
frontendSiteModuleList.forEach(tempFrontendSiteModule ->
fetchModuleActionsPackages(moduleModuleActionsPackageMap, rootPath, path, tempFrontendSiteModule, frontendAction, site, projectCode, bridgehead, method));
});
return moduleModuleActionsPackageMap.values();
}

private Map<String, ModuleActionsPackage> fetchModuleActionsPackages(Map<String, ModuleActionsPackage> moduleModuleActionsPackageMap,
String rootPath, Optional<String> path,
FrontendSiteModule frontendSiteModule, FrontendAction frontendAction,
String site, Optional<String> projectCode,
Optional<String> bridgehead, Method method) {
if (frontendSiteModule != null && site.equals(frontendSiteModule.site()) && frontendAction != null && path.isPresent()) {
Optional<RoleConstraints> roleConstraints = Optional.ofNullable(method.getAnnotation(RoleConstraints.class));
Optional<ResponseEntity> responseEntity = this.constraintsService.checkRoleConstraints(roleConstraints, projectCode, bridgehead);
if (responseEntity.isEmpty()) {
Optional<StateConstraints> stateConstraints = Optional.ofNullable(method.getAnnotation(StateConstraints.class));
responseEntity = this.constraintsService.checkStateConstraints(stateConstraints, projectCode, bridgehead);
}
if (responseEntity.isEmpty()) { // If there are no restrictions
addAction(moduleModuleActionsPackageMap, frontendSiteModule, frontendAction, rootPath, path);
}
}
return moduleModuleActionsPackageMap;
}

private void addAction(Map<String, ModuleActionsPackage> moduleModuleActionsPackageMap, FrontendSiteModule frontendSiteModule, FrontendAction frontendAction, String rootPath, Optional<String> path) {
ModuleActionsPackage moduleActionsPackage = moduleModuleActionsPackageMap.get(frontendSiteModule.module());
if (moduleActionsPackage == null) {
moduleActionsPackage = new ModuleActionsPackage();
moduleActionsPackage.setModule(frontendSiteModule.module());
moduleModuleActionsPackageMap.put(frontendSiteModule.module(), moduleActionsPackage);
}
moduleActionsPackage.addAction(new Action(frontendAction.action(), rootPath + path.get()));
}

public String fetchUrl(String site, Map<String, String> parameters) {
UriComponentsBuilder result = UriComponentsBuilder.fromHttpUrl(frontendConfiguration.getBaseUrl());
if (site != null) {
Expand Down

0 comments on commit cb227ae

Please sign in to comment.