package eu.eudat.controllers; import com.fasterxml.jackson.core.JsonProcessingException; import eu.eudat.audit.AuditableAction; import eu.eudat.authorization.AuthorizationFlags; import eu.eudat.data.TenantConfigurationEntity; import eu.eudat.model.DescriptionTemplateType; import eu.eudat.model.builder.tenantconfiguration.TenantConfigurationBuilder; import eu.eudat.model.censorship.tenantconfiguration.TenantConfigurationCensor; import eu.eudat.model.persist.tenantconfiguration.TenantConfigurationPersist; import eu.eudat.model.result.QueryResult; import eu.eudat.model.tenantconfiguration.TenantConfiguration; import eu.eudat.query.TenantConfigurationQuery; import eu.eudat.query.lookup.TenantConfigurationLookup; import eu.eudat.service.tenantconfiguration.TenantConfigurationService; 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 org.slf4j.LoggerFactory; import org.springframework.context.MessageSource; import org.springframework.context.i18n.LocaleContextHolder; import org.springframework.transaction.annotation.Transactional; import org.springframework.web.bind.annotation.*; import javax.management.InvalidApplicationException; import java.util.*; @RestController @RequestMapping(path = "api/tenant-configuration") public class TenantConfigurationController { private static final LoggerService logger = new LoggerService(LoggerFactory.getLogger(TenantConfigurationController.class)); private final BuilderFactory builderFactory; private final AuditService auditService; private final TenantConfigurationService tenantConfigurationService; private final CensorFactory censorFactory; private final QueryFactory queryFactory; private final MessageSource messageSource; public TenantConfigurationController( BuilderFactory builderFactory, AuditService auditService, TenantConfigurationService tenantConfigurationService, CensorFactory censorFactory, QueryFactory queryFactory, MessageSource messageSource) { this.builderFactory = builderFactory; this.auditService = auditService; this.tenantConfigurationService = tenantConfigurationService; this.censorFactory = censorFactory; this.queryFactory = queryFactory; this.messageSource = messageSource; } @PostMapping("query") public QueryResult Query(@RequestBody TenantConfigurationLookup lookup) throws MyApplicationException, MyForbiddenException { logger.debug("querying {}", TenantConfiguration.class.getSimpleName()); this.censorFactory.censor(TenantConfigurationCensor.class).censor(lookup.getProject(), null); TenantConfigurationQuery query = lookup.enrich(this.queryFactory).authorize(AuthorizationFlags.OwnerOrDmpAssociatedOrPermission); List data = query.collectAs(lookup.getProject()); List models = this.builderFactory.builder(TenantConfigurationBuilder.class).authorize(AuthorizationFlags.OwnerOrDmpAssociatedOrPermission).build(lookup.getProject(), data); long count = (lookup.getMetadata() != null && lookup.getMetadata().getCountAll()) ? query.count() : models.size(); this.auditService.track(AuditableAction.TenantConfiguration_Query, "lookup", lookup); return new QueryResult<>(models, count); } @GetMapping("{id}") public TenantConfiguration Get(@PathVariable("id") UUID id, FieldSet fieldSet, Locale locale) throws MyApplicationException, MyForbiddenException, MyNotFoundException { logger.debug(new MapLogEntry("retrieving" + TenantConfiguration.class.getSimpleName()).And("id", id).And("fields", fieldSet)); this.censorFactory.censor(TenantConfigurationCensor.class).censor(fieldSet, null); TenantConfigurationQuery query = this.queryFactory.query(TenantConfigurationQuery.class).authorize(AuthorizationFlags.OwnerOrDmpAssociatedOrPermission).ids(id); TenantConfiguration model = this.builderFactory.builder(TenantConfigurationBuilder.class).authorize(AuthorizationFlags.OwnerOrDmpAssociatedOrPermission).build(fieldSet, query.firstAs(fieldSet)); if (model == null) throw new MyNotFoundException(messageSource.getMessage("General_ItemNotFound", new Object[]{id, TenantConfiguration.class.getSimpleName()}, LocaleContextHolder.getLocale())); this.auditService.track(AuditableAction.TenantConfiguration_Lookup, Map.ofEntries( new AbstractMap.SimpleEntry("id", id), new AbstractMap.SimpleEntry("fields", fieldSet) )); return model; } @PostMapping("persist") @Transactional @ValidationFilterAnnotation(validator = TenantConfigurationPersist.TenantConfigurationPersistValidator.ValidatorName, argumentName = "model") public TenantConfiguration Persist(@RequestBody TenantConfigurationPersist model, FieldSet fieldSet) throws MyApplicationException, MyForbiddenException, MyNotFoundException, InvalidApplicationException, JsonProcessingException { logger.debug(new MapLogEntry("persisting" + DescriptionTemplateType.class.getSimpleName()).And("model", model).And("fieldSet", fieldSet)); TenantConfiguration persisted = this.tenantConfigurationService.persist(model, fieldSet); this.auditService.track(AuditableAction.TenantConfiguration_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" + TenantConfiguration.class.getSimpleName()).And("id", id)); this.tenantConfigurationService.deleteAndSave(id); this.auditService.track(AuditableAction.TenantConfiguration_Delete, "id", id); } }