package eu.eudat.controllers.v2; import com.fasterxml.jackson.core.JsonProcessingException; import eu.eudat.audit.AuditableAction; import eu.eudat.authorization.AuthorizationFlags; import eu.eudat.data.DmpEntity; import eu.eudat.file.transformer.model.file.FileFormat; import eu.eudat.model.Dmp; import eu.eudat.model.DmpUser; import eu.eudat.model.builder.DmpBuilder; import eu.eudat.model.censorship.DmpCensor; import eu.eudat.model.file.FileEnvelope; import eu.eudat.model.persist.*; import eu.eudat.model.result.QueryResult; import eu.eudat.models.data.dmp.DataManagementPlan; import eu.eudat.models.data.helpers.responses.ResponseItem; import eu.eudat.query.DmpQuery; import eu.eudat.query.lookup.DmpLookup; import eu.eudat.service.dmp.DmpService; import eu.eudat.service.transformer.FileTransformerService; import eu.eudat.types.ApiMessageCode; 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 gr.cite.tools.validation.MyValidate; import jakarta.xml.bind.JAXBException; import org.slf4j.LoggerFactory; import org.springframework.context.MessageSource; import org.springframework.context.i18n.LocaleContextHolder; import org.springframework.http.HttpStatus; 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.*; @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 FileTransformerService fileTransformerService; public DmpController( BuilderFactory builderFactory, AuditService auditService, DmpService dmpService, CensorFactory censorFactory, QueryFactory queryFactory, MessageSource messageSource, FileTransformerService fileTransformerService) { this.builderFactory = builderFactory; this.auditService = auditService; this.dmpService = dmpService; this.censorFactory = censorFactory; this.queryFactory = queryFactory; this.messageSource = messageSource; this.fileTransformerService = fileTransformerService; } @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); DmpQuery query = lookup.enrich(this.queryFactory).authorize(AuthorizationFlags.OwnerOrDmpAssociatedOrPermissionOrPublic); List data = query.collectAs(lookup.getProject()); List models = this.builderFactory.builder(DmpBuilder.class).authorize(AuthorizationFlags.OwnerOrDmpAssociatedOrPermissionOrPublic).build(lookup.getProject(), data); long count = (lookup.getMetadata() != null && lookup.getMetadata().getCountAll()) ? query.count() : models.size(); this.auditService.track(AuditableAction.Dmp_Query, "lookup", lookup); return new QueryResult<>(models, count); } @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.OwnerOrDmpAssociatedOrPermissionOrPublic).ids(id); Dmp model = this.builderFactory.builder(DmpBuilder.class).authorize(AuthorizationFlags.OwnerOrDmpAssociatedOrPermissionOrPublic).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 public Dmp Persist(@MyValidate @RequestBody DmpPersist model, FieldSet fieldSet) throws MyApplicationException, MyForbiddenException, MyNotFoundException, InvalidApplicationException, JsonProcessingException { 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("clone") public Dmp buildClone(@MyValidate @RequestBody CloneDmpPersist model, FieldSet fieldSet) throws MyApplicationException, MyForbiddenException, MyNotFoundException { 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 public Dmp createNewVersion(@MyValidate @RequestBody NewVersionDmpPersist model, FieldSet fieldSet) throws MyApplicationException, MyForbiddenException, MyNotFoundException, JAXBException, JsonProcessingException, 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 public QueryResult assignUsers(@PathVariable("id") UUID id, @MyValidate @RequestBody List model, FieldSet fieldSet) throws InvalidApplicationException { logger.debug(new MapLogEntry("assigning users to dmp").And("model", model).And("fieldSet", fieldSet)); List persisted = this.dmpService.assignUsers(id, model, fieldSet); 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 public QueryResult removeUser(@MyValidate @RequestBody DmpUserRemovePersist model, FieldSet fieldSet) throws InvalidApplicationException { 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/{type}") public ResponseEntity export(@PathVariable("id") UUID id, @PathVariable("type") String exportType) throws InvalidApplicationException, IOException { logger.debug(new MapLogEntry("exporting dmp")); return this.dmpService.export(id, exportType); } @GetMapping("/export/formats") public List getAvailableExportFormats() { return this.fileTransformerService.getAvailableConfigurations(); } @PostMapping("{id}/invite-users") @Transactional public ResponseEntity inviteUsers(@PathVariable("id") UUID id, @MyValidate @RequestBody DmpUserInvitePersist model) throws InvalidApplicationException, JAXBException { logger.debug(new MapLogEntry("inviting users to dmp").And("model", model)); this.dmpService.inviteUsers(id, model); this.auditService.track(AuditableAction.Dmp_Invite_Users, Map.ofEntries( new AbstractMap.SimpleEntry("model", model) )); return ResponseEntity.status(HttpStatus.OK).body(new ResponseItem().status(ApiMessageCode.SUCCESS_MESSAGE).payload("Invite Users Success")); } @GetMapping("{id}/token/{token}/invite-accept") @Transactional public ResponseEntity inviteUsers(@PathVariable("id") UUID id, @PathVariable("token") String token) throws InvalidApplicationException, JAXBException { 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 ResponseEntity.status(HttpStatus.OK).body(new ResponseItem().status(ApiMessageCode.SUCCESS_MESSAGE).payload("Invite Accept Success")); } }