2024-02-13 08:53:33 +01:00
|
|
|
package eu.eudat.controllers;
|
2023-11-22 17:01:58 +01:00
|
|
|
|
|
|
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
|
|
|
import eu.eudat.audit.AuditableAction;
|
|
|
|
import eu.eudat.authorization.AuthorizationFlags;
|
2024-01-17 10:20:02 +01:00
|
|
|
import gr.cite.tools.validation.ValidationFilterAnnotation;
|
2023-11-22 17:01:58 +01:00
|
|
|
import eu.eudat.data.TenantEntity;
|
|
|
|
import eu.eudat.model.Tenant;
|
|
|
|
import eu.eudat.model.builder.TenantBuilder;
|
|
|
|
import eu.eudat.model.censorship.TenantCensor;
|
|
|
|
import eu.eudat.model.persist.TenantPersist;
|
|
|
|
import eu.eudat.model.result.QueryResult;
|
|
|
|
import eu.eudat.query.TenantQuery;
|
|
|
|
import eu.eudat.query.lookup.TenantLookup;
|
|
|
|
import eu.eudat.service.tenant.TenantService;
|
|
|
|
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;
|
|
|
|
import org.springframework.context.MessageSource;
|
|
|
|
import org.springframework.context.i18n.LocaleContextHolder;
|
|
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
|
2023-11-24 14:12:02 +01:00
|
|
|
import javax.crypto.BadPaddingException;
|
|
|
|
import javax.crypto.IllegalBlockSizeException;
|
|
|
|
import javax.crypto.NoSuchPaddingException;
|
2023-11-22 17:01:58 +01:00
|
|
|
import javax.management.InvalidApplicationException;
|
|
|
|
import javax.xml.parsers.ParserConfigurationException;
|
|
|
|
import javax.xml.transform.TransformerException;
|
2023-11-24 14:12:02 +01:00
|
|
|
import java.security.InvalidAlgorithmParameterException;
|
|
|
|
import java.security.InvalidKeyException;
|
|
|
|
import java.security.NoSuchAlgorithmException;
|
2023-11-22 17:01:58 +01:00
|
|
|
import java.util.AbstractMap;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.UUID;
|
|
|
|
|
|
|
|
@RestController
|
|
|
|
@RequestMapping(path = "api/tenant")
|
|
|
|
public class TenantController {
|
|
|
|
|
|
|
|
private static final LoggerService logger = new LoggerService(LoggerFactory.getLogger(TenantController.class));
|
|
|
|
|
|
|
|
private final BuilderFactory builderFactory;
|
|
|
|
|
|
|
|
private final AuditService auditService;
|
|
|
|
|
|
|
|
private final TenantService tenantService;
|
|
|
|
|
|
|
|
private final CensorFactory censorFactory;
|
|
|
|
|
|
|
|
private final QueryFactory queryFactory;
|
|
|
|
|
|
|
|
private final MessageSource messageSource;
|
|
|
|
|
|
|
|
public TenantController(
|
|
|
|
BuilderFactory builderFactory,
|
|
|
|
AuditService auditService,
|
|
|
|
TenantService tenantService,
|
|
|
|
CensorFactory censorFactory,
|
|
|
|
QueryFactory queryFactory,
|
|
|
|
MessageSource messageSource) {
|
|
|
|
this.builderFactory = builderFactory;
|
|
|
|
this.auditService = auditService;
|
|
|
|
this.tenantService = tenantService;
|
|
|
|
this.censorFactory = censorFactory;
|
|
|
|
this.queryFactory = queryFactory;
|
|
|
|
this.messageSource = messageSource;
|
|
|
|
}
|
|
|
|
|
|
|
|
@PostMapping("query")
|
2023-11-24 14:12:02 +01:00
|
|
|
public QueryResult<Tenant> query(@RequestBody TenantLookup lookup) throws MyApplicationException, MyForbiddenException, InvalidAlgorithmParameterException, NoSuchPaddingException, IllegalBlockSizeException, NoSuchAlgorithmException, BadPaddingException, InvalidKeyException {
|
2023-11-22 17:01:58 +01:00
|
|
|
logger.debug("querying {}", Tenant.class.getSimpleName());
|
|
|
|
|
|
|
|
this.censorFactory.censor(TenantCensor.class).censor(lookup.getProject(), null);
|
2024-03-12 17:27:16 +01:00
|
|
|
TenantQuery query = lookup.enrich(this.queryFactory).authorize(AuthorizationFlags.OwnerOrDmpAssociatedOrPermission);
|
2023-11-22 17:01:58 +01:00
|
|
|
|
|
|
|
List<TenantEntity> data = query.collectAs(lookup.getProject());
|
2024-03-12 17:27:16 +01:00
|
|
|
List<Tenant> models = this.builderFactory.builder(TenantBuilder.class).authorize(AuthorizationFlags.OwnerOrDmpAssociatedOrPermission).build(lookup.getProject(), data);
|
2024-01-04 10:51:56 +01:00
|
|
|
for (Tenant model : models) {
|
2023-11-24 14:12:02 +01:00
|
|
|
models.set(models.indexOf(model), this.tenantService.decryptTenant(model));
|
|
|
|
}
|
2023-11-22 17:01:58 +01:00
|
|
|
long count = (lookup.getMetadata() != null && lookup.getMetadata().getCountAll()) ? query.count() : models.size();
|
|
|
|
|
|
|
|
this.auditService.track(AuditableAction.Tenant_Query, "lookup", lookup);
|
|
|
|
|
|
|
|
return new QueryResult<>(models, count);
|
|
|
|
}
|
|
|
|
|
|
|
|
@GetMapping("{id}")
|
2023-11-24 14:12:02 +01:00
|
|
|
public Tenant get(@PathVariable("id") UUID id, FieldSet fieldSet) throws MyApplicationException, MyForbiddenException, MyNotFoundException, NoSuchAlgorithmException, InvalidAlgorithmParameterException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException, InvalidKeyException {
|
2023-11-22 17:01:58 +01:00
|
|
|
logger.debug(new MapLogEntry("retrieving" + Tenant.class.getSimpleName()).And("id", id).And("fields", fieldSet));
|
|
|
|
|
|
|
|
this.censorFactory.censor(TenantCensor.class).censor(fieldSet, null);
|
|
|
|
|
2024-03-12 17:27:16 +01:00
|
|
|
TenantQuery query = this.queryFactory.query(TenantQuery.class).authorize(AuthorizationFlags.OwnerOrDmpAssociatedOrPermission).ids(id);
|
|
|
|
Tenant model = this.builderFactory.builder(TenantBuilder.class).authorize(AuthorizationFlags.OwnerOrDmpAssociatedOrPermission).build(fieldSet, query.firstAs(fieldSet));
|
2023-11-22 17:01:58 +01:00
|
|
|
if (model == null)
|
|
|
|
throw new MyNotFoundException(messageSource.getMessage("General_ItemNotFound", new Object[]{id, Tenant.class.getSimpleName()}, LocaleContextHolder.getLocale()));
|
|
|
|
|
2023-11-24 14:12:02 +01:00
|
|
|
model = this.tenantService.decryptTenant(model);
|
|
|
|
|
2023-11-22 17:01:58 +01:00
|
|
|
this.auditService.track(AuditableAction.Tenant_Lookup, Map.ofEntries(
|
|
|
|
new AbstractMap.SimpleEntry<String, Object>("id", id),
|
|
|
|
new AbstractMap.SimpleEntry<String, Object>("fields", fieldSet)
|
|
|
|
));
|
|
|
|
|
|
|
|
return model;
|
|
|
|
}
|
|
|
|
|
|
|
|
@PostMapping("persist")
|
|
|
|
@Transactional
|
2024-01-04 10:51:56 +01:00
|
|
|
@ValidationFilterAnnotation(validator = TenantPersist.TenantPersistValidator.ValidatorName, argumentName = "model")
|
|
|
|
public Tenant persist(@RequestBody TenantPersist model, FieldSet fieldSet) throws MyApplicationException, MyForbiddenException, MyNotFoundException, InvalidApplicationException, JAXBException, ParserConfigurationException, JsonProcessingException, TransformerException, InvalidAlgorithmParameterException, NoSuchPaddingException, IllegalBlockSizeException, NoSuchAlgorithmException, BadPaddingException, InvalidKeyException {
|
2023-11-22 17:01:58 +01:00
|
|
|
logger.debug(new MapLogEntry("persisting" + Tenant.class.getSimpleName()).And("model", model).And("fieldSet", fieldSet));
|
|
|
|
this.censorFactory.censor(TenantCensor.class).censor(fieldSet, null);
|
2024-01-04 10:51:56 +01:00
|
|
|
|
2023-11-22 17:01:58 +01:00
|
|
|
Tenant persisted = this.tenantService.persist(model, fieldSet);
|
|
|
|
|
|
|
|
this.auditService.track(AuditableAction.Tenant_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" + Tenant.class.getSimpleName()).And("id", id));
|
|
|
|
|
|
|
|
this.tenantService.deleteAndSave(id);
|
|
|
|
|
|
|
|
this.auditService.track(AuditableAction.Tenant_Delete, "id", id);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|