Updating description template type controller with apropriate auditing and logging, added censor, refactored query building to conform with other projects workflows

This commit is contained in:
Thomas Georgios Giannos 2023-10-09 11:17:16 +03:00
parent cab2f1bc64
commit 55aecd9c57
7 changed files with 146 additions and 60 deletions

View File

@ -0,0 +1,23 @@
package eu.eudat.model.censorship;
import eu.eudat.convention.ConventionService;
import gr.cite.tools.data.censor.Censor;
import gr.cite.tools.fieldset.FieldSet;
public class BaseCensor implements Censor {
protected final ConventionService conventionService;
public BaseCensor(ConventionService conventionService) {
this.conventionService = conventionService;
}
protected Boolean isEmpty(FieldSet fields) {
return fields == null || fields.isEmpty();
}
protected String asIndexerPrefix(String part) {
return this.conventionService.asPrefix(part);
}
}

View File

@ -0,0 +1,28 @@
package eu.eudat.model.censorship;
import eu.eudat.convention.ConventionService;
import gr.cite.tools.fieldset.FieldSet;
import gr.cite.tools.logging.DataLogEntry;
import gr.cite.tools.logging.LoggerService;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
@Component
@Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public class DescriptionTemplateTypeCensor extends BaseCensor{
private static final LoggerService logger = new LoggerService(LoggerFactory.getLogger(DescriptionTemplateTypeCensor.class));
public DescriptionTemplateTypeCensor(ConventionService conventionService) {
super(conventionService);
}
public void censor(FieldSet fields) {
logger.debug(new DataLogEntry("censoring fields", fields));
if (fields.isEmpty())
return;
}
}

View File

