This commit is contained in:
Efstratios Giannopoulos 2024-05-28 11:00:05 +03:00
parent a26233b8c2
commit 578d705412
4 changed files with 40 additions and 13 deletions

View File

@ -1,14 +1,12 @@
package org.opencdmp.authorization.authorizationcontentresolver;
import gr.cite.tools.cache.CacheService;
import org.opencdmp.authorization.AffiliatedResource;
import org.opencdmp.convention.ConventionService;
import gr.cite.tools.cache.CacheService;
import gr.cite.tools.exception.MyApplicationException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.UUID;
@ -35,7 +33,7 @@ public class AffiliationCacheService extends CacheService<AffiliationCacheServic
private AffiliatedResource affiliatedResource;
public UUID getUserId() {
return userId;
return this.userId;
}
public void setUserId(UUID userId) {
@ -43,7 +41,7 @@ public class AffiliationCacheService extends CacheService<AffiliationCacheServic
}
public UUID getTenantId() {
return tenantId;
return this.tenantId;
}
public void setTenantId(UUID tenantId) {
@ -51,7 +49,7 @@ public class AffiliationCacheService extends CacheService<AffiliationCacheServic
}
public UUID getEntityId() {
return entityId;
return this.entityId;
}
public void setEntityId(UUID entityId) {
@ -59,7 +57,7 @@ public class AffiliationCacheService extends CacheService<AffiliationCacheServic
}
public String getEntityType() {
return entityType;
return this.entityType;
}
public void setEntityType(String entityType) {
@ -67,7 +65,7 @@ public class AffiliationCacheService extends CacheService<AffiliationCacheServic
}
public AffiliatedResource getAffiliatedResource() {
return affiliatedResource;
return this.affiliatedResource;
}
public void setAffiliatedResource(AffiliatedResource affiliatedResource) {

View File

@ -18,7 +18,9 @@ import org.opencdmp.commons.types.description.FieldEntity;
import org.opencdmp.convention.ConventionService;
import org.opencdmp.data.ReferenceEntity;
import org.opencdmp.data.StorageFileEntity;
import org.opencdmp.data.TagEntity;
import org.opencdmp.model.StorageFile;
import org.opencdmp.model.Tag;
import org.opencdmp.model.builder.commonmodels.BaseCommonModelBuilder;
import org.opencdmp.model.builder.commonmodels.CommonModelBuilderItemResponse;
import org.opencdmp.model.builder.commonmodels.reference.ReferenceCommonModelBuilder;
@ -26,6 +28,7 @@ import org.opencdmp.model.persist.StorageFilePersist;
import org.opencdmp.model.reference.Reference;
import org.opencdmp.query.ReferenceQuery;
import org.opencdmp.query.StorageFileQuery;
import org.opencdmp.query.TagQuery;
import org.opencdmp.service.storage.StorageFileProperties;
import org.opencdmp.service.storage.StorageFileService;
import org.slf4j.LoggerFactory;
@ -87,6 +90,7 @@ public class FieldCommonModelBuilder extends BaseCommonModelBuilder<FieldModel,
FieldType fieldType = this.fieldEntity != null && this.fieldEntity.getData() != null ? this.fieldEntity.getData().getFieldType() : FieldType.FREE_TEXT;
Map<UUID, ReferenceModel> referenceItemsMap = FieldType.isReferenceType(fieldType) ? this.collectReferences(data) : null;
Map<UUID, String> tagItemsMap = FieldType.isTagType(fieldType) ? this.collectTags(data) : null;
List<CommonModelBuilderItemResponse<FieldModel, FieldEntity>> models = new ArrayList<>();
for (FieldEntity d : data) {
@ -95,13 +99,20 @@ public class FieldCommonModelBuilder extends BaseCommonModelBuilder<FieldModel,
if (FieldType.isDateType(fieldType)) m.setDateValue(d.getDateValue());
if (FieldType.isBooleanType(fieldType)) m.setBooleanValue(d.getBooleanValue());
if (FieldType.isTextType(fieldType)) m.setTextValue(d.getTextValue());
if (FieldType.isTagType(fieldType)) m.setTextListValue(d.getTextListValue());
if (FieldType.isTextListType(fieldType)) m.setTextListValue(d.getTextListValue());
if (FieldType.isReferenceType(fieldType) && referenceItemsMap != null && d.getTextListValue() != null && !d.getTextListValue().isEmpty()) {
m.setReferences(new ArrayList<>());
for (UUID referenceId : d.getTextListValue().stream().map(UUID::fromString).toList()){
for (UUID referenceId : d.getTextListValue().stream().map(UUID::fromString).distinct().toList()){
if (referenceItemsMap.containsKey(referenceId)) m.getReferences().add(referenceItemsMap.get(referenceId));
}
}
if (FieldType.isTagType(fieldType) && tagItemsMap != null && d.getTextListValue() != null && !d.getTextListValue().isEmpty()) {
m.setTextListValue(new ArrayList<>());
for (UUID tagId : d.getTextListValue().stream().map(UUID::fromString).distinct().toList()){
if (tagItemsMap.containsKey(tagId)) m.getTextListValue().add(tagItemsMap.get(tagId));
}
}
if (FieldType.UPLOAD.equals(fieldType) && d.getTextValue() != null && !d.getTextValue().isEmpty()) {
try {
@ -144,6 +155,21 @@ public class FieldCommonModelBuilder extends BaseCommonModelBuilder<FieldModel,
return persisted.getFileRef();
}
private Map<UUID, String> collectTags(List<FieldEntity> data){
if (data.isEmpty())
return null;
this.logger.debug("checking related - {}", TagEntity.class.getSimpleName());
List<UUID> tagIds = data.stream().map(FieldEntity::getTextListValue).filter(Objects::nonNull).flatMap(List::stream).filter(x-> !this.conventionService.isNullOrEmpty(x)).map(UUID::fromString).distinct().collect(Collectors.toList());
List<TagEntity> existingTags = this.queryFactory.query(TagQuery.class).authorize(AuthorizationFlags.OwnerOrDmpAssociatedOrPermission).disableTracking().ids(tagIds).collectAs(new BaseFieldSet().ensure(Tag._id).ensure(Tag._label));
Map<UUID, String> itemMap = new HashMap<>();
for (UUID tag : tagIds){
existingTags.stream().filter(x -> x.getId().equals(tag)).findFirst().ifPresent(existingTag -> itemMap.put(tag, existingTag.getId().toString()));
}
return itemMap;
}
private Map<UUID, ReferenceModel> collectReferences(List<FieldEntity> data) throws MyApplicationException {
if (data.isEmpty())
return null;
@ -151,7 +177,7 @@ public class FieldCommonModelBuilder extends BaseCommonModelBuilder<FieldModel,
Map<UUID, ReferenceModel> itemMap;
ReferenceQuery q = this.queryFactory.query(ReferenceQuery.class).disableTracking().authorize(this.authorize).ids(data.stream().filter(x-> x.getTextListValue() != null).map(FieldEntity::getTextListValue).flatMap(List::stream).filter(x-> !this.conventionService.isNullOrEmpty(x)).map(UUID::fromString).distinct().collect(Collectors.toList()));
ReferenceQuery q = this.queryFactory.query(ReferenceQuery.class).disableTracking().authorize(this.authorize).ids(data.stream().map(FieldEntity::getTextListValue).filter(Objects::nonNull).flatMap(List::stream).filter(x-> !this.conventionService.isNullOrEmpty(x)).map(UUID::fromString).distinct().collect(Collectors.toList()));
itemMap = this.builderFactory.builder(ReferenceCommonModelBuilder.class).authorize(this.authorize).asForeignKey(q, ReferenceEntity::getId);
return itemMap;

View File

@ -98,14 +98,14 @@ public class FieldBuilder extends BaseBuilder<Field, FieldEntity> {
}
if (!referenceFields.isEmpty() && FieldType.isReferenceType(fieldType) && referenceItemsMap != null && d.getTextListValue() != null && !d.getTextListValue().isEmpty()) {
m.setReferences(new ArrayList<>());
for (UUID referenceId : d.getTextListValue().stream().map(UUID::fromString).toList()){
for (UUID referenceId : d.getTextListValue().stream().map(UUID::fromString).distinct().toList()){
if (referenceItemsMap.containsKey(referenceId)) m.getReferences().add(referenceItemsMap.get(referenceId));
}
}
if (!tagFields.isEmpty() && FieldType.isTagType(fieldType) && tagsItemsMap != null && d.getTextListValue() != null && !d.getTextListValue().isEmpty()) {
m.setTags(new ArrayList<>());
for (UUID tagId : d.getTextListValue().stream().map(UUID::fromString).toList()){
for (UUID tagId : d.getTextListValue().stream().map(UUID::fromString).distinct().toList()){
if (tagsItemsMap.containsKey(tagId)) m.getTags().add(tagsItemsMap.get(tagId));
}
}

View File

@ -644,7 +644,10 @@ public class DescriptionServiceImpl implements DescriptionService {
fieldPersist.getReferences().add(fieldPersist.getReference());
}
if (!this.conventionService.isListNullOrEmpty(fieldPersist.getReferences())) {
List<UUID> usedReferences = new ArrayList<>();
for (ReferencePersist referencePersist : fieldPersist.getReferences()) {
if (usedReferences.contains(referencePersist.getId())) continue;
usedReferences.add(referencePersist.getId());
DescriptionReferencePersist descriptionReferencePersist = new DescriptionReferencePersist();
descriptionReferencePersist.setData(new DescriptionReferenceDataPersist());
descriptionReferencePersist.getData().setFieldId(fieldId);
@ -1053,7 +1056,7 @@ public class DescriptionServiceImpl implements DescriptionService {
}
}
else if (FieldType.isTagType(fieldType) && fieldEntity != null ) {
if (!this.conventionService.isListNullOrEmpty(data.getTextListValue()) && !this.conventionService.isListNullOrEmpty(references)){
if (!this.conventionService.isListNullOrEmpty(data.getTextListValue())){
List<UUID> tagIdsInField = data.getTextListValue().stream().filter(x -> this.conventionService.isValidGuid(UUID.fromString(x))).toList().stream().map(UUID::fromString).collect(Collectors.toList());
if (!this.conventionService.isListNullOrEmpty(tagIdsInField)){
List<TagEntity> tagsInField = this.queryFactory.query(TagQuery.class).isActive(IsActive.Active).ids(tagIdsInField).disableTracking().authorize(AuthorizationFlags.OwnerOrDmpAssociatedOrPermissionOrPublic).collect();