|
|
|
@ -58,6 +58,7 @@ import gr.cite.tools.exception.MyValidationException;
|
|
|
|
|
import gr.cite.tools.fieldset.BaseFieldSet;
|
|
|
|
|
import gr.cite.tools.fieldset.FieldSet;
|
|
|
|
|
import gr.cite.tools.logging.LoggerService;
|
|
|
|
|
import gr.cite.tools.logging.MapLogEntry;
|
|
|
|
|
import gr.cite.tools.validation.ValidatorFactory;
|
|
|
|
|
import jakarta.persistence.EntityManager;
|
|
|
|
|
import jakarta.xml.bind.JAXBException;
|
|
|
|
@ -73,8 +74,6 @@ import org.springframework.http.ResponseEntity;
|
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
|
|
|
|
import javax.management.InvalidApplicationException;
|
|
|
|
|
import javax.xml.parsers.ParserConfigurationException;
|
|
|
|
|
import javax.xml.transform.TransformerException;
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.time.Instant;
|
|
|
|
|
import java.util.*;
|
|
|
|
@ -297,24 +296,61 @@ public class DmpServiceImpl implements DmpService {
|
|
|
|
|
logger.debug("deleting dmp: {}", id);
|
|
|
|
|
|
|
|
|
|
this.authorizationService.authorizeAtLeastOneForce(List.of(this.authorizationContentResolver.dmpAffiliation(id)), Permission.DeleteDmp);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
DmpEntity data = this.entityManager.find(DmpEntity.class, id);
|
|
|
|
|
if (data == null) throw new MyNotFoundException(messageSource.getMessage("General_ItemNotFound", new Object[]{id, Dmp.class.getSimpleName()}, LocaleContextHolder.getLocale()));
|
|
|
|
|
|
|
|
|
|
EntityDoiQuery entityDoiQuery = this.queryFactory.query(EntityDoiQuery.class).authorize(AuthorizationFlags.OwnerOrDmpAssociatedOrPermission).types(EntityType.DMP).entityIds(data.getId());
|
|
|
|
|
if (entityDoiQuery.count() > 0) throw new MyApplicationException("DMP is deposited can not deleted");
|
|
|
|
|
|
|
|
|
|
DmpEntity previousFinalized = null;
|
|
|
|
|
if (data.getVersionStatus().equals(DmpVersionStatus.Current)){
|
|
|
|
|
DmpQuery dmpQuery = this.queryFactory.query(DmpQuery.class)
|
|
|
|
|
.statuses(DmpStatus.Finalized)
|
|
|
|
|
.excludedIds(data.getId())
|
|
|
|
|
.isActive(IsActive.Active)
|
|
|
|
|
.groupIds(data.getGroupId());
|
|
|
|
|
|
|
|
|
|
dmpQuery.setOrder(new Ordering().addDescending(Dmp._version));
|
|
|
|
|
previousFinalized = dmpQuery.first();
|
|
|
|
|
if (previousFinalized != null){
|
|
|
|
|
previousFinalized.setVersionStatus(DmpVersionStatus.Current);
|
|
|
|
|
this.entityManager.merge(previousFinalized);
|
|
|
|
|
}
|
|
|
|
|
data.setVersionStatus(DmpVersionStatus.NotFinalized);
|
|
|
|
|
this.entityManager.merge(data);
|
|
|
|
|
this.entityManager.flush();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.deleterFactory.deleter(DmpDeleter.class).deleteAndSaveByIds(List.of(id), false);
|
|
|
|
|
if (previousFinalized != null) this.elasticService.persistDmp(previousFinalized);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public Dmp createNewVersion(NewVersionDmpPersist model, FieldSet fields) throws MyForbiddenException, MyValidationException, MyApplicationException, MyNotFoundException, InvalidApplicationException, JAXBException, ParserConfigurationException, IOException, TransformerException {
|
|
|
|
|
public Dmp createNewVersion(NewVersionDmpPersist model, FieldSet fields) throws MyForbiddenException, MyValidationException, MyApplicationException, MyNotFoundException, InvalidApplicationException, IOException {
|
|
|
|
|
logger.debug(new MapLogEntry("persisting data bew version").And("model", model).And("fields", fields));
|
|
|
|
|
this.authorizationService.authorizeAtLeastOneForce(List.of(this.authorizationContentResolver.dmpAffiliation( model.getId())), Permission.CreateNewVersionDmp);
|
|
|
|
|
|
|
|
|
|
DmpEntity oldDmpEntity = this.entityManager.find(DmpEntity.class, model.getId());
|
|
|
|
|
if (oldDmpEntity == null) throw new MyNotFoundException(messageSource.getMessage("General_ItemNotFound", new Object[]{model.getId(), Dmp.class.getSimpleName()}, LocaleContextHolder.getLocale()));
|
|
|
|
|
if (!this.conventionService.hashValue(oldDmpEntity.getUpdatedAt()).equals(model.getHash())) throw new MyValidationException(this.errors.getHashConflict().getCode(), this.errors.getHashConflict().getMessage());
|
|
|
|
|
DmpQuery latestVersionDmpEntityQuery = this.queryFactory.query(DmpQuery.class).groupIds(oldDmpEntity.getGroupId()).versionStatuses(DmpVersionStatus.Current);
|
|
|
|
|
List<DmpEntity> latestVersionDmps = latestVersionDmpEntityQuery.collect();
|
|
|
|
|
|
|
|
|
|
List<DmpEntity> latestVersionDmps = this.queryFactory.query(DmpQuery.class)
|
|
|
|
|
.groupIds(oldDmpEntity.getGroupId())
|
|
|
|
|
.isActive(IsActive.Active)
|
|
|
|
|
.versionStatuses(DmpVersionStatus.Current)
|
|
|
|
|
.collect();
|
|
|
|
|
if (latestVersionDmps.isEmpty()) throw new MyValidationException("Previous dmp not found");
|
|
|
|
|
if (latestVersionDmps.size() > 1) throw new MyValidationException("Multiple previous dmps found");
|
|
|
|
|
if (!latestVersionDmps.getFirst().getVersion().equals(oldDmpEntity.getVersion())){
|
|
|
|
|
throw new MyValidationException(this.errors.getDmpNewVersionConflict().getCode(), this.errors.getDmpNewVersionConflict().getMessage());
|
|
|
|
|
}
|
|
|
|
|
Long notFinalizedCount = this.queryFactory.query(DmpQuery.class)
|
|
|
|
|
.versionStatuses(DmpVersionStatus.NotFinalized)
|
|
|
|
|
.groupIds(oldDmpEntity.getGroupId())
|
|
|
|
|
.isActive(IsActive.Active)
|
|
|
|
|
.count();
|
|
|
|
|
if (notFinalizedCount > 0) throw new MyValidationException("Already created draft for this template");
|
|
|
|
|
|
|
|
|
|
DmpEntity newDmp = new DmpEntity();
|
|
|
|
|
newDmp.setId(UUID.randomUUID());
|
|
|
|
@ -322,7 +358,7 @@ public class DmpServiceImpl implements DmpService {
|
|
|
|
|
newDmp.setCreatedAt(Instant.now());
|
|
|
|
|
newDmp.setUpdatedAt(Instant.now());
|
|
|
|
|
newDmp.setGroupId(oldDmpEntity.getGroupId());
|
|
|
|
|
newDmp.setVersionStatus(DmpVersionStatus.Current);
|
|
|
|
|
newDmp.setVersionStatus(DmpVersionStatus.NotFinalized);
|
|
|
|
|
newDmp.setVersion((short)(oldDmpEntity.getVersion() + 1));
|
|
|
|
|
newDmp.setDescription(model.getDescription());
|
|
|
|
|
newDmp.setLabel(model.getLabel());
|
|
|
|
@ -389,13 +425,12 @@ public class DmpServiceImpl implements DmpService {
|
|
|
|
|
for (UUID descriptionId : model.getDescriptions()) {
|
|
|
|
|
this.descriptionService.clone(newDmp.getId(), descriptionId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
oldDmpEntity.setVersionStatus(DmpVersionStatus.Previous);
|
|
|
|
|
this.entityManager.merge(oldDmpEntity);
|
|
|
|
|
|
|
|
|
|
this.entityManager.flush();
|
|
|
|
|
|
|
|
|
|
this.updateVersionStatusAndSave(newDmp, DmpStatus.Draft, newDmp.getStatus());
|
|
|
|
|
|
|
|
|
|
this.entityManager.flush();
|
|
|
|
|
|
|
|
|
|
this.elasticService.persistDmp(oldDmpEntity);
|
|
|
|
|
this.elasticService.persistDmp(newDmp);
|
|
|
|
@ -403,6 +438,42 @@ public class DmpServiceImpl implements DmpService {
|
|
|
|
|
return this.builderFactory.builder(DmpBuilder.class).build(BaseFieldSet.build(fields, Dmp._id), newDmp);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void updateVersionStatusAndSave(DmpEntity data, DmpStatus previousStatus, DmpStatus newStatus) {
|
|
|
|
|
if (previousStatus.equals(newStatus))
|
|
|
|
|
return;
|
|
|
|
|
if (previousStatus.equals(DmpStatus.Finalized) && newStatus.equals(DmpStatus.Draft)){
|
|
|
|
|
boolean alreadyCreatedNewVersion = this.queryFactory.query(DmpQuery.class)
|
|
|
|
|
.versionStatuses(DmpVersionStatus.NotFinalized, DmpVersionStatus.Current)
|
|
|
|
|
.excludedIds(data.getId())
|
|
|
|
|
.isActive(IsActive.Active)
|
|
|
|
|
.groupIds(data.getGroupId())
|
|
|
|
|
.count() > 0;
|
|
|
|
|
if (alreadyCreatedNewVersion) throw new MyValidationException("Already new version is created");
|
|
|
|
|
|
|
|
|
|
data.setVersionStatus(DmpVersionStatus.NotFinalized);
|
|
|
|
|
this.entityManager.merge(data);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (newStatus.equals(DmpStatus.Finalized)) {
|
|
|
|
|
List<DmpEntity> latestVersionDmps = this.queryFactory.query(DmpQuery.class)
|
|
|
|
|
.versionStatuses(DmpVersionStatus.Current).isActive(IsActive.Active).groupIds(data.getGroupId()).collect();
|
|
|
|
|
if (latestVersionDmps.size() > 1)
|
|
|
|
|
throw new MyValidationException("Multiple previous template found");
|
|
|
|
|
DmpEntity oldDmpEntity = latestVersionDmps.stream().findFirst().orElse(null);
|
|
|
|
|
|
|
|
|
|
data.setVersionStatus(DmpVersionStatus.Current);
|
|
|
|
|
|
|
|
|
|
if (oldDmpEntity != null) {
|
|
|
|
|
data.setVersion((short) (oldDmpEntity.getVersion() + 1));
|
|
|
|
|
|
|
|
|
|
oldDmpEntity.setVersionStatus(DmpVersionStatus.Previous);
|
|
|
|
|
this.entityManager.merge(oldDmpEntity);
|
|
|
|
|
} else {
|
|
|
|
|
data.setVersion((short) 1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public Dmp buildClone(CloneDmpPersist model, FieldSet fields) throws MyForbiddenException, MyValidationException, MyApplicationException, MyNotFoundException, IOException, InvalidApplicationException {
|
|
|
|
|
this.authorizationService.authorizeAtLeastOneForce(List.of(this.authorizationContentResolver.dmpAffiliation( model.getId())), Permission.CloneDmp);
|
|
|
|
@ -418,7 +489,7 @@ public class DmpServiceImpl implements DmpService {
|
|
|
|
|
newDmp.setUpdatedAt(Instant.now());
|
|
|
|
|
newDmp.setGroupId(UUID.randomUUID());
|
|
|
|
|
newDmp.setVersion((short) 1);
|
|
|
|
|
newDmp.setVersionStatus(DmpVersionStatus.Current);
|
|
|
|
|
newDmp.setVersionStatus(DmpVersionStatus.NotFinalized);
|
|
|
|
|
newDmp.setDescription(model.getDescription());
|
|
|
|
|
newDmp.setLabel(model.getLabel());
|
|
|
|
|
newDmp.setLanguage(existingDmpEntity.getLanguage());
|
|
|
|
@ -586,12 +657,13 @@ public class DmpServiceImpl implements DmpService {
|
|
|
|
|
data.setGroupId(UUID.randomUUID());
|
|
|
|
|
data.setVersion((short) 1);
|
|
|
|
|
data.setStatus(DmpStatus.Draft);
|
|
|
|
|
data.setVersionStatus(DmpVersionStatus.Current);
|
|
|
|
|
data.setVersionStatus(DmpVersionStatus.NotFinalized);
|
|
|
|
|
data.setCreatorId(userScope.getUserId());
|
|
|
|
|
data.setBlueprintId(model.getBlueprint());
|
|
|
|
|
data.setIsActive(IsActive.Active);
|
|
|
|
|
data.setCreatedAt(Instant.now());
|
|
|
|
|
}
|
|
|
|
|
DmpStatus previousStatus = data.getStatus();
|
|
|
|
|
|
|
|
|
|
data.setLabel(model.getLabel());
|
|
|
|
|
data.setLanguage(model.getLanguage());
|
|
|
|
@ -607,6 +679,10 @@ public class DmpServiceImpl implements DmpService {
|
|
|
|
|
|
|
|
|
|
this.entityManager.flush();
|
|
|
|
|
|
|
|
|
|
this.updateVersionStatusAndSave(data, previousStatus, data.getStatus());
|
|
|
|
|
|
|
|
|
|
this.entityManager.flush();
|
|
|
|
|
|
|
|
|
|
return data;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -692,7 +768,7 @@ public class DmpServiceImpl implements DmpService {
|
|
|
|
|
List<UUID> updatedCreatedIds = new ArrayList<>();
|
|
|
|
|
for (DmpReferencePersist model : models) {
|
|
|
|
|
ReferencePersist referencePersist = model.getReference();
|
|
|
|
|
ReferenceEntity referenceEntity = null;
|
|
|
|
|
ReferenceEntity referenceEntity;
|
|
|
|
|
if (this.conventionService.isValidGuid(referencePersist.getId())){
|
|
|
|
|
referenceEntity = this.entityManager.find(ReferenceEntity.class, referencePersist.getId());
|
|
|
|
|
if (referenceEntity == null) throw new MyNotFoundException(messageSource.getMessage("General_ItemNotFound", new Object[]{referencePersist.getId(), Reference.class.getSimpleName()}, LocaleContextHolder.getLocale()));
|
|
|
|
@ -807,15 +883,6 @@ public class DmpServiceImpl implements DmpService {
|
|
|
|
|
return data;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private DmpUserEntity checkUserRoleIfExists(List<DmpUserEntity> dmpUserEntities, UUID dmp, UUID user, DmpUserRole role) {
|
|
|
|
|
for (DmpUserEntity dmpUser : dmpUserEntities) {
|
|
|
|
|
if (dmpUser.getDmpId().equals(dmp) && dmpUser.getUserId().equals(user) && dmpUser.getRole() == role) {
|
|
|
|
|
return dmpUser;
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void finalize(UUID id, List<UUID> descriptionIds) throws MyForbiddenException, MyValidationException, MyApplicationException, MyNotFoundException, InvalidApplicationException, IOException {
|
|
|
|
|
this.authorizationService.authorizeAtLeastOneForce(List.of(this.authorizationContentResolver.dmpAffiliation(id)), Permission.FinalizeDmp);
|
|
|
|
|
DmpEntity dmp = this.queryFactory.query(DmpQuery.class).authorize(AuthorizationFlags.OwnerOrDmpAssociatedOrPermission).ids(id).isActive(IsActive.Active).first();
|
|
|
|
@ -828,7 +895,8 @@ public class DmpServiceImpl implements DmpService {
|
|
|
|
|
throw new MyApplicationException("DMP is already finalized");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
List<DescriptionEntity> descriptions = this.queryFactory.query(DescriptionQuery.class).authorize(AuthorizationFlags.OwnerOrDmpAssociatedOrPermission).dmpIds(id).isActive(IsActive.Active).collect();
|
|
|
|
|
List<DescriptionEntity> descriptions = this.queryFactory.query(DescriptionQuery.class)
|
|
|
|
|
.authorize(AuthorizationFlags.OwnerOrDmpAssociatedOrPermission).dmpIds(id).isActive(IsActive.Active).collect();
|
|
|
|
|
|
|
|
|
|
for (DescriptionEntity description: descriptions) {
|
|
|
|
|
if (descriptionIds.contains(description.getId())){
|
|
|
|
@ -836,7 +904,7 @@ public class DmpServiceImpl implements DmpService {
|
|
|
|
|
if (description.getStatus().equals(DescriptionStatus.Finalized)){
|
|
|
|
|
throw new MyApplicationException("Description is already finalized");
|
|
|
|
|
}
|
|
|
|
|
if (this.descriptionService.validate(List.of(description.getId())).get(0).getResult().equals(DescriptionValidationOutput.Invalid)){
|
|
|
|
|
if (this.descriptionService.validate(List.of(description.getId())).getFirst().getResult().equals(DescriptionValidationOutput.Invalid)){
|
|
|
|
|
throw new MyApplicationException("Description is invalid");
|
|
|
|
|
}
|
|
|
|
|
description.setStatus(DescriptionStatus.Finalized);
|
|
|
|
@ -849,39 +917,41 @@ public class DmpServiceImpl implements DmpService {
|
|
|
|
|
this.deleterFactory.deleter(DescriptionDeleter.class).delete(List.of(description), true);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
DmpStatus previousStatus = dmp.getStatus();
|
|
|
|
|
dmp.setStatus(DmpStatus.Finalized);
|
|
|
|
|
dmp.setUpdatedAt(Instant.now());
|
|
|
|
|
dmp.setFinalizedAt(Instant.now());
|
|
|
|
|
|
|
|
|
|
this.entityManager.merge(dmp);
|
|
|
|
|
this.entityManager.flush();
|
|
|
|
|
|
|
|
|
|
this.updateVersionStatusAndSave(dmp, previousStatus, dmp.getStatus());
|
|
|
|
|
this.entityManager.flush();
|
|
|
|
|
|
|
|
|
|
this.elasticService.persistDmp(dmp);
|
|
|
|
|
this.sendNotification(dmp);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void undoFinalize(UUID id, FieldSet fields) throws MyForbiddenException, MyValidationException, MyApplicationException, MyNotFoundException, InvalidApplicationException, IOException {
|
|
|
|
|
public void undoFinalize(UUID id, FieldSet fields) throws MyForbiddenException, MyValidationException, MyApplicationException, MyNotFoundException, InvalidApplicationException {
|
|
|
|
|
this.authorizationService.authorizeAtLeastOneForce(List.of(this.authorizationContentResolver.dmpAffiliation(id)), Permission.UndoFinalizeDmp);
|
|
|
|
|
DmpEntity dmp = this.queryFactory.query(DmpQuery.class).authorize(AuthorizationFlags.OwnerOrDmpAssociatedOrPermission).ids(id).isActive(IsActive.Active).firstAs(fields);
|
|
|
|
|
|
|
|
|
|
if (dmp == null){
|
|
|
|
|
throw new MyNotFoundException(messageSource.getMessage("General_ItemNotFound", new Object[]{id, Dmp.class.getSimpleName()}, LocaleContextHolder.getLocale()));
|
|
|
|
|
}
|
|
|
|
|
if (dmp == null) throw new MyNotFoundException(messageSource.getMessage("General_ItemNotFound", new Object[]{id, Dmp.class.getSimpleName()}, LocaleContextHolder.getLocale()));
|
|
|
|
|
|
|
|
|
|
if (dmp.getStatus().equals(DmpStatus.Draft)){
|
|
|
|
|
throw new MyApplicationException("DMP is already drafted");
|
|
|
|
|
}
|
|
|
|
|
if (!dmp.getStatus().equals(DmpStatus.Finalized)) throw new MyApplicationException("DMP is already drafted");
|
|
|
|
|
|
|
|
|
|
EntityDoiQuery entityDoiQuery = this.queryFactory.query(EntityDoiQuery.class).authorize(AuthorizationFlags.OwnerOrDmpAssociatedOrPermission).types(EntityType.DMP).entityIds(dmp.getId());
|
|
|
|
|
if (entityDoiQuery != null && entityDoiQuery.count() > 0){
|
|
|
|
|
throw new MyApplicationException("DMP is deposited");
|
|
|
|
|
}
|
|
|
|
|
if (entityDoiQuery.count() > 0) throw new MyApplicationException("DMP is deposited");
|
|
|
|
|
|
|
|
|
|
dmp.setStatus(DmpStatus.Draft);
|
|
|
|
|
dmp.setUpdatedAt(Instant.now());
|
|
|
|
|
|
|
|
|
|
this.entityManager.merge(dmp);
|
|
|
|
|
this.entityManager.flush();
|
|
|
|
|
|
|
|
|
|
this.updateVersionStatusAndSave(dmp, DmpStatus.Finalized, dmp.getStatus());
|
|
|
|
|
this.entityManager.flush();
|
|
|
|
|
this.sendNotification(dmp);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -986,7 +1056,7 @@ public class DmpServiceImpl implements DmpService {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void dmpInvitationAccept(String token) throws InvalidApplicationException, IOException {
|
|
|
|
|
public void dmpInvitationAccept(String token) {
|
|
|
|
|
ActionConfirmationEntity action = this.queryFactory.query(ActionConfirmationQuery.class).tokens(token).types(ActionConfirmationType.DmpInvitation).isActive(IsActive.Active).first();
|
|
|
|
|
|
|
|
|
|
if (action == null){
|
|
|
|
|