Adding auditing, naming convention change in API endpoints for description types

This commit is contained in:
Thomas Georgios Giannos 2023-10-06 12:13:09 +03:00
parent 05a724d757
commit 0b901205ee
11 changed files with 119 additions and 22 deletions

View File

@ -14,6 +14,13 @@
<properties> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties> </properties>
<dependencies>
<dependency>
<groupId>jakarta.validation</groupId>
<artifactId>jakarta.validation-api</artifactId>
<version>2.0.2</version>
</dependency>
</dependencies>
<build> <build>
<plugins> <plugins>

View File

@ -0,0 +1,13 @@
package eu.eudat.audit;
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_Persist = new EventId(1001, "DescriptionTemplateType_Persist");
public static final EventId DescriptionTemplateType_Delete = new EventId(1002, "DescriptionTemplateType_Delete");
}

View File

@ -0,0 +1,36 @@
package eu.eudat.commons.validation;
import eu.eudat.errorcode.ErrorThesaurusProperties;
import gr.cite.tools.exception.MyValidationException;
import gr.cite.tools.validation.BaseValidationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import javax.validation.Validator;
import java.util.List;
import java.util.Map;
@Service
@Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public class ValidationServiceImpl extends BaseValidationService {
private ErrorThesaurusProperties errors;
@Autowired
public ValidationServiceImpl(Validator validator, ErrorThesaurusProperties errors){
super(validator);
this.errors = errors;
}
@Override
public <T> void validateForce(T item, Class<?>... groups) {
List<Map.Entry<String, List<String>>> validationErrors = this.validate(item, groups);
if (validationErrors != null && !validationErrors.isEmpty()) {
throw new MyValidationException(this.errors.getModelValidation().getCode(),
this.errors.getModelValidation().getMessage(),
validationErrors);
}
}
}

View File

@ -1,11 +1,13 @@
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 jakarta.persistence.EntityManager; import jakarta.persistence.EntityManager;
import jakarta.persistence.PersistenceContext; import jakarta.persistence.PersistenceContext;
@ -28,27 +30,38 @@ public class DescriptionTemplateTypeService {
private final PlatformTransactionManager transactionManager; private final PlatformTransactionManager transactionManager;
private final AuditService auditService;
@PersistenceContext @PersistenceContext
private EntityManager entityManager; private EntityManager entityManager;
public DescriptionTemplateTypeService(ApplicationContext applicationContext, BuilderFactory builderFactory, PlatformTransactionManager platformTransactionManager) { public DescriptionTemplateTypeService(ApplicationContext applicationContext, BuilderFactory builderFactory, PlatformTransactionManager platformTransactionManager, AuditService auditService) {
this.applicationContext = applicationContext; this.applicationContext = applicationContext;
this.builderFactory = builderFactory; this.builderFactory = builderFactory;
this.transactionManager = platformTransactionManager; this.transactionManager = platformTransactionManager;
this.auditService = auditService;
} }
public List<DescriptionTemplateType> getAll(DescriptionTemplateTypeLookup lookup) { public List<DescriptionTemplateType> query(DescriptionTemplateTypeLookup lookup) {
DescriptionTemplateTypeQuery query = applicationContext.getBean(DescriptionTemplateTypeQuery.class); DescriptionTemplateTypeQuery query = applicationContext.getBean(DescriptionTemplateTypeQuery.class);
return builderFactory List<DescriptionTemplateType> results = builderFactory
.builder(DescriptionTemplateTypeBuilder.class) .builder(DescriptionTemplateTypeBuilder.class)
.build(lookup.getProject(), query.fromLookup(lookup).collect()); .build(lookup.getProject(), query.fromLookup(lookup).collect());
auditService.track(AuditableAction.DescriptionTemplateType_Query, "lookup", lookup);
return results;
} }
public DescriptionTemplateType getById(UUID id) { public DescriptionTemplateType get(UUID id) {
DescriptionTemplateTypeQuery query = applicationContext.getBean(DescriptionTemplateTypeQuery.class); DescriptionTemplateTypeQuery query = applicationContext.getBean(DescriptionTemplateTypeQuery.class);
return builderFactory DescriptionTemplateType result = 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) {
@ -56,7 +69,7 @@ public class DescriptionTemplateTypeService {
return query.names(name).first(); return query.names(name).first();
} }
public DescriptionTemplateType create(DescriptionTemplateType payload) { public DescriptionTemplateType persist(DescriptionTemplateType payload) {
DescriptionTemplateTypeEntity created = new DescriptionTemplateTypeEntity(); DescriptionTemplateTypeEntity created = new DescriptionTemplateTypeEntity();
created.setId(UUID.randomUUID()); created.setId(UUID.randomUUID());
created.setName(payload.getName()); created.setName(payload.getName());
@ -79,6 +92,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)
@ -91,6 +106,9 @@ public class DescriptionTemplateTypeService {
entity.setStatus(payload.getStatus()); entity.setStatus(payload.getStatus());
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);
} }
@ -101,6 +119,9 @@ public class DescriptionTemplateTypeService {
entity.setStatus(DescriptionTemplateTypeStatus.DELETED.getValue()); entity.setStatus(DescriptionTemplateTypeStatus.DELETED.getValue());
entityManager.merge(entity); entityManager.merge(entity);
entityManager.flush(); entityManager.flush();
auditService.track(AuditableAction.DescriptionTemplateType_Delete, "delete", id);
return true; return true;
} }