@ -1,9 +1,6 @@
package eu.eudat.query; package eu.eudat.query;
import eu.eudat.commons.enums.DescriptionTemplateTypeStatus;
import eu.eudat.data.DescriptionTemplateTypeEntity; import eu.eudat.data.DescriptionTemplateTypeEntity;
import eu.eudat.query.lookup.DescriptionTemplateTypeLookup;
import eu.eudat.query.lookup.LookupAware;
import gr.cite.tools.data.query.FieldResolver; import gr.cite.tools.data.query.FieldResolver;
import gr.cite.tools.data.query.QueryBase; import gr.cite.tools.data.query.QueryBase;
import gr.cite.tools.data.query.QueryContext; import gr.cite.tools.data.query.QueryContext;
@ -18,7 +15,7 @@ import java.util.*;
@Component @Component
@Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE) @Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public class DescriptionTemplateTypeQuery extends QueryBase<DescriptionTemplateTypeEntity> implements LookupAware<DescriptionTemplateTypeQuery, DescriptionTemplateTypeLookup> { public class DescriptionTemplateTypeQuery extends QueryBase<DescriptionTemplateTypeEntity> {
private Collection<UUID> ids; private Collection<UUID> ids;
@ -71,19 +68,6 @@ public class DescriptionTemplateTypeQuery extends QueryBase<DescriptionTemplateT
return this; return this;
} }
@Override
public DescriptionTemplateTypeQuery fromLookup(DescriptionTemplateTypeLookup lookup) {
if (lookup == null)
return this;
if (lookup.getStatus() == null)
statuses(DescriptionTemplateTypeStatus.SAVED.getValue(), DescriptionTemplateTypeStatus.FINALIZED.getValue());
if (lookup.getName() != null)
names(lookup.getName());
if (lookup.getStatus() != null)
statuses(lookup.getStatus());
return this;
}
@Override @Override
protected Boolean isFalseQuery() { protected Boolean isFalseQuery() {
return Boolean.FALSE; return Boolean.FALSE;

View File

@ -1,6 +1,8 @@
package eu.eudat.query.lookup; package eu.eudat.query.lookup;
import eu.eudat.query.DescriptionTemplateTypeQuery;
import gr.cite.tools.data.query.Lookup; import gr.cite.tools.data.query.Lookup;
import gr.cite.tools.data.query.QueryFactory;
public class DescriptionTemplateTypeLookup extends Lookup { public class DescriptionTemplateTypeLookup extends Lookup {
@ -24,4 +26,14 @@ public class DescriptionTemplateTypeLookup extends Lookup {
this.status = status; this.status = status;
} }
public DescriptionTemplateTypeQuery enrich(QueryFactory queryFactory) {
DescriptionTemplateTypeQuery query = queryFactory.query(DescriptionTemplateTypeQuery.class);
if (name != null) query.names(name);
if (status != null ) query.statuses(status);
enrichCommon(query);
return query;
}
} }

View File

@ -1,12 +0,0 @@
package eu.eudat.query.lookup;
public interface LookupAware<Q, L> {
/**
* Every query that supports building from a lookup object should implement this interface
* @param lookup The object containing all the lookup options
* @return The query built from all the set options
*/
Q fromLookup(L lookup);
}

View File

@ -1,14 +1,14 @@
package eu.eudat.service; package eu.eudat.service;
import eu.eudat.audit.AuditableAction;
import eu.eudat.commons.enums.DescriptionTemplateTypeStatus; import eu.eudat.commons.enums.DescriptionTemplateTypeStatus;
import eu.eudat.data.DescriptionTemplateTypeEntity; import eu.eudat.data.DescriptionTemplateTypeEntity;
import eu.eudat.model.DescriptionTemplateType; import eu.eudat.model.DescriptionTemplateType;
import eu.eudat.model.builder.DescriptionTemplateTypeBuilder; import eu.eudat.model.builder.DescriptionTemplateTypeBuilder;
import eu.eudat.query.DescriptionTemplateTypeQuery; import eu.eudat.query.DescriptionTemplateTypeQuery;
import eu.eudat.query.lookup.DescriptionTemplateTypeLookup; import eu.eudat.query.lookup.DescriptionTemplateTypeLookup;
import gr.cite.tools.auditing.AuditService;
import gr.cite.tools.data.builder.BuilderFactory; import gr.cite.tools.data.builder.BuilderFactory;
import gr.cite.tools.data.query.QueryFactory;
import gr.cite.tools.exception.MyNotFoundException;
import jakarta.persistence.EntityManager; import jakarta.persistence.EntityManager;
import jakarta.persistence.PersistenceContext; import jakarta.persistence.PersistenceContext;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
@ -28,44 +28,38 @@ public class DescriptionTemplateTypeService {
private final BuilderFactory builderFactory; private final BuilderFactory builderFactory;
private final PlatformTransactionManager transactionManager; private final QueryFactory queryFactory;
private final AuditService auditService; private final PlatformTransactionManager transactionManager;
@PersistenceContext @PersistenceContext
private EntityManager entityManager; private EntityManager entityManager;
public DescriptionTemplateTypeService(ApplicationContext applicationContext, BuilderFactory builderFactory, PlatformTransactionManager platformTransactionManager, AuditService auditService) { public DescriptionTemplateTypeService(ApplicationContext applicationContext, BuilderFactory builderFactory, QueryFactory queryFactory, PlatformTransactionManager platformTransactionManager) {
this.applicationContext = applicationContext; this.applicationContext = applicationContext;
this.builderFactory = builderFactory; this.builderFactory = builderFactory;
this.queryFactory = queryFactory;
this.transactionManager = platformTransactionManager; this.transactionManager = platformTransactionManager;
this.auditService = auditService;
} }
public List<DescriptionTemplateType> query(DescriptionTemplateTypeLookup lookup) { public List<DescriptionTemplateType> query(DescriptionTemplateTypeLookup lookup) {
DescriptionTemplateTypeQuery query = applicationContext.getBean(DescriptionTemplateTypeQuery.class); DescriptionTemplateTypeQuery query = lookup.enrich(queryFactory);
List<DescriptionTemplateType> results = builderFactory List<DescriptionTemplateTypeEntity> data = query.collectAs(lookup.getProject());
.builder(DescriptionTemplateTypeBuilder.class)
.build(lookup.getProject(), query.fromLookup(lookup).collect());
auditService.track(AuditableAction.DescriptionTemplateType_Query, "lookup", lookup); return builderFactory.builder(DescriptionTemplateTypeBuilder.class).build(lookup.getProject(), data);
return results;
} }
public DescriptionTemplateType get(UUID id) { public DescriptionTemplateType get(UUID id) {
DescriptionTemplateTypeQuery query = applicationContext.getBean(DescriptionTemplateTypeQuery.class); DescriptionTemplateTypeQuery query = applicationContext.getBean(DescriptionTemplateTypeQuery.class);
DescriptionTemplateType result = builderFactory
return builderFactory
.builder(DescriptionTemplateTypeBuilder.class) .builder(DescriptionTemplateTypeBuilder.class)
.build(null, query.ids(id).first()); .build(null, query.ids(id).first());
auditService.track(AuditableAction.DescriptionTemplateType_Query, "id", id);
return result;
} }
public DescriptionTemplateTypeEntity getEntityByName(String name) { public DescriptionTemplateTypeEntity getEntityByName(String name) {
DescriptionTemplateTypeQuery query = applicationContext.getBean(DescriptionTemplateTypeQuery.class); DescriptionTemplateTypeQuery query = applicationContext.getBean(DescriptionTemplateTypeQuery.class);
return query.names(name).first(); return query.names(name).first();
} }
@ -91,9 +85,8 @@ public class DescriptionTemplateTypeService {
throw ex; throw ex;
} }
auditService.track(AuditableAction.DescriptionTemplateType_Persist, "persist", payload);
DescriptionTemplateTypeQuery query = applicationContext.getBean(DescriptionTemplateTypeQuery.class); DescriptionTemplateTypeQuery query = applicationContext.getBean(DescriptionTemplateTypeQuery.class);
return builderFactory return builderFactory
.builder(DescriptionTemplateTypeBuilder.class) .builder(DescriptionTemplateTypeBuilder.class)
.build(null, query.ids(created.getId()).first()); .build(null, query.ids(created.getId()).first());
@ -106,8 +99,6 @@ public class DescriptionTemplateTypeService {
entityManager.merge(entity); entityManager.merge(entity);
entityManager.flush(); entityManager.flush();
auditService.track(AuditableAction.DescriptionTemplateType_Persist, "update", payload);
return builderFactory.builder(DescriptionTemplateTypeBuilder.class).build(null, entity); return builderFactory.builder(DescriptionTemplateTypeBuilder.class).build(null, entity);
} }
@ -119,8 +110,6 @@ public class DescriptionTemplateTypeService {
entityManager.merge(entity); entityManager.merge(entity);
entityManager.flush(); entityManager.flush();
auditService.track(AuditableAction.DescriptionTemplateType_Delete, "delete", id);
return true; return true;
} }

