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

200 lines
9.9 KiB
Java
Raw Normal View History

2024-02-13 08:53:33 +01:00
package eu.eudat.controllers;
2023-10-23 09:21:25 +02:00
2023-10-24 17:00:11 +02:00
import com.fasterxml.jackson.core.JsonProcessingException;
2023-10-23 09:21:25 +02:00
import eu.eudat.audit.AuditableAction;
import eu.eudat.authorization.AuthorizationFlags;
import eu.eudat.model.persist.NewVersionDmpBlueprintPersist;
2024-01-17 10:20:02 +01:00
import gr.cite.tools.validation.ValidationFilterAnnotation;
2023-10-23 09:21:25 +02:00
import eu.eudat.data.DmpBlueprintEntity;
import eu.eudat.model.DmpBlueprint;
import eu.eudat.model.builder.DmpBlueprintBuilder;
import eu.eudat.model.censorship.DmpBlueprintCensor;
2023-10-23 14:58:33 +02:00
import eu.eudat.model.persist.DmpBlueprintPersist;
2023-10-23 09:21:25 +02:00
import eu.eudat.model.result.QueryResult;
import eu.eudat.query.DmpBlueprintQuery;
2023-10-23 09:21:25 +02:00
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;
2023-10-24 17:00:11 +02:00
import jakarta.xml.bind.JAXBException;
2023-10-23 09:21:25 +02:00
import org.slf4j.LoggerFactory;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
2023-10-23 17:13:34 +02:00
import org.springframework.http.ResponseEntity;
2023-10-23 09:21:25 +02:00
import org.springframework.web.bind.annotation.*;
2023-10-23 17:13:34 +02:00
import org.springframework.web.multipart.MultipartFile;
2023-10-24 17:00:11 +02:00
import org.xml.sax.SAXException;
2023-10-23 09:21:25 +02:00
import javax.management.InvalidApplicationException;
2023-10-24 17:00:11 +02:00
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerException;
2023-10-23 17:13:34 +02:00
import java.io.IOException;
2023-10-24 17:00:11 +02:00
import java.util.AbstractMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
2023-10-23 09:21:25 +02:00
@RestController
@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;
2023-10-23 09:21:25 +02:00
public DmpBlueprintController(
BuilderFactory builderFactory,
AuditService auditService,
DmpBlueprintService dmpBlueprintService,
CensorFactory censorFactory,
QueryFactory queryFactory,
2023-11-15 13:34:48 +01:00
MessageSource messageSource) {
2023-10-23 09:21:25 +02:00
this.builderFactory = builderFactory;
this.auditService = auditService;
this.dmpBlueprintService = dmpBlueprintService;
this.censorFactory = censorFactory;
this.queryFactory = queryFactory;
this.messageSource = messageSource;
}
@PostMapping("query")
2023-10-23 14:58:33 +02:00
public QueryResult<DmpBlueprint> query(@RequestBody DmpBlueprintLookup lookup) throws MyApplicationException, MyForbiddenException {
2023-10-23 09:21:25 +02:00
logger.debug("querying {}", DmpBlueprint.class.getSimpleName());
this.censorFactory.censor(DmpBlueprintCensor.class).censor(lookup.getProject(), null);
2024-03-12 17:27:16 +01:00
DmpBlueprintQuery query = lookup.enrich(this.queryFactory).authorize(AuthorizationFlags.OwnerOrDmpAssociatedOrPermission);
2023-10-23 09:21:25 +02:00
List<DmpBlueprintEntity> data = query.collectAs(lookup.getProject());
2024-03-12 17:27:16 +01:00
List<DmpBlueprint> models = this.builderFactory.builder(DmpBlueprintBuilder.class).authorize(AuthorizationFlags.OwnerOrDmpAssociatedOrPermission).build(lookup.getProject(), data);
2023-10-23 09:21:25 +02:00
long count = (lookup.getMetadata() != null && lookup.getMetadata().getCountAll()) ? query.count() : models.size();
this.auditService.track(AuditableAction.DmpBlueprint_Query, "lookup", lookup);
return new QueryResult<>(models, count);
}
@GetMapping("{id}")
2023-10-23 14:58:33 +02:00
public DmpBlueprint get(@PathVariable("id") UUID id, FieldSet fieldSet) throws MyApplicationException, MyForbiddenException, MyNotFoundException {
2023-10-23 09:21:25 +02:00
logger.debug(new MapLogEntry("retrieving" + DmpBlueprint.class.getSimpleName()).And("id", id).And("fields", fieldSet));
this.censorFactory.censor(DmpBlueprintCensor.class).censor(fieldSet, null);
2024-03-12 17:27:16 +01:00
DmpBlueprintQuery query = this.queryFactory.query(DmpBlueprintQuery.class).authorize(AuthorizationFlags.OwnerOrDmpAssociatedOrPermission).ids(id);
DmpBlueprint model = this.builderFactory.builder(DmpBlueprintBuilder.class).authorize(AuthorizationFlags.OwnerOrDmpAssociatedOrPermission).build(fieldSet, query.firstAs(fieldSet));
2023-10-23 09:21:25 +02:00
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<String, Object>("id", id),
new AbstractMap.SimpleEntry<String, Object>("fields", fieldSet)
));
return model;
}
2023-10-23 14:58:33 +02:00
@PostMapping("persist")
@Transactional
@ValidationFilterAnnotation(validator = DmpBlueprintPersist.DmpBlueprintPersistValidator.ValidatorName, argumentName = "model")
2024-01-05 13:02:22 +01:00
public DmpBlueprint persist(@RequestBody DmpBlueprintPersist model, FieldSet fieldSet) throws MyApplicationException, MyForbiddenException, MyNotFoundException, InvalidApplicationException, JAXBException, ParserConfigurationException, JsonProcessingException, TransformerException {
2023-10-23 14:58:33 +02:00
logger.debug(new MapLogEntry("persisting" + DmpBlueprint.class.getSimpleName()).And("model", model).And("fieldSet", fieldSet));
this.censorFactory.censor(DmpBlueprintCensor.class).censor(fieldSet, null);
2023-10-23 14:58:33 +02:00
DmpBlueprint persisted = this.dmpBlueprintService.persist(model, fieldSet);
this.auditService.track(AuditableAction.DmpBlueprint_Persist, Map.ofEntries(
new AbstractMap.SimpleEntry<String, Object>("model", model),
new AbstractMap.SimpleEntry<String, Object>("fields", fieldSet)
));
2024-01-05 13:02:22 +01:00
2023-10-23 14:58:33 +02:00
return persisted;
}
2023-10-23 09:21:25 +02:00
@DeleteMapping("{id}")
@Transactional
2023-10-23 14:58:33 +02:00
public void delete(@PathVariable("id") UUID id) throws MyForbiddenException, InvalidApplicationException {
2023-10-23 09:21:25 +02:00
logger.debug(new MapLogEntry("retrieving" + DmpBlueprint.class.getSimpleName()).And("id", id));
this.dmpBlueprintService.deleteAndSave(id);
this.auditService.track(AuditableAction.DmpBlueprint_Delete, "id", id);
}
2023-10-23 17:13:34 +02:00
2023-10-24 17:00:11 +02:00
@GetMapping("clone/{id}")
public DmpBlueprint 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);
DmpBlueprint model = this.dmpBlueprintService.buildClone(id, fieldSet);
this.auditService.track(AuditableAction.DmpBlueprint_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 = NewVersionDmpBlueprintPersist.NewVersionDmpBlueprintPersistValidator.ValidatorName, argumentName = "model")
public DmpBlueprint createNewVersion(@RequestBody NewVersionDmpBlueprintPersist model, FieldSet fieldSet) throws JAXBException, InvalidApplicationException, ParserConfigurationException, JsonProcessingException, TransformerException {
logger.debug(new MapLogEntry("persisting" + NewVersionDmpBlueprintPersist.class.getSimpleName()).And("model", model).And("fieldSet", fieldSet));
DmpBlueprint persisted = this.dmpBlueprintService.createNewVersion(model, fieldSet);
this.auditService.track(AuditableAction.DmpBlueprint_PersistNewVersion, Map.ofEntries(
new AbstractMap.SimpleEntry<String, Object>("model", model),
new AbstractMap.SimpleEntry<String, Object>("fields", fieldSet)
));
return persisted;
}
2023-10-24 17:00:11 +02: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" + DmpBlueprint.class.getSimpleName()).And("id", id));
2023-10-24 17:00:11 +02:00
ResponseEntity<byte[]> response = this.dmpBlueprintService.exportXml(id);
2023-10-24 17:00:11 +02:00
this.auditService.track(AuditableAction.DmpBlueprint_GetXml, Map.ofEntries(
new AbstractMap.SimpleEntry<String, Object>("id", id)
));
return response;
}
@RequestMapping(method = RequestMethod.POST, value = {"/xml/import"})
2023-10-27 17:46:34 +02:00
public DmpBlueprint importXml(@RequestParam("file") MultipartFile file, FieldSet fieldSet) throws IOException, JAXBException, InvalidApplicationException, ParserConfigurationException, TransformerException, InstantiationException, IllegalAccessException, SAXException {
2023-10-24 17:00:11 +02:00
logger.debug(new MapLogEntry("clone" + DmpBlueprint.class.getSimpleName()).And("file", file));
this.censorFactory.censor(DmpBlueprintCensor.class).censor(fieldSet, null);
DmpBlueprint model = this.dmpBlueprintService.importXml(file.getBytes(), file.getOriginalFilename(), fieldSet);
2023-11-03 10:55:26 +01:00
this.auditService.track(AuditableAction.DmpBlueprint_Import, Map.ofEntries(
2023-10-24 17:00:11 +02:00
new AbstractMap.SimpleEntry<String, Object>("file", file),
new AbstractMap.SimpleEntry<String, Object>("fields", fieldSet)
));
return model;
}
2023-10-23 09:21:25 +02:00
}