133 lines
6.2 KiB
Java
133 lines
6.2 KiB
Java
package gr.cite.annotation.web.controllers;
|
|
|
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
|
import gr.cite.annotation.audit.AuditableAction;
|
|
import gr.cite.annotation.authorization.AuthorizationFlags;
|
|
import gr.cite.annotation.data.AnnotationEntity;
|
|
import gr.cite.annotation.model.Annotation;
|
|
import gr.cite.annotation.model.builder.AnnotationBuilder;
|
|
import gr.cite.annotation.model.censorship.AnnotationCensor;
|
|
import gr.cite.annotation.model.persist.AnnotationPersist;
|
|
import gr.cite.annotation.query.AnnotationQuery;
|
|
import gr.cite.annotation.query.lookup.AnnotationLookup;
|
|
import gr.cite.annotation.service.annotation.AnnotationService;
|
|
import gr.cite.annotation.web.model.QueryResult;
|
|
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.context.MessageSource;
|
|
import org.springframework.context.i18n.LocaleContextHolder;
|
|
import org.springframework.http.MediaType;
|
|
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/annotation", produces = MediaType.APPLICATION_JSON_VALUE)
|
|
public class AnnotationController {
|
|
|
|
private static final LoggerService logger = new LoggerService(LoggerFactory.getLogger(AnnotationController.class));
|
|
|
|
private final CensorFactory censorFactory;
|
|
|
|
private final QueryFactory queryFactory;
|
|
|
|
private final BuilderFactory builderFactory;
|
|
|
|
private final AuditService auditService;
|
|
|
|
private final MessageSource messageSource;
|
|
|
|
private final AnnotationService annotationService;
|
|
|
|
public AnnotationController(CensorFactory censorFactory, QueryFactory queryFactory, BuilderFactory builderFactory, AuditService auditService, MessageSource messageSource, AnnotationService annotationService) {
|
|
this.censorFactory = censorFactory;
|
|
this.queryFactory = queryFactory;
|
|
this.builderFactory = builderFactory;
|
|
this.auditService = auditService;
|
|
this.messageSource = messageSource;
|
|
this.annotationService = annotationService;
|
|
}
|
|
|
|
@PostMapping("query")
|
|
public QueryResult<Annotation> query(@RequestBody AnnotationLookup lookup) {
|
|
logger.debug("querying {}", Annotation.class.getSimpleName());
|
|
|
|
this.censorFactory.censor(AnnotationCensor.class).censor(lookup.getProject(), null);
|
|
|
|
AnnotationQuery query = lookup.enrich(this.queryFactory).authorize(AuthorizationFlags.OwnerOrPermissionAssociated);
|
|
List<AnnotationEntity> data = query.collect();
|
|
List<Annotation> models = this.builderFactory.builder(AnnotationBuilder.class).authorize(AuthorizationFlags.OwnerOrPermissionAssociated).build(lookup.getProject(), data);
|
|
long count = (lookup.getMetadata() != null && lookup.getMetadata().getCountAll()) ? query.count() : models.size();
|
|
|
|
this.auditService.track(AuditableAction.Annotation_Query, "lookup", lookup);
|
|
|
|
return new QueryResult<>(models, count);
|
|
}
|
|
|
|
@GetMapping("{id}")
|
|
public Annotation get(@PathVariable("id") UUID id, FieldSet fieldSet) throws MyApplicationException, MyForbiddenException, MyNotFoundException {
|
|
logger.debug(new MapLogEntry("retrieving" + Annotation.class.getSimpleName()).And("id", id).And("fields", fieldSet));
|
|
|
|
this.censorFactory.censor(AnnotationCensor.class).censor(fieldSet, null);
|
|
|
|
AnnotationQuery query = this.queryFactory.query(AnnotationQuery.class).authorize(AuthorizationFlags.OwnerOrPermissionAssociated).ids(id);
|
|
Annotation model = this.builderFactory.builder(AnnotationBuilder.class).authorize(AuthorizationFlags.OwnerOrPermissionAssociated).build(fieldSet, query.firstAs(fieldSet));
|
|
if (model == null)
|
|
throw new MyNotFoundException(messageSource.getMessage("General_ItemNotFound", new Object[]{id, Annotation.class.getSimpleName()}, LocaleContextHolder.getLocale()));
|
|
|
|
this.auditService.track(AuditableAction.Annotation_Lookup, Map.ofEntries(
|
|
new AbstractMap.SimpleEntry<String, Object>("id", id),
|
|
new AbstractMap.SimpleEntry<String, Object>("fields", fieldSet)
|
|
));
|
|
|
|
return model;
|
|
}
|
|
|
|
@PostMapping("persist")
|
|
@Transactional
|
|
@ValidationFilterAnnotation(validator = AnnotationPersist.AnnotationPersistValidator.ValidatorName, argumentName = "model")
|
|
public Annotation persist(@RequestBody AnnotationPersist model, FieldSet fieldSet) throws MyApplicationException, MyForbiddenException, MyNotFoundException, InvalidApplicationException, JAXBException, ParserConfigurationException, JsonProcessingException, TransformerException {
|
|
logger.debug(new MapLogEntry("persisting" + Annotation.class.getSimpleName()).And("model", model).And("fieldSet", fieldSet));
|
|
|
|
this.censorFactory.censor(AnnotationCensor.class).censor(fieldSet, null);
|
|
|
|
Annotation persisted = this.annotationService.persist(model, fieldSet);
|
|
|
|
this.auditService.track(AuditableAction.Annotation_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" + Annotation.class.getSimpleName()).And("id", id));
|
|
|
|
this.annotationService.deleteAndSave(id);
|
|
|
|
this.auditService.track(AuditableAction.Annotation_Delete, "id", id);
|
|
}
|
|
|
|
}
|