package eu.eudat.controllers; import eu.eudat.audit.AuditableAction; import eu.eudat.authorization.AuthorizationFlags; import eu.eudat.commons.enums.DmpAccessType; import eu.eudat.commons.enums.DmpStatus; import eu.eudat.commons.enums.IsActive; import eu.eudat.model.builder.PublicDmpBuilder; import gr.cite.tools.validation.ValidationFilterAnnotation; import eu.eudat.model.*; import eu.eudat.model.builder.DmpBuilder; import eu.eudat.model.censorship.DmpCensor; import eu.eudat.model.censorship.PublicDmpCensor; import eu.eudat.model.persist.*; import eu.eudat.model.result.QueryResult; import eu.eudat.query.DmpQuery; import eu.eudat.query.lookup.DmpLookup; import eu.eudat.service.dmp.DmpService; import eu.eudat.service.elastic.ElasticQueryHelperService; import gr.cite.tools.auditing.AuditService; import gr.cite.tools.data.builder.BuilderFactory; import gr.cite.tools.data.censor.CensorFactory; import gr.cite.tools.data.query.QueryFactory; import gr.cite.tools.exception.MyApplicationException; import gr.cite.tools.exception.MyForbiddenException; import gr.cite.tools.exception.MyNotFoundException; import gr.cite.tools.fieldset.FieldSet; import gr.cite.tools.logging.LoggerService; import gr.cite.tools.logging.MapLogEntry; import jakarta.xml.bind.JAXBException; import org.slf4j.LoggerFactory; import org.springframework.context.MessageSource; import org.springframework.context.i18n.LocaleContextHolder; import org.springframework.http.ResponseEntity; import org.springframework.transaction.annotation.Transactional; import org.springframework.web.bind.annotation.*; import javax.management.InvalidApplicationException; import javax.xml.parsers.ParserConfigurationException; import javax.xml.transform.TransformerException; import java.io.IOException; import java.util.*; import static eu.eudat.authorization.AuthorizationFlags.Public; @RestController @RequestMapping(path = "api/dmp") public class DmpController { private static final LoggerService logger = new LoggerService(LoggerFactory.getLogger(DmpController.class)); private final BuilderFactory builderFactory; private final AuditService auditService; private final DmpService dmpService; private final CensorFactory censorFactory; private final QueryFactory queryFactory; private final MessageSource messageSource; private final ElasticQueryHelperService elasticQueryHelperService; public DmpController( BuilderFactory builderFactory, AuditService auditService, DmpService dmpService, CensorFactory censorFactory, QueryFactory queryFactory, MessageSource messageSource, ElasticQueryHelperService elasticQueryHelperService) { this.builderFactory = builderFactory; this.auditService = auditService; this.dmpService = dmpService; this.censorFactory = censorFactory; this.queryFactory = queryFactory; this.messageSource = messageSource; this.elasticQueryHelperService = elasticQueryHelperService; } @PostMapping("public/query") public QueryResult publicQuery(@RequestBody DmpLookup lookup) throws MyApplicationException, MyForbiddenException { logger.debug("querying {}", Dmp.class.getSimpleName()); this.censorFactory.censor(PublicDmpCensor.class).censor(lookup.getProject()); //DmpQuery query = lookup.enrich(this.queryFactory).authorize(EnumSet.of(Public)).dmpSubQuery(this.queryFactory.query(DmpQuery.class).isActive(IsActive.Active).statuses(DmpStatus.Finalized).accessTypes(DmpAccessType.Public)); QueryResult queryResult = this.elasticQueryHelperService.collectPublic(lookup, EnumSet.of(Public), null); this.auditService.track(AuditableAction.Dmp_PublicQuery, "lookup", lookup); return queryResult; } @GetMapping("public/{id}") public PublicDmp publicGet(@PathVariable("id") UUID id, FieldSet fieldSet, Locale locale) throws MyApplicationException, MyForbiddenException, MyNotFoundException { logger.debug(new MapLogEntry("retrieving" + Dmp.class.getSimpleName()).And("id", id).And("fields", fieldSet)); this.censorFactory.censor(PublicDmpCensor.class).censor(fieldSet); DmpQuery query = this.queryFactory.query(DmpQuery.class).authorize(EnumSet.of(Public)).ids(id).isActive(IsActive.Active).statuses(DmpStatus.Finalized).accessTypes(DmpAccessType.Public); PublicDmp model = this.builderFactory.builder(PublicDmpBuilder.class).authorize(EnumSet.of(Public)).build(fieldSet, query.firstAs(fieldSet)); if (model == null) throw new MyNotFoundException(messageSource.getMessage("General_ItemNotFound", new Object[]{id, Dmp.class.getSimpleName()}, LocaleContextHolder.getLocale())); this.auditService.track(AuditableAction.Dmp_PublicLookup, Map.ofEntries( new AbstractMap.SimpleEntry("id", id), new AbstractMap.SimpleEntry("fields", fieldSet) )); return model; } @PostMapping("query") public QueryResult Query(@RequestBody DmpLookup lookup) throws MyApplicationException, MyForbiddenException { logger.debug("querying {}", Dmp.class.getSimpleName()); this.censorFactory.censor(DmpCensor.class).censor(lookup.getProject(), null); QueryResult queryResult = this.elasticQueryHelperService.collect(lookup, AuthorizationFlags.OwnerOrDmpAssociatedOrPermission, null); this.auditService.track(AuditableAction.Dmp_Query, "lookup", lookup); return queryResult; } @GetMapping("{id}") public Dmp Get(@PathVariable("id") UUID id, FieldSet fieldSet, Locale locale) throws MyApplicationException, MyForbiddenException, MyNotFoundException { logger.debug(new MapLogEntry("retrieving" + Dmp.class.getSimpleName()).And("id", id).And("fields", fieldSet)); this.censorFactory.censor(DmpCensor.class).censor(fieldSet, null); DmpQuery query = this.queryFactory.query(DmpQuery.class).authorize(AuthorizationFlags.OwnerOrDmpAssociatedOrPermission).ids(id); Dmp model = this.builderFactory.builder(DmpBuilder.class).authorize(AuthorizationFlags.OwnerOrDmpAssociatedOrPermission).build(fieldSet, query.firstAs(fieldSet)); if (model == null) throw new MyNotFoundException(messageSource.getMessage("General_ItemNotFound", new Object[]{id, Dmp.class.getSimpleName()}, LocaleContextHolder.getLocale())); this.auditService.track(AuditableAction.Dmp_Lookup, Map.ofEntries( new AbstractMap.SimpleEntry("id", id), new AbstractMap.SimpleEntry("fields", fieldSet) )); return model; } @PostMapping("persist") @Transactional @ValidationFilterAnnotation(validator = DmpPersist.DmpPersistValidator.ValidatorName, argumentName = "model") public Dmp Persist(@RequestBody DmpPersist model, FieldSet fieldSet) throws MyApplicationException, MyForbiddenException, MyNotFoundException, InvalidApplicationException, IOException, JAXBException { logger.debug(new MapLogEntry("persisting" + Dmp.class.getSimpleName()).And("model", model).And("fieldSet", fieldSet)); Dmp persisted = this.dmpService.persist(model, fieldSet); this.auditService.track(AuditableAction.Dmp_Persist, Map.ofEntries( new AbstractMap.SimpleEntry("model", model), new AbstractMap.SimpleEntry("fields", fieldSet) )); return persisted; } @DeleteMapping("{id}") @Transactional public void Delete(@PathVariable("id") UUID id) throws MyForbiddenException, InvalidApplicationException, IOException { logger.debug(new MapLogEntry("retrieving" + Dmp.class.getSimpleName()).And("id", id)); this.dmpService.deleteAndSave(id); this.auditService.track(AuditableAction.Dmp_Delete, "id", id); } @PostMapping("finalize/{id}") @Transactional public boolean finalize(@PathVariable("id") UUID id, @RequestBody DescriptionsToBeFinalized descriptions) throws MyApplicationException, MyForbiddenException, MyNotFoundException, InvalidApplicationException, IOException { logger.debug(new MapLogEntry("finalizing" + Dmp.class.getSimpleName()).And("id", id).And("descriptionIds", descriptions.getDescriptionIds())); this.dmpService.finalize(id, descriptions.getDescriptionIds()); this.auditService.track(AuditableAction.Dmp_Finalize, Map.ofEntries( new AbstractMap.SimpleEntry("id", id), new AbstractMap.SimpleEntry("descriptionIds", descriptions.getDescriptionIds()) )); return true; } @GetMapping("undo-finalize/{id}") @Transactional public boolean undoFinalize(@PathVariable("id") UUID id, FieldSet fieldSet) throws MyApplicationException, MyForbiddenException, MyNotFoundException, InvalidApplicationException, IOException, JAXBException { logger.debug(new MapLogEntry("undo-finalizing" + Dmp.class.getSimpleName()).And("id", id)); this.censorFactory.censor(DmpCensor.class).censor(fieldSet, null); this.dmpService.undoFinalize(id, fieldSet); this.auditService.track(AuditableAction.Dmp_Undo_Finalize, Map.ofEntries( new AbstractMap.SimpleEntry("id", id) )); return true; } @GetMapping("validate/{id}") public DmpValidationResult validate(@PathVariable("id") UUID id) throws MyApplicationException, MyForbiddenException, MyNotFoundException, InvalidApplicationException { logger.debug(new MapLogEntry("validating" + Dmp.class.getSimpleName()).And("id", id)); this.censorFactory.censor(DmpCensor.class).censor(null, null); DmpValidationResult result = this.dmpService.validate(id); this.auditService.track(AuditableAction.Dmp_Validate, Map.ofEntries( new AbstractMap.SimpleEntry("id", id) )); return result; } @PostMapping("clone") @Transactional @ValidationFilterAnnotation(validator = CloneDmpPersist.CloneDmpPersistValidator.ValidatorName, argumentName = "model") public Dmp buildClone(@RequestBody CloneDmpPersist model, FieldSet fieldSet) throws MyApplicationException, MyForbiddenException, MyNotFoundException, IOException, InvalidApplicationException { logger.debug(new MapLogEntry("clone" + Dmp.class.getSimpleName()).And("model", model).And("fields", fieldSet)); this.censorFactory.censor(DmpCensor.class).censor(fieldSet, null); Dmp clone = this.dmpService.buildClone(model, fieldSet); this.auditService.track(AuditableAction.Dmp_Clone, Map.ofEntries( new AbstractMap.SimpleEntry("model", model), new AbstractMap.SimpleEntry("fields", fieldSet) )); return clone; } @PostMapping("new-version") @Transactional @ValidationFilterAnnotation(validator = NewVersionDmpPersist.NewVersionDmpPersistValidator.ValidatorName, argumentName = "model") public Dmp createNewVersion(@RequestBody NewVersionDmpPersist model, FieldSet fieldSet) throws MyApplicationException, MyForbiddenException, MyNotFoundException, JAXBException, IOException, TransformerException, InvalidApplicationException, ParserConfigurationException { logger.debug(new MapLogEntry("persisting" + NewVersionDmpPersist.class.getSimpleName()).And("model", model).And("fieldSet", fieldSet)); Dmp persisted = this.dmpService.createNewVersion(model, fieldSet); this.auditService.track(AuditableAction.Dmp_PersistNewVersion, Map.ofEntries( new AbstractMap.SimpleEntry("model", model), new AbstractMap.SimpleEntry("fields", fieldSet) )); return persisted; } @PostMapping("{id}/assign-users") @Transactional @ValidationFilterAnnotation(validator = DmpUserPersist.DmpUserPersistValidator.ValidatorName, argumentName = "model") public QueryResult assignUsers(@PathVariable("id") UUID id, @RequestBody List model, FieldSet fieldSet) throws InvalidApplicationException, IOException { logger.debug(new MapLogEntry("assigning users to dmp").And("model", model).And("fieldSet", fieldSet)); List persisted = this.dmpService.assignUsers(id, model, fieldSet, false); this.auditService.track(AuditableAction.Dmp_Assign_Users, Map.ofEntries( new AbstractMap.SimpleEntry("model", model), new AbstractMap.SimpleEntry("fields", fieldSet) )); return new QueryResult<>(persisted); } @PostMapping("remove-user") @Transactional @ValidationFilterAnnotation(validator = DmpUserRemovePersist.DmpUserRemovePersistValidator.ValidatorName, argumentName = "model") public QueryResult removeUser(@RequestBody DmpUserRemovePersist model, FieldSet fieldSet) throws InvalidApplicationException, IOException { logger.debug(new MapLogEntry("remove user from dmp").And("model", model).And("fieldSet", fieldSet)); Dmp persisted = this.dmpService.removeUser(model, fieldSet); this.auditService.track(AuditableAction.Dmp_RemoveUser, Map.ofEntries( new AbstractMap.SimpleEntry("model", model), new AbstractMap.SimpleEntry("fields", fieldSet) )); return new QueryResult<>(persisted); } @GetMapping("{id}/export/{transformerId}/{type}") public ResponseEntity export(@PathVariable("id") UUID id, @PathVariable("transformerId") String transformerId, @PathVariable("type") String exportType) throws InvalidApplicationException, IOException { logger.debug(new MapLogEntry("exporting dmp").And("id", id).And("transformerId", transformerId).And("exportType", exportType)); ResponseEntity bytes = this.dmpService.export(id, transformerId, exportType); this.auditService.track(AuditableAction.Dmp_Export, Map.ofEntries( new AbstractMap.SimpleEntry("id", id), new AbstractMap.SimpleEntry("transformerId", transformerId), new AbstractMap.SimpleEntry("exportType", exportType) )); return bytes; } @PostMapping("{id}/invite-users") @Transactional @ValidationFilterAnnotation(validator = DmpUserInvitePersist.DmpUserInvitePersistValidator.ValidatorName, argumentName = "model") public boolean inviteUsers(@PathVariable("id") UUID id, @RequestBody DmpUserInvitePersist model) throws InvalidApplicationException, JAXBException, IOException { logger.debug(new MapLogEntry("inviting users to dmp").And("model", model)); this.dmpService.inviteUserOrAssignUsers(id, model.getUsers()); this.auditService.track(AuditableAction.Dmp_Invite_Users, Map.ofEntries( new AbstractMap.SimpleEntry("model", model) )); return true; } @GetMapping("{id}/token/{token}/invite-accept") @Transactional public boolean acceptInvitation(@PathVariable("id") UUID id, @PathVariable("token") String token) throws InvalidApplicationException, JAXBException, IOException { logger.debug(new MapLogEntry("inviting users to dmp").And("id", id)); this.dmpService.dmpInvitationAccept(token); this.auditService.track(AuditableAction.Dmp_Invite_Accept, Map.ofEntries( new AbstractMap.SimpleEntry("token", token) )); return true; } }