2024-02-13 08:53:33 +01:00
|
|
|
package eu.eudat.controllers;
|
2023-10-18 09:31:57 +02:00
|
|
|
|
2023-11-03 17:09:19 +01:00
|
|
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
|
|
|
import eu.eudat.audit.AuditableAction;
|
|
|
|
import eu.eudat.authorization.AuthorizationFlags;
|
2024-02-19 16:28:46 +01:00
|
|
|
import eu.eudat.commons.enums.SupportiveMaterialFieldType;
|
|
|
|
import gr.cite.tools.fieldset.BaseFieldSet;
|
2024-01-17 10:20:02 +01:00
|
|
|
import gr.cite.tools.validation.ValidationFilterAnnotation;
|
2023-11-03 17:09:19 +01:00
|
|
|
import eu.eudat.data.SupportiveMaterialEntity;
|
|
|
|
import eu.eudat.model.SupportiveMaterial;
|
|
|
|
import eu.eudat.model.builder.SupportiveMaterialBuilder;
|
|
|
|
import eu.eudat.model.censorship.SupportiveMaterialCensor;
|
2023-11-03 12:30:11 +01:00
|
|
|
import eu.eudat.model.persist.SupportiveMaterialPersist;
|
2023-11-03 17:09:19 +01:00
|
|
|
import eu.eudat.model.result.QueryResult;
|
|
|
|
import eu.eudat.query.SupportiveMaterialQuery;
|
|
|
|
import eu.eudat.query.lookup.SupportiveMaterialLookup;
|
2023-10-18 09:31:57 +02:00
|
|
|
import eu.eudat.service.supportivematerial.SupportiveMaterialService;
|
2023-11-03 17:09:19 +01: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;
|
|
|
|
import jakarta.transaction.Transactional;
|
|
|
|
import jakarta.xml.bind.JAXBException;
|
|
|
|
import org.slf4j.LoggerFactory;
|
2023-10-18 09:31:57 +02:00
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
2023-11-03 17:09:19 +01:00
|
|
|
import org.springframework.context.MessageSource;
|
|
|
|
import org.springframework.context.i18n.LocaleContextHolder;
|
2024-02-19 16:28:46 +01:00
|
|
|
import org.springframework.http.HttpHeaders;
|
2023-10-18 09:31:57 +02:00
|
|
|
import org.springframework.http.HttpStatus;
|
2024-02-19 16:28:46 +01:00
|
|
|
import org.springframework.http.MediaType;
|
2023-10-18 09:31:57 +02:00
|
|
|
import org.springframework.http.ResponseEntity;
|
|
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
|
2023-11-03 17:09:19 +01:00
|
|
|
import javax.management.InvalidApplicationException;
|
|
|
|
import javax.xml.parsers.ParserConfigurationException;
|
|
|
|
import javax.xml.transform.TransformerException;
|
2023-10-18 09:31:57 +02:00
|
|
|
import java.io.IOException;
|
2023-11-03 17:09:19 +01:00
|
|
|
import java.util.*;
|
2023-10-18 09:31:57 +02:00
|
|
|
|
|
|
|
@RestController
|
2023-11-03 17:09:19 +01:00
|
|
|
@RequestMapping(path = {"/api/supportive-material"})
|
2023-10-18 09:31:57 +02:00
|
|
|
public class SupportiveMaterialController {
|
|
|
|
|
2023-11-03 17:09:19 +01:00
|
|
|
private static final LoggerService logger = new LoggerService(LoggerFactory.getLogger(SupportiveMaterialController.class));
|
2024-01-04 10:51:56 +01:00
|
|
|
|
|
|
|
|
2023-11-03 17:09:19 +01:00
|
|
|
private final BuilderFactory builderFactory;
|
2024-01-04 10:51:56 +01:00
|
|
|
|
2023-11-03 17:09:19 +01:00
|
|
|
private final AuditService auditService;
|
|
|
|
|
|
|
|
private final CensorFactory censorFactory;
|
2024-01-04 10:51:56 +01:00
|
|
|
|
2023-11-03 17:09:19 +01:00
|
|
|
private final QueryFactory queryFactory;
|
2024-01-04 10:51:56 +01:00
|
|
|
|
2023-11-03 17:09:19 +01:00
|
|
|
private final MessageSource messageSource;
|
2024-01-04 10:51:56 +01:00
|
|
|
|
2023-11-03 17:09:19 +01:00
|
|
|
private final SupportiveMaterialService supportiveMaterialService;
|
2024-02-19 16:28:46 +01:00
|
|
|
|
2023-10-18 09:31:57 +02:00
|
|
|
|
|
|
|
@Autowired
|
2024-02-19 16:28:46 +01:00
|
|
|
public SupportiveMaterialController(SupportiveMaterialService supportiveMaterialService, BuilderFactory builderFactory,
|
|
|
|
AuditService auditService, CensorFactory censorFactory, QueryFactory queryFactory, MessageSource messageSource) {
|
2023-10-18 09:31:57 +02:00
|
|
|
this.supportiveMaterialService = supportiveMaterialService;
|
2023-11-03 17:09:19 +01:00
|
|
|
this.builderFactory = builderFactory;
|
|
|
|
this.auditService = auditService;
|
|
|
|
this.censorFactory = censorFactory;
|
|
|
|
this.queryFactory = queryFactory;
|
|
|
|
this.messageSource = messageSource;
|
2023-10-18 09:31:57 +02:00
|
|
|
}
|
|
|
|
|
2023-11-03 17:09:19 +01:00
|
|
|
@PostMapping("query")
|
|
|
|
public QueryResult<SupportiveMaterial> query(@RequestBody SupportiveMaterialLookup lookup) throws MyApplicationException, MyForbiddenException {
|
|
|
|
logger.debug("querying {}", SupportiveMaterial.class.getSimpleName());
|
|
|
|
|
|
|
|
this.censorFactory.censor(SupportiveMaterialCensor.class).censor(lookup.getProject(), null);
|
|
|
|
|
2024-03-12 17:27:16 +01:00
|
|
|
SupportiveMaterialQuery query = lookup.enrich(this.queryFactory).authorize(AuthorizationFlags.OwnerOrDmpAssociatedOrPermission);
|
2023-11-13 08:39:31 +01:00
|
|
|
List<SupportiveMaterialEntity> data = query.collectAs(lookup.getProject());
|
2024-03-12 17:27:16 +01:00
|
|
|
List<SupportiveMaterial> models = this.builderFactory.builder(SupportiveMaterialBuilder.class).authorize(AuthorizationFlags.OwnerOrDmpAssociatedOrPermission).build(lookup.getProject(), data);
|
2023-11-03 17:09:19 +01:00
|
|
|
long count = (lookup.getMetadata() != null && lookup.getMetadata().getCountAll()) ? query.count() : models.size();
|
|
|
|
|
|
|
|
this.auditService.track(AuditableAction.SupportiveMaterial_Query, "lookup", lookup);
|
|
|
|
|
2024-02-19 16:28:46 +01:00
|
|
|
return new QueryResult<SupportiveMaterial>(models, count);
|
2023-11-03 17:09:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@GetMapping("{id}")
|
|
|
|
public SupportiveMaterial get(@PathVariable("id") UUID id, FieldSet fieldSet) throws MyApplicationException, MyForbiddenException, MyNotFoundException {
|
|
|
|
logger.debug(new MapLogEntry("retrieving " + SupportiveMaterial.class.getSimpleName()).And("id", id).And("fields", fieldSet));
|
|
|
|
|
|
|
|
this.censorFactory.censor(SupportiveMaterialCensor.class).censor(fieldSet, null);
|
|
|
|
|
2024-03-12 17:27:16 +01:00
|
|
|
SupportiveMaterialQuery query = this.queryFactory.query(SupportiveMaterialQuery.class).authorize(AuthorizationFlags.OwnerOrDmpAssociatedOrPermission).ids(id);
|
|
|
|
SupportiveMaterial model = this.builderFactory.builder(SupportiveMaterialBuilder.class).authorize(AuthorizationFlags.OwnerOrDmpAssociatedOrPermission).build(fieldSet, query.firstAs(fieldSet));
|
2023-11-03 17:09:19 +01:00
|
|
|
if (model == null)
|
|
|
|
throw new MyNotFoundException(messageSource.getMessage("General_ItemNotFound", new Object[]{id, SupportiveMaterial.class.getSimpleName()}, LocaleContextHolder.getLocale()));
|
|
|
|
|
|
|
|
this.auditService.track(AuditableAction.SupportiveMaterial_Lookup, Map.ofEntries(
|
|
|
|
new AbstractMap.SimpleEntry<String, Object>("id", id),
|
|
|
|
new AbstractMap.SimpleEntry<String, Object>("fields", fieldSet)
|
|
|
|
));
|
|
|
|
|
|
|
|
return model;
|
|
|
|
}
|
|
|
|
|
2024-02-19 16:28:46 +01:00
|
|
|
@GetMapping("get-payload/{type}/{language}")
|
|
|
|
public ResponseEntity<byte[]> getPayload(@PathVariable("type") Short type, @PathVariable("type") String language) throws IOException {
|
2023-11-06 14:25:04 +01:00
|
|
|
logger.debug("querying {}", SupportiveMaterial.class.getSimpleName());
|
|
|
|
|
|
|
|
|
2024-03-12 17:27:16 +01:00
|
|
|
SupportiveMaterialQuery query = this.queryFactory.query(SupportiveMaterialQuery.class).types(SupportiveMaterialFieldType.of(type)).languageCodes(language).authorize(AuthorizationFlags.OwnerOrDmpAssociatedOrPermission);
|
2024-02-19 16:28:46 +01:00
|
|
|
List<SupportiveMaterialEntity> data = query.collectAs(new BaseFieldSet().ensure(SupportiveMaterial._id).ensure(SupportiveMaterial._payload));
|
|
|
|
byte[] content;
|
|
|
|
if (data.size() == 1) content = data.getFirst().getPayload().getBytes();
|
|
|
|
else content = this.supportiveMaterialService.loadFromFile(language, SupportiveMaterialFieldType.of(type));
|
|
|
|
|
|
|
|
HttpHeaders responseHeaders = new HttpHeaders();
|
|
|
|
responseHeaders.setContentLength(content.length);
|
|
|
|
responseHeaders.setContentType(MediaType.TEXT_HTML);
|
|
|
|
responseHeaders.set("Content-Disposition", "attachment;filename=" + SupportiveMaterialFieldType.of(type).name().toLowerCase(Locale.ROOT) + "_" + language + "html");
|
|
|
|
responseHeaders.set("Access-Control-Expose-Headers", "Content-Disposition");
|
|
|
|
responseHeaders.get("Access-Control-Expose-Headers").add("Content-Type");
|
|
|
|
|
|
|
|
return new ResponseEntity<>(content, responseHeaders, HttpStatus.OK);
|
|
|
|
}
|
|
|
|
|
|
|
|
@GetMapping("public/get-payload/{type}/{language}")
|
|
|
|
public ResponseEntity<byte[]> getPayloadPublic(@PathVariable("type") Short type, @PathVariable("type") String language) throws IOException {
|
|
|
|
logger.debug("querying {}", SupportiveMaterial.class.getSimpleName());
|
|
|
|
|
|
|
|
byte[] content = this.supportiveMaterialService.loadFromFile(language, SupportiveMaterialFieldType.of(type));
|
|
|
|
|
|
|
|
HttpHeaders responseHeaders = new HttpHeaders();
|
|
|
|
responseHeaders.setContentLength(content.length);
|
|
|
|
responseHeaders.setContentType(MediaType.TEXT_HTML);
|
|
|
|
responseHeaders.set("Content-Disposition", "attachment;filename=" + SupportiveMaterialFieldType.of(type).name().toLowerCase(Locale.ROOT) + "_" + language + "html");
|
|
|
|
responseHeaders.set("Access-Control-Expose-Headers", "Content-Disposition");
|
|
|
|
responseHeaders.get("Access-Control-Expose-Headers").add("Content-Type");
|
2023-11-03 12:30:11 +01:00
|
|
|
|
2024-02-19 16:28:46 +01:00
|
|
|
return new ResponseEntity<>(content, responseHeaders, HttpStatus.OK);
|
2023-10-18 09:31:57 +02:00
|
|
|
}
|
|
|
|
|
2023-11-03 12:30:11 +01:00
|
|
|
@PostMapping("persist")
|
2023-11-03 17:09:19 +01:00
|
|
|
@Transactional
|
2024-01-04 10:51:56 +01:00
|
|
|
@ValidationFilterAnnotation(validator = SupportiveMaterialPersist.SupportiveMaterialPersistValidator.ValidatorName, argumentName = "model")
|
|
|
|
public SupportiveMaterial persist(@RequestBody SupportiveMaterialPersist model, FieldSet fieldSet) throws MyApplicationException, MyForbiddenException, MyNotFoundException, InvalidApplicationException, JAXBException, ParserConfigurationException, JsonProcessingException, TransformerException {
|
2023-11-03 17:09:19 +01:00
|
|
|
logger.debug(new MapLogEntry("persisting" + SupportiveMaterial.class.getSimpleName()).And("model", model).And("fieldSet", fieldSet));
|
|
|
|
this.censorFactory.censor(SupportiveMaterialCensor.class).censor(fieldSet, null);
|
|
|
|
|
|
|
|
SupportiveMaterial persisted = this.supportiveMaterialService.persist(model, fieldSet);
|
|
|
|
|
|
|
|
this.auditService.track(AuditableAction.SupportiveMaterial_Persist, Map.ofEntries(
|
|
|
|
new AbstractMap.SimpleEntry<String, Object>("model", model),
|
|
|
|
new AbstractMap.SimpleEntry<String, Object>("fields", fieldSet)
|
|
|
|
));
|
|
|
|
return persisted;
|
|
|
|
}
|
|
|
|
|
|
|
|
@DeleteMapping("{id}")
|
|
|
|
@Transactional
|
|
|
|
public void delete(@PathVariable("id") UUID id) throws MyForbiddenException, InvalidApplicationException {
|
|
|
|
logger.debug(new MapLogEntry("retrieving" + SupportiveMaterial.class.getSimpleName()).And("id", id));
|
|
|
|
|
|
|
|
this.supportiveMaterialService.deleteAndSave(id);
|
2023-11-03 12:30:11 +01:00
|
|
|
|
2023-11-03 17:09:19 +01:00
|
|
|
this.auditService.track(AuditableAction.SupportiveMaterial_Delete, "id", id);
|
2023-10-18 09:31:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|