package eu.eudat.controllers; import com.fasterxml.jackson.core.JsonProcessingException; import eu.eudat.audit.AuditableAction; import eu.eudat.authorization.AuthorizationFlags; import eu.eudat.data.PrefillingSourceEntity; import eu.eudat.model.Description; import eu.eudat.model.Prefilling; import eu.eudat.model.PrefillingSource; import eu.eudat.model.builder.PrefillingSourceBuilder; import eu.eudat.model.censorship.DescriptionCensor; import eu.eudat.model.censorship.PrefillingCensor; import eu.eudat.model.censorship.PrefillingSourceCensor; import eu.eudat.model.persist.PrefillingSearchRequest; import eu.eudat.model.persist.DescriptionPrefillingRequest; import eu.eudat.model.persist.PrefillingSourcePersist; import eu.eudat.model.result.QueryResult; import eu.eudat.query.PrefillingSourceQuery; import eu.eudat.query.lookup.PrefillingSourceLookup; import eu.eudat.service.prefillingsource.PrefillingSourceService; 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.ValidationFilterAnnotation; import jakarta.transaction.Transactional; import jakarta.xml.bind.JAXBException; 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.web.bind.annotation.*; import org.xml.sax.SAXException; import javax.management.InvalidApplicationException; import javax.xml.parsers.ParserConfigurationException; import java.io.IOException; import java.util.AbstractMap; import java.util.List; import java.util.Map; import java.util.UUID; @RestController @RequestMapping(path = {"api/prefilling-source"}) public class PrefillingSourceController { private static final LoggerService logger = new LoggerService(LoggerFactory.getLogger(PrefillingSourceController.class)); private final BuilderFactory builderFactory; private final AuditService auditService; private final CensorFactory censorFactory; private final QueryFactory queryFactory; private final MessageSource messageSource; private final PrefillingSourceService prefillingSourceService; @Autowired public PrefillingSourceController( BuilderFactory builderFactory, AuditService auditService, CensorFactory censorFactory, QueryFactory queryFactory, MessageSource messageSource, PrefillingSourceService prefillingSourceService) { this.builderFactory = builderFactory; this.auditService = auditService; this.censorFactory = censorFactory; this.queryFactory = queryFactory; this.messageSource = messageSource; this.prefillingSourceService = prefillingSourceService; } @PostMapping("query") public QueryResult query(@RequestBody PrefillingSourceLookup lookup) throws MyApplicationException, MyForbiddenException { logger.debug("querying {}", PrefillingSource.class.getSimpleName()); this.censorFactory.censor(PrefillingSourceCensor.class).censor(lookup.getProject(), null); PrefillingSourceQuery query = lookup.enrich(this.queryFactory).authorize(AuthorizationFlags.OwnerOrDmpAssociatedOrPermission); List data = query.collectAs(lookup.getProject()); List models = this.builderFactory.builder(PrefillingSourceBuilder.class).authorize(AuthorizationFlags.OwnerOrDmpAssociatedOrPermission).build(lookup.getProject(), data); long count = (lookup.getMetadata() != null && lookup.getMetadata().getCountAll()) ? query.count() : models.size(); this.auditService.track(AuditableAction.PrefillingSource_Query, "lookup", lookup); return new QueryResult<>(models, count); } @GetMapping("{id}") public PrefillingSource get(@PathVariable("id") UUID id, FieldSet fieldSet) throws MyApplicationException, MyForbiddenException, MyNotFoundException { logger.debug(new MapLogEntry("retrieving" + PrefillingSource.class.getSimpleName()).And("id", id).And("fields", fieldSet)); this.censorFactory.censor(PrefillingSourceCensor.class).censor(fieldSet, null); PrefillingSourceQuery query = this.queryFactory.query(PrefillingSourceQuery.class).authorize(AuthorizationFlags.OwnerOrDmpAssociatedOrPermission).ids(id); PrefillingSource model = this.builderFactory.builder(PrefillingSourceBuilder.class).authorize(AuthorizationFlags.OwnerOrDmpAssociatedOrPermission).build(fieldSet, query.firstAs(fieldSet)); if (model == null) throw new MyNotFoundException(messageSource.getMessage("General_ItemNotFound", new Object[]{id, PrefillingSource.class.getSimpleName()}, LocaleContextHolder.getLocale())); this.auditService.track(AuditableAction.PrefillingSource_Lookup, Map.ofEntries( new AbstractMap.SimpleEntry("id", id), new AbstractMap.SimpleEntry("fields", fieldSet) )); return model; } @PostMapping("persist") @Transactional @ValidationFilterAnnotation(validator = PrefillingSourcePersist.PrefillingSourcePersistValidator.ValidatorName, argumentName = "model") public PrefillingSource persist(@RequestBody PrefillingSourcePersist model, FieldSet fieldSet) throws MyApplicationException, MyForbiddenException, MyNotFoundException, InvalidApplicationException, JAXBException, JsonProcessingException, InvalidApplicationException { logger.debug(new MapLogEntry("persisting" + PrefillingSource.class.getSimpleName()).And("model", model).And("fieldSet", fieldSet)); this.censorFactory.censor(PrefillingSourceCensor.class).censor(fieldSet, null); PrefillingSource persisted = this.prefillingSourceService.persist(model, fieldSet); this.auditService.track(AuditableAction.PrefillingSource_Persist, Map.ofEntries( new AbstractMap.SimpleEntry("model", model), new AbstractMap.SimpleEntry("fields", fieldSet) )); return persisted; } @DeleteMapping("{id}") @Transactional public void delete(@PathVariable("id") UUID id) throws MyForbiddenException, InvalidApplicationException { logger.debug(new MapLogEntry("retrieving" + PrefillingSource.class.getSimpleName()).And("id", id)); this.prefillingSourceService.deleteAndSave(id); this.auditService.track(AuditableAction.PrefillingSource_Delete, "id", id); } @PostMapping("search") @ValidationFilterAnnotation(validator = PrefillingSearchRequest.PrefillingSearchRequestValidator.ValidatorName, argumentName = "model") public List search(@RequestBody PrefillingSearchRequest model) throws MyApplicationException, MyForbiddenException, MyNotFoundException, JAXBException, ParserConfigurationException, IOException, InstantiationException, IllegalAccessException, SAXException { logger.debug(new MapLogEntry("searching" + Prefilling.class.getSimpleName()).And("model", model)); this.censorFactory.censor(PrefillingCensor.class).censor(null, null); List item = this.prefillingSourceService.searchPrefillings(model); this.auditService.track(AuditableAction.PrefillingSource_Generate, Map.ofEntries( new AbstractMap.SimpleEntry("model", model) )); return item; } @PostMapping("generate") @ValidationFilterAnnotation(validator = DescriptionPrefillingRequest.DescriptionProfilingRequestValidator.ValidatorName, argumentName = "model") public Description generate(@RequestBody DescriptionPrefillingRequest model, FieldSet fieldSet) throws MyApplicationException, MyForbiddenException, MyNotFoundException, JAXBException, ParserConfigurationException, IOException, InstantiationException, IllegalAccessException, SAXException, InvalidApplicationException { logger.debug(new MapLogEntry("persisting" + Prefilling.class.getSimpleName()).And("model", model)); this.censorFactory.censor(DescriptionCensor.class).censor(fieldSet, null); Description item = this.prefillingSourceService.getPrefilledDescription(model, fieldSet); this.auditService.track(AuditableAction.PrefillingSource_Generate, Map.ofEntries( new AbstractMap.SimpleEntry("model", model) )); return item; } }