2023-10-23 13:45:05 +02:00
|
|
|
package eu.eudat.controllers.v2;
|
|
|
|
|
2023-11-03 17:09:19 +01:00
|
|
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
2023-10-23 13:45:05 +02:00
|
|
|
import eu.eudat.audit.AuditableAction;
|
|
|
|
import eu.eudat.authorization.AuthorizationFlags;
|
2023-12-21 17:45:49 +01:00
|
|
|
import eu.eudat.commons.validation.ValidationFilterAnnotation;
|
2023-10-23 13:45:05 +02:00
|
|
|
import eu.eudat.data.DmpEntity;
|
2024-01-09 13:53:48 +01:00
|
|
|
import eu.eudat.model.*;
|
2023-10-23 13:45:05 +02:00
|
|
|
import eu.eudat.model.builder.DmpBuilder;
|
|
|
|
import eu.eudat.model.censorship.DmpCensor;
|
2024-01-09 13:53:48 +01:00
|
|
|
import eu.eudat.model.censorship.PublicDescriptionCensor;
|
|
|
|
import eu.eudat.model.censorship.PublicDmpCensor;
|
2023-12-04 17:16:57 +01:00
|
|
|
import eu.eudat.model.persist.*;
|
2023-10-23 13:45:05 +02:00
|
|
|
import eu.eudat.model.result.QueryResult;
|
2023-12-14 13:02:30 +01:00
|
|
|
import eu.eudat.models.data.helpers.responses.ResponseItem;
|
2023-10-23 13:45:05 +02:00
|
|
|
import eu.eudat.query.DmpQuery;
|
2024-01-09 13:53:48 +01:00
|
|
|
import eu.eudat.query.lookup.DescriptionLookup;
|
2023-10-23 13:45:05 +02:00
|
|
|
import eu.eudat.query.lookup.DmpLookup;
|
|
|
|
import eu.eudat.service.dmp.DmpService;
|
2024-01-09 13:53:48 +01:00
|
|
|
import eu.eudat.service.elastic.ElasticQueryHelperService;
|
2023-12-18 11:55:19 +01:00
|
|
|
import eu.eudat.service.transformer.FileTransformerService;
|
2023-12-14 13:02:30 +01:00
|
|
|
import eu.eudat.types.ApiMessageCode;
|
2023-10-23 13:45:05 +02:00
|
|
|
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;
|
2023-11-09 13:10:32 +01:00
|
|
|
import jakarta.xml.bind.JAXBException;
|
2023-10-23 13:45:05 +02:00
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
import org.springframework.context.MessageSource;
|
|
|
|
import org.springframework.context.i18n.LocaleContextHolder;
|
2023-12-14 13:02:30 +01:00
|
|
|
import org.springframework.http.HttpStatus;
|
2023-11-14 10:44:01 +01:00
|
|
|
import org.springframework.http.ResponseEntity;
|
2023-10-23 13:45:05 +02:00
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
|
|
|
|
import javax.management.InvalidApplicationException;
|
2023-11-09 13:10:32 +01:00
|
|
|
import javax.xml.parsers.ParserConfigurationException;
|
|
|
|
import javax.xml.transform.TransformerException;
|
2023-11-14 12:41:57 +01:00
|
|
|
import java.io.IOException;
|
2023-10-23 13:45:05 +02:00
|
|
|
import java.util.*;
|
|
|
|
|
2024-01-09 13:53:48 +01:00
|
|
|
import static eu.eudat.authorization.AuthorizationFlags.Public;
|
|
|
|
|
2023-10-23 13:45:05 +02:00
|
|
|
@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;
|
2024-01-09 13:53:48 +01:00
|
|
|
private final ElasticQueryHelperService elasticQueryHelperService;
|
2023-10-23 13:45:05 +02:00
|
|
|
|
2023-12-19 17:09:09 +01:00
|
|
|
|
2023-10-23 13:45:05 +02:00
|
|
|
public DmpController(
|
2024-01-09 13:53:48 +01:00
|
|
|
BuilderFactory builderFactory,
|
|
|
|
AuditService auditService,
|
|
|
|
DmpService dmpService,
|
|
|
|
CensorFactory censorFactory,
|
|
|
|
QueryFactory queryFactory,
|
|
|
|
MessageSource messageSource,
|
|
|
|
ElasticQueryHelperService elasticQueryHelperService) {
|
2023-10-23 13:45:05 +02:00
|
|
|
this.builderFactory = builderFactory;
|
|
|
|
this.auditService = auditService;
|
|
|
|
this.dmpService = dmpService;
|
|
|
|
this.censorFactory = censorFactory;
|
|
|
|
this.queryFactory = queryFactory;
|
|
|
|
this.messageSource = messageSource;
|
2024-01-09 13:53:48 +01:00
|
|
|
this.elasticQueryHelperService = elasticQueryHelperService;
|
|
|
|
}
|
|
|
|
|
|
|
|
@PostMapping("public/query")
|
|
|
|
public QueryResult<PublicDmp> publicQuery(@RequestBody DmpLookup lookup) throws MyApplicationException, MyForbiddenException {
|
|
|
|
logger.debug("querying {}", Dmp.class.getSimpleName());
|
|
|
|
|
|
|
|
this.censorFactory.censor(PublicDmpCensor.class).censor(lookup.getProject());
|
|
|
|
|
|
|
|
//DmpQuery query = lookup.enrich(this.queryFactory).authorize(EnumSet.of(Public)).dmpSubQuery(this.queryFactory.query(DmpQuery.class).isActive(IsActive.Active).statuses(DmpStatus.Finalized).accessTypes(DmpAccessType.Public));
|
|
|
|
QueryResult<PublicDmp> queryResult = this.elasticQueryHelperService.collectPublic(lookup, EnumSet.of(Public), null);
|
|
|
|
|
|
|
|
this.auditService.track(AuditableAction.Dmp_PublicQuery, "lookup", lookup);
|
|
|
|
|
|
|
|
return queryResult;
|
2023-10-23 13:45:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@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);
|
|
|
|
|
|
|
|
|
2024-01-09 13:53:48 +01:00
|
|
|
QueryResult<Dmp> queryResult = this.elasticQueryHelperService.collect(lookup, AuthorizationFlags.OwnerOrDmpAssociatedOrPermissionOrPublic, null);
|
2023-10-23 13:45:05 +02:00
|
|
|
|
|
|
|
this.auditService.track(AuditableAction.Dmp_Query, "lookup", lookup);
|
|
|
|
|
2024-01-09 13:53:48 +01:00
|
|
|
return queryResult;
|
2023-10-23 13:45:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@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);
|
|
|
|
|
2023-11-17 18:01:44 +01:00
|
|
|
DmpQuery query = this.queryFactory.query(DmpQuery.class).authorize(AuthorizationFlags.OwnerOrDmpAssociatedOrPermissionOrPublic).ids(id);
|
|
|
|
Dmp model = this.builderFactory.builder(DmpBuilder.class).authorize(AuthorizationFlags.OwnerOrDmpAssociatedOrPermissionOrPublic).build(fieldSet, query.firstAs(fieldSet));
|
2023-10-23 13:45:05 +02:00
|
|
|
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
|
2024-01-02 16:39:50 +01:00
|
|
|
@ValidationFilterAnnotation(validator = DmpPersist.DmpPersistValidator.ValidatorName, argumentName = "model")
|
2024-01-09 15:02:12 +01:00
|
|
|
public Dmp Persist(@RequestBody DmpPersist model, FieldSet fieldSet) throws MyApplicationException, MyForbiddenException, MyNotFoundException, InvalidApplicationException, IOException {
|
2023-10-23 13:45:05 +02:00
|
|
|
logger.debug(new MapLogEntry("persisting" + Dmp.class.getSimpleName()).And("model", model).And("fieldSet", fieldSet));
|
2023-11-13 15:46:35 +01:00
|
|
|
|
2023-10-23 13:45:05 +02:00
|
|
|
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
|
2023-11-14 12:41:57 +01:00
|
|
|
public void Delete(@PathVariable("id") UUID id) throws MyForbiddenException, InvalidApplicationException, IOException {
|
2023-10-23 13:45:05 +02:00
|
|
|
logger.debug(new MapLogEntry("retrieving" + Dmp.class.getSimpleName()).And("id", id));
|
|
|
|
|
|
|
|
this.dmpService.deleteAndSave(id);
|
|
|
|
|
|
|
|
this.auditService.track(AuditableAction.Dmp_Delete, "id", id);
|
|
|
|
}
|
|
|
|
|
2023-11-09 13:10:32 +01:00
|
|
|
@PostMapping("clone")
|
2023-12-21 17:45:49 +01:00
|
|
|
@Transactional
|
|
|
|
@ValidationFilterAnnotation(validator = CloneDmpPersist.CloneDmpPersistValidator.ValidatorName, argumentName = "model")
|
2024-01-09 15:02:12 +01:00
|
|
|
public Dmp buildClone(@RequestBody CloneDmpPersist model, FieldSet fieldSet) throws MyApplicationException, MyForbiddenException, MyNotFoundException, IOException {
|
2023-11-10 15:13:55 +01:00
|
|
|
logger.debug(new MapLogEntry("clone" + Dmp.class.getSimpleName()).And("model", model).And("fields", fieldSet));
|
2023-11-09 13:10:32 +01:00
|
|
|
|
|
|
|
this.censorFactory.censor(DmpCensor.class).censor(fieldSet, null);
|
|
|
|
|
2023-11-10 15:13:55 +01:00
|
|
|
Dmp clone = this.dmpService.buildClone(model, fieldSet);
|
2023-11-09 13:10:32 +01:00
|
|
|
|
|
|
|
this.auditService.track(AuditableAction.Dmp_Clone, Map.ofEntries(
|
2023-11-10 15:13:55 +01:00
|
|
|
new AbstractMap.SimpleEntry<String, Object>("model", model),
|
2023-11-09 13:10:32 +01:00
|
|
|
new AbstractMap.SimpleEntry<String, Object>("fields", fieldSet)
|
|
|
|
));
|
|
|
|
|
2023-11-10 15:13:55 +01:00
|
|
|
return clone;
|
2023-11-09 13:10:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@PostMapping("new-version")
|
|
|
|
@Transactional
|
2024-01-03 15:45:57 +01:00
|
|
|
@ValidationFilterAnnotation(validator = NewVersionDmpPersist.NewVersionDmpPersistValidator.ValidatorName, argumentName = "model")
|
2024-01-09 15:02:12 +01:00
|
|
|
public Dmp createNewVersion(@RequestBody NewVersionDmpPersist model, FieldSet fieldSet) throws MyApplicationException, MyForbiddenException, MyNotFoundException, JAXBException, IOException, TransformerException, InvalidApplicationException, ParserConfigurationException {
|
2023-11-09 13:10:32 +01:00
|
|
|
logger.debug(new MapLogEntry("persisting" + NewVersionDmpPersist.class.getSimpleName()).And("model", model).And("fieldSet", fieldSet));
|
2023-11-13 15:46:35 +01:00
|
|
|
|
2023-11-09 13:10:32 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2023-11-13 15:46:35 +01:00
|
|
|
@PostMapping("{id}/assign-users")
|
|
|
|
@Transactional
|
2024-01-03 15:45:57 +01:00
|
|
|
@ValidationFilterAnnotation(validator = DmpUserPersist.DmpUserPersistValidator.ValidatorName, argumentName = "model")
|
2024-01-09 15:02:12 +01:00
|
|
|
public QueryResult<DmpUser> assignUsers(@PathVariable("id") UUID id, @RequestBody List<DmpUserPersist> model, FieldSet fieldSet) throws InvalidApplicationException, IOException {
|
2023-11-13 15:46:35 +01:00
|
|
|
logger.debug(new MapLogEntry("assigning users to dmp").And("model", model).And("fieldSet", fieldSet));
|
|
|
|
|
2023-12-04 17:16:57 +01:00
|
|
|
List<DmpUser> persisted = this.dmpService.assignUsers(id, model, fieldSet);
|
2023-11-13 15:46:35 +01:00
|
|
|
|
|
|
|
this.auditService.track(AuditableAction.Dmp_Assign_Users, Map.ofEntries(
|
2024-01-05 13:02:22 +01:00
|
|
|
new AbstractMap.SimpleEntry<String, Object>("model", model),
|
|
|
|
new AbstractMap.SimpleEntry<String, Object>("fields", fieldSet)
|
2023-11-13 15:46:35 +01:00
|
|
|
));
|
|
|
|
|
2023-12-04 17:16:57 +01:00
|
|
|
return new QueryResult<>(persisted);
|
|
|
|
}
|
|
|
|
|
|
|
|
@PostMapping("remove-user")
|
|
|
|
@Transactional
|
2024-01-03 15:45:57 +01:00
|
|
|
@ValidationFilterAnnotation(validator = DmpUserRemovePersist.DmpUserRemovePersistValidator.ValidatorName, argumentName = "model")
|
2024-01-09 15:02:12 +01:00
|
|
|
public QueryResult<Dmp> removeUser(@RequestBody DmpUserRemovePersist model, FieldSet fieldSet) throws InvalidApplicationException, IOException {
|
2023-12-04 17:16:57 +01:00
|
|
|
logger.debug(new MapLogEntry("remove user from dmp").And("model", model).And("fieldSet", fieldSet));
|
|
|
|
|
|
|
|
Dmp persisted = this.dmpService.removeUser(model, fieldSet);
|
|
|
|
|
|
|
|
this.auditService.track(AuditableAction.Dmp_RemoveUser, Map.ofEntries(
|
2024-01-05 13:02:22 +01:00
|
|
|
new AbstractMap.SimpleEntry<String, Object>("model", model),
|
|
|
|
new AbstractMap.SimpleEntry<String, Object>("fields", fieldSet)
|
2023-12-04 17:16:57 +01:00
|
|
|
));
|
2023-11-13 15:46:35 +01:00
|
|
|
|
2023-12-04 17:16:57 +01:00
|
|
|
return new QueryResult<>(persisted);
|
2023-11-13 15:46:35 +01:00
|
|
|
}
|
|
|
|
|
2023-11-14 10:44:01 +01:00
|
|
|
@GetMapping("{id}/export/{type}")
|
2023-12-18 11:55:19 +01:00
|
|
|
public ResponseEntity<byte[]> export(@PathVariable("id") UUID id, @PathVariable("type") String exportType) throws InvalidApplicationException, IOException {
|
2023-11-14 10:44:01 +01:00
|
|
|
logger.debug(new MapLogEntry("exporting dmp"));
|
|
|
|
|
2023-12-19 17:09:09 +01:00
|
|
|
return this.dmpService.export(id, exportType);
|
|
|
|
}
|
|
|
|
|
2023-12-14 13:02:30 +01:00
|
|
|
@PostMapping("{id}/invite-users")
|
|
|
|
@Transactional
|
2024-01-03 15:45:57 +01:00
|
|
|
@ValidationFilterAnnotation(validator = DmpUserInvitePersist.DmpUserInvitePersistValidator.ValidatorName, argumentName = "model")
|
2024-01-09 15:02:12 +01:00
|
|
|
public ResponseEntity<ResponseItem<String>> inviteUsers(@PathVariable("id") UUID id, @RequestBody DmpUserInvitePersist model) throws InvalidApplicationException, JAXBException, IOException {
|
2023-12-14 13:02:30 +01:00
|
|
|
logger.debug(new MapLogEntry("inviting users to dmp").And("model", model));
|
|
|
|
|
|
|
|
this.dmpService.inviteUsers(id, model);
|
|
|
|
|
|
|
|
this.auditService.track(AuditableAction.Dmp_Invite_Users, Map.ofEntries(
|
|
|
|
new AbstractMap.SimpleEntry<String, Object>("model", model)
|
|
|
|
));
|
|
|
|
|
|
|
|
return ResponseEntity.status(HttpStatus.OK).body(new ResponseItem<String>().status(ApiMessageCode.SUCCESS_MESSAGE).payload("Invite Users Success"));
|
|
|
|
}
|
|
|
|
|
|
|
|
@GetMapping("{id}/token/{token}/invite-accept")
|
|
|
|
@Transactional
|
2024-01-09 15:02:12 +01:00
|
|
|
public ResponseEntity<ResponseItem<String>> acceptInvitation(@PathVariable("id") UUID id, @PathVariable("token") String token) throws InvalidApplicationException, JAXBException, IOException {
|
2023-12-14 13:02:30 +01:00
|
|
|
logger.debug(new MapLogEntry("inviting users to dmp").And("id", id));
|
|
|
|
|
|
|
|
this.dmpService.dmpInvitationAccept(token);
|
|
|
|
|
|
|
|
this.auditService.track(AuditableAction.Dmp_Invite_Accept, Map.ofEntries(
|
|
|
|
new AbstractMap.SimpleEntry<String, Object>("token", token)
|
|
|
|
));
|
|
|
|
|
|
|
|
return ResponseEntity.status(HttpStatus.OK).body(new ResponseItem<String>().status(ApiMessageCode.SUCCESS_MESSAGE).payload("Invite Accept Success"));
|
|
|
|
}
|
|
|
|
|
2023-10-23 13:45:05 +02:00
|
|
|
}
|