From 2a4e61f08f9963ada33131c5196bf1e09d87beee Mon Sep 17 00:00:00 2001 From: Thomas Georgios Giannos Date: Thu, 19 Oct 2023 17:48:41 +0300 Subject: [PATCH] Added controller for EntityDoi entity --- .../java/eu/eudat/audit/AuditableAction.java | 11 ++ ...=> DescriptionTemplateTypeController.java} | 17 ++- .../controllers/v2/EntityDoiController.java | 129 ++++++++++++++++++ 3 files changed, 151 insertions(+), 6 deletions(-) rename dmp-backend/web/src/main/java/eu/eudat/controllers/v2/{DescriptionTemplateTypeV2Controller.java => DescriptionTemplateTypeController.java} (93%) create mode 100644 dmp-backend/web/src/main/java/eu/eudat/controllers/v2/EntityDoiController.java 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 1dc08d4ce..2c7da92db 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 @@ -5,8 +5,19 @@ import gr.cite.tools.logging.EventId; public class AuditableAction { public static final EventId DescriptionTemplateType_Query = new EventId(1000, "DescriptionTemplateType_Query"); + public static final EventId DescriptionTemplateType_Lookup = new EventId(1001, "DescriptionTemplateType_Lookup"); + public static final EventId DescriptionTemplateType_Persist = new EventId(1002, "DescriptionTemplateType_Persist"); + public static final EventId DescriptionTemplateType_Delete = new EventId(1003, "DescriptionTemplateType_Delete"); + public static final EventId EntityDoi_Query = new EventId(2000, "EntityDoi_Query"); + + public static final EventId EntityDoi_Lookup = new EventId(2001, "EntityDoi_Lookup"); + + public static final EventId EntityDoi_Persist = new EventId(2002, "EntityDoi_Persist"); + + public static final EventId EntityDoi_Delete = new EventId(2003, "EntityDoi_Delete"); + } diff --git a/dmp-backend/web/src/main/java/eu/eudat/controllers/v2/DescriptionTemplateTypeV2Controller.java b/dmp-backend/web/src/main/java/eu/eudat/controllers/v2/DescriptionTemplateTypeController.java similarity index 93% rename from dmp-backend/web/src/main/java/eu/eudat/controllers/v2/DescriptionTemplateTypeV2Controller.java rename to dmp-backend/web/src/main/java/eu/eudat/controllers/v2/DescriptionTemplateTypeController.java index 087b9a077..816f97b63 100644 --- a/dmp-backend/web/src/main/java/eu/eudat/controllers/v2/DescriptionTemplateTypeV2Controller.java +++ b/dmp-backend/web/src/main/java/eu/eudat/controllers/v2/DescriptionTemplateTypeController.java @@ -23,7 +23,6 @@ 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.beans.factory.annotation.Autowired; import org.springframework.context.MessageSource; import org.springframework.context.i18n.LocaleContextHolder; import org.springframework.transaction.annotation.Transactional; @@ -35,18 +34,23 @@ import java.util.*; @RestController @CrossOrigin @RequestMapping(path = "api/description-template-type") -public class DescriptionTemplateTypeV2Controller { - private static final LoggerService logger = new LoggerService(LoggerFactory.getLogger(DescriptionTemplateTypeV2Controller.class)); +public class DescriptionTemplateTypeController { + + private static final LoggerService logger = new LoggerService(LoggerFactory.getLogger(DescriptionTemplateTypeController.class)); private final BuilderFactory builderFactory; + private final AuditService auditService; + private final DescriptionTemplateTypeService descriptionTemplateTypeService; + private final CensorFactory censorFactory; + private final QueryFactory queryFactory; + private final MessageSource messageSource; - @Autowired - public DescriptionTemplateTypeV2Controller( + public DescriptionTemplateTypeController( BuilderFactory builderFactory, AuditService auditService, DescriptionTemplateTypeService descriptionTemplateTypeService, @@ -87,7 +91,8 @@ public class DescriptionTemplateTypeV2Controller { DescriptionTemplateTypeQuery query = this.queryFactory.query(DescriptionTemplateTypeQuery.class).authorize(AuthorizationFlags.OwnerOrPermission).ids(id); DescriptionTemplateType model = this.builderFactory.builder(DescriptionTemplateTypeBuilder.class).authorize(AuthorizationFlags.OwnerOrPermission).build(fieldSet, query.firstAs(fieldSet)); - if (model == null) throw new MyNotFoundException(messageSource.getMessage("General_ItemNotFound", new Object[]{id, DescriptionTemplateType.class.getSimpleName()}, LocaleContextHolder.getLocale())); + if (model == null) + throw new MyNotFoundException(messageSource.getMessage("General_ItemNotFound", new Object[]{id, DescriptionTemplateType.class.getSimpleName()}, LocaleContextHolder.getLocale())); this.auditService.track(AuditableAction.DescriptionTemplateType_Lookup, Map.ofEntries( new AbstractMap.SimpleEntry("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 new file mode 100644 index 000000000..812c23565 --- /dev/null +++ b/dmp-backend/web/src/main/java/eu/eudat/controllers/v2/EntityDoiController.java @@ -0,0 +1,129 @@ +package eu.eudat.controllers.v2; + +import eu.eudat.audit.AuditableAction; +import eu.eudat.authorization.AuthorizationFlags; +import eu.eudat.data.EntityDoiEntity; +import eu.eudat.model.DescriptionTemplateType; +import eu.eudat.model.EntityDoi; +import eu.eudat.model.builder.EntityDoiBuilder; +import eu.eudat.model.censorship.EntityDoiCensor; +import eu.eudat.model.persist.EntityDoiPersist; +import eu.eudat.model.result.QueryResult; +import eu.eudat.query.EntityDoiQuery; +import eu.eudat.query.lookup.EntityDoiLookup; +import eu.eudat.service.EntityDoiService; +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 +@CrossOrigin +@RequestMapping(path = "api/entity-doi") +public class EntityDoiController { + + private static final LoggerService logger = new LoggerService(LoggerFactory.getLogger(DescriptionTemplateTypeController.class)); + + private final BuilderFactory builderFactory; + + private final AuditService auditService; + + private final EntityDoiService entityDoiService; + + private final CensorFactory censorFactory; + + private final QueryFactory queryFactory; + + private final MessageSource messageSource; + + public EntityDoiController( + BuilderFactory builderFactory, + AuditService auditService, + EntityDoiService entityDoiService, + CensorFactory censorFactory, + QueryFactory queryFactory, + MessageSource messageSource) { + this.builderFactory = builderFactory; + this.auditService = auditService; + this.entityDoiService = entityDoiService; + this.censorFactory = censorFactory; + this.queryFactory = queryFactory; + this.messageSource = messageSource; + } + + @PostMapping("query") + public QueryResult Query(@RequestBody EntityDoiLookup lookup) throws MyApplicationException, MyForbiddenException { + logger.debug("querying {}", EntityDoi.class.getSimpleName()); + + this.censorFactory.censor(EntityDoiCensor.class).censor(lookup.getProject(), null); + + EntityDoiQuery query = lookup.enrich(this.queryFactory).authorize(AuthorizationFlags.OwnerOrPermission); + + List data = query.collectAs(lookup.getProject()); + List models = this.builderFactory.builder(EntityDoiBuilder.class).authorize(AuthorizationFlags.OwnerOrPermission).build(lookup.getProject(), data); + long count = (lookup.getMetadata() != null && lookup.getMetadata().getCountAll()) ? query.count() : models.size(); + + this.auditService.track(AuditableAction.EntityDoi_Query, "lookup", lookup); + + return new QueryResult<>(models, count); + } + + @GetMapping("{id}") + public EntityDoi Get(@PathVariable("id") UUID id, FieldSet fieldSet, Locale locale) throws MyApplicationException, MyForbiddenException, MyNotFoundException { + logger.debug(new MapLogEntry("retrieving" + EntityDoi.class.getSimpleName()).And("id", id).And("fields", fieldSet)); + + this.censorFactory.censor(EntityDoiCensor.class).censor(fieldSet, null); + + EntityDoiQuery query = this.queryFactory.query(EntityDoiQuery.class).authorize(AuthorizationFlags.OwnerOrPermission).ids(id); + EntityDoi model = this.builderFactory.builder(EntityDoiBuilder.class).authorize(AuthorizationFlags.OwnerOrPermission).build(fieldSet, query.firstAs(fieldSet)); + if (model == null) + throw new MyNotFoundException(messageSource.getMessage("General_ItemNotFound", new Object[]{id, EntityDoi.class.getSimpleName()}, LocaleContextHolder.getLocale())); + + this.auditService.track(AuditableAction.EntityDoi_Lookup, Map.ofEntries( + new AbstractMap.SimpleEntry("id", id), + new AbstractMap.SimpleEntry("fields", fieldSet) + )); + + return model; + } + + @PostMapping("persist") + @Transactional + public EntityDoi Persist(@MyValidate @RequestBody EntityDoiPersist model, FieldSet fieldSet) throws MyApplicationException, MyForbiddenException, MyNotFoundException, InvalidApplicationException { + logger.debug(new MapLogEntry("persisting" + DescriptionTemplateType.class.getSimpleName()).And("model", model).And("fieldSet", fieldSet)); + EntityDoi persisted = this.entityDoiService.persist(model, fieldSet); + + this.auditService.track(AuditableAction.EntityDoi_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" + EntityDoi.class.getSimpleName()).And("id", id)); + + this.entityDoiService.deleteAndSave(id); + + this.auditService.track(AuditableAction.DescriptionTemplateType_Delete, "id", id); + } + +}