160 lines
8.1 KiB
Java
160 lines
8.1 KiB
Java
package eu.eudat.controllers.v2;
|
|
|
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
|
import eu.eudat.audit.AuditableAction;
|
|
import eu.eudat.authorization.AuthorizationFlags;
|
|
import eu.eudat.commons.enums.SupportiveMaterialFieldType;
|
|
import eu.eudat.data.SupportiveMaterialEntity;
|
|
import eu.eudat.model.SupportiveMaterial;
|
|
import eu.eudat.model.builder.SupportiveMaterialBuilder;
|
|
import eu.eudat.model.censorship.SupportiveMaterialCensor;
|
|
import eu.eudat.model.persist.SupportiveMaterialPersist;
|
|
import eu.eudat.model.result.QueryResult;
|
|
import eu.eudat.query.SupportiveMaterialQuery;
|
|
import eu.eudat.query.lookup.SupportiveMaterialLookup;
|
|
import eu.eudat.service.supportivematerial.SupportiveMaterialService;
|
|
import eu.eudat.service.supportivematerial.SupportiveMaterialServiceImpl;
|
|
import gr.cite.commons.web.authz.service.AuthorizationService;
|
|
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 gr.cite.tools.validation.MyValidate;
|
|
import jakarta.transaction.Transactional;
|
|
import jakarta.xml.bind.JAXBException;
|
|
import org.apache.commons.lang3.EnumUtils;
|
|
import org.slf4j.LoggerFactory;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.context.MessageSource;
|
|
import org.springframework.context.i18n.LocaleContextHolder;
|
|
import org.springframework.core.env.Environment;
|
|
import org.springframework.http.HttpStatus;
|
|
import org.springframework.http.ResponseEntity;
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
import javax.management.InvalidApplicationException;
|
|
import javax.xml.parsers.ParserConfigurationException;
|
|
import javax.xml.transform.TransformerException;
|
|
import java.io.IOException;
|
|
import java.nio.file.Files;
|
|
import java.nio.file.Path;
|
|
import java.nio.file.Paths;
|
|
import java.util.*;
|
|
import java.util.stream.Stream;
|
|
|
|
@RestController
|
|
@RequestMapping(path = {"/api/supportive-material"})
|
|
public class SupportiveMaterialController {
|
|
|
|
private static final LoggerService logger = new LoggerService(LoggerFactory.getLogger(SupportiveMaterialController.class));
|
|
private Environment environment;
|
|
private final BuilderFactory builderFactory;
|
|
private final AuditService auditService;
|
|
|
|
private final CensorFactory censorFactory;
|
|
private final QueryFactory queryFactory;
|
|
private final MessageSource messageSource;
|
|
private final SupportiveMaterialService supportiveMaterialService;
|
|
private final AuthorizationService authorizationService;
|
|
|
|
@Autowired
|
|
public SupportiveMaterialController(Environment environment, SupportiveMaterialService supportiveMaterialService, BuilderFactory builderFactory,
|
|
AuditService auditService, CensorFactory censorFactory, QueryFactory queryFactory, MessageSource messageSource,
|
|
AuthorizationService authorizationService) {
|
|
this.environment = environment;
|
|
this.supportiveMaterialService = supportiveMaterialService;
|
|
this.builderFactory = builderFactory;
|
|
this.auditService = auditService;
|
|
this.censorFactory = censorFactory;
|
|
this.queryFactory = queryFactory;
|
|
this.messageSource = messageSource;
|
|
this.authorizationService = authorizationService;
|
|
}
|
|
|
|
@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);
|
|
|
|
SupportiveMaterialQuery query = lookup.enrich(this.queryFactory).authorize(AuthorizationFlags.OwnerOrPermission);
|
|
List<SupportiveMaterialEntity> datas = query.collectAs(lookup.getProject());
|
|
List<SupportiveMaterial> models = this.builderFactory.builder(SupportiveMaterialBuilder.class).authorize(AuthorizationFlags.OwnerOrPermission).build(lookup.getProject(), datas);
|
|
long count = (lookup.getMetadata() != null && lookup.getMetadata().getCountAll()) ? query.count() : models.size();
|
|
|
|
this.auditService.track(AuditableAction.SupportiveMaterial_Query, "lookup", lookup);
|
|
|
|
return new QueryResult(models, count);
|
|
}
|
|
|
|
@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);
|
|
|
|
SupportiveMaterialQuery query = this.queryFactory.query(SupportiveMaterialQuery.class).authorize(AuthorizationFlags.OwnerOrPermission).ids(id);
|
|
SupportiveMaterial model = this.builderFactory.builder(SupportiveMaterialBuilder.class).authorize(AuthorizationFlags.OwnerOrPermission).build(fieldSet, query.firstAs(fieldSet));
|
|
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;
|
|
}
|
|
|
|
@PostMapping("public")
|
|
public ResponseEntity<byte[]> queryPublic(@RequestBody SupportiveMaterialLookup lookup) throws IOException {
|
|
logger.debug("querying {}", SupportiveMaterial.class.getSimpleName());
|
|
|
|
this.censorFactory.censor(SupportiveMaterialCensor.class).censor(lookup.getProject(), null);
|
|
|
|
SupportiveMaterialQuery query = lookup.enrich(this.queryFactory).authorize(AuthorizationFlags.OwnerOrPermission);
|
|
List<SupportiveMaterialEntity> datas = query.collectAs(lookup.getProject());
|
|
if (datas.size() == 1){
|
|
return new ResponseEntity<>(datas.get(0).getPayload().getBytes(), HttpStatus.OK);
|
|
}
|
|
|
|
try (Stream<Path> paths = Files.walk(Paths.get(Objects.requireNonNull(this.environment.getProperty(lookup.getTypes().stream().toList().get(0).name().toLowerCase() +".path"))))) {
|
|
return this.supportiveMaterialService.getResponseEntity(lookup.getLanguageCodes().get(0), paths);
|
|
}
|
|
}
|
|
|
|
|
|
@PostMapping("persist")
|
|
@Transactional
|
|
public SupportiveMaterial persist(@MyValidate @RequestBody SupportiveMaterialPersist model, FieldSet fieldSet) throws MyApplicationException, MyForbiddenException, MyNotFoundException, InvalidApplicationException, JAXBException, ParserConfigurationException, JsonProcessingException, TransformerException {
|
|
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);
|
|
|
|
this.auditService.track(AuditableAction.SupportiveMaterial_Delete, "id", id);
|
|
}
|
|
|
|
|
|
}
|