visibility changes
parent
2f283ae594
commit
48a310e561
@ -0,0 +1,32 @@
|
||||
package eu.eudat.commons.types.descriptiontemplate;
|
||||
|
||||
import jakarta.xml.bind.annotation.XmlAccessType;
|
||||
import jakarta.xml.bind.annotation.XmlAccessorType;
|
||||
import jakarta.xml.bind.annotation.XmlAttribute;
|
||||
|
||||
@XmlAccessorType(XmlAccessType.FIELD)
|
||||
public class ExternalIdentifierEntity {
|
||||
|
||||
@XmlAttribute(name="identifier")
|
||||
private String identifier;
|
||||
|
||||
@XmlAttribute(name="type")
|
||||
private String type;
|
||||
|
||||
public String getIdentifier() {
|
||||
return identifier;
|
||||
}
|
||||
|
||||
public void setIdentifier(String identifier) {
|
||||
this.identifier = identifier;
|
||||
}
|
||||
|
||||
public String getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
public void setType(String type) {
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,32 @@
|
||||
package eu.eudat.commons.types.descriptiontemplate.importexport;
|
||||
|
||||
import jakarta.xml.bind.annotation.XmlAccessType;
|
||||
import jakarta.xml.bind.annotation.XmlAccessorType;
|
||||
import jakarta.xml.bind.annotation.XmlAttribute;
|
||||
|
||||
@XmlAccessorType(XmlAccessType.FIELD)
|
||||
public class ExternalIdentifierImportExport {
|
||||
|
||||
@XmlAttribute(name="identifier")
|
||||
private String identifier;
|
||||
|
||||
@XmlAttribute(name="type")
|
||||
private String type;
|
||||
|
||||
public String getIdentifier() {
|
||||
return identifier;
|
||||
}
|
||||
|
||||
public void setIdentifier(String identifier) {
|
||||
this.identifier = identifier;
|
||||
}
|
||||
|
||||
public String getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
public void setType(String type) {
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
}
|
@ -1,55 +0,0 @@
|
||||
package eu.eudat.model.builder.commonmodels.descriptiontemplate;
|
||||
|
||||
import eu.eudat.authorization.AuthorizationFlags;
|
||||
import eu.eudat.commonmodels.models.descriptiotemplate.RuleModel;
|
||||
import eu.eudat.commons.types.descriptiontemplate.RuleEntity;
|
||||
import eu.eudat.convention.ConventionService;
|
||||
import eu.eudat.model.builder.commonmodels.BaseCommonModelBuilder;
|
||||
import eu.eudat.model.builder.commonmodels.CommonModelBuilderItemResponse;
|
||||
import gr.cite.tools.exception.MyApplicationException;
|
||||
import gr.cite.tools.logging.LoggerService;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
|
||||
import org.springframework.context.annotation.Scope;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.EnumSet;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
@Component
|
||||
@Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
|
||||
public class RuleCommonModelBuilder extends BaseCommonModelBuilder<RuleModel, RuleEntity> {
|
||||
private EnumSet<AuthorizationFlags> authorize = EnumSet.of(AuthorizationFlags.None);
|
||||
@Autowired
|
||||
public RuleCommonModelBuilder(
|
||||
ConventionService conventionService
|
||||
) {
|
||||
super(conventionService, new LoggerService(LoggerFactory.getLogger(RuleCommonModelBuilder.class)));
|
||||
}
|
||||
|
||||
public RuleCommonModelBuilder authorize(EnumSet<AuthorizationFlags> values) {
|
||||
this.authorize = values;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected List<CommonModelBuilderItemResponse<RuleModel, RuleEntity>> buildInternal(List<RuleEntity> data) throws MyApplicationException {
|
||||
this.logger.debug("building for {}", Optional.ofNullable(data).map(List::size).orElse(0));
|
||||
if (data == null || data.isEmpty()) return new ArrayList<>();
|
||||
|
||||
List<CommonModelBuilderItemResponse<RuleModel, RuleEntity>> models = new ArrayList<>();
|
||||
for (RuleEntity d : data) {
|
||||
RuleModel m = new RuleModel();
|
||||
m.setTarget(d.getTarget());
|
||||
m.setValue(d.getValue());
|
||||
models.add(new CommonModelBuilderItemResponse<>(m, d));
|
||||
}
|
||||
|
||||
this.logger.debug("build {} items", Optional.of(models).map(List::size).orElse(0));
|
||||
|
||||
return models;
|
||||
}
|
||||
}
|
@ -0,0 +1,58 @@
|
||||
package eu.eudat.model.builder.descriptiontemplatedefinition;
|
||||
|
||||
import eu.eudat.authorization.AuthorizationFlags;
|
||||
import eu.eudat.commons.types.descriptiontemplate.ExternalIdentifierEntity;
|
||||
import eu.eudat.convention.ConventionService;
|
||||
import eu.eudat.model.builder.BaseBuilder;
|
||||
import eu.eudat.model.descriptiontemplatedefinition.ExternalIdentifier;
|
||||
import eu.eudat.service.fielddatahelper.FieldDataHelperServiceProvider;
|
||||
import gr.cite.tools.data.builder.BuilderFactory;
|
||||
import gr.cite.tools.exception.MyApplicationException;
|
||||
import gr.cite.tools.fieldset.FieldSet;
|
||||
import gr.cite.tools.logging.DataLogEntry;
|
||||
import gr.cite.tools.logging.LoggerService;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
|
||||
import org.springframework.context.annotation.Scope;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
@Component("descriptiontemplatedefinition.ExternalIdentifierBuilder")
|
||||
@Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
|
||||
public class ExternalIdentifierBuilder extends BaseBuilder<ExternalIdentifier, ExternalIdentifierEntity> {
|
||||
|
||||
private EnumSet<AuthorizationFlags> authorize = EnumSet.of(AuthorizationFlags.None);
|
||||
|
||||
@Autowired
|
||||
public ExternalIdentifierBuilder(
|
||||
ConventionService conventionService, BuilderFactory builderFactory, FieldDataHelperServiceProvider fieldDataHelperServiceProvider) {
|
||||
super(conventionService, new LoggerService(LoggerFactory.getLogger(ExternalIdentifierBuilder.class)));
|
||||
}
|
||||
|
||||
public ExternalIdentifierBuilder authorize(EnumSet<AuthorizationFlags> values) {
|
||||
this.authorize = values;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<ExternalIdentifier> build(FieldSet fields, List<ExternalIdentifierEntity> data) throws MyApplicationException {
|
||||
this.logger.debug("building for {} items requesting {} fields", Optional.ofNullable(data).map(List::size).orElse(0), Optional.ofNullable(fields).map(FieldSet::getFields).map(Set::size).orElse(0));
|
||||
this.logger.trace(new DataLogEntry("requested fields", fields));
|
||||
if (fields == null || data == null || fields.isEmpty())
|
||||
return new ArrayList<>();
|
||||
|
||||
|
||||
List<ExternalIdentifier> models = new ArrayList<>();
|
||||
for (ExternalIdentifierEntity d : data) {
|
||||
ExternalIdentifier m = new ExternalIdentifier();
|
||||
if (fields.hasField(this.asIndexer(ExternalIdentifier._identifier))) m.setIdentifier(d.getIdentifier());
|
||||
if (fields.hasField(this.asIndexer(ExternalIdentifier._type))) m.setType(d.getType());
|
||||
|
||||
models.add(m);
|
||||
}
|
||||
this.logger.debug("build {} items", Optional.of(models).map(List::size).orElse(0));
|
||||
return models;
|
||||
}
|
||||
}
|
@ -0,0 +1,27 @@
|
||||
package eu.eudat.model.descriptiontemplatedefinition;
|
||||
|
||||
public class ExternalIdentifier {
|
||||
|
||||
private String identifier;
|
||||
public static final String _identifier = "identifier";
|
||||
|
||||
private String type;
|
||||
public static final String _type = "type";
|
||||
|
||||
public String getIdentifier() {
|
||||
return identifier;
|
||||
}
|
||||
|
||||
public void setIdentifier(String identifier) {
|
||||
this.identifier = identifier;
|
||||
}
|
||||
|
||||
public String getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
public void setType(String type) {
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,68 @@
|
||||
package eu.eudat.model.persist.descriptiontemplatedefinition;
|
||||
|
||||
import eu.eudat.commons.validation.BaseValidator;
|
||||
import eu.eudat.convention.ConventionService;
|
||||
import eu.eudat.errorcode.ErrorThesaurusProperties;
|
||||
import gr.cite.tools.validation.specification.Specification;
|
||||
import org.springframework.context.MessageSource;
|
||||
import org.springframework.context.i18n.LocaleContextHolder;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
public class ExternalIdentifierPersist {
|
||||
|
||||
private String identifier;
|
||||
public static final String _identifier = "identifier";
|
||||
|
||||
private String type;
|
||||
public static final String _type = "type";
|
||||
|
||||
public String getIdentifier() {
|
||||
return identifier;
|
||||
}
|
||||
|
||||
public void setIdentifier(String identifier) {
|
||||
this.identifier = identifier;
|
||||
}
|
||||
|
||||
public String getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
public void setType(String type) {
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
@Component(PersistValidator.ValidatorName)
|
||||
public static class PersistValidator extends BaseValidator<ExternalIdentifierPersist> {
|
||||
|
||||
public static final String ValidatorName = "descriptiontemplatedefinition.ExternalIdentifierPersistPersistValidator";
|
||||
|
||||
private final MessageSource messageSource;
|
||||
|
||||
protected PersistValidator(ConventionService conventionService, ErrorThesaurusProperties errors, MessageSource messageSource) {
|
||||
super(conventionService, errors);
|
||||
this.messageSource = messageSource;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Class<ExternalIdentifierPersist> modelClass() {
|
||||
return ExternalIdentifierPersist.class;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected List<Specification> specifications(ExternalIdentifierPersist item) {
|
||||
return Arrays.asList(
|
||||
this.spec()
|
||||
.must(() -> !this.isEmpty(item.getIdentifier()))
|
||||
.failOn(ExternalIdentifierPersist._identifier).failWith(messageSource.getMessage("Validation_Required", new Object[]{ExternalIdentifierPersist._identifier}, LocaleContextHolder.getLocale())),
|
||||
this.spec()
|
||||
.must(() -> !this.isEmpty(item.getType()))
|
||||
.failOn(ExternalIdentifierPersist._type).failWith(messageSource.getMessage("Validation_Required", new Object[]{ExternalIdentifierPersist._type}, LocaleContextHolder.getLocale()))
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,27 @@
|
||||
package eu.eudat.service.visibility;
|
||||
|
||||
import eu.eudat.commons.types.description.ExternalIdentifierEntity;
|
||||
import eu.eudat.model.persist.descriptionproperties.ExternalIdentifierPersist;
|
||||
|
||||
public class ExternalIdentifier {
|
||||
private final String identifier;
|
||||
private final String type;
|
||||
|
||||
public String getIdentifier() {
|
||||
return identifier;
|
||||
}
|
||||
|
||||
public String getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
public ExternalIdentifier(ExternalIdentifierPersist persist){
|
||||
this.identifier = persist.getIdentifier();
|
||||
this.type = persist.getType();
|
||||
}
|
||||
|
||||
public ExternalIdentifier(ExternalIdentifierEntity entity){
|
||||
this.identifier = entity.getIdentifier();
|
||||
this.type = entity.getType();
|
||||
}
|
||||
}
|
@ -0,0 +1,63 @@
|
||||
package eu.eudat.service.visibility;
|
||||
|
||||
import eu.eudat.commons.types.description.FieldEntity;
|
||||
import eu.eudat.model.persist.descriptionproperties.FieldPersist;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.util.List;
|
||||
|
||||
public class Field {
|
||||
|
||||
private final String textValue;
|
||||
|
||||
private final List<String> textListValue;
|
||||
|
||||
private final Instant dateValue;
|
||||
|
||||
private final ExternalIdentifier externalIdentifier;
|
||||
|
||||
public String getTextValue() {
|
||||
return textValue;
|
||||
}
|
||||
|
||||
|
||||
public List<String> getTextListValue() {
|
||||
return textListValue;
|
||||
}
|
||||
|
||||
|
||||
public Instant getDateValue() {
|
||||
return dateValue;
|
||||
}
|
||||
|
||||
|
||||
public ExternalIdentifier getExternalIdentifier() {
|
||||
return externalIdentifier;
|
||||
}
|
||||
|
||||
|
||||
public Field(FieldPersist persist){
|
||||
List<String> tempTextListValue;
|
||||
this.textValue = persist.getTextValue();
|
||||
this.dateValue = persist.getDateValue();
|
||||
tempTextListValue = persist.getTextListValue();
|
||||
if (persist.getExternalIdentifier() != null) this.externalIdentifier = new ExternalIdentifier(persist.getExternalIdentifier());
|
||||
else this.externalIdentifier = null;
|
||||
if (persist.getReferences() != null && !persist.getReferences().isEmpty()){
|
||||
tempTextListValue = persist.getReferences().stream().filter(x-> x.getId() != null).map(x-> x.getId().toString()).toList();
|
||||
}
|
||||
this.textListValue = tempTextListValue;
|
||||
}
|
||||
|
||||
|
||||
public Field(FieldEntity entity){
|
||||
this.textValue = entity.getTextValue();
|
||||
this.dateValue = entity.getDateValue();
|
||||
this.textListValue = entity.getTextListValue();
|
||||
if (entity.getExternalIdentifier() != null) this.externalIdentifier = new ExternalIdentifier(entity.getExternalIdentifier());
|
||||
else this.externalIdentifier = null;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
@ -0,0 +1,40 @@
|
||||
package eu.eudat.service.visibility;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
public class FieldKey {
|
||||
private final String fieldId;
|
||||
private final Integer ordinal;
|
||||
private final int hashCode;
|
||||
|
||||
|
||||
public FieldKey(String fieldId, Integer ordinal) {
|
||||
this.fieldId = fieldId;
|
||||
this.ordinal = ordinal;
|
||||
hashCode = Objects.hash(this.fieldId, this.ordinal);
|
||||
}
|
||||
|
||||
public Integer getOrdinal() {
|
||||
return ordinal;
|
||||
}
|
||||
|
||||
public String getFieldId() {
|
||||
return fieldId;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o)
|
||||
return true;
|
||||
if (o == null || getClass() != o.getClass())
|
||||
return false;
|
||||
FieldKey that = (FieldKey) o;
|
||||
return Objects.equals(ordinal, that.getOrdinal()) && Objects.equals(fieldId, that.getFieldId());
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return this.hashCode;
|
||||
}
|
||||
}
|
@ -0,0 +1,34 @@
|
||||
package eu.eudat.service.visibility;
|
||||
|
||||
import eu.eudat.commons.types.description.PropertyDefinitionEntity;
|
||||
import eu.eudat.commons.types.description.PropertyDefinitionFieldSetEntity;
|
||||
import eu.eudat.model.persist.descriptionproperties.PropertyDefinitionFieldSetPersist;
|
||||
import eu.eudat.model.persist.descriptionproperties.PropertyDefinitionPersist;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class PropertyDefinition {
|
||||
|
||||
private final Map<String, PropertyDefinitionFieldSet> fieldSets;
|
||||
|
||||
public Map<String, PropertyDefinitionFieldSet> getFieldSets() {
|
||||
return fieldSets;
|
||||
}
|
||||
|
||||
public PropertyDefinition(PropertyDefinitionPersist persist){
|
||||
this.fieldSets = new HashMap<>();
|
||||
if (persist == null || persist.getFieldSets() == null || persist.getFieldSets().isEmpty()) return;
|
||||
for (Map.Entry<String, PropertyDefinitionFieldSetPersist> item : persist.getFieldSets().entrySet()){
|
||||
this.fieldSets.put(item.getKey(), new PropertyDefinitionFieldSet(item.getValue()));
|
||||
}
|
||||
}
|
||||
|
||||
public PropertyDefinition(PropertyDefinitionEntity entity){
|
||||
this.fieldSets = new HashMap<>();
|
||||
if (entity == null || entity.getFieldSets() == null || entity.getFieldSets().isEmpty()) return;
|
||||
for (Map.Entry<String, PropertyDefinitionFieldSetEntity> item : entity.getFieldSets().entrySet()){
|
||||
this.fieldSets.put(item.getKey(), new PropertyDefinitionFieldSet(item.getValue()));
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,42 @@
|
||||
package eu.eudat.service.visibility;
|
||||
|
||||
import eu.eudat.commons.types.description.PropertyDefinitionFieldSetEntity;
|
||||
import eu.eudat.commons.types.description.PropertyDefinitionFieldSetItemEntity;
|
||||
import eu.eudat.model.persist.descriptionproperties.PropertyDefinitionFieldSetItemPersist;
|
||||
import eu.eudat.model.persist.descriptionproperties.PropertyDefinitionFieldSetPersist;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class PropertyDefinitionFieldSet {
|
||||
|
||||
private final List<PropertyDefinitionFieldSetItem> items;
|
||||
|
||||
public List<PropertyDefinitionFieldSetItem> getItems() {
|
||||
return items;
|
||||
}
|
||||
|
||||
public PropertyDefinitionFieldSet(PropertyDefinitionFieldSetPersist persist){
|
||||
if (persist == null || persist.getItems() == null || persist.getItems().isEmpty()) {
|
||||
this.items = null;
|
||||
return;
|
||||
}
|
||||
this.items = new ArrayList<>();
|
||||
for (PropertyDefinitionFieldSetItemPersist item : persist.getItems()){
|
||||
this.items.add(new PropertyDefinitionFieldSetItem(item));
|
||||
}
|
||||
}
|
||||
|
||||
public PropertyDefinitionFieldSet(PropertyDefinitionFieldSetEntity entity){
|
||||
if (entity == null || entity.getItems() == null || entity.getItems().isEmpty()) {
|
||||
this.items = null;
|
||||
return;
|
||||
}
|
||||
this.items = new ArrayList<>();
|
||||
for (PropertyDefinitionFieldSetItemEntity item : entity.getItems()){
|
||||
this.items.add(new PropertyDefinitionFieldSetItem(item));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -0,0 +1,54 @@
|
||||
package eu.eudat.service.visibility;
|
||||
|
||||
import eu.eudat.commons.types.description.FieldEntity;
|
||||
import eu.eudat.commons.types.description.PropertyDefinitionFieldSetItemEntity;
|
||||
import eu.eudat.model.persist.descriptionproperties.FieldPersist;
|
||||
import eu.eudat.model.persist.descriptionproperties.PropertyDefinitionFieldSetItemPersist;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class PropertyDefinitionFieldSetItem {
|
||||
|
||||
private final Map<String, Field> fields;
|
||||
|
||||
private final Integer ordinal;
|
||||
|
||||
public Map<String, Field> getFields() {
|
||||
return fields;
|
||||
}
|
||||
|
||||
|
||||
public Integer getOrdinal() {
|
||||
return ordinal;
|
||||
}
|
||||
|
||||
|
||||
public PropertyDefinitionFieldSetItem(PropertyDefinitionFieldSetItemPersist persist){
|
||||
this.ordinal = persist.getOrdinal();
|
||||
|
||||
if (persist.getFields() == null || persist.getFields().isEmpty()) {
|
||||
this.fields = null;
|
||||
return;
|
||||
}
|
||||
this.fields = new HashMap<>();
|
||||
for (Map.Entry<String, FieldPersist> item : persist.getFields().entrySet()){
|
||||
this.fields.put(item.getKey(), new Field(item.getValue()));
|
||||
}
|
||||
}
|
||||
|
||||
public PropertyDefinitionFieldSetItem(PropertyDefinitionFieldSetItemEntity entity){
|
||||
this.ordinal = entity.getOrdinal();
|
||||
|
||||
if (entity.getFields() == null || entity.getFields().isEmpty()) {
|
||||
this.fields = null;
|
||||
return;
|
||||
}
|
||||
this.fields = new HashMap<>();
|
||||
for (Map.Entry<String, FieldEntity> item : entity.getFields().entrySet()){
|
||||
this.fields.put(item.getKey(), new Field(item.getValue()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -0,0 +1,49 @@
|
||||
package eu.eudat.service.visibility;
|
||||
|
||||
import eu.eudat.commons.types.descriptiontemplate.FieldEntity;
|
||||
import eu.eudat.commons.types.descriptiontemplate.RuleEntity;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.util.List;
|
||||
|
||||
public class RuleWithTarget{
|
||||
private final String target;
|
||||
private final String source;
|
||||
private final String textValue;
|
||||
private final List<String> textListValue;
|
||||
private final Instant dateValue;
|
||||
private final FieldEntity fieldEntity;
|
||||
|
||||
public RuleWithTarget(String source, RuleEntity rule, FieldEntity fieldEntity) {
|
||||
this.target = rule.getTarget();
|
||||
this.source = source;
|
||||
this.fieldEntity = fieldEntity;
|
||||
this.textValue = rule.getTextValue();
|
||||
this.textListValue = rule.getTextListValue();
|
||||
this.dateValue = rule.getDateValue();
|
||||
}
|
||||
|
||||
public String getTarget() {
|
||||
return target;
|
||||
}
|
||||
|
||||
public String getSource() {
|
||||
return source;
|
||||
}
|
||||
|
||||
public String getTextValue() {
|
||||
return textValue;
|
||||
}
|
||||
|
||||
public List<String> getTextListValue() {
|
||||
return textListValue;
|
||||
}
|
||||
|
||||
public Instant getDateValue() {
|
||||
return dateValue;
|
||||
}
|
||||
|
||||
public FieldEntity getFieldEntity() {
|
||||
return fieldEntity;
|
||||
}
|
||||
}
|
@ -0,0 +1,5 @@
|
||||
package eu.eudat.service.visibility;
|
||||
|
||||
public interface VisibilityService {
|
||||
boolean isVisible(String id, Integer ordinal);
|
||||
}
|
@ -0,0 +1,373 @@
|
||||
package eu.eudat.service.visibility;
|
||||
|
||||
import eu.eudat.commons.types.description.PropertyDefinitionEntity;
|
||||
import eu.eudat.commons.types.descriptiontemplate.*;
|
||||
import eu.eudat.model.persist.descriptionproperties.PropertyDefinitionPersist;
|
||||
|
||||
import javax.ws.rs.NotSupportedException;
|
||||
import java.util.*;
|
||||
|
||||
public class VisibilityServiceImpl implements VisibilityService {
|
||||
|
||||
private final DefinitionEntity definition;
|
||||
private final PropertyDefinition propertyDefinition;
|
||||
|
||||
private Map<String, List<RuleWithTarget>> rulesBySources;
|
||||
private Map<FieldKey, Boolean> visibility;
|
||||
|
||||
public VisibilityServiceImpl(DefinitionEntity definition, PropertyDefinitionPersist propertyDefinition) {
|
||||
this.definition = definition;
|
||||
this.propertyDefinition = new PropertyDefinition(propertyDefinition);
|
||||
}
|
||||
|
||||
public VisibilityServiceImpl(DefinitionEntity definition, PropertyDefinitionEntity propertyDefinition) {
|
||||
this.definition = definition;
|
||||
this.propertyDefinition = new PropertyDefinition(propertyDefinition);
|
||||
}
|
||||
|
||||
private void initRules(){
|
||||
if (this.rulesBySources == null) return;
|
||||
rulesBySources = new HashMap<>();
|
||||
for (FieldEntity fieldEntity : this.definition.getAllField()){
|
||||
if (fieldEntity.getVisibilityRules() != null && !fieldEntity.getVisibilityRules().isEmpty()) {
|
||||
for (RuleEntity rule : fieldEntity.getVisibilityRules()){
|
||||
if (!rulesBySources.containsKey(fieldEntity.getId())) rulesBySources.put(fieldEntity.getId(), new ArrayList<>());
|
||||
RuleWithTarget ruleWithTarget = new RuleWithTarget(fieldEntity.getId(), rule, fieldEntity);
|
||||
rulesBySources.get(fieldEntity.getId()).add(ruleWithTarget);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isVisible(String id, Integer ordinal) {
|
||||
this.calculateVisibility();
|
||||
FieldKey fieldKey = new FieldKey(id, ordinal);
|
||||
return this.visibility.getOrDefault(fieldKey, false);
|
||||
}
|
||||
|
||||
private void calculateVisibility(){
|
||||
if (visibility != null) return;
|
||||
|
||||
this.initRules();
|
||||
this.buildTargetVisibility();
|
||||
this.expandVisibilityToChildren();
|
||||
this.setDefaultVisibilityForNotCaclucted();
|
||||
this.hideParentIfAllChildrenAreHidden();
|
||||
}
|
||||
|
||||
private void buildTargetVisibility(){
|
||||
visibility = new HashMap<>();
|
||||
for (Map.Entry<String, List<RuleWithTarget>> ruleForSource : rulesBySources.entrySet()){
|
||||
for (RuleWithTarget rule : ruleForSource.getValue()){
|
||||
if (propertyDefinition.getFieldSets() != null && !propertyDefinition.getFieldSets().isEmpty()){
|
||||
for (PropertyDefinitionFieldSet propertyDefinitionFieldSet: propertyDefinition.getFieldSets().values()) {
|
||||
if (propertyDefinitionFieldSet.getItems() != null && !propertyDefinitionFieldSet.getItems().isEmpty()) {
|
||||
for (PropertyDefinitionFieldSetItem definitionFieldSetItem : propertyDefinitionFieldSet.getItems()) {
|
||||
if (definitionFieldSetItem.getFields() != null && !definitionFieldSetItem.getFields().isEmpty()) {
|
||||
for (String key : definitionFieldSetItem.getFields().keySet()) {
|
||||
if (rule.getSource().equals(key)){
|
||||
Field field = definitionFieldSetItem.getFields().get(key);
|
||||
|
||||
if (definitionFieldSetItem.getFields().containsKey(rule.getTarget())){ //Rule applies only for current multiple item
|
||||
FieldKey fieldKey = new FieldKey(rule.getTarget(), definitionFieldSetItem.getOrdinal());
|
||||
boolean currentState = this.visibility.getOrDefault(fieldKey, false);
|
||||
this.visibility.put(fieldKey, currentState || ruleIsTrue(rule, field));
|
||||
} else if (!this.definition.getFieldById(rule.getTarget()).isEmpty() || !this.definition.getFieldSetById(rule.getTarget()).isEmpty()) { //Rule applies to different fieldset, so we apply for all multiple items
|
||||
List<Integer> ordinals = this.getKeyOrdinals(rule.getTarget());
|
||||
for (Integer ordinal : ordinals){
|
||||
FieldKey fieldKey = new FieldKey(rule.getTarget(), ordinal);
|
||||
boolean currentState = this.visibility.getOrDefault(fieldKey, false);
|
||||
this.visibility.put(fieldKey, currentState || ruleIsTrue(rule, field));
|
||||
}
|
||||
} else {
|
||||
FieldKey fieldKey = new FieldKey(rule.getTarget(), null); //Ordinal is null if target not on field
|
||||
boolean currentState = this.visibility.getOrDefault(fieldKey, false);
|
||||
this.visibility.put(fieldKey, currentState || ruleIsTrue(rule, field));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private List<Integer> getKeyOrdinals(String key){
|
||||
if (propertyDefinition.getFieldSets() != null && !propertyDefinition.getFieldSets().isEmpty()){
|
||||
for (Map.Entry<String, PropertyDefinitionFieldSet> propertyDefinitionFieldSet: propertyDefinition.getFieldSets().entrySet()) {
|
||||
if (propertyDefinitionFieldSet.getKey().equals(key)) return propertyDefinitionFieldSet.getValue().getItems().stream().map(PropertyDefinitionFieldSetItem::getOrdinal).toList();
|
||||
|
||||
if (propertyDefinitionFieldSet.getValue() != null && propertyDefinitionFieldSet.getValue().getItems() != null && !propertyDefinitionFieldSet.getValue().getItems().isEmpty()) {
|
||||
for (PropertyDefinitionFieldSetItem definitionFieldSetItem : propertyDefinitionFieldSet.getValue().getItems()) {
|
||||
if (definitionFieldSetItem.getFields() != null && !definitionFieldSetItem.getFields().isEmpty()) {
|
||||
for (String fieldKey : definitionFieldSetItem.getFields().keySet()) {
|
||||
if (fieldKey.equals(key)) return propertyDefinitionFieldSet.getValue().getItems().stream().map(PropertyDefinitionFieldSetItem::getOrdinal).toList();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return new ArrayList<>();
|
||||
}
|
||||
|
||||
private void expandVisibilityToChildren(){
|
||||
if (this.definition.getPages() == null) return;
|
||||
for (PageEntity pageEntity : this.definition.getPages()){
|
||||
FieldKey fieldKey = new FieldKey(pageEntity.getId(), null);
|
||||
Boolean currentValue = this.visibility.getOrDefault(fieldKey, null);
|
||||
this.expandPageVisibility(pageEntity, currentValue);
|
||||
}
|
||||
}
|
||||
|
||||
private void expandPageVisibility(PageEntity pageEntity, Boolean parentVisibility){
|
||||
if (pageEntity.getSections() == null) return;
|
||||
for (SectionEntity sectionEntity : pageEntity.getSections()){
|
||||
FieldKey fieldKey = new FieldKey(sectionEntity.getId(), null);
|
||||
Boolean currentValue = this.visibility.getOrDefault(fieldKey, null);
|
||||
if (currentValue != null){
|
||||
if (parentVisibility != null && !parentVisibility) { //Parent is hidden so all childs should be hidden
|
||||
this.visibility.put(fieldKey, false);
|
||||
this.expandSectionVisibility(sectionEntity, currentValue);
|
||||
} else {
|
||||
this.expandSectionVisibility(sectionEntity, currentValue);
|
||||
}
|
||||
} else {
|
||||
if (parentVisibility != null) this.visibility.put(fieldKey, parentVisibility);
|
||||
this.expandSectionVisibility(sectionEntity, parentVisibility);
|
||||
}
|
||||
}
|
||||
}
|
||||
private void expandSectionVisibility(SectionEntity sectionEntity, Boolean parentVisibility){
|
||||
if (sectionEntity.getSections() != null) {
|
||||
for (SectionEntity subSectionEntity : sectionEntity.getSections()) {
|
||||
FieldKey fieldKey = new FieldKey(subSectionEntity.getId(), null);
|
||||
Boolean currentValue = this.visibility.getOrDefault(fieldKey, null);
|
||||
if (currentValue != null){
|
||||
if (parentVisibility != null && !parentVisibility) { //Parent is hidden so all childs should be hidden
|
||||
this.visibility.put(fieldKey, false);
|
||||
this.expandSectionVisibility(subSectionEntity, currentValue);
|
||||
} else {
|
||||
this.expandSectionVisibility(subSectionEntity, currentValue);
|
||||
}
|
||||
} else {
|
||||
if (parentVisibility != null) this.visibility.put(fieldKey, parentVisibility);
|
||||
this.expandSectionVisibility(subSectionEntity, parentVisibility);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (sectionEntity.getFieldSets() != null) {
|
||||
for (FieldSetEntity fieldSetEntity : sectionEntity.getFieldSets()) {
|
||||
if (propertyDefinition.getFieldSets() != null && !propertyDefinition.getFieldSets().isEmpty()){
|
||||
PropertyDefinitionFieldSet propertyDefinitionFieldSet = propertyDefinition.getFieldSets().getOrDefault(fieldSetEntity.getId(), null);
|
||||
if (propertyDefinitionFieldSet != null && propertyDefinitionFieldSet.getItems() != null && !propertyDefinitionFieldSet.getItems().isEmpty()) {
|
||||
for (PropertyDefinitionFieldSetItem definitionFieldSetItem : propertyDefinitionFieldSet.getItems()) {
|
||||
FieldKey fieldKey = new FieldKey(fieldSetEntity.getId(), definitionFieldSetItem.getOrdinal());
|
||||
Boolean currentValue = this.visibility.getOrDefault(fieldKey, null);
|
||||
if (currentValue != null){
|
||||
if (parentVisibility != null && !parentVisibility) { //Parent is hidden so all childs should be hidden
|
||||
this.visibility.put(fieldKey, false);
|
||||
this.expandFieldSetVisibility(fieldSetEntity, currentValue, definitionFieldSetItem.getOrdinal());
|
||||
} else {
|
||||
this.expandFieldSetVisibility(fieldSetEntity, currentValue, definitionFieldSetItem.getOrdinal());
|
||||
}
|
||||
} else {
|
||||
if (parentVisibility != null) this.visibility.put(fieldKey, parentVisibility);
|
||||
this.expandFieldSetVisibility(fieldSetEntity, parentVisibility, definitionFieldSetItem.getOrdinal());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void expandFieldSetVisibility(FieldSetEntity fieldSetEntity, Boolean parentVisibility, Integer ordinal){
|
||||
if (fieldSetEntity.getFields() != null) {
|
||||
for (FieldEntity fieldEntity : fieldSetEntity.getFields()) {
|
||||
FieldKey fieldKey = new FieldKey(fieldEntity.getId(), ordinal);
|
||||
Boolean currentValue = this.visibility.getOrDefault(fieldKey, null);
|
||||
if (currentValue != null){
|
||||
if (parentVisibility != null && !parentVisibility) { //Parent is hidden so all childs should be hidden
|
||||
this.visibility.put(fieldKey, false);
|
||||
}
|
||||
} else if (parentVisibility != null){
|
||||
this.visibility.put(fieldKey, parentVisibility);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private void setDefaultVisibilityForNotCaclucted(){
|
||||
if (this.definition.getPages() == null) return;
|
||||
for (PageEntity pageEntity : this.definition.getPages()){
|
||||
FieldKey fieldKey = new FieldKey(pageEntity.getId(), null);
|
||||
Boolean currentValue = this.visibility.getOrDefault(fieldKey, null);
|
||||
if (currentValue == null) this.visibility.put(fieldKey, true);
|
||||
this.setDefaultPageVisibility(pageEntity);
|
||||
}
|
||||
}
|
||||
|
||||
private void setDefaultPageVisibility(PageEntity pageEntity){
|
||||
if (pageEntity.getSections() == null) return;
|
||||
for (SectionEntity sectionEntity : pageEntity.getSections()){
|
||||
FieldKey fieldKey = new FieldKey(sectionEntity.getId(), null);
|
||||
Boolean currentValue = this.visibility.getOrDefault(fieldKey, null);
|
||||
if (currentValue == null){
|
||||
this.visibility.put(fieldKey, true);
|
||||
this.setDefaultSectionVisibility(sectionEntity);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void setDefaultSectionVisibility(SectionEntity sectionEntity){
|
||||
if (sectionEntity.getSections() != null) {
|
||||
for (SectionEntity subSectionEntity : sectionEntity.getSections()) {
|
||||
FieldKey fieldKey = new FieldKey(subSectionEntity.getId(), null);
|
||||
Boolean currentValue = this.visibility.getOrDefault(fieldKey, null);
|
||||
if (currentValue == null){
|
||||
this.visibility.put(fieldKey, true);
|
||||
this.setDefaultSectionVisibility(sectionEntity);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (sectionEntity.getFieldSets() != null) {
|
||||
for (FieldSetEntity fieldSetEntity : sectionEntity.getFieldSets()) {
|
||||
if (propertyDefinition.getFieldSets() != null && !propertyDefinition.getFieldSets().isEmpty()){
|
||||
PropertyDefinitionFieldSet propertyDefinitionFieldSet = propertyDefinition.getFieldSets().getOrDefault(fieldSetEntity.getId(), null);
|
||||
if (propertyDefinitionFieldSet != null && propertyDefinitionFieldSet.getItems() != null && !propertyDefinitionFieldSet.getItems().isEmpty()) {
|
||||
for (PropertyDefinitionFieldSetItem definitionFieldSetItem : propertyDefinitionFieldSet.getItems()) {
|
||||
FieldKey fieldKey = new FieldKey(fieldSetEntity.getId(), definitionFieldSetItem.getOrdinal());
|
||||
Boolean currentValue = this.visibility.getOrDefault(fieldKey, null);
|
||||
if (currentValue == null){
|
||||
this.visibility.put(fieldKey, true);
|
||||
this.setDefaultFieldSetVisibility(fieldSetEntity, definitionFieldSetItem.getOrdinal());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void setDefaultFieldSetVisibility(FieldSetEntity fieldSetEntity, Integer ordinal){
|
||||
if (fieldSetEntity.getFields() != null) {
|
||||
for (FieldEntity fieldEntity : fieldSetEntity.getFields()) {
|
||||
FieldKey fieldKey = new FieldKey(fieldEntity.getId(), ordinal);
|
||||
Boolean currentValue = this.visibility.getOrDefault(fieldKey, null);
|
||||
if (currentValue == null){
|
||||
this.visibility.put(fieldKey, true);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void hideParentIfAllChildrenAreHidden(){
|
||||
if (this.definition.getPages() == null) return;
|
||||
for (PageEntity pageEntity : this.definition.getPages()){
|
||||
FieldKey fieldKey = new FieldKey(pageEntity.getId(), null);
|
||||
boolean isCurrentHidden = this.isHiddenPageVisibilityIfAllChildrenIsHidden(pageEntity);
|
||||
if (this.visibility.getOrDefault(fieldKey, true) && isCurrentHidden){
|
||||
this.visibility.put(fieldKey, false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private boolean isHiddenPageVisibilityIfAllChildrenIsHidden(PageEntity pageEntity){
|
||||
boolean isHidden = true;
|
||||
if (pageEntity.getSections() == null) return isHidden;
|
||||
|
||||
for (SectionEntity sectionEntity : pageEntity.getSections()){
|
||||
FieldKey fieldKey = new FieldKey(sectionEntity.getId(), null);
|
||||
boolean isCurrentHidden = this.isHiddenSectionIfAllChildrenIsHidden(sectionEntity);
|
||||
if (this.visibility.getOrDefault(fieldKey, true) && isCurrentHidden){
|
||||
this.visibility.put(fieldKey, false);
|
||||
}
|
||||
isHidden = isHidden && isCurrentHidden;
|
||||
}
|
||||
return isHidden;
|
||||
}
|
||||
|
||||
private boolean isHiddenSectionIfAllChildrenIsHidden(SectionEntity sectionEntity){
|
||||
boolean isHidden = true;
|
||||
if (sectionEntity.getSections() != null) {
|
||||
for (SectionEntity subSectionEntity : sectionEntity.getSections()) {
|
||||
FieldKey fieldKey = new FieldKey(subSectionEntity.getId(), null);
|
||||
boolean isCurrentHidden = this.isHiddenSectionIfAllChildrenIsHidden(subSectionEntity);
|
||||
if (this.visibility.getOrDefault(fieldKey, true) && isCurrentHidden){
|
||||
this.visibility.put(fieldKey, false);
|
||||
}
|
||||
isHidden = isHidden && isCurrentHidden;
|
||||
}
|
||||
}
|
||||
if (sectionEntity.getFieldSets() != null) {
|
||||
for (FieldSetEntity fieldSetEntity : sectionEntity.getFieldSets()) {
|
||||
if (propertyDefinition.getFieldSets() != null && !propertyDefinition.getFieldSets().isEmpty()){
|
||||
PropertyDefinitionFieldSet propertyDefinitionFieldSet = propertyDefinition.getFieldSets().getOrDefault(fieldSetEntity.getId(), null);
|
||||
if (propertyDefinitionFieldSet != null && propertyDefinitionFieldSet.getItems() != null && !propertyDefinitionFieldSet.getItems().isEmpty()) {
|
||||
for (PropertyDefinitionFieldSetItem definitionFieldSetItem : propertyDefinitionFieldSet.getItems()) {
|
||||
FieldKey fieldKey = new FieldKey(fieldSetEntity.getId(), definitionFieldSetItem.getOrdinal());
|
||||
boolean isCurrentHidden = this.isHiddenFieldSetIfAllChildrenIsHidden(fieldSetEntity, definitionFieldSetItem.getOrdinal());
|
||||
if (this.visibility.getOrDefault(fieldKey, true) && isCurrentHidden){
|
||||
this.visibility.put(fieldKey, false);
|
||||
}
|
||||
isHidden = isHidden && isCurrentHidden;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return isHidden;
|
||||
}
|
||||
|
||||
private boolean isHiddenFieldSetIfAllChildrenIsHidden(FieldSetEntity fieldSetEntity, Integer ordinal){
|
||||
boolean isHidden = true;
|
||||
if (fieldSetEntity.getFields() != null) {
|
||||
for (FieldEntity fieldEntity : fieldSetEntity.getFields()) {
|
||||
FieldKey fieldKey = new FieldKey(fieldEntity.getId(), ordinal);
|
||||
Boolean currentValue = this.visibility.getOrDefault(fieldKey, true);
|
||||
isHidden = isHidden && !currentValue;
|
||||
}
|
||||
return isHidden;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private boolean ruleIsTrue(RuleWithTarget rule, Field field){
|
||||
if (field != null){
|
||||
eu.eudat.commons.enums.FieldType fieldType = rule.getFieldEntity() != null && rule.getFieldEntity().getData() != null ? rule.getFieldEntity().getData().getFieldType() : eu.eudat.commons.enums.FieldType.FREE_TEXT;
|
||||
if (eu.eudat.commons.enums.FieldType.isTextType(fieldType) && field.getTextValue() != null && !field.getTextValue().isBlank()) {
|
||||
if (eu.eudat.commons.enums.FieldType.UPLOAD.equals(fieldType)){
|
||||
throw new NotSupportedException("Upload file rule not supported");
|
||||
} else {
|
||||
return field.getTextValue().equals(rule.getTextValue());
|
||||
}
|
||||
}
|
||||
else if (eu.eudat.commons.enums.FieldType.isTextListType(fieldType) && field.getTextListValue() != null && !field.getTextListValue().isEmpty()) {
|
||||
return rule.getTextListValue() != null &&
|
||||
new HashSet<>(field.getTextListValue()).containsAll(rule.getTextListValue()) &&
|
||||
new HashSet<>(rule.getTextListValue()).containsAll(field.getTextListValue());
|
||||
}
|
||||
else if (eu.eudat.commons.enums.FieldType.isReferenceType(fieldType)) {
|
||||
return rule.getTextListValue() != null &&
|
||||
new HashSet<>(field.getTextListValue()).containsAll(rule.getTextListValue()) &&
|
||||
new HashSet<>(rule.getTextListValue()).containsAll(field.getTextListValue());
|
||||
}
|
||||
else if (eu.eudat.commons.enums.FieldType.isDateType(fieldType) && field.getDateValue() != null) return field.getDateValue().equals(rule.getDateValue());
|
||||
else if (eu.eudat.commons.enums.FieldType.isExternalIdentifierType(fieldType) && field.getExternalIdentifier() != null) {
|
||||
throw new NotSupportedException("External identifier rule not supported");
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue