diff --git a/dmp-backend/core/src/main/java/eu/eudat/audit/AuditableAction.java b/dmp-backend/core/src/main/java/eu/eudat/audit/AuditableAction.java index bc17a621b..de927d759 100644 --- a/dmp-backend/core/src/main/java/eu/eudat/audit/AuditableAction.java +++ b/dmp-backend/core/src/main/java/eu/eudat/audit/AuditableAction.java @@ -31,4 +31,9 @@ public class AuditableAction { public static final EventId Dmp_Persist = new EventId(5002, "Dmp_Persist"); public static final EventId Dmp_Delete = new EventId(5003, "Dmp_Delete"); + public static final EventId Description_Query = new EventId(6000, "Description_Query"); + public static final EventId Description_Lookup = new EventId(6001, "Description_Lookup"); + public static final EventId Description_Persist = new EventId(6002, "Description_Persist"); + public static final EventId Description_Delete = new EventId(6003, "Description_Delete"); + } diff --git a/dmp-backend/web/src/main/java/eu/eudat/controllers/v2/DescriptionController.java b/dmp-backend/web/src/main/java/eu/eudat/controllers/v2/DescriptionController.java new file mode 100644 index 000000000..1e4714926 --- /dev/null +++ b/dmp-backend/web/src/main/java/eu/eudat/controllers/v2/DescriptionController.java @@ -0,0 +1,128 @@ +package eu.eudat.controllers.v2; + +import eu.eudat.audit.AuditableAction; +import eu.eudat.authorization.AuthorizationFlags; +import eu.eudat.data.DescriptionEntity; +import eu.eudat.model.Description; +import eu.eudat.model.Dmp; +import eu.eudat.model.builder.DescriptionBuilder; +import eu.eudat.model.censorship.DescriptionCensor; +import eu.eudat.model.persist.DescriptionPersist; +import eu.eudat.model.result.QueryResult; +import eu.eudat.query.DescriptionQuery; +import eu.eudat.query.lookup.DescriptionLookup; +import eu.eudat.service.description.DescriptionService; +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 org.slf4j.LoggerFactory; +import org.springframework.context.MessageSource; +import org.springframework.context.i18n.LocaleContextHolder; +import org.springframework.transaction.annotation.Transactional; +import org.springframework.web.bind.annotation.*; + +import javax.management.InvalidApplicationException; +import java.util.*; + +@RestController +@RequestMapping(path = "api/description") +public class DescriptionController { + + private static final LoggerService logger = new LoggerService(LoggerFactory.getLogger(DescriptionController.class)); + + private final BuilderFactory builderFactory; + + private final AuditService auditService; + + private final DescriptionService descriptionService; + + private final CensorFactory censorFactory; + + private final QueryFactory queryFactory; + + private final MessageSource messageSource; + + public DescriptionController( + BuilderFactory builderFactory, + AuditService auditService, + DescriptionService descriptionService, + CensorFactory censorFactory, + QueryFactory queryFactory, + MessageSource messageSource) { + this.builderFactory = builderFactory; + this.auditService = auditService; + this.descriptionService = descriptionService; + this.censorFactory = censorFactory; + this.queryFactory = queryFactory; + this.messageSource = messageSource; + } + + @PostMapping("query") + public QueryResult Query(@RequestBody DescriptionLookup lookup) throws MyApplicationException, MyForbiddenException { + logger.debug("querying {}", Description.class.getSimpleName()); + + this.censorFactory.censor(DescriptionCensor.class).censor(lookup.getProject(), null); + + DescriptionQuery query = lookup.enrich(this.queryFactory).authorize(AuthorizationFlags.OwnerOrPermission); + + List data = query.collectAs(lookup.getProject()); + List models = this.builderFactory.builder(DescriptionBuilder.class).authorize(AuthorizationFlags.OwnerOrPermission).build(lookup.getProject(), data); + long count = (lookup.getMetadata() != null && lookup.getMetadata().getCountAll()) ? query.count() : models.size(); + + this.auditService.track(AuditableAction.Description_Query, "lookup", lookup); + + return new QueryResult<>(models, count); + } + + @GetMapping("{id}") + public Description Get(@PathVariable("id") UUID id, FieldSet fieldSet, Locale locale) throws MyApplicationException, MyForbiddenException, MyNotFoundException { + logger.debug(new MapLogEntry("retrieving" + Description.class.getSimpleName()).And("id", id).And("fields", fieldSet)); + + this.censorFactory.censor(DescriptionCensor.class).censor(fieldSet, null); + + DescriptionQuery query = this.queryFactory.query(DescriptionQuery.class).authorize(AuthorizationFlags.OwnerOrPermission).ids(id); + Description model = this.builderFactory.builder(DescriptionBuilder.class).authorize(AuthorizationFlags.OwnerOrPermission).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.Description_Lookup, Map.ofEntries( + new AbstractMap.SimpleEntry("id", id), + new AbstractMap.SimpleEntry("fields", fieldSet) + )); + + return model; + } + + @PostMapping("persist") + @Transactional + public Description Persist(@MyValidate @RequestBody DescriptionPersist model, FieldSet fieldSet) throws MyApplicationException, MyForbiddenException, MyNotFoundException, InvalidApplicationException { + logger.debug(new MapLogEntry("persisting" + Description.class.getSimpleName()).And("model", model).And("fieldSet", fieldSet)); + Description persisted = this.descriptionService.persist(model, fieldSet); + + this.auditService.track(AuditableAction.Description_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" + Description.class.getSimpleName()).And("id", id)); + + this.descriptionService.deleteAndSave(id); + + this.auditService.track(AuditableAction.Description_Delete, "id", id); + } + +} diff --git a/dmp-backend/web/src/main/java/eu/eudat/controllers/v2/EntityDoiController.java b/dmp-backend/web/src/main/java/eu/eudat/controllers/v2/EntityDoiController.java index 7f9a4eba9..ab9216f52 100644 --- a/dmp-backend/web/src/main/java/eu/eudat/controllers/v2/EntityDoiController.java +++ b/dmp-backend/web/src/main/java/eu/eudat/controllers/v2/EntityDoiController.java @@ -122,7 +122,7 @@ public class EntityDoiController { this.entityDoiService.deleteAndSave(id); - this.auditService.track(AuditableAction.DescriptionTemplateType_Delete, "id", id); + this.auditService.track(AuditableAction.EntityDoi_Delete, "id", id); } }