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

206 lines
11 KiB
Java
Raw Normal View History

2024-02-13 08:53:33 +01:00
package eu.eudat.controllers;
2023-10-27 17:46:34 +02:00
2023-10-30 17:02:20 +01:00
import com.fasterxml.jackson.core.JsonProcessingException;
2023-10-27 17:46:34 +02:00
import eu.eudat.audit.AuditableAction;
import eu.eudat.authorization.AuthorizationFlags;
2024-01-25 19:55:42 +01:00
import gr.cite.tools.fieldset.BaseFieldSet;
2024-01-17 10:20:02 +01:00
import gr.cite.tools.validation.ValidationFilterAnnotation;
2023-10-27 17:46:34 +02:00
import eu.eudat.data.DescriptionTemplateEntity;
import eu.eudat.model.DescriptionTemplate;
2023-11-01 11:03:23 +01:00
import eu.eudat.model.DmpBlueprint;
2023-10-27 17:46:34 +02:00
import eu.eudat.model.builder.DescriptionTemplateBuilder;
import eu.eudat.model.censorship.DescriptionTemplateCensor;
2023-11-01 11:03:23 +01:00
import eu.eudat.model.censorship.DmpBlueprintCensor;
2023-10-30 14:07:59 +01:00
import eu.eudat.model.persist.DescriptionTemplatePersist;
2023-11-01 11:03:23 +01:00
import eu.eudat.model.persist.NewVersionDescriptionTemplatePersist;
2023-10-27 17:46:34 +02:00
import eu.eudat.model.result.QueryResult;
import eu.eudat.query.DescriptionTemplateQuery;
import eu.eudat.query.lookup.DescriptionTemplateLookup;
2023-10-30 14:07:59 +01:00
import eu.eudat.service.descriptiontemplate.DescriptionTemplateService;
2023-10-27 17:46:34 +02:00
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;
2023-10-30 17:02:20 +01:00
import jakarta.xml.bind.JAXBException;
2023-10-27 17:46:34 +02:00
import org.slf4j.LoggerFactory;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
2023-11-03 10:55:26 +01:00
import org.springframework.http.ResponseEntity;
2023-10-30 14:07:59 +01:00
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
2023-11-01 17:12:59 +01:00
import org.springframework.web.multipart.MultipartFile;
import org.xml.sax.SAXException;
2023-10-27 17:46:34 +02:00
import javax.management.InvalidApplicationException;
2023-10-30 17:02:20 +01:00
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerException;
2023-11-01 17:12:59 +01:00
import java.io.IOException;
import java.util.AbstractMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
2023-10-27 17:46:34 +02:00
@RestController
@RequestMapping(path = "api/description-template")
public class DescriptionTemplateController {
private static final LoggerService logger = new LoggerService(LoggerFactory.getLogger(DescriptionTemplateController.class));
private final BuilderFactory builderFactory;
private final AuditService auditService;
2023-10-30 14:07:59 +01:00
private final DescriptionTemplateService descriptionTemplateTypeService;
2023-10-27 17:46:34 +02:00
private final CensorFactory censorFactory;
private final QueryFactory queryFactory;
private final MessageSource messageSource;
public DescriptionTemplateController(
BuilderFactory builderFactory,
AuditService auditService,
2023-10-30 14:07:59 +01:00
DescriptionTemplateService descriptionTemplateTypeService,
2023-10-27 17:46:34 +02:00
CensorFactory censorFactory,
QueryFactory queryFactory,
MessageSource messageSource) {
this.builderFactory = builderFactory;
this.auditService = auditService;
2023-10-30 14:07:59 +01:00
this.descriptionTemplateTypeService = descriptionTemplateTypeService;
2023-10-27 17:46:34 +02:00
this.censorFactory = censorFactory;
this.queryFactory = queryFactory;
this.messageSource = messageSource;
}
@PostMapping("query")
2023-11-01 11:03:23 +01:00
public QueryResult<DescriptionTemplate> query(@RequestBody DescriptionTemplateLookup lookup) throws MyApplicationException, MyForbiddenException {
2023-10-27 17:46:34 +02:00
logger.debug("querying {}", DescriptionTemplate.class.getSimpleName());
this.censorFactory.censor(DescriptionTemplateCensor.class).censor(lookup.getProject(), null);
2024-03-12 17:27:16 +01:00
DescriptionTemplateQuery query = lookup.enrich(this.queryFactory).authorize(AuthorizationFlags.OwnerOrDmpAssociatedOrPermission);
2023-10-27 17:46:34 +02:00
List<DescriptionTemplateEntity> data = query.collectAs(lookup.getProject());
2024-03-12 17:27:16 +01:00
List<DescriptionTemplate> models = this.builderFactory.builder(DescriptionTemplateBuilder.class).authorize(AuthorizationFlags.OwnerOrDmpAssociatedOrPermission).build(lookup.getProject(), data);
2023-10-27 17:46:34 +02:00
long count = (lookup.getMetadata() != null && lookup.getMetadata().getCountAll()) ? query.count() : models.size();
this.auditService.track(AuditableAction.DescriptionTemplate_Query, "lookup", lookup);
return new QueryResult<>(models, count);
}
@GetMapping("{id}")
2023-12-13 10:42:59 +01:00
public DescriptionTemplate get(@PathVariable("id") UUID id, FieldSet fieldSet) throws MyApplicationException, MyForbiddenException, MyNotFoundException {
2023-10-27 17:46:34 +02:00
logger.debug(new MapLogEntry("retrieving" + DescriptionTemplate.class.getSimpleName()).And("id", id).And("fields", fieldSet));
this.censorFactory.censor(DescriptionTemplateCensor.class).censor(fieldSet, null);
2024-03-12 17:27:16 +01:00
DescriptionTemplateQuery query = this.queryFactory.query(DescriptionTemplateQuery.class).authorize(AuthorizationFlags.OwnerOrDmpAssociatedOrPermission).ids(id);
DescriptionTemplate model = this.builderFactory.builder(DescriptionTemplateBuilder.class).authorize(AuthorizationFlags.OwnerOrDmpAssociatedOrPermission).build(fieldSet, query.firstAs(fieldSet));
2023-10-27 17:46:34 +02:00
if (model == null)
throw new MyNotFoundException(messageSource.getMessage("General_ItemNotFound", new Object[]{id, DescriptionTemplate.class.getSimpleName()}, LocaleContextHolder.getLocale()));
this.auditService.track(AuditableAction.DescriptionTemplate_Lookup, Map.ofEntries(
new AbstractMap.SimpleEntry<String, Object>("id", id),
new AbstractMap.SimpleEntry<String, Object>("fields", fieldSet)
));
return model;
}
2023-10-30 14:07:59 +01:00
@PostMapping("persist")
@Transactional
@ValidationFilterAnnotation(validator = DescriptionTemplatePersist.DescriptionTemplatePersistValidator.ValidatorName, argumentName = "model")
public DescriptionTemplate persist(@RequestBody DescriptionTemplatePersist model, FieldSet fieldSet) throws MyApplicationException, MyForbiddenException, MyNotFoundException, InvalidApplicationException, JAXBException, ParserConfigurationException, JsonProcessingException, TransformerException {
2023-10-30 14:07:59 +01:00
logger.debug(new MapLogEntry("persisting" + DescriptionTemplate.class.getSimpleName()).And("model", model).And("fieldSet", fieldSet));
2024-01-25 19:55:42 +01:00
new BaseFieldSet(fieldSet.getFields()).ensure(DescriptionTemplate._id);
2023-10-30 14:07:59 +01:00
DescriptionTemplate persisted = this.descriptionTemplateTypeService.persist(model, fieldSet);
this.auditService.track(AuditableAction.DescriptionTemplate_Persist, Map.ofEntries(
new AbstractMap.SimpleEntry<String, Object>("model", model),
new AbstractMap.SimpleEntry<String, Object>("fields", fieldSet)
));
2023-10-30 14:07:59 +01:00
return persisted;
}
@DeleteMapping("{id}")
@Transactional
2023-11-01 11:03:23 +01:00
public void delete(@PathVariable("id") UUID id) throws MyForbiddenException, InvalidApplicationException {
2023-10-30 14:07:59 +01:00
logger.debug(new MapLogEntry("retrieving" + DescriptionTemplate.class.getSimpleName()).And("id", id));
this.descriptionTemplateTypeService.deleteAndSave(id);
this.auditService.track(AuditableAction.DescriptionTemplate_Delete, "id", id);
}
2023-11-01 11:03:23 +01:00
@GetMapping("clone/{id}")
public DescriptionTemplate buildClone(@PathVariable("id") UUID id, FieldSet fieldSet) throws MyApplicationException, MyForbiddenException, MyNotFoundException {
logger.debug(new MapLogEntry("clone" + DmpBlueprint.class.getSimpleName()).And("id", id).And("fields", fieldSet));
this.censorFactory.censor(DmpBlueprintCensor.class).censor(fieldSet, null);
DescriptionTemplate model = this.descriptionTemplateTypeService.buildClone(id, fieldSet);
this.auditService.track(AuditableAction.DescriptionTemplate_Clone, Map.ofEntries(
new AbstractMap.SimpleEntry<String, Object>("id", id),
new AbstractMap.SimpleEntry<String, Object>("fields", fieldSet)
));
return model;
}
@PostMapping("new-version")
@Transactional
@ValidationFilterAnnotation(validator = NewVersionDescriptionTemplatePersist.NewVersionDescriptionTemplatePersistValidator.ValidatorName, argumentName = "model")
public DescriptionTemplate createNewVersion(@RequestBody NewVersionDescriptionTemplatePersist model, FieldSet fieldSet) throws MyApplicationException, MyForbiddenException, MyNotFoundException, InvalidApplicationException, JAXBException, ParserConfigurationException, JsonProcessingException, TransformerException {
2023-11-01 11:03:23 +01:00
logger.debug(new MapLogEntry("persisting" + NewVersionDescriptionTemplatePersist.class.getSimpleName()).And("model", model).And("fieldSet", fieldSet));
DescriptionTemplate persisted = this.descriptionTemplateTypeService.createNewVersion(model, fieldSet);
this.auditService.track(AuditableAction.DescriptionTemplate_PersistNewVersion, Map.ofEntries(
new AbstractMap.SimpleEntry<String, Object>("model", model),
new AbstractMap.SimpleEntry<String, Object>("fields", fieldSet)
));
2023-11-01 11:03:23 +01:00
return persisted;
}
2023-11-01 17:12:59 +01:00
2023-11-03 10:55:26 +01:00
@RequestMapping(method = RequestMethod.GET, value = {"/xml/export/{id}"}, produces = "application/xml")
public @ResponseBody ResponseEntity<byte[]> getXml(@PathVariable UUID id) throws JAXBException, ParserConfigurationException, IOException, TransformerException, InstantiationException, IllegalAccessException, SAXException, InvalidApplicationException {
2023-11-03 10:55:26 +01:00
logger.debug(new MapLogEntry("export" + DescriptionTemplate.class.getSimpleName()).And("id", id));
ResponseEntity<byte[]> response = this.descriptionTemplateTypeService.exportXml(id);
2023-11-03 10:55:26 +01:00
this.auditService.track(AuditableAction.DescriptionTemplate_GetXml, Map.ofEntries(
new AbstractMap.SimpleEntry<String, Object>("id", id)
));
2023-11-03 10:55:26 +01:00
return response;
}
2023-11-01 17:12:59 +01:00
@RequestMapping(method = RequestMethod.POST, value = {"/xml/import/{id}", "/xml/import"})
public DescriptionTemplate importXml(@RequestParam("file") MultipartFile file, @PathVariable(value = "id", required = false) UUID id, FieldSet fieldSet) throws IOException, JAXBException, InvalidApplicationException, ParserConfigurationException, TransformerException, InstantiationException, IllegalAccessException, SAXException {
logger.debug(new MapLogEntry("import" + DescriptionTemplate.class.getSimpleName()).And("file", file).And("id", id));
this.censorFactory.censor(DescriptionTemplateCensor.class).censor(fieldSet, null);
DescriptionTemplate model = this.descriptionTemplateTypeService.importXml(file.getBytes(), id, file.getOriginalFilename(), fieldSet);
2023-11-03 10:55:26 +01:00
this.auditService.track(AuditableAction.DescriptionTemplate_Import, Map.ofEntries(
2023-11-01 17:12:59 +01:00
new AbstractMap.SimpleEntry<String, Object>("file", file),
new AbstractMap.SimpleEntry<String, Object>("id", id),
new AbstractMap.SimpleEntry<String, Object>("fields", fieldSet)
));
return model;
}
2023-11-06 16:16:55 +01:00
2023-10-27 17:46:34 +02:00
}