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;
|
|
|
|
import eu.eudat.data.DmpEntity;
|
2023-12-19 17:09:09 +01:00
|
|
|
import eu.eudat.file.transformer.model.file.FileFormat;
|
2023-10-23 13:45:05 +02:00
|
|
|
import eu.eudat.model.Dmp;
|
2023-11-13 15:46:35 +01:00
|
|
|
import eu.eudat.model.DmpUser;
|
2023-10-23 13:45:05 +02:00
|
|
|
import eu.eudat.model.builder.DmpBuilder;
|
|
|
|
import eu.eudat.model.censorship.DmpCensor;
|
2023-12-18 11:55:19 +01:00
|
|
|
import eu.eudat.model.file.FileEnvelope;
|
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.dmp.DataManagementPlan;
|
|
|
|
import eu.eudat.models.data.helpers.responses.ResponseItem;
|
2023-10-23 13:45:05 +02:00
|
|
|
import eu.eudat.query.DmpQuery;
|
|
|
|
import eu.eudat.query.lookup.DmpLookup;
|
|
|
|
import eu.eudat.service.dmp.DmpService;
|
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;
|
|
|
|
import gr.cite.tools.validation.MyValidate;
|
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.*;
|
|
|
|
|
|
|
|
@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;
|
|
|
|
|
2023-12-19 17:09:09 +01:00
|
|
|
private final FileTransformerService fileTransformerService;
|
|
|
|
|
2023-10-23 13:45:05 +02:00
|
|
|
public DmpController(
|
|
|
|
BuilderFactory builderFactory,
|
|
|
|
AuditService auditService,
|
|
|
|
DmpService dmpService,
|
|
|
|
CensorFactory censorFactory,
|
|
|
|
QueryFactory queryFactory,
|
2023-12-19 17:09:09 +01:00
|
|
|
MessageSource messageSource, FileTransformerService fileTransformerService) {
|
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;
|
2023-12-19 17:09:09 +01:00
|
|
|
this.fileTransformerService = fileTransformerService;
|
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);
|
|
|
|
|
2023-11-17 18:01:44 +01:00
|
|
|
DmpQuery query = lookup.enrich(this.queryFactory).authorize(AuthorizationFlags.OwnerOrDmpAssociatedOrPermissionOrPublic);
|
2023-10-23 13:45:05 +02:00
|
|
|
|
|
|
|
List<DmpEntity> data = query.collectAs(lookup.getProject());
|
2023-11-17 18:01:44 +01:00
|
|
|
List<Dmp> models = this.builderFactory.builder(DmpBuilder.class).authorize(AuthorizationFlags.OwnerOrDmpAssociatedOrPermissionOrPublic).build(lookup.getProject(), data);
|
2023-10-23 13:45:05 +02:00
|
|
|
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);
|
|
|
|
|
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
|
2023-11-03 17:09:19 +01:00
|
|
|
public Dmp Persist(@MyValidate @RequestBody DmpPersist model, FieldSet fieldSet) throws MyApplicationException, MyForbiddenException, MyNotFoundException, InvalidApplicationException, JsonProcessingException {
|
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-11-10 15:13:55 +01:00
|
|
|
public Dmp buildClone(@MyValidate @RequestBody CloneDmpPersist model, FieldSet fieldSet) throws MyApplicationException, MyForbiddenException, MyNotFoundException {
|
|
|
|
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
|
|
|
|
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));
|
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
|
|
|
|
public QueryResult<DmpUser> assignUsers(@PathVariable("id") UUID id, @MyValidate @RequestBody List<DmpUserPersist> model, FieldSet fieldSet) throws InvalidApplicationException {
|
|
|
|
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(
|
2023-12-04 17:16:57 +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
|
|
|
|
public QueryResult<Dmp> removeUser(@MyValidate @RequestBody DmpUserRemovePersist model, FieldSet fieldSet) throws InvalidApplicationException {
|
|
|
|
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(
|
|
|
|
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);
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
@GetMapping("/export/formats")
|
|
|
|
public List<FileFormat> getAvailableExportFormats() {
|
|
|
|
return this.fileTransformerService.getAvailableConfigurations();
|
2023-11-14 10:44:01 +01:00
|
|
|
}
|
|
|
|
|
2023-12-14 13:02:30 +01:00
|
|
|
@PostMapping("{id}/invite-users")
|
|
|
|
@Transactional
|
|
|
|
public ResponseEntity inviteUsers(@PathVariable("id") UUID id, @MyValidate @RequestBody DmpUserInvitePersist model) throws InvalidApplicationException, JAXBException {
|
|
|
|
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
|
|
|
|
public ResponseEntity inviteUsers(@PathVariable("id") UUID id, @PathVariable("token") String token) throws InvalidApplicationException, JAXBException {
|
|
|
|
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
|
|
|
}
|