View File

@ -314,6 +314,16 @@
<artifactId>exceptions</artifactId> <artifactId>exceptions</artifactId>
<version>2.1.0</version> <version>2.1.0</version>
</dependency> </dependency>
<dependency>
<groupId>gr.cite</groupId>
<artifactId>logging</artifactId>
<version>2.1.0</version>
</dependency>
<dependency>
<groupId>gr.cite</groupId>
<artifactId>validation</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies> </dependencies>

View File

@ -0,0 +1,10 @@
package eu.eudat.configurations;
import org.springframework.context.annotation.Configuration;
@Configuration
public class AppMessageSourceConfiguration {
}

View File

@ -29,19 +29,19 @@ public class DescriptionTemplateTypeV2Controller {
this.descriptionTemplateTypeService = descriptionTemplateTypeService; this.descriptionTemplateTypeService = descriptionTemplateTypeService;
} }
@PostMapping("all") @PostMapping("query")
public QueryResult<DescriptionTemplateType> getAll(@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) {
return extractQueryResultWithCount(descriptionTemplateTypeService::getAll, 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, @ClaimedAuthorities(claims = {Authorities.ADMIN, Authorities.MANAGER, Authorities.USER, Authorities.ANONYMOUS}) Principal ignoredPrincipal) {
return new QueryResult<>(descriptionTemplateTypeService.getById(id)); return new QueryResult<>(descriptionTemplateTypeService.get(id));
} }
@PostMapping("create") @PostMapping("persist")
public QueryResult<DescriptionTemplateType> create(@RequestBody DescriptionTemplateType payload, @ClaimedAuthorities(claims = {Authorities.ADMIN}) Principal ignoredPrincipal) { public QueryResult<DescriptionTemplateType> persist(@RequestBody DescriptionTemplateType payload, @ClaimedAuthorities(claims = {Authorities.ADMIN}) Principal ignoredPrincipal) {
return new QueryResult<>(descriptionTemplateTypeService.create(payload)); return new QueryResult<>(descriptionTemplateTypeService.persist(payload));
} }
@PostMapping("update") @PostMapping("update")

View File

@ -15,18 +15,18 @@ export class DescriptionTemplateTypeService {
constructor(private http: BaseHttpV2Service, private configurationService: ConfigurationService) {} constructor(private http: BaseHttpV2Service, private configurationService: ConfigurationService) {}
getAll(lookup: DescriptionTemplateTypeLookup): Observable<QueryResult<DescriptionTemplateType>> { query(lookup: DescriptionTemplateTypeLookup): Observable<QueryResult<DescriptionTemplateType>> {
const url = `${this.apiBase}/all`; const url = `${this.apiBase}/query`;
return this.http.post<QueryResult<DescriptionTemplateType>>(url, lookup, { headers: this.headers }); return this.http.post<QueryResult<DescriptionTemplateType>>(url, lookup, { headers: this.headers });
} }
getSingle(id: string): Observable<QueryResult<DescriptionTemplateType>> { get(id: string): Observable<QueryResult<DescriptionTemplateType>> {
const url = `${this.apiBase}/${id}`; const url = `${this.apiBase}/${id}`;
return this.http.get<QueryResult<DescriptionTemplateType>>(url , { headers: this.headers }); return this.http.get<QueryResult<DescriptionTemplateType>>(url , { headers: this.headers });
} }
create(payload: DescriptionTemplateType): Observable<QueryResult<DescriptionTemplateType>> { persist(payload: DescriptionTemplateType): Observable<QueryResult<DescriptionTemplateType>> {
const url = `${this.apiBase}/create`; const url = `${this.apiBase}/persist`;
return this.http.post<QueryResult<DescriptionTemplateType>>(url, payload, { headers: this.headers }); return this.http.post<QueryResult<DescriptionTemplateType>>(url, payload, { headers: this.headers });
} }

View File

@ -617,7 +617,7 @@ export class DatasetProfileEditorComponent extends CheckDeactivateBaseComponent
nameof<DescriptionTemplateType>(x => x.status) nameof<DescriptionTemplateType>(x => x.status)
] ]
}; };
this.descriptionTemplateTypeService.getAll(lookup).pipe(takeUntil(this._destroyed)) this.descriptionTemplateTypeService.query(lookup).pipe(takeUntil(this._destroyed))
.subscribe(types => { .subscribe(types => {
this.descriptionTemplateTypes = types.items.filter(type => type.status === DescriptionTemplateTypeStatus.Finalized); this.descriptionTemplateTypes = types.items.filter(type => type.status === DescriptionTemplateTypeStatus.Finalized);
}); });

View File

@ -44,7 +44,7 @@ export class DescriptionTypeEditorComponent extends BaseComponent implements OnI
if (this.descriptionTemplateTypeId != null) { if (this.descriptionTemplateTypeId != null) {
this.isNew = false; this.isNew = false;
this.descriptionTemplateTypeService.getSingle(this.descriptionTemplateTypeId) this.descriptionTemplateTypeService.get(this.descriptionTemplateTypeId)
.pipe(takeUntil(this._destroyed)).subscribe( .pipe(takeUntil(this._destroyed)).subscribe(
type => { type => {
this.descriptionTypeModel = new DescriptionTypeEditorModel().fromModel(type.items[0]); this.descriptionTypeModel = new DescriptionTypeEditorModel().fromModel(type.items[0]);
@ -84,7 +84,7 @@ export class DescriptionTypeEditorComponent extends BaseComponent implements OnI
onSubmit(): void { onSubmit(): void {
if (this.isNew) { if (this.isNew) {
this.descriptionTemplateTypeService.create(this.formGroup.value) this.descriptionTemplateTypeService.persist(this.formGroup.value)
.pipe(takeUntil(this._destroyed)) .pipe(takeUntil(this._destroyed))
.subscribe( .subscribe(
complete => this.onCallbackSuccess(true), complete => this.onCallbackSuccess(true),

View File

@ -152,7 +152,7 @@ export class DescriptionTypesDataSource extends DataSource<DescriptionTemplateTy
] ]
}; };
return this._service.getAll(lookup) return this._service.query(lookup)
}), }),
map(result => { map(result => {
return result; return result;