159 lines
7.7 KiB
Java
159 lines
7.7 KiB
Java
package eu.eudat.controllers;
|
|
|
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
|
import eu.eudat.audit.AuditableAction;
|
|
import eu.eudat.authorization.AuthorizationFlags;
|
|
import gr.cite.tools.validation.ValidationFilterAnnotation;
|
|
import eu.eudat.data.ReferenceTypeEntity;
|
|
import eu.eudat.model.ReferenceType;
|
|
import eu.eudat.model.builder.ReferenceTypeBuilder;
|
|
import eu.eudat.model.censorship.ReferenceTypeCensor;
|
|
import eu.eudat.model.persist.ReferenceTypePersist;
|
|
import eu.eudat.model.result.QueryResult;
|
|
import eu.eudat.query.ReferenceTypeQuery;
|
|
import eu.eudat.query.lookup.ReferenceTypeLookup;
|
|
import eu.eudat.service.referencetype.ReferenceTypeService;
|
|
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 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 javax.management.InvalidApplicationException;
|
|
import java.util.AbstractMap;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
import java.util.UUID;
|
|
|
|
@RestController
|
|
@RequestMapping(path = {"api/reference-type"})
|
|
public class ReferenceTypeController{
|
|
|
|
private static final LoggerService logger = new LoggerService(LoggerFactory.getLogger(ReferenceTypeController.class));
|
|
|
|
private final BuilderFactory builderFactory;
|
|
|
|
private final AuditService auditService;
|
|
|
|
private final CensorFactory censorFactory;
|
|
|
|
private final QueryFactory queryFactory;
|
|
|
|
private final MessageSource messageSource;
|
|
|
|
private final AuthorizationService authorizationService;
|
|
|
|
private final ReferenceTypeService referenceTypeService;
|
|
|
|
@Autowired
|
|
public ReferenceTypeController(
|
|
BuilderFactory builderFactory,
|
|
AuditService auditService,
|
|
CensorFactory censorFactory,
|
|
QueryFactory queryFactory,
|
|
MessageSource messageSource, AuthorizationService authorizationService, ReferenceTypeService referenceTypeService) {
|
|
this.builderFactory = builderFactory;
|
|
this.auditService = auditService;
|
|
this.censorFactory = censorFactory;
|
|
this.queryFactory = queryFactory;
|
|
this.messageSource = messageSource;
|
|
this.authorizationService = authorizationService;
|
|
this.referenceTypeService = referenceTypeService;
|
|
}
|
|
|
|
@PostMapping("query")
|
|
public QueryResult<ReferenceType> query(@RequestBody ReferenceTypeLookup lookup) throws MyApplicationException, MyForbiddenException {
|
|
logger.debug("querying {}", ReferenceType.class.getSimpleName());
|
|
|
|
this.censorFactory.censor(ReferenceTypeCensor.class).censor(lookup.getProject(), null);
|
|
|
|
ReferenceTypeQuery query = lookup.enrich(this.queryFactory).authorize(AuthorizationFlags.OwnerOrDmpAssociatedOrPermission);
|
|
List<ReferenceTypeEntity> data = query.collectAs(lookup.getProject());
|
|
List<ReferenceType> models = this.builderFactory.builder(ReferenceTypeBuilder.class).authorize(AuthorizationFlags.OwnerOrDmpAssociatedOrPermission).build(lookup.getProject(), data);
|
|
long count = (lookup.getMetadata() != null && lookup.getMetadata().getCountAll()) ? query.count() : models.size();
|
|
|
|
this.auditService.track(AuditableAction.ReferenceType_Query, "lookup", lookup);
|
|
|
|
return new QueryResult<>(models, count);
|
|
}
|
|
|
|
@GetMapping("{id}")
|
|
public ReferenceType get(@PathVariable("id") UUID id, FieldSet fieldSet) throws MyApplicationException, MyForbiddenException, MyNotFoundException {
|
|
logger.debug(new MapLogEntry("retrieving" + ReferenceType.class.getSimpleName()).And("id", id).And("fields", fieldSet));
|
|
|
|
this.censorFactory.censor(ReferenceTypeCensor.class).censor(fieldSet, null);
|
|
|
|
ReferenceTypeQuery query = this.queryFactory.query(ReferenceTypeQuery.class).authorize(AuthorizationFlags.OwnerOrDmpAssociatedOrPermission).ids(id);
|
|
ReferenceType model = this.builderFactory.builder(ReferenceTypeBuilder.class).authorize(AuthorizationFlags.OwnerOrDmpAssociatedOrPermission).build(fieldSet, query.firstAs(fieldSet));
|
|
if (model == null)
|
|
throw new MyNotFoundException(messageSource.getMessage("General_ItemNotFound", new Object[]{id, ReferenceType.class.getSimpleName()}, LocaleContextHolder.getLocale()));
|
|
|
|
this.auditService.track(AuditableAction.ReferenceType_Lookup, Map.ofEntries(
|
|
new AbstractMap.SimpleEntry<String, Object>("id", id),
|
|
new AbstractMap.SimpleEntry<String, Object>("fields", fieldSet)
|
|
));
|
|
|
|
return model;
|
|
}
|
|
|
|
@GetMapping("code/{code}")
|
|
public ReferenceType get(@PathVariable("code") String code, FieldSet fieldSet) throws MyApplicationException, MyForbiddenException, MyNotFoundException {
|
|
logger.debug(new MapLogEntry("retrieving" + ReferenceType.class.getSimpleName()).And("code", code).And("fields", fieldSet));
|
|
|
|
this.censorFactory.censor(ReferenceTypeCensor.class).censor(fieldSet, null);
|
|
|
|
ReferenceTypeQuery query = this.queryFactory.query(ReferenceTypeQuery.class).authorize(AuthorizationFlags.OwnerOrDmpAssociatedOrPermission).codes(code);
|
|
ReferenceType model = this.builderFactory.builder(ReferenceTypeBuilder.class).authorize(AuthorizationFlags.OwnerOrDmpAssociatedOrPermission).build(fieldSet, query.firstAs(fieldSet));
|
|
if (model == null)
|
|
throw new MyNotFoundException(messageSource.getMessage("General_ItemNotFound", new Object[]{code, ReferenceType.class.getSimpleName()}, LocaleContextHolder.getLocale()));
|
|
|
|
this.auditService.track(AuditableAction.ReferenceType_Lookup, Map.ofEntries(
|
|
new AbstractMap.SimpleEntry<String, Object>("code", code),
|
|
new AbstractMap.SimpleEntry<String, Object>("fields", fieldSet)
|
|
));
|
|
|
|
return model;
|
|
}
|
|
|
|
@PostMapping("persist")
|
|
@Transactional
|
|
@ValidationFilterAnnotation(validator = ReferenceTypePersist.ReferenceTypePersistValidator.ValidatorName, argumentName = "model")
|
|
public ReferenceType persist(@RequestBody ReferenceTypePersist model, FieldSet fieldSet) throws MyApplicationException, MyForbiddenException, MyNotFoundException, InvalidApplicationException, JAXBException, JsonProcessingException, InvalidApplicationException {
|
|
logger.debug(new MapLogEntry("persisting" + ReferenceType.class.getSimpleName()).And("model", model).And("fieldSet", fieldSet));
|
|
this.censorFactory.censor(ReferenceTypeCensor.class).censor(fieldSet, null);
|
|
|
|
ReferenceType persisted = this.referenceTypeService.persist(model, fieldSet);
|
|
|
|
this.auditService.track(AuditableAction.ReferenceType_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" + ReferenceType.class.getSimpleName()).And("id", id));
|
|
|
|
this.referenceTypeService.deleteAndSave(id);
|
|
|
|
this.auditService.track(AuditableAction.ReferenceType_Delete, "id", id);
|
|
}
|
|
|
|
}
|