View File

@ -1,18 +1,31 @@
package eu.eudat.controllers.v2; package eu.eudat.controllers.v2;
import eu.eudat.audit.AuditableAction;
import eu.eudat.logic.security.claims.ClaimedAuthorities; import eu.eudat.logic.security.claims.ClaimedAuthorities;
import eu.eudat.model.DescriptionTemplateType; import eu.eudat.model.DescriptionTemplateType;
import eu.eudat.model.censorship.DescriptionTemplateTypeCensor;
import eu.eudat.model.result.QueryResult; import eu.eudat.model.result.QueryResult;
import eu.eudat.models.data.security.Principal; import eu.eudat.models.data.security.Principal;
import eu.eudat.query.lookup.DescriptionTemplateTypeLookup; import eu.eudat.query.lookup.DescriptionTemplateTypeLookup;
import eu.eudat.service.DescriptionTemplateTypeService; import eu.eudat.service.DescriptionTemplateTypeService;
import eu.eudat.types.Authorities; import eu.eudat.types.Authorities;
import gr.cite.tools.auditing.AuditService;
import gr.cite.tools.data.censor.CensorFactory;
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 org.slf4j.LoggerFactory;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import java.util.AbstractMap;
import java.util.Map;
import java.util.UUID; import java.util.UUID;
import static eu.eudat.controllers.v2.BaseController.extractQueryResultWithCount; import static eu.eudat.controllers.v2.BaseController.extractQueryResultWithCount;
@ -23,34 +36,83 @@ import static eu.eudat.controllers.v2.BaseController.extractQueryResultWithCount
@RequestMapping(path = "api/v2/descriptionTemplateType", produces = MediaType.APPLICATION_JSON_VALUE) @RequestMapping(path = "api/v2/descriptionTemplateType", produces = MediaType.APPLICATION_JSON_VALUE)
public class DescriptionTemplateTypeV2Controller { public class DescriptionTemplateTypeV2Controller {
private static final LoggerService logger = new LoggerService(LoggerFactory.getLogger(DescriptionTemplateTypeV2Controller.class));
private final AuditService auditService;
private final DescriptionTemplateTypeService descriptionTemplateTypeService; private final DescriptionTemplateTypeService descriptionTemplateTypeService;
public DescriptionTemplateTypeV2Controller(DescriptionTemplateTypeService descriptionTemplateTypeService) { private final MessageSource messageSource;
private final CensorFactory censorFactory;
public DescriptionTemplateTypeV2Controller(AuditService auditService, DescriptionTemplateTypeService descriptionTemplateTypeService, MessageSource messageSource, CensorFactory censorFactory) {
this.auditService = auditService;
this.descriptionTemplateTypeService = descriptionTemplateTypeService; this.descriptionTemplateTypeService = descriptionTemplateTypeService;
this.messageSource = messageSource;
this.censorFactory = censorFactory;
} }
@PostMapping("query") @PostMapping("query")
public QueryResult<DescriptionTemplateType> query(@RequestBody DescriptionTemplateTypeLookup lookup, @ClaimedAuthorities(claims = {Authorities.ADMIN, Authorities.MANAGER, Authorities.USER, Authorities.ANONYMOUS}) Principal ignoredPrincipal) { public QueryResult<DescriptionTemplateType> query(@RequestBody DescriptionTemplateTypeLookup lookup, @ClaimedAuthorities(claims = {Authorities.ADMIN, Authorities.MANAGER, Authorities.USER, Authorities.ANONYMOUS}) Principal ignoredPrincipal) {
logger.debug("querying {}", DescriptionTemplateType.class.getSimpleName());
censorFactory.censor(DescriptionTemplateTypeCensor.class).censor(lookup.getProject());
auditService.track(AuditableAction.DescriptionTemplateType_Query, "lookup", lookup);
return extractQueryResultWithCount(descriptionTemplateTypeService::query, lookup); return extractQueryResultWithCount(descriptionTemplateTypeService::query, lookup);
} }
@GetMapping("{id}") @GetMapping("{id}")
public QueryResult<DescriptionTemplateType> get(@PathVariable("id") UUID id, @ClaimedAuthorities(claims = {Authorities.ADMIN, Authorities.MANAGER, Authorities.USER, Authorities.ANONYMOUS}) Principal ignoredPrincipal) { public QueryResult<DescriptionTemplateType> get(@PathVariable("id") UUID id, FieldSet fieldSet, @ClaimedAuthorities(claims = {Authorities.ADMIN, Authorities.MANAGER, Authorities.USER, Authorities.ANONYMOUS}) Principal ignoredPrincipal) {
return new QueryResult<>(descriptionTemplateTypeService.get(id)); logger.debug(new MapLogEntry("retrieving" + DescriptionTemplateType.class.getSimpleName()).And("id", id));
censorFactory.censor(DescriptionTemplateTypeCensor.class).censor(fieldSet);
DescriptionTemplateType model = descriptionTemplateTypeService.get(id);
if (model == null)
throw new MyNotFoundException(messageSource.getMessage("General_ItemNotFound", new Object[]{id, DescriptionTemplateType.class.getSimpleName()}, LocaleContextHolder.getLocale()));
auditService.track(AuditableAction.DescriptionTemplateType_Query, "id", id);
return new QueryResult<>(model);
} }
@PostMapping("persist") @PostMapping("persist")
public QueryResult<DescriptionTemplateType> persist(@RequestBody DescriptionTemplateType payload, @ClaimedAuthorities(claims = {Authorities.ADMIN}) Principal ignoredPrincipal) { public QueryResult<DescriptionTemplateType> persist(@RequestBody DescriptionTemplateType payload, FieldSet fieldSet, @ClaimedAuthorities(claims = {Authorities.ADMIN}) Principal ignoredPrincipal) {
return new QueryResult<>(descriptionTemplateTypeService.persist(payload)); logger.debug(new MapLogEntry("persisting" + DescriptionTemplateType.class.getSimpleName()).And("model", payload).And("fieldSet", fieldSet));
DescriptionTemplateType persisted = descriptionTemplateTypeService.persist(payload);
auditService.track(AuditableAction.DescriptionTemplateType_Persist, Map.ofEntries(
new AbstractMap.SimpleEntry<String, Object>("model", payload),
new AbstractMap.SimpleEntry<String, Object>("fields", fieldSet)
));
return new QueryResult<>(persisted);
} }
@PostMapping("update") @PostMapping("update")
public QueryResult<DescriptionTemplateType> update(@RequestBody DescriptionTemplateType payload, @ClaimedAuthorities(claims = {Authorities.ADMIN}) Principal ignoredPrincipal) { public QueryResult<DescriptionTemplateType> update(@RequestBody DescriptionTemplateType payload, FieldSet fieldSet, @ClaimedAuthorities(claims = {Authorities.ADMIN}) Principal ignoredPrincipal) {
return new QueryResult<>(descriptionTemplateTypeService.update(payload)); logger.debug(new MapLogEntry("persisting" + DescriptionTemplateType.class.getSimpleName()).And("model", payload).And("fieldSet", fieldSet));
DescriptionTemplateType persisted = descriptionTemplateTypeService.update(payload);
auditService.track(AuditableAction.DescriptionTemplateType_Persist, Map.ofEntries(
new AbstractMap.SimpleEntry<String, Object>("model", payload),
new AbstractMap.SimpleEntry<String, Object>("fields", fieldSet)
));
return new QueryResult<>(persisted);
} }
@DeleteMapping("delete/{id}") @DeleteMapping("delete/{id}")
public ResponseEntity<?> delete(@PathVariable(value = "id") UUID id, @ClaimedAuthorities(claims = {Authorities.ADMIN}) Principal ignoredPrincipal) { public ResponseEntity<?> delete(@PathVariable(value = "id") UUID id, @ClaimedAuthorities(claims = {Authorities.ADMIN}) Principal ignoredPrincipal) {
logger.debug(new MapLogEntry("deleting" + DescriptionTemplateType.class.getSimpleName()).And("id", id));
auditService.track(AuditableAction.DescriptionTemplateType_Delete, "delete", id);
if (descriptionTemplateTypeService.delete(id)) if (descriptionTemplateTypeService.delete(id))
return ResponseEntity.status(HttpStatus.OK).build(); return ResponseEntity.status(HttpStatus.OK).build();
else else