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

164 lines
7.2 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.data.DmpEntity;
import eu.eudat.model.Dmp;
import eu.eudat.model.builder.DmpBuilder;
import eu.eudat.model.censorship.DmpCensor;
import eu.eudat.model.persist.CloneDmpPersist;
import eu.eudat.model.persist.DmpPersist;
import eu.eudat.model.persist.NewVersionDmpPersist;
import eu.eudat.model.result.QueryResult;
import eu.eudat.query.DmpQuery;
import eu.eudat.query.lookup.DmpLookup;
import eu.eudat.service.dmp.DmpService;
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.MyValidate;
import jakarta.xml.bind.JAXBException;
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 javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerException;
import java.util.*;
@RestController
@RequestMapping(path = "api/dmp")
public class DmpController {
private static final LoggerService logger = new LoggerService(LoggerFactory.getLogger(DmpController.class));
private final BuilderFactory builderFactory;
private final AuditService auditService;
private final DmpService dmpService;
private final CensorFactory censorFactory;
private final QueryFactory queryFactory;
private final MessageSource messageSource;
public DmpController(
BuilderFactory builderFactory,
AuditService auditService,
DmpService dmpService,
CensorFactory censorFactory,
QueryFactory queryFactory,
MessageSource messageSource) {
this.builderFactory = builderFactory;
this.auditService = auditService;
this.dmpService = dmpService;
this.censorFactory = censorFactory;
this.queryFactory = queryFactory;
this.messageSource = messageSource;
}
@PostMapping("query")
public QueryResult<Dmp> Query(@RequestBody DmpLookup lookup) throws MyApplicationException, MyForbiddenException {
logger.debug("querying {}", Dmp.class.getSimpleName());
this.censorFactory.censor(DmpCensor.class).censor(lookup.getProject(), null);
DmpQuery query = lookup.enrich(this.queryFactory).authorize(AuthorizationFlags.OwnerOrPermissionOrMemberOrPublic);
List<DmpEntity> data = query.collectAs(lookup.getProject());
List<Dmp> models = this.builderFactory.builder(DmpBuilder.class).authorize(AuthorizationFlags.OwnerOrPermissionOrMemberOrPublic).build(lookup.getProject(), data);
long count = (lookup.getMetadata() != null && lookup.getMetadata().getCountAll()) ? query.count() : models.size();
this.auditService.track(AuditableAction.Dmp_Query, "lookup", lookup);
return new QueryResult<>(models, count);
}
@GetMapping("{id}")
public Dmp Get(@PathVariable("id") UUID id, FieldSet fieldSet, Locale locale) throws MyApplicationException, MyForbiddenException, MyNotFoundException {
logger.debug(new MapLogEntry("retrieving" + Dmp.class.getSimpleName()).And("id", id).And("fields", fieldSet));
this.censorFactory.censor(DmpCensor.class).censor(fieldSet, null);
DmpQuery query = this.queryFactory.query(DmpQuery.class).authorize(AuthorizationFlags.OwnerOrPermissionOrMemberOrPublic).ids(id);
Dmp model = this.builderFactory.builder(DmpBuilder.class).authorize(AuthorizationFlags.OwnerOrPermissionOrMemberOrPublic).build(fieldSet, query.firstAs(fieldSet));
if (model == null)
throw new MyNotFoundException(messageSource.getMessage("General_ItemNotFound", new Object[]{id, Dmp.class.getSimpleName()}, LocaleContextHolder.getLocale()));
this.auditService.track(AuditableAction.Dmp_Lookup, Map.ofEntries(
new AbstractMap.SimpleEntry<String, Object>("id", id),
new AbstractMap.SimpleEntry<String, Object>("fields", fieldSet)
));
return model;
}
@PostMapping("persist")
@Transactional
public Dmp Persist(@MyValidate @RequestBody DmpPersist model, FieldSet fieldSet) throws MyApplicationException, MyForbiddenException, MyNotFoundException, InvalidApplicationException, JsonProcessingException {
logger.debug(new MapLogEntry("persisting" + Dmp.class.getSimpleName()).And("model", model).And("fieldSet", fieldSet));
Dmp persisted = this.dmpService.persist(model, fieldSet);
this.auditService.track(AuditableAction.Dmp_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" + Dmp.class.getSimpleName()).And("id", id));
this.dmpService.deleteAndSave(id);
this.auditService.track(AuditableAction.Dmp_Delete, "id", id);
}
@PostMapping("clone")
public Dmp buildClone(@MyValidate @RequestBody CloneDmpPersist persist, FieldSet fieldSet) throws MyApplicationException, MyForbiddenException, MyNotFoundException {
logger.debug(new MapLogEntry("clone" + Dmp.class.getSimpleName()).And("model", persist).And("fields", fieldSet));
this.censorFactory.censor(DmpCensor.class).censor(fieldSet, null);
Dmp model = this.dmpService.buildClone(persist, fieldSet);
this.auditService.track(AuditableAction.Dmp_Clone, Map.ofEntries(
new AbstractMap.SimpleEntry<String, Object>("model", persist),
new AbstractMap.SimpleEntry<String, Object>("fields", fieldSet)
));
return model;
}
@PostMapping("new-version")
@Transactional
public Dmp createNewVersion(@MyValidate @RequestBody NewVersionDmpPersist model, FieldSet fieldSet) throws MyApplicationException, MyForbiddenException, MyNotFoundException, JAXBException, JsonProcessingException, TransformerException, InvalidApplicationException, ParserConfigurationException {
logger.debug(new MapLogEntry("persisting" + NewVersionDmpPersist.class.getSimpleName()).And("model", model).And("fieldSet", fieldSet));
Dmp persisted = this.dmpService.createNewVersion(model, fieldSet);
this.auditService.track(AuditableAction.Dmp_PersistNewVersion, Map.ofEntries(
new AbstractMap.SimpleEntry<String, Object>("model", model),
new AbstractMap.SimpleEntry<String, Object>("fields", fieldSet)
));
return persisted;
}
}