argos/dmp-backend/web/src/main/java/eu/eudat/controllers/DescriptionController.java

287 lines
15 KiB
Java
Raw Normal View History

2024-02-13 08:53:33 +01:00
package eu.eudat.controllers;
import eu.eudat.audit.AuditableAction;
import eu.eudat.authorization.AuthorizationFlags;
2023-11-14 15:08:48 +01:00
import eu.eudat.commons.enums.DmpAccessType;
import eu.eudat.commons.enums.DmpStatus;
import eu.eudat.commons.enums.IsActive;
2024-02-06 14:03:49 +01:00
import eu.eudat.convention.ConventionService;
import eu.eudat.data.StorageFileEntity;
2024-03-15 08:39:36 +01:00
import eu.eudat.model.*;
2024-03-11 08:47:03 +01:00
import eu.eudat.model.builder.PublicDescriptionBuilder;
2024-02-06 14:03:49 +01:00
import eu.eudat.model.persist.DescriptionFieldFilePersist;
2024-03-19 16:21:50 +01:00
import eu.eudat.model.persist.DescriptionSectionPermissionResolver;
2024-02-06 14:03:49 +01:00
import eu.eudat.service.storage.StorageFileService;
2024-01-17 10:20:02 +01:00
import gr.cite.tools.validation.ValidationFilterAnnotation;
import eu.eudat.model.builder.DescriptionBuilder;
import eu.eudat.model.censorship.DescriptionCensor;
2023-11-14 15:08:48 +01:00
import eu.eudat.model.censorship.PublicDescriptionCensor;
import eu.eudat.model.persist.DescriptionPersist;
2023-12-04 17:38:23 +01:00
import eu.eudat.model.persist.DescriptionStatusPersist;
import eu.eudat.model.persist.UpdateDescriptionTemplatePersist;
import eu.eudat.model.result.QueryResult;
import eu.eudat.query.DescriptionQuery;
2023-11-14 15:08:48 +01:00
import eu.eudat.query.DmpQuery;
import eu.eudat.query.lookup.DescriptionLookup;
import eu.eudat.service.description.DescriptionService;
2023-11-15 13:34:48 +01:00
import eu.eudat.service.elastic.ElasticQueryHelperService;
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;
2024-04-08 17:07:47 +02:00
import jakarta.xml.bind.JAXBException;
import org.slf4j.LoggerFactory;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
2024-02-06 14:03:49 +01:00
import org.springframework.core.io.ByteArrayResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
2023-12-18 11:55:19 +01:00
import org.springframework.http.ResponseEntity;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
2024-02-06 14:03:49 +01:00
import org.springframework.web.multipart.MultipartFile;
import javax.management.InvalidApplicationException;
2023-11-14 12:41:57 +01:00
import java.io.IOException;
import java.util.*;
2023-11-14 15:08:48 +01:00
import static eu.eudat.authorization.AuthorizationFlags.Public;
@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;
2023-11-15 13:34:48 +01:00
private final ElasticQueryHelperService elasticQueryHelperService;
2024-02-06 14:03:49 +01:00
private final StorageFileService storageFileService;
private final ConventionService conventionService;
public DescriptionController(
2024-02-06 14:03:49 +01:00
BuilderFactory builderFactory,
AuditService auditService,
DescriptionService descriptionService,
CensorFactory censorFactory,
QueryFactory queryFactory,
MessageSource messageSource,
ElasticQueryHelperService elasticQueryHelperService, StorageFileService storageFileService, ConventionService conventionService) {
this.builderFactory = builderFactory;
this.auditService = auditService;
this.descriptionService = descriptionService;
this.censorFactory = censorFactory;
this.queryFactory = queryFactory;
this.messageSource = messageSource;
2023-11-15 13:34:48 +01:00
this.elasticQueryHelperService = elasticQueryHelperService;
2024-02-06 14:03:49 +01:00
this.storageFileService = storageFileService;
this.conventionService = conventionService;
}
2023-11-14 15:08:48 +01:00
@PostMapping("public/query")
public QueryResult<PublicDescription> publicQuery(@RequestBody DescriptionLookup lookup) throws MyApplicationException, MyForbiddenException {
2024-03-11 08:47:03 +01:00
logger.debug("querying {}", PublicDescription.class.getSimpleName());
2023-11-14 15:08:48 +01:00
this.censorFactory.censor(PublicDescriptionCensor.class).censor(lookup.getProject());
2023-11-15 13:34:48 +01:00
//DescriptionQuery query = lookup.enrich(this.queryFactory).authorize(EnumSet.of(Public)).dmpSubQuery(this.queryFactory.query(DmpQuery.class).isActive(IsActive.Active).statuses(DmpStatus.Finalized).accessTypes(DmpAccessType.Public));
QueryResult<PublicDescription> queryResult = this.elasticQueryHelperService.collectPublic(lookup, EnumSet.of(Public), null);
2023-11-14 15:08:48 +01:00
this.auditService.track(AuditableAction.Description_PublicQuery, "lookup", lookup);
2023-11-15 13:34:48 +01:00
return queryResult;
2023-11-14 15:08:48 +01:00
}
@GetMapping("public/{id}")
2024-03-11 08:47:03 +01:00
public PublicDescription publicGet(@PathVariable("id") UUID id, FieldSet fieldSet, Locale locale) throws MyApplicationException, MyForbiddenException, MyNotFoundException {
logger.debug(new MapLogEntry("retrieving" + PublicDescription.class.getSimpleName()).And("id", id).And("fields", fieldSet));
2023-11-14 15:08:48 +01:00
2024-03-07 16:14:39 +01:00
this.censorFactory.censor(PublicDescriptionCensor.class).censor(fieldSet);
2023-11-14 15:08:48 +01:00
DescriptionQuery query = this.queryFactory.query(DescriptionQuery.class).authorize(EnumSet.of(Public)).ids(id).dmpSubQuery(this.queryFactory.query(DmpQuery.class).isActive(IsActive.Active).statuses(DmpStatus.Finalized).accessTypes(DmpAccessType.Public));
2024-03-11 08:47:03 +01:00
PublicDescription model = this.builderFactory.builder(PublicDescriptionBuilder.class).authorize(EnumSet.of(Public)).build(fieldSet, query.firstAs(fieldSet));
2024-03-15 08:39:36 +01:00
if (model == null) throw new MyNotFoundException(messageSource.getMessage("General_ItemNotFound", new Object[]{id, PublicDescription.class.getSimpleName()}, LocaleContextHolder.getLocale()));
2023-11-14 15:08:48 +01:00
this.auditService.track(AuditableAction.Description_PublicLookup, Map.ofEntries(
new AbstractMap.SimpleEntry<String, Object>("id", id),
new AbstractMap.SimpleEntry<String, Object>("fields", fieldSet)
));
return model;
}
@PostMapping("query")
2023-11-14 15:08:48 +01:00
public QueryResult<Description> query(@RequestBody DescriptionLookup lookup) throws MyApplicationException, MyForbiddenException {
logger.debug("querying {}", Description.class.getSimpleName());
this.censorFactory.censor(DescriptionCensor.class).censor(lookup.getProject(), null);
2024-03-12 17:27:16 +01:00
QueryResult<Description> queryResult = this.elasticQueryHelperService.collect(lookup, AuthorizationFlags.OwnerOrDmpAssociatedOrPermission, null);
this.auditService.track(AuditableAction.Description_Query, "lookup", lookup);
2023-11-15 13:34:48 +01:00
return queryResult;
}
2023-11-14 15:08:48 +01:00
@GetMapping("{id}")
2023-11-14 15:08:48 +01:00
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);
2024-03-12 17:27:16 +01:00
DescriptionQuery query = this.queryFactory.query(DescriptionQuery.class).authorize(AuthorizationFlags.OwnerOrDmpAssociatedOrPermission).ids(id);
Description model = this.builderFactory.builder(DescriptionBuilder.class).authorize(AuthorizationFlags.OwnerOrDmpAssociatedOrPermission).build(fieldSet, query.firstAs(fieldSet));
if (model == null)
2024-03-15 08:39:36 +01:00
throw new MyNotFoundException(messageSource.getMessage("General_ItemNotFound", new Object[]{id, Description.class.getSimpleName()}, LocaleContextHolder.getLocale()));
this.auditService.track(AuditableAction.Description_Lookup, Map.ofEntries(
new AbstractMap.SimpleEntry<String, Object>("id", id),
new AbstractMap.SimpleEntry<String, Object>("fields", fieldSet)
));
return model;
}
@PostMapping("persist")
@Transactional
@ValidationFilterAnnotation(validator = DescriptionPersist.DescriptionPersistValidator.ValidatorName, argumentName = "model")
2024-01-09 15:02:12 +01:00
public Description persist(@RequestBody DescriptionPersist model, FieldSet fieldSet) throws MyApplicationException, MyForbiddenException, MyNotFoundException, InvalidApplicationException, IOException {
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<String, Object>("model", model),
new AbstractMap.SimpleEntry<String, Object>("fields", fieldSet)
));
return persisted;
}
2023-12-04 17:38:23 +01:00
@PostMapping("persist-status")
@Transactional
@ValidationFilterAnnotation(validator = DescriptionStatusPersist.DescriptionStatusPersistValidator.ValidatorName, argumentName = "model")
2024-04-01 17:36:03 +02:00
public Description persistStatus(@RequestBody DescriptionStatusPersist model, FieldSet fieldSet) throws MyApplicationException, MyForbiddenException, MyNotFoundException, IOException, InvalidApplicationException {
2023-12-04 17:38:23 +01:00
logger.debug(new MapLogEntry("persisting" + Description.class.getSimpleName()).And("model", model).And("fieldSet", fieldSet));
Description persisted = this.descriptionService.persistStatus(model, fieldSet);
this.auditService.track(AuditableAction.Description_PersistStatus, Map.ofEntries(
new AbstractMap.SimpleEntry<String, Object>("model", model),
new AbstractMap.SimpleEntry<String, Object>("fields", fieldSet)
));
return persisted;
}
2024-03-19 16:21:50 +01:00
@PostMapping("get-description-section-permissions")
@ValidationFilterAnnotation(validator = DescriptionSectionPermissionResolver.DescriptionSectionPermissionResolverPersistValidator.ValidatorName, argumentName = "model")
public Map<UUID, List<String>> getDescriptionSectionPermissions(@RequestBody DescriptionSectionPermissionResolver model) {
logger.debug(new MapLogEntry("persisting" + Description.class.getSimpleName()).And("model", model));
Map<UUID, List<String>> persisted = this.descriptionService.getDescriptionSectionPermissions(model);
this.auditService.track(AuditableAction.Description_GetDescriptionSectionPermissions, Map.ofEntries(
new AbstractMap.SimpleEntry<String, Object>("model", model)
));
return persisted;
}
2024-03-15 08:39:36 +01:00
@GetMapping("validate")
2024-04-01 17:36:03 +02:00
public List<DescriptionValidationResult> validate(@RequestParam(value="descriptionIds") List<UUID> descriptionIds) throws MyApplicationException, MyForbiddenException, MyNotFoundException, InvalidApplicationException {
2024-03-15 08:39:36 +01:00
logger.debug(new MapLogEntry("validating" + Description.class.getSimpleName()).And("descriptionIds", descriptionIds));
this.censorFactory.censor(DescriptionCensor.class).censor(null, null);
List<DescriptionValidationResult> descriptionValidationResults = this.descriptionService.validate(descriptionIds);
this.auditService.track(AuditableAction.Description_Validate, Map.ofEntries(
new AbstractMap.SimpleEntry<String, Object>("descriptionIds", descriptionIds)
));
return descriptionValidationResults;
}
@DeleteMapping("{id}")
@Transactional
2023-11-14 15:08:48 +01:00
public void delete(@PathVariable("id") UUID id) throws MyForbiddenException, InvalidApplicationException, IOException {
logger.debug(new MapLogEntry("retrieving" + Description.class.getSimpleName()).And("id", id));
this.descriptionService.deleteAndSave(id);
this.auditService.track(AuditableAction.Description_Delete, "id", id);
}
2023-12-18 11:55:19 +01:00
@GetMapping("{id}/export/{type}")
public ResponseEntity<byte[]> export(@PathVariable("id") UUID id, @PathVariable("type") String exportType) throws InvalidApplicationException, IOException {
logger.debug(new MapLogEntry("exporting description"));
return this.descriptionService.export(id, exportType);
}
2024-02-06 14:03:49 +01:00
@PostMapping("field-file/upload")
@Transactional
@ValidationFilterAnnotation(validator = DescriptionFieldFilePersist.PersistValidator.ValidatorName, argumentName = "model")
public StorageFile uploadFieldFiles(@RequestParam("file") MultipartFile file, @RequestParam("model") DescriptionFieldFilePersist model, FieldSet fieldSet) throws IOException {
logger.debug(new MapLogEntry("uploadFieldFiles" + Description.class.getSimpleName()).And("model", model).And("fieldSet", fieldSet));
StorageFile persisted = this.descriptionService.uploadFieldFile(model, file, fieldSet);
this.auditService.track(AuditableAction.Description_UploadFieldFiles, Map.ofEntries(
new AbstractMap.SimpleEntry<String, Object>("model", model),
new AbstractMap.SimpleEntry<String, Object>("fields", fieldSet)
));
return persisted;
}
@GetMapping("{id}/field-file/{fileId}")
public ResponseEntity<ByteArrayResource> getFieldFile(@PathVariable("id") UUID id, @PathVariable("fileId") UUID fileId) throws MyApplicationException, MyForbiddenException, MyNotFoundException {
logger.debug(new MapLogEntry("get Field File" + Description.class.getSimpleName()).And("id", id).And("fileId", fileId));
StorageFileEntity storageFile = this.descriptionService.getFieldFile(id, fileId);
byte[] file = this.storageFileService.readAsBytesSafe(id);
if (file == null) throw new MyNotFoundException(messageSource.getMessage("General_ItemNotFound", new Object[]{id, StorageFile.class.getSimpleName()}, LocaleContextHolder.getLocale()));
this.auditService.track(AuditableAction.Description_GetFieldFile, Map.ofEntries(
new AbstractMap.SimpleEntry<String, Object>("id", id)
));
String contentType = storageFile.getMimeType();
if (this.conventionService.isNullOrEmpty(contentType)) contentType = MediaType.APPLICATION_OCTET_STREAM_VALUE;
return ResponseEntity.ok()
.contentType(MediaType.valueOf(contentType))
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + storageFile.getName() + (storageFile.getExtension().startsWith(".") ? "" : ".") + storageFile.getExtension() + "\"")
.body(new ByteArrayResource(file));
}
@PostMapping("update-description-template")
2024-04-05 17:32:49 +02:00
@Transactional
@ValidationFilterAnnotation(validator = UpdateDescriptionTemplatePersist.UpdateDescriptionTemplatePersistValidator.ValidatorName, argumentName = "model")
2024-04-08 17:07:47 +02:00
public Boolean updateDescriptionTemplate(@RequestBody UpdateDescriptionTemplatePersist model) throws MyApplicationException, MyForbiddenException, MyNotFoundException, InvalidApplicationException, IOException, JAXBException {
2024-04-05 17:32:49 +02:00
logger.debug(new MapLogEntry("update description template" + Description.class.getSimpleName()).And("model", model));
this.descriptionService.updateDescriptionTemplate(model);
2024-04-05 17:32:49 +02:00
this.auditService.track(AuditableAction.Description_UpdateDescriptionTemplate, Map.ofEntries(
new AbstractMap.SimpleEntry<String, Object>("model", model)
));
return true;
}
}