|
|
|
@ -17,8 +17,7 @@ import eu.eudat.data.*;
|
|
|
|
|
import eu.eudat.errorcode.ErrorThesaurusProperties;
|
|
|
|
|
import eu.eudat.event.DescriptionTouchedEvent;
|
|
|
|
|
import eu.eudat.event.EventBroker;
|
|
|
|
|
import eu.eudat.model.Description;
|
|
|
|
|
import eu.eudat.model.DescriptionReference;
|
|
|
|
|
import eu.eudat.model.*;
|
|
|
|
|
import eu.eudat.model.builder.DescriptionBuilder;
|
|
|
|
|
import eu.eudat.model.deleter.DescriptionDeleter;
|
|
|
|
|
import eu.eudat.model.deleter.DescriptionReferenceDeleter;
|
|
|
|
@ -54,9 +53,7 @@ import org.springframework.stereotype.Service;
|
|
|
|
|
import javax.management.InvalidApplicationException;
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.time.Instant;
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.UUID;
|
|
|
|
|
import java.util.*;
|
|
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
|
|
|
|
@Service
|
|
|
|
@ -130,32 +127,104 @@ public class DescriptionServiceImpl implements DescriptionService {
|
|
|
|
|
data = this.entityManager.find(DescriptionEntity.class, model.getId());
|
|
|
|
|
if (data == null) throw new MyNotFoundException(messageSource.getMessage("General_ItemNotFound", new Object[]{model.getId(), Description.class.getSimpleName()}, LocaleContextHolder.getLocale()));
|
|
|
|
|
if (!this.conventionService.hashValue(data.getUpdatedAt()).equals(model.getHash())) throw new MyValidationException(this.errors.getHashConflict().getCode(), this.errors.getHashConflict().getMessage());
|
|
|
|
|
if (data.getStatus() == DescriptionStatus.Finalized) throw new MyForbiddenException("Can not update finalized template");
|
|
|
|
|
if (data.getStatus().equals(DescriptionStatus.Finalized)) throw new MyValidationException(this.errors.getDescriptionIsFinalized().getCode(), this.errors.getDescriptionIsFinalized().getMessage());
|
|
|
|
|
if (!data.getDmpId().equals(model.getDmpId())) throw new MyValidationException(this.errors.getDmpCanNotChange().getCode(), this.errors.getDmpCanNotChange().getMessage());
|
|
|
|
|
if (!data.getDmpDescriptionTemplateId().equals(model.getDmpDescriptionTemplateId())) throw new MyValidationException(this.errors.getDmpDescriptionTemplateCanNotChange().getCode(), this.errors.getDmpDescriptionTemplateCanNotChange().getMessage());
|
|
|
|
|
} else {
|
|
|
|
|
data = new DescriptionEntity();
|
|
|
|
|
data.setId(UUID.randomUUID());
|
|
|
|
|
data.setIsActive(IsActive.Active);
|
|
|
|
|
data.setCreatedAt(Instant.now());
|
|
|
|
|
data.setDmpId(model.getDmpId());
|
|
|
|
|
data.setDmpDescriptionTemplateId(model.getDmpDescriptionTemplateId());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DmpDescriptionTemplateEntity dmpDescriptionTemplate = this.entityManager.find(DmpDescriptionTemplateEntity.class, data.getDescriptionTemplateId());
|
|
|
|
|
if (dmpDescriptionTemplate == null) throw new MyNotFoundException(messageSource.getMessage("General_ItemNotFound", new Object[]{data.getDescriptionTemplateId(), DmpDescriptionTemplate.class.getSimpleName()}, LocaleContextHolder.getLocale()));
|
|
|
|
|
|
|
|
|
|
DescriptionTemplateEntity descriptionTemplateEntity = this.entityManager.find(DescriptionTemplateEntity.class, model.getDescriptionTemplateId());
|
|
|
|
|
if (descriptionTemplateEntity == null) throw new MyNotFoundException(messageSource.getMessage("General_ItemNotFound", new Object[]{model.getDescriptionTemplateId(), DescriptionTemplate.class.getSimpleName()}, LocaleContextHolder.getLocale()));
|
|
|
|
|
|
|
|
|
|
if (!dmpDescriptionTemplate.getDescriptionTemplateGroupId().equals(descriptionTemplateEntity.getGroupId())) throw new MyValidationException(this.errors.getInvalidDescriptionTemplate().getCode(), this.errors.getInvalidDescriptionTemplate().getMessage());
|
|
|
|
|
|
|
|
|
|
DmpEntity dmp = this.entityManager.find(DmpEntity.class, data.getDmpId());
|
|
|
|
|
if (dmp == null) throw new MyNotFoundException(messageSource.getMessage("General_ItemNotFound", new Object[]{data.getDmpId(), Dmp.class.getSimpleName()}, LocaleContextHolder.getLocale()));
|
|
|
|
|
|
|
|
|
|
if (dmp.getStatus().equals(DmpStatus.Finalized) && isUpdate) throw new MyValidationException(this.errors.getDmpIsFinalized().getCode(), this.errors.getDmpIsFinalized().getMessage());
|
|
|
|
|
|
|
|
|
|
data.setLabel(model.getLabel());
|
|
|
|
|
data.setProperties(this.jsonHandlingService.toJson(this.buildPropertyDefinitionEntity(model.getProperties())));
|
|
|
|
|
data.setStatus(model.getStatus());
|
|
|
|
|
data.setDescription(model.getDescription());
|
|
|
|
|
data.setDescriptionTemplateId(model.getDescriptionTemplateId());
|
|
|
|
|
data.setDmpDescriptionTemplateId(model.getDmpDescriptionTemplateId());
|
|
|
|
|
data.setUpdatedAt(Instant.now());
|
|
|
|
|
if (isUpdate) this.entityManager.merge(data);
|
|
|
|
|
else this.entityManager.persist(data);
|
|
|
|
|
|
|
|
|
|
this.persistTags(data.getId(), model.getTags());
|
|
|
|
|
this.persistDescriptionReferences(data.getId(), model.getReferences());
|
|
|
|
|
|
|
|
|
|
this.entityManager.flush();
|
|
|
|
|
|
|
|
|
|
if (isUpdate){
|
|
|
|
|
if (!data.getStatus() .equals(DescriptionStatus.Finalized)) {
|
|
|
|
|
//TODO
|
|
|
|
|
//this.sendNotification(dataset1, dataset1.getDmp(), userInfo, NotificationType.DATASET_MODIFIED);
|
|
|
|
|
} else {
|
|
|
|
|
//TODO
|
|
|
|
|
//this.sendNotification(dataset1, dataset1.getDmp(), userInfo, NotificationType.DATASET_MODIFIED_FINALISED);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//this.deleteOldFilesAndAddNew(datasetWizardModel, userInfo); //TODO
|
|
|
|
|
this.eventBroker.emit(new DescriptionTouchedEvent(data.getId()));
|
|
|
|
|
return this.builderFactory.builder(DescriptionBuilder.class).authorize(AuthorizationFlags.OwnerOrDmpAssociatedOrPermissionOrPublic).build(BaseFieldSet.build(fields, Description._id), data);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void descriptionForce(DescriptionEntity description) throws Exception {
|
|
|
|
|
List<String> datasetProfileValidators = new LinkedList<>();
|
|
|
|
|
DescriptionTemplateEntity descriptionTemplateEntity = this.entityManager.find(DescriptionTemplateEntity.class, description.getDescriptionTemplateId());
|
|
|
|
|
if (descriptionTemplateEntity == null) throw new MyNotFoundException(messageSource.getMessage("General_ItemNotFound", new Object[]{description.getDescriptionTemplateId(), DescriptionTemplate.class.getSimpleName()}, LocaleContextHolder.getLocale()));
|
|
|
|
|
eu.eudat.commons.types.descriptiontemplate.DefinitionEntity definition = this.xmlHandlingService.fromXml(eu.eudat.commons.types.descriptiontemplate.DefinitionEntity.class, descriptionTemplateEntity.getDefinition());
|
|
|
|
|
// DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
|
|
|
|
|
// DocumentBuilder builder = builderFactory.newDocumentBuilder();
|
|
|
|
|
// Document xmlDocument = builder.parse(new ByteArrayInputStream(profile.getDefinition().getBytes()));
|
|
|
|
|
//
|
|
|
|
|
// XPath xPath = XPathFactory.newInstance().newXPath();
|
|
|
|
|
// String expression = "//validation/@type[.=1]/ancestor::field/@id";
|
|
|
|
|
// NodeList nodeList = (NodeList) xPath.compile(expression).evaluate(xmlDocument, XPathConstants.NODESET);
|
|
|
|
|
//
|
|
|
|
|
// for (int i = 0; i < nodeList.getLength(); i++) {
|
|
|
|
|
// Node node = nodeList.item(i);
|
|
|
|
|
// datasetProfileValidators.add(node.getNodeValue());
|
|
|
|
|
// }
|
|
|
|
|
//
|
|
|
|
|
// expression = "//validation/@type[.=1]/ancestor::fieldSet";
|
|
|
|
|
// nodeList = (NodeList) xPath.compile(expression).evaluate(xmlDocument, XPathConstants.NODESET);
|
|
|
|
|
//
|
|
|
|
|
//
|
|
|
|
|
// JSONObject obj = new JSONObject(description.getProperties());
|
|
|
|
|
// VisibilityRuleService visibilityRuleService = new VisibilityRuleServiceImpl();
|
|
|
|
|
// visibilityRuleService.setProperties(obj.toMap());
|
|
|
|
|
//
|
|
|
|
|
// description.setProfile(profile);
|
|
|
|
|
// PagedDatasetProfile pagedDatasetProfile = this.getPagedProfile(new DatasetWizardModel(), description);
|
|
|
|
|
// visibilityRuleService.buildVisibilityContext(pagedDatasetProfile.getRules());
|
|
|
|
|
//
|
|
|
|
|
//
|
|
|
|
|
// String failedField = null;
|
|
|
|
|
//
|
|
|
|
|
// for (String validator : datasetProfileValidators) {
|
|
|
|
|
// if (obj.has(validator) && isNullOrEmpty(obj.getString(validator)) && isElementVisible(nodeList, validator, visibilityRuleService)) {
|
|
|
|
|
// //throw new Exception("Field value of " + validator + " must be filled.");
|
|
|
|
|
// failedField = validator;
|
|
|
|
|
// break;
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// return failedField;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public Description persistStatus(DescriptionStatusPersist model, FieldSet fields) {
|
|
|
|
|
logger.debug(new MapLogEntry("persisting data dmp").And("model", model).And("fields", fields));
|
|
|
|
@ -247,29 +316,34 @@ public class DescriptionServiceImpl implements DescriptionService {
|
|
|
|
|
this.deleterFactory.deleter(DescriptionTagDeleter.class).delete(toDelete);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void persistDescriptionReferences(UUID id, List<DescriptionReferencePersist> models) throws InvalidApplicationException {
|
|
|
|
|
if (models == null) models = new ArrayList<>();
|
|
|
|
|
private void persistDescriptionReferences(UUID id, List<DescriptionReferencePersist> persists) throws InvalidApplicationException {
|
|
|
|
|
if (persists == null) persists = new ArrayList<>();
|
|
|
|
|
|
|
|
|
|
List<DescriptionReferenceEntity> items = this.queryFactory.query(DescriptionReferenceQuery.class).isActive(IsActive.Active).descriptionIds(id).collect();
|
|
|
|
|
List<ReferenceEntity> existingReferences = this.queryFactory.query(ReferenceQuery.class).isActive(IsActive.Active).ids(models.stream().filter(x-> this.conventionService.isValidGuid(x.getReference().getId())).map(x-> x.getReference().getId()).distinct().toList()).collect();
|
|
|
|
|
List<DescriptionReferenceEntity> existingItems = this.queryFactory.query(DescriptionReferenceQuery.class).isActive(IsActive.Active).descriptionIds(id).collect();
|
|
|
|
|
Map<UUID, ReferenceEntity> existingReferencesMap = this.queryFactory.query(ReferenceQuery.class).isActive(IsActive.Active).ids(persists.stream().filter(x-> x.getReference().getId() != null).map(x-> x.getReference().getId()).toList()).collect()
|
|
|
|
|
.stream()
|
|
|
|
|
.collect(Collectors.toMap(ReferenceEntity::getId, x-> x));;
|
|
|
|
|
|
|
|
|
|
List<UUID> updatedCreatedIds = new ArrayList<>();
|
|
|
|
|
for (DescriptionReferencePersist model : models) {
|
|
|
|
|
Boolean isUpdate = this.conventionService.isValidGuid(model.getId());
|
|
|
|
|
for (DescriptionReferencePersist persist : persists) {
|
|
|
|
|
Boolean isUpdate = this.conventionService.isValidGuid(persist.getId());
|
|
|
|
|
|
|
|
|
|
DescriptionReferenceEntity data;
|
|
|
|
|
if (isUpdate) {
|
|
|
|
|
data = items.stream().filter(x -> x.getId().equals(model.getId())).findFirst().orElse(null);
|
|
|
|
|
if (data == null) throw new MyNotFoundException(messageSource.getMessage("General_ItemNotFound", new Object[]{model.getId(), DescriptionReference.class.getSimpleName()}, LocaleContextHolder.getLocale()));
|
|
|
|
|
if (!this.conventionService.hashValue(data.getUpdatedAt()).equals(model.getHash())) throw new MyValidationException(this.errors.getHashConflict().getCode(), this.errors.getHashConflict().getMessage());
|
|
|
|
|
if (!data.getReferenceId().equals(model.getReference().getId())) throw new MyForbiddenException("Can not change reference of DescriptionReferenceEntity");
|
|
|
|
|
data = this.entityManager.find(DescriptionReferenceEntity.class, persist.getId());
|
|
|
|
|
if (data == null) throw new MyNotFoundException(messageSource.getMessage("General_ItemNotFound", new Object[]{persist.getId(), DescriptionReference.class.getSimpleName()}, LocaleContextHolder.getLocale()));
|
|
|
|
|
if (!this.conventionService.hashValue(data.getUpdatedAt()).equals(persist.getHash())) throw new MyValidationException(this.errors.getHashConflict().getCode(), this.errors.getHashConflict().getMessage());
|
|
|
|
|
if (!data.getReferenceId().equals(persist.getReference().getId())) throw new MyValidationException("reference can not changed");
|
|
|
|
|
} else {
|
|
|
|
|
data = new DescriptionReferenceEntity();
|
|
|
|
|
data.setId(UUID.randomUUID());
|
|
|
|
|
data.setIsActive(IsActive.Active);
|
|
|
|
|
data.setDescriptionId(id);
|
|
|
|
|
data.setCreatedAt(Instant.now());
|
|
|
|
|
data.setIsActive(IsActive.Active);
|
|
|
|
|
}
|
|
|
|
|
ReferenceEntity referenceEntity = this.persisReference(existingReferences, model.getReference());
|
|
|
|
|
data.setReferenceId(referenceEntity.getId());
|
|
|
|
|
|
|
|
|
|
ReferenceEntity reference = this.persistReference(existingReferencesMap, persist.getReference());
|
|
|
|
|
data.setReferenceId(reference.getId());
|
|
|
|
|
data.setUpdatedAt(Instant.now());
|
|
|
|
|
|
|
|
|
|
if (isUpdate) this.entityManager.merge(data);
|
|
|
|
@ -277,32 +351,33 @@ public class DescriptionServiceImpl implements DescriptionService {
|
|
|
|
|
|
|
|
|
|
updatedCreatedIds.add(data.getId());
|
|
|
|
|
}
|
|
|
|
|
List<DescriptionReferenceEntity> toDelete = items.stream().filter(x-> updatedCreatedIds.stream().noneMatch(y-> y.equals(x.getId()))).collect(Collectors.toList());
|
|
|
|
|
List<DescriptionReferenceEntity> toDelete = existingItems.stream().filter(x-> updatedCreatedIds.stream().noneMatch(y-> y.equals(x.getId()))).collect(Collectors.toList());
|
|
|
|
|
|
|
|
|
|
this.deleterFactory.deleter(DescriptionReferenceDeleter.class).delete(toDelete);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private ReferenceEntity persisReference(List<ReferenceEntity> existingReferences, ReferencePersist model) throws InvalidApplicationException {
|
|
|
|
|
Boolean isUpdate = this.conventionService.isValidGuid(model.getId());
|
|
|
|
|
private ReferenceEntity persistReference(Map<UUID, ReferenceEntity> existingReferencesMap , ReferencePersist persist){
|
|
|
|
|
Boolean isUpdate = this.conventionService.isValidGuid(persist.getId());
|
|
|
|
|
|
|
|
|
|
ReferenceEntity data;
|
|
|
|
|
if (isUpdate) {
|
|
|
|
|
data = existingReferences.stream().filter(x -> x.getId().equals(model.getId())).findFirst().orElse(null);
|
|
|
|
|
if (data == null) throw new MyNotFoundException(messageSource.getMessage("General_ItemNotFound", new Object[]{model.getId(), DescriptionReference.class.getSimpleName()}, LocaleContextHolder.getLocale()));
|
|
|
|
|
if (!this.conventionService.hashValue(data.getUpdatedAt()).equals(model.getHash())) throw new MyValidationException(this.errors.getHashConflict().getCode(), this.errors.getHashConflict().getMessage());
|
|
|
|
|
data = this.entityManager.find(ReferenceEntity.class, persist.getId());
|
|
|
|
|
if (data == null) throw new MyNotFoundException(messageSource.getMessage("General_ItemNotFound", new Object[]{persist.getId(), Reference.class.getSimpleName()}, LocaleContextHolder.getLocale()));
|
|
|
|
|
if (!this.conventionService.hashValue(data.getUpdatedAt()).equals(persist.getHash())) throw new MyValidationException(this.errors.getHashConflict().getCode(), this.errors.getHashConflict().getMessage());
|
|
|
|
|
} else {
|
|
|
|
|
data = new ReferenceEntity();
|
|
|
|
|
data.setId(UUID.randomUUID());
|
|
|
|
|
data.setIsActive(IsActive.Active);
|
|
|
|
|
data.setCreatedAt(Instant.now());
|
|
|
|
|
data.setIsActive(IsActive.Active);
|
|
|
|
|
}
|
|
|
|
|
data.setDefinition(this.xmlHandlingService.toXmlSafe(this.buildDefinitionEntity(persist.getDefinition())));
|
|
|
|
|
data.setUpdatedAt(Instant.now());
|
|
|
|
|
data.setReference(persist.getReference());
|
|
|
|
|
data.setAbbreviation(persist.getAbbreviation());
|
|
|
|
|
data.setSource(persist.getSource());
|
|
|
|
|
data.setSourceType(persist.getSourceType());
|
|
|
|
|
data.setUpdatedAt(Instant.now());
|
|
|
|
|
|
|
|
|
|
data.setDefinition(this.xmlHandlingService.toXmlSafe(this.buildDefinitionEntity(model.getDefinition())));
|
|
|
|
|
data.setUpdatedAt(Instant.now());
|
|
|
|
|
data.setReference(model.getReference());
|
|
|
|
|
data.setAbbreviation(model.getAbbreviation());
|
|
|
|
|
data.setSource(model.getSource());
|
|
|
|
|
data.setSourceType(model.getSourceType());
|
|
|
|
|
data.setUpdatedAt(Instant.now());
|
|
|
|
|
|
|
|
|
|
if (isUpdate) this.entityManager.merge(data);
|
|
|
|
|
else this.entityManager.persist(data);
|
|
|
|
|