argos/dmp-backend/web/src/main/java/eu/eudat/controllers/v2/ReferenceController.java

187 lines
9.5 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.authorization.Permission;
import eu.eudat.commons.validation.ValidationFilterAnnotation;
import eu.eudat.controllers.BaseController;
import eu.eudat.data.ReferenceEntity;
import eu.eudat.commons.exceptions.HugeResultSetException;
import eu.eudat.logic.services.ApiContext;
import eu.eudat.service.reference.ReferenceService;
import eu.eudat.model.Reference;
import eu.eudat.model.builder.ReferenceBuilder;
import eu.eudat.model.censorship.ReferenceCensor;
import eu.eudat.model.persist.ReferencePersist;
import eu.eudat.model.result.QueryResult;
import eu.eudat.models.data.helpers.responses.ResponseItem;
import eu.eudat.query.ReferenceQuery;
import eu.eudat.query.lookup.ReferenceDefinitionSearchLookup;
import eu.eudat.query.lookup.ReferenceLookup;
import eu.eudat.query.lookup.ReferenceSearchLookup;
import eu.eudat.types.ApiMessageCode;
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.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.util.AbstractMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
@RestController
@RequestMapping(path = {"api/reference"})
public class ReferenceController extends BaseController {
private static final LoggerService logger = new LoggerService(LoggerFactory.getLogger(ReferenceController.class));
private final BuilderFactory builderFactory;
private final AuditService auditService;
private final ReferenceService referenceService;
private final CensorFactory censorFactory;
private final QueryFactory queryFactory;
private final MessageSource messageSource;
private final AuthorizationService authorizationService;
@Autowired
public ReferenceController(
ApiContext apiContext,
BuilderFactory builderFactory,
ReferenceService referenceService,
AuditService auditService,
CensorFactory censorFactory,
QueryFactory queryFactory,
MessageSource messageSource, AuthorizationService authorizationService) {
super(apiContext);
this.builderFactory = builderFactory;
this.referenceService = referenceService;
this.auditService = auditService;
this.censorFactory = censorFactory;
this.queryFactory = queryFactory;
this.messageSource = messageSource;
this.authorizationService = authorizationService;
}
@PostMapping("query")
public QueryResult<Reference> query(@RequestBody ReferenceLookup lookup) throws MyApplicationException, MyForbiddenException {
logger.debug("querying {}", Reference.class.getSimpleName());
this.censorFactory.censor(ReferenceCensor.class).censor(lookup.getProject(), null);
ReferenceQuery query = lookup.enrich(this.queryFactory).authorize(AuthorizationFlags.OwnerOrDmpAssociatedOrPermissionOrPublic);
List<ReferenceEntity> data = query.collectAs(lookup.getProject());
List<Reference> models = this.builderFactory.builder(ReferenceBuilder.class).authorize(AuthorizationFlags.OwnerOrDmpAssociatedOrPermissionOrPublic).build(lookup.getProject(), data);
long count = (lookup.getMetadata() != null && lookup.getMetadata().getCountAll()) ? query.count() : models.size();
this.auditService.track(AuditableAction.Reference_Query, "lookup", lookup);
return new QueryResult<>(models, count);
}
@PostMapping("search")
public @ResponseBody ResponseEntity<ResponseItem<List<Reference>>> searchReference(@RequestBody ReferenceSearchLookup lookup) throws HugeResultSetException, MyNotFoundException, InvalidApplicationException {
this.authorizationService.authorizeForce(Permission.AuthenticatedRole);
// ReferenceType referenceType = ReferenceType.of((short) externalType);
if (lookup.getType() != null) {
List<Reference> references = this.referenceService.searchReference(lookup);
return ResponseEntity.status(HttpStatus.OK).body(new ResponseItem<List<Reference>>().status(ApiMessageCode.NO_MESSAGE).payload(references));
}
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(new ResponseItem<List<Reference>>().status(ApiMessageCode.NO_MESSAGE));
}
@PostMapping("search-with-db-definition")
public @ResponseBody ResponseEntity<ResponseItem<List<Reference>>> searchReferenceWithDefinition(@RequestBody ReferenceDefinitionSearchLookup lookup) throws HugeResultSetException, MyNotFoundException, InvalidApplicationException {
this.authorizationService.authorizeForce(Permission.AuthenticatedRole);
List<Reference> references = this.referenceService.searchReferenceWithDefinition(lookup);
return ResponseEntity.status(HttpStatus.OK).body(new ResponseItem<List<Reference>>().status(ApiMessageCode.NO_MESSAGE).payload(references));
}
@GetMapping("{id}")
public Reference get(@PathVariable("id") UUID id, FieldSet fieldSet) throws MyApplicationException, MyForbiddenException, MyNotFoundException {
logger.debug(new MapLogEntry("retrieving" + Reference.class.getSimpleName()).And("id", id).And("fields", fieldSet));
this.censorFactory.censor(ReferenceCensor.class).censor(fieldSet, null);
ReferenceQuery query = this.queryFactory.query(ReferenceQuery.class).authorize(AuthorizationFlags.OwnerOrDmpAssociatedOrPermissionOrPublic).ids(id);
Reference model = this.builderFactory.builder(ReferenceBuilder.class).authorize(AuthorizationFlags.OwnerOrDmpAssociatedOrPermissionOrPublic).build(fieldSet, query.firstAs(fieldSet));
if (model == null)
throw new MyNotFoundException(messageSource.getMessage("General_ItemNotFound", new Object[]{id, Reference.class.getSimpleName()}, LocaleContextHolder.getLocale()));
this.auditService.track(AuditableAction.Reference_Lookup, Map.ofEntries(
new AbstractMap.SimpleEntry<String, Object>("id", id),
new AbstractMap.SimpleEntry<String, Object>("fields", fieldSet)
));
return model;
}
@PostMapping("persist")
@Transactional
@ValidationFilterAnnotation(validator = ReferencePersist.ReferencePersistValidator.ValidatorName, argumentName = "model")
public Reference persist(@RequestBody ReferencePersist model, FieldSet fieldSet) throws MyApplicationException, MyForbiddenException, MyNotFoundException, InvalidApplicationException, JAXBException, ParserConfigurationException, JsonProcessingException, TransformerException {
logger.debug(new MapLogEntry("persisting" + Reference.class.getSimpleName()).And("model", model).And("fieldSet", fieldSet));
this.censorFactory.censor(ReferenceCensor.class).censor(fieldSet, null);
Reference persisted = this.referenceService.persist(model, fieldSet);
this.auditService.track(AuditableAction.Reference_Persist, Map.ofEntries(
new AbstractMap.SimpleEntry<String, Object>("model", model),
new AbstractMap.SimpleEntry<String, Object>("fields", fieldSet)
));
return persisted;
}
// @GetMapping(path = {"search/{externalType}"}, produces = "application/json")
// public @ResponseBody ResponseEntity<ResponseItem<List<FetcherReference>>> searchReference(@PathVariable(value = "externalType") int externalType,
// @RequestParam(value = "query", required = false) String query,
// @RequestParam(value = "type", required = false) String type
// ) throws HugeResultSet, MyNotFoundException, InvalidApplicationException {
// this.authorizationService.authorizeForce(Permission.AuthenticatedRole);
// ReferenceType referenceType = ReferenceType.of((short) externalType);
//
// List<FetcherReference> fetcherReferences = this.referenceService.searchReference(referenceType, query, type);
// return ResponseEntity.status(HttpStatus.OK).body(new ResponseItem<List<FetcherReference>>().status(ApiMessageCode.NO_MESSAGE).payload(fetcherReferences));
// }
@DeleteMapping("{id}")
@Transactional
public void delete(@PathVariable("id") UUID id) throws MyForbiddenException, InvalidApplicationException {
logger.debug(new MapLogEntry("retrieving" + Reference.class.getSimpleName()).And("id", id));
this.referenceService.deleteAndSave(id);
this.auditService.track(AuditableAction.Reference_Delete, "id", id);
}
}