package eu.eudat.controllers; import com.fasterxml.jackson.core.JsonProcessingException; import eu.eudat.audit.AuditableAction; import eu.eudat.authorization.AuthorizationFlags; import eu.eudat.model.persist.NewVersionDmpBlueprintPersist; import gr.cite.tools.validation.ValidationFilterAnnotation; import eu.eudat.data.DmpBlueprintEntity; import eu.eudat.model.DmpBlueprint; import eu.eudat.model.builder.DmpBlueprintBuilder; import eu.eudat.model.censorship.DmpBlueprintCensor; import eu.eudat.model.persist.DmpBlueprintPersist; import eu.eudat.model.result.QueryResult; import eu.eudat.query.DmpBlueprintQuery; import eu.eudat.query.lookup.DmpBlueprintLookup; import eu.eudat.service.dmpblueprint.DmpBlueprintService; 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.transaction.Transactional; 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.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; import org.xml.sax.SAXException; import javax.management.InvalidApplicationException; import javax.xml.parsers.ParserConfigurationException; import javax.xml.transform.TransformerException; import java.io.IOException; import java.util.AbstractMap; import java.util.List; import java.util.Map; import java.util.UUID; @RestController @RequestMapping(path = "api/dmp-blueprint") public class DmpBlueprintController { private static final LoggerService logger = new LoggerService(LoggerFactory.getLogger(DmpBlueprintController.class)); private final BuilderFactory builderFactory; private final AuditService auditService; private final DmpBlueprintService dmpBlueprintService; private final CensorFactory censorFactory; private final QueryFactory queryFactory; private final MessageSource messageSource; public DmpBlueprintController( BuilderFactory builderFactory, AuditService auditService, DmpBlueprintService dmpBlueprintService, CensorFactory censorFactory, QueryFactory queryFactory, MessageSource messageSource) { this.builderFactory = builderFactory; this.auditService = auditService; this.dmpBlueprintService = dmpBlueprintService; this.censorFactory = censorFactory; this.queryFactory = queryFactory; this.messageSource = messageSource; } @PostMapping("query") public QueryResult query(@RequestBody DmpBlueprintLookup lookup) throws MyApplicationException, MyForbiddenException { logger.debug("querying {}", DmpBlueprint.class.getSimpleName()); this.censorFactory.censor(DmpBlueprintCensor.class).censor(lookup.getProject(), null); DmpBlueprintQuery query = lookup.enrich(this.queryFactory).authorize(AuthorizationFlags.OwnerOrDmpAssociatedOrPermission); List data = query.collectAs(lookup.getProject()); List models = this.builderFactory.builder(DmpBlueprintBuilder.class).authorize(AuthorizationFlags.OwnerOrDmpAssociatedOrPermission).build(lookup.getProject(), data); long count = (lookup.getMetadata() != null && lookup.getMetadata().getCountAll()) ? query.count() : models.size(); this.auditService.track(AuditableAction.DmpBlueprint_Query, "lookup", lookup); return new QueryResult<>(models, count); } @GetMapping("{id}") public DmpBlueprint get(@PathVariable("id") UUID id, FieldSet fieldSet) throws MyApplicationException, MyForbiddenException, MyNotFoundException { logger.debug(new MapLogEntry("retrieving" + DmpBlueprint.class.getSimpleName()).And("id", id).And("fields", fieldSet)); this.censorFactory.censor(DmpBlueprintCensor.class).censor(fieldSet, null); DmpBlueprintQuery query = this.queryFactory.query(DmpBlueprintQuery.class).authorize(AuthorizationFlags.OwnerOrDmpAssociatedOrPermission).ids(id); DmpBlueprint model = this.builderFactory.builder(DmpBlueprintBuilder.class).authorize(AuthorizationFlags.OwnerOrDmpAssociatedOrPermission).build(fieldSet, query.firstAs(fieldSet)); if (model == null) throw new MyNotFoundException(messageSource.getMessage("General_ItemNotFound", new Object[]{id, DmpBlueprint.class.getSimpleName()}, LocaleContextHolder.getLocale())); this.auditService.track(AuditableAction.DmpBlueprint_Lookup, Map.ofEntries( new AbstractMap.SimpleEntry("id", id), new AbstractMap.SimpleEntry("fields", fieldSet) )); return model; } @PostMapping("persist") @Transactional @ValidationFilterAnnotation(validator = DmpBlueprintPersist.DmpBlueprintPersistValidator.ValidatorName, argumentName = "model") public DmpBlueprint persist(@RequestBody DmpBlueprintPersist model, FieldSet fieldSet) throws MyApplicationException, MyForbiddenException, MyNotFoundException, InvalidApplicationException, JAXBException, ParserConfigurationException, JsonProcessingException, TransformerException { logger.debug(new MapLogEntry("persisting" + DmpBlueprint.class.getSimpleName()).And("model", model).And("fieldSet", fieldSet)); this.censorFactory.censor(DmpBlueprintCensor.class).censor(fieldSet, null); DmpBlueprint persisted = this.dmpBlueprintService.persist(model, fieldSet); this.auditService.track(AuditableAction.DmpBlueprint_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 { logger.debug(new MapLogEntry("retrieving" + DmpBlueprint.class.getSimpleName()).And("id", id)); this.dmpBlueprintService.deleteAndSave(id); this.auditService.track(AuditableAction.DmpBlueprint_Delete, "id", id); } @GetMapping("clone/{id}") public DmpBlueprint buildClone(@PathVariable("id") UUID id, FieldSet fieldSet) throws MyApplicationException, MyForbiddenException, MyNotFoundException { logger.debug(new MapLogEntry("clone" + DmpBlueprint.class.getSimpleName()).And("id", id).And("fields", fieldSet)); this.censorFactory.censor(DmpBlueprintCensor.class).censor(fieldSet, null); DmpBlueprint model = this.dmpBlueprintService.buildClone(id, fieldSet); this.auditService.track(AuditableAction.DmpBlueprint_Clone, Map.ofEntries( new AbstractMap.SimpleEntry("id", id), new AbstractMap.SimpleEntry("fields", fieldSet) )); return model; } @PostMapping("new-version") @Transactional @ValidationFilterAnnotation(validator = NewVersionDmpBlueprintPersist.NewVersionDmpBlueprintPersistValidator.ValidatorName, argumentName = "model") public DmpBlueprint createNewVersion(@RequestBody NewVersionDmpBlueprintPersist model, FieldSet fieldSet) throws JAXBException, InvalidApplicationException, ParserConfigurationException, JsonProcessingException, TransformerException { logger.debug(new MapLogEntry("persisting" + NewVersionDmpBlueprintPersist.class.getSimpleName()).And("model", model).And("fieldSet", fieldSet)); DmpBlueprint persisted = this.dmpBlueprintService.createNewVersion(model, fieldSet); this.auditService.track(AuditableAction.DmpBlueprint_PersistNewVersion, Map.ofEntries( new AbstractMap.SimpleEntry("model", model), new AbstractMap.SimpleEntry("fields", fieldSet) )); return persisted; } @RequestMapping(method = RequestMethod.GET, value = {"/xml/export/{id}"}, produces = "application/xml") public @ResponseBody ResponseEntity getXml(@PathVariable UUID id) throws JAXBException, ParserConfigurationException, IOException, TransformerException, InstantiationException, IllegalAccessException, SAXException, InvalidApplicationException { logger.debug(new MapLogEntry("export" + DmpBlueprint.class.getSimpleName()).And("id", id)); ResponseEntity response = this.dmpBlueprintService.exportXml(id); this.auditService.track(AuditableAction.DmpBlueprint_GetXml, Map.ofEntries( new AbstractMap.SimpleEntry("id", id) )); return response; } @RequestMapping(method = RequestMethod.POST, value = {"/xml/import"}) public DmpBlueprint importXml(@RequestParam("file") MultipartFile file, FieldSet fieldSet) throws IOException, JAXBException, InvalidApplicationException, ParserConfigurationException, TransformerException, InstantiationException, IllegalAccessException, SAXException { logger.debug(new MapLogEntry("clone" + DmpBlueprint.class.getSimpleName()).And("file", file)); this.censorFactory.censor(DmpBlueprintCensor.class).censor(fieldSet, null); DmpBlueprint model = this.dmpBlueprintService.importXml(file.getBytes(), file.getOriginalFilename(), fieldSet); this.auditService.track(AuditableAction.DmpBlueprint_Import, Map.ofEntries( new AbstractMap.SimpleEntry("file", file), new AbstractMap.SimpleEntry("fields", fieldSet) )); return model; } }