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 2c7da92db..3d4642bc0 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 @@ -20,4 +20,13 @@ public class AuditableAction { public static final EventId EntityDoi_Delete = new EventId(2003, "EntityDoi_Delete"); + + public static final EventId DmpBlueprint_Query = new EventId(3000, "DmpBlueprint_Query"); + + public static final EventId DmpBlueprint_Lookup = new EventId(3001, "DmpBlueprint_Lookup"); + + public static final EventId DmpBlueprint_Persist = new EventId(3002, "DmpBlueprint_Persist"); + + public static final EventId DmpBlueprint_Delete = new EventId(3003, "DmpBlueprint_Delete"); + } diff --git a/dmp-backend/core/src/main/java/eu/eudat/query/lookup/DmpBlueprintLookup.java b/dmp-backend/core/src/main/java/eu/eudat/query/lookup/DmpBlueprintLookup.java new file mode 100644 index 000000000..9aa750fc2 --- /dev/null +++ b/dmp-backend/core/src/main/java/eu/eudat/query/lookup/DmpBlueprintLookup.java @@ -0,0 +1,69 @@ +package eu.eudat.query.lookup; + +import eu.eudat.commons.enums.DmpBlueprintStatus; +import eu.eudat.commons.enums.IsActive; +import eu.eudat.query.DmpBlueprintQuery; +import gr.cite.tools.data.query.Lookup; +import gr.cite.tools.data.query.QueryFactory; + +import java.util.List; +import java.util.UUID; + +public class DmpBlueprintLookup extends Lookup { + + private String like; + + private List isActive; + + private List statuses; + + private List ids; + + private List excludedIds; + + public String getLike() { + return like; + } + + public void setLike(String like) { + this.like = like; + } + + public List getIsActive() { + return isActive; + } + + public void setIsActive(List isActive) { + this.isActive = isActive; + } + + public List getIds() { + return ids; + } + + public void setIds(List ids) { + this.ids = ids; + } + + public List getExcludedIds() { + return excludedIds; + } + + public void setExcludedIds(List excludeIds) { + this.excludedIds = excludeIds; + } + + public DmpBlueprintQuery enrich(QueryFactory queryFactory) { + DmpBlueprintQuery query = queryFactory.query(DmpBlueprintQuery.class); + if (this.like != null) query.like(this.like); + if (this.isActive != null) query.isActive(this.isActive); + if (this.statuses != null) query.statuses(this.statuses); + if (this.ids != null) query.ids(this.ids); + if (this.excludedIds != null) query.excludedIds(this.excludedIds); + + this.enrichCommon(query); + + return query; + } + +} diff --git a/dmp-backend/core/src/main/java/eu/eudat/service/dmpblueprint/DmpBlueprintService.java b/dmp-backend/core/src/main/java/eu/eudat/service/dmpblueprint/DmpBlueprintService.java new file mode 100644 index 000000000..faa160a3b --- /dev/null +++ b/dmp-backend/core/src/main/java/eu/eudat/service/dmpblueprint/DmpBlueprintService.java @@ -0,0 +1,19 @@ +package eu.eudat.service.dmpblueprint; + +import eu.eudat.model.DmpBlueprint; +import gr.cite.tools.exception.MyApplicationException; +import gr.cite.tools.exception.MyForbiddenException; +import gr.cite.tools.exception.MyNotFoundException; +import gr.cite.tools.exception.MyValidationException; +import gr.cite.tools.fieldset.FieldSet; + +import javax.management.InvalidApplicationException; +import java.util.UUID; + +public interface DmpBlueprintService { + +// DmpBlueprint persist(DmpBlueprintPersist model, FieldSet fields) throws MyForbiddenException, MyValidationException, MyApplicationException, MyNotFoundException, InvalidApplicationException; + + void deleteAndSave(UUID id) throws MyForbiddenException, InvalidApplicationException; + +} diff --git a/dmp-backend/core/src/main/java/eu/eudat/service/dmpblueprint/DmpBlueprintServiceImpl.java b/dmp-backend/core/src/main/java/eu/eudat/service/dmpblueprint/DmpBlueprintServiceImpl.java new file mode 100644 index 000000000..0b1794cd7 --- /dev/null +++ b/dmp-backend/core/src/main/java/eu/eudat/service/dmpblueprint/DmpBlueprintServiceImpl.java @@ -0,0 +1,129 @@ +package eu.eudat.service.dmpblueprint; + +import eu.eudat.authorization.AuthorizationFlags; +import eu.eudat.authorization.Permission; +import eu.eudat.commons.JsonHandlingService; +import eu.eudat.commons.enums.IsActive; +import eu.eudat.convention.ConventionService; +import eu.eudat.data.DmpBlueprintEntity; +import eu.eudat.errorcode.ErrorThesaurusProperties; +import eu.eudat.event.EventBroker; +import eu.eudat.model.DmpBlueprint; +import eu.eudat.model.builder.DmpBlueprintBuilder; +import eu.eudat.model.deleter.DmpBlueprintDeleter; +import gr.cite.commons.web.authz.service.AuthorizationService; +import gr.cite.tools.data.builder.BuilderFactory; +import gr.cite.tools.data.deleter.DeleterFactory; +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.exception.MyValidationException; +import gr.cite.tools.fieldset.BaseFieldSet; +import gr.cite.tools.fieldset.FieldSet; +import gr.cite.tools.logging.LoggerService; +import gr.cite.tools.logging.MapLogEntry; +import jakarta.persistence.EntityManager; +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.stereotype.Service; + +import javax.management.InvalidApplicationException; +import java.time.Instant; +import java.util.List; +import java.util.UUID; + +@Service +public class DmpBlueprintServiceImpl implements DmpBlueprintService { + + private static final LoggerService logger = new LoggerService(LoggerFactory.getLogger(DmpBlueprintServiceImpl.class)); + + private final EntityManager entityManager; + + private final AuthorizationService authorizationService; + + private final DeleterFactory deleterFactory; + + private final BuilderFactory builderFactory; + + private final ConventionService conventionService; + + private final ErrorThesaurusProperties errors; + + private final MessageSource messageSource; + + private final EventBroker eventBroker; + + private final QueryFactory queryFactory; + + private final JsonHandlingService jsonHandlingService; + + @Autowired + public DmpBlueprintServiceImpl( + EntityManager entityManager, + AuthorizationService authorizationService, + DeleterFactory deleterFactory, + BuilderFactory builderFactory, + ConventionService conventionService, + ErrorThesaurusProperties errors, + MessageSource messageSource, + EventBroker eventBroker, + QueryFactory queryFactory, + JsonHandlingService jsonHandlingService) { + this.entityManager = entityManager; + this.authorizationService = authorizationService; + this.deleterFactory = deleterFactory; + this.builderFactory = builderFactory; + this.conventionService = conventionService; + this.errors = errors; + this.messageSource = messageSource; + this.eventBroker = eventBroker; + this.queryFactory = queryFactory; + this.jsonHandlingService = jsonHandlingService; + } + +// public DmpBlueprint persist(DmpBlueprintPersist model, FieldSet fields) throws MyForbiddenException, MyValidationException, MyApplicationException, MyNotFoundException, InvalidApplicationException { +// logger.debug(new MapLogEntry("persisting data dmpBlueprint").And("model", model).And("fields", fields)); +// +// this.authorizationService.authorizeForce(Permission.EditDmpBlueprint); +// +// Boolean isUpdate = this.conventionService.isValidGuid(model.getId()); +// +// DmpBlueprintEntity data; +// if (isUpdate) { +// data = this.entityManager.find(DmpBlueprintEntity.class, model.getId()); +// if (data == null) +// throw new MyNotFoundException(messageSource.getMessage("General_ItemNotFound", new Object[]{model.getId(), DmpBlueprint.class.getSimpleName()}, LocaleContextHolder.getLocale())); +// } else { +// data = new DmpBlueprintEntity(); +// data.setId(UUID.randomUUID()); +// data.setIsActive(IsActive.Active); +// data.setCreatedAt(Instant.now()); +// } +// +// data.setName(model.getName()); +// data.setStatus(model.getStatus()); +// data.setUpdatedAt(Instant.now()); +// if (isUpdate) +// this.entityManager.merge(data); +// else +// this.entityManager.persist(data); +// +// this.entityManager.flush(); +// +// this.eventBroker.emit(new DmpBlueprintTouchedEvent(data.getId())); +// return this.builderFactory.builder(DmpBlueprintBuilder.class).authorize(AuthorizationFlags.OwnerOrPermission).build(BaseFieldSet.build(fields, DmpBlueprint._id), data); +// } + + public void deleteAndSave(UUID id) throws MyForbiddenException, InvalidApplicationException { + logger.debug("deleting dataset: {}", id); + + this.authorizationService.authorizeForce(Permission.DeleteDmpBlueprint); + + this.deleterFactory.deleter(DmpBlueprintDeleter.class).deleteAndSaveByIds(List.of(id)); + } + +} + diff --git a/dmp-backend/web/src/main/java/eu/eudat/controllers/v2/DmpBlueprintController.java b/dmp-backend/web/src/main/java/eu/eudat/controllers/v2/DmpBlueprintController.java index b32979b50..db0d6a1c5 100644 --- a/dmp-backend/web/src/main/java/eu/eudat/controllers/v2/DmpBlueprintController.java +++ b/dmp-backend/web/src/main/java/eu/eudat/controllers/v2/DmpBlueprintController.java @@ -1,130 +1,128 @@ -//package eu.eudat.controllers.v2; -// -//import eu.eudat.audit.AuditableAction; -//import eu.eudat.authorization.AuthorizationFlags; -//import eu.eudat.data.DescriptionTemplateTypeEntity; -//import eu.eudat.model.DmpBlueprint; -//import eu.eudat.model.builder.DescriptionTemplateTypeBuilder; -//import eu.eudat.model.censorship.DescriptionTemplateTypeCensor; -//import eu.eudat.model.persist.DescriptionTemplateTypePersist; -//import eu.eudat.model.result.QueryResult; -//import eu.eudat.query.DescriptionTemplateTypeQuery; -//import eu.eudat.query.lookup.DescriptionTemplateTypeLookup; -//import eu.eudat.service.DescriptionTemplateTypeService; -//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/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 DescriptionTemplateTypeService descriptionTemplateTypeService; -// -// private final CensorFactory censorFactory; -// -// private final QueryFactory queryFactory; -// -// private final MessageSource messageSource; -// -// public DmpBlueprintController( -// BuilderFactory builderFactory, -// AuditService auditService, -// DescriptionTemplateTypeService descriptionTemplateTypeService, -// CensorFactory censorFactory, -// QueryFactory queryFactory, -// MessageSource messageSource) { -// this.builderFactory = builderFactory; -// this.auditService = auditService; -// this.descriptionTemplateTypeService = descriptionTemplateTypeService; -// this.censorFactory = censorFactory; -// this.queryFactory = queryFactory; -// this.messageSource = messageSource; -// } -// -// @PostMapping("query") -// public QueryResult Query(@RequestBody DescriptionTemplateTypeLookup lookup) throws MyApplicationException, MyForbiddenException { -// logger.debug("querying {}", DmpBlueprint.class.getSimpleName()); -// -// this.censorFactory.censor(DescriptionTemplateTypeCensor.class).censor(lookup.getProject(), null); -// -// DescriptionTemplateTypeQuery query = lookup.enrich(this.queryFactory).authorize(AuthorizationFlags.OwnerOrPermission); -// -// List data = query.collectAs(lookup.getProject()); -// List models = this.builderFactory.builder(DescriptionTemplateTypeBuilder.class).authorize(AuthorizationFlags.OwnerOrPermission).build(lookup.getProject(), data); -// long count = (lookup.getMetadata() != null && lookup.getMetadata().getCountAll()) ? query.count() : models.size(); -// -// this.auditService.track(AuditableAction.DescriptionTemplateType_Query, "lookup", lookup); -// //this.auditService.trackIdentity(AuditableAction.IdentityTracking_Action); -// -// return new QueryResult<>(models, count); -// } -// -// @GetMapping("{id}") -// public DmpBlueprint Get(@PathVariable("id") UUID id, FieldSet fieldSet, Locale locale) throws MyApplicationException, MyForbiddenException, MyNotFoundException { -// logger.debug(new MapLogEntry("retrieving" + DmpBlueprint.class.getSimpleName()).And("id", id).And("fields", fieldSet)); -// -// this.censorFactory.censor(DescriptionTemplateTypeCensor.class).censor(fieldSet, null); -// -// DescriptionTemplateTypeQuery query = this.queryFactory.query(DescriptionTemplateTypeQuery.class).authorize(AuthorizationFlags.OwnerOrPermission).ids(id); -// DmpBlueprint 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, DmpBlueprint.class.getSimpleName()}, LocaleContextHolder.getLocale())); -// -// this.auditService.track(AuditableAction.DescriptionTemplateType_Lookup, Map.ofEntries( -// new AbstractMap.SimpleEntry("id", id), -// new AbstractMap.SimpleEntry("fields", fieldSet) -// )); -// //this.auditService.trackIdentity(AuditableAction.IdentityTracking_Action); -// -// return model; -// } -// +package eu.eudat.controllers.v2; + +import eu.eudat.audit.AuditableAction; +import eu.eudat.authorization.AuthorizationFlags; +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.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 org.slf4j.LoggerFactory; +import org.springframework.context.MessageSource; +import org.springframework.context.i18n.LocaleContextHolder; +import org.springframework.web.bind.annotation.*; + +import javax.management.InvalidApplicationException; +import java.util.*; + +@RestController +@CrossOrigin +@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.OwnerOrPermission); + + List data = query.collectAs(lookup.getProject()); + List models = this.builderFactory.builder(DmpBlueprintBuilder.class).authorize(AuthorizationFlags.OwnerOrPermission).build(lookup.getProject(), data); + long count = (lookup.getMetadata() != null && lookup.getMetadata().getCountAll()) ? query.count() : models.size(); + + this.auditService.track(AuditableAction.DmpBlueprint_Query, "lookup", lookup); + //this.auditService.trackIdentity(AuditableAction.IdentityTracking_Action); + + return new QueryResult<>(models, count); + } + + @GetMapping("{id}") + public DmpBlueprint Get(@PathVariable("id") UUID id, FieldSet fieldSet, Locale locale) 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.OwnerOrPermission).ids(id); + DmpBlueprint model = this.builderFactory.builder(DmpBlueprintBuilder.class).authorize(AuthorizationFlags.OwnerOrPermission).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) + )); + //this.auditService.trackIdentity(AuditableAction.IdentityTracking_Action); + + return model; + } + // @PostMapping("persist") // @Transactional -// public DmpBlueprint Persist(@MyValidate @RequestBody DescriptionTemplateTypePersist model, FieldSet fieldSet) throws MyApplicationException, MyForbiddenException, MyNotFoundException, InvalidApplicationException { +// public DmpBlueprint Persist(@MyValidate @RequestBody DmpBlueprintPersist model, FieldSet fieldSet) throws MyApplicationException, MyForbiddenException, MyNotFoundException, InvalidApplicationException { // logger.debug(new MapLogEntry("persisting" + DmpBlueprint.class.getSimpleName()).And("model", model).And("fieldSet", fieldSet)); -// DmpBlueprint persisted = this.descriptionTemplateTypeService.persist(model, fieldSet); +// DmpBlueprint persisted = this.dmpBlueprintService.persist(model, fieldSet); // -// this.auditService.track(AuditableAction.DescriptionTemplateType_Persist, Map.ofEntries( +// this.auditService.track(AuditableAction.DmpBlueprint_Persist, Map.ofEntries( // new AbstractMap.SimpleEntry("model", model), // new AbstractMap.SimpleEntry("fields", fieldSet) // )); // //this.auditService.trackIdentity(AuditableAction.IdentityTracking_Action); // 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.descriptionTemplateTypeService.deleteAndSave(id); -// -// this.auditService.track(AuditableAction.DescriptionTemplateType_Delete, "id", id); -// //this.auditService.trackIdentity(AuditableAction.IdentityTracking_Action); -// } -//} + + @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); + //this.auditService.trackIdentity(AuditableAction.IdentityTracking_Action); + } +}