All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.molgenis.semanticmapper.mapping.model.EntityMapping Maven / Gradle / Ivy

package org.molgenis.semanticmapper.mapping.model;

import static org.molgenis.semanticmapper.mapping.model.AttributeMapping.AlgorithmState.MISSING_TARGET;

import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Stream;
import org.molgenis.data.meta.model.Attribute;
import org.molgenis.data.meta.model.EntityType;

public class EntityMapping {
  private String identifier;
  private final EntityType sourceEntityType;
  private final EntityType targetEntityType;
  private final Map attributeMappings;

  /** Creates a new empty EntityMapping with no {@link AttributeMapping}s. */
  public EntityMapping(EntityType source, EntityType target) {
    this.identifier = null;
    this.sourceEntityType = source;
    this.targetEntityType = target;
    this.attributeMappings = new LinkedHashMap<>();
  }

  public EntityMapping(
      String identifier,
      EntityType sourceEntityType,
      EntityType targetEntityType,
      List attributeMappings) {
    this.identifier = identifier;
    this.sourceEntityType = sourceEntityType;
    this.targetEntityType = targetEntityType;
    this.attributeMappings = new LinkedHashMap<>();
    for (AttributeMapping mapping : attributeMappings) {
      Attribute targetAttribute = mapping.getTargetAttribute();
      if (targetAttribute != null) {
        this.attributeMappings.put(mapping.getTargetAttribute().getName(), mapping);
      } else {
        mapping.setAlgorithmState(MISSING_TARGET);
        this.attributeMappings.put(mapping.getTargetAttributeName(), mapping);
      }
    }
  }

  public String getIdentifier() {
    return identifier;
  }

  public void setIdentifier(String identifier) {
    this.identifier = identifier;
  }

  public void removeIdentifiers() {
    identifier = null;
    attributeMappings.values().forEach(a -> a.setIdentifier(null));
  }

  public String getName() {
    if (sourceEntityType == null) {
      return null;
    }
    return sourceEntityType.getId();
  }

  public String getLabel() {
    if (sourceEntityType == null) {
      return null;
    }
    return sourceEntityType.getLabel();
  }

  public EntityType getSourceEntityType() {
    return sourceEntityType;
  }

  public EntityType getTargetEntityType() {
    return targetEntityType;
  }

  public Collection getAttributeMappings() {
    return attributeMappings.values();
  }

  public AttributeMapping getAttributeMapping(String name) {
    return attributeMappings.get(name);
  }

  public Stream getMissingTargetAttributeNames() {
    return attributeMappings.values().stream()
        .filter(attributeMapping -> attributeMapping.getAlgorithmState() == MISSING_TARGET)
        .map(AttributeMapping::getTargetAttributeName);
  }

  public void addAttributeMapping(AttributeMapping attributeMapping) {
    String targetAttributeName = attributeMapping.getTargetAttributeName();
    if (attributeMappings.containsKey(targetAttributeName)) {
      throw new IllegalStateException(
          "AttributeMapping already exists for target attribute " + targetAttributeName);
    }
    attributeMappings.put(targetAttributeName, attributeMapping);
  }

  /**
   * Adds a new empty attribute mapping to a target attribute
   *
   * @return the newly created attribute mapping.
   */
  public AttributeMapping addAttributeMapping(String targetAttributeName) {
    if (attributeMappings.containsKey(targetAttributeName)) {
      throw new IllegalStateException(
          "AttributeMapping already exists for target attribute " + targetAttributeName);
    }
    Attribute targetAttribute = targetEntityType.getAttribute(targetAttributeName);
    AttributeMapping attributeMapping = new AttributeMapping(targetAttribute);
    attributeMappings.put(targetAttributeName, attributeMapping);
    return attributeMapping;
  }

  @Override
  public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + ((attributeMappings == null) ? 0 : attributeMappings.hashCode());
    result = prime * result + ((identifier == null) ? 0 : identifier.hashCode());
    result = prime * result + ((sourceEntityType == null) ? 0 : sourceEntityType.hashCode());
    result = prime * result + ((targetEntityType == null) ? 0 : targetEntityType.hashCode());
    return result;
  }

  @Override
  public boolean equals(Object obj) {
    if (this == obj) return true;
    if (obj == null) return false;
    if (getClass() != obj.getClass()) return false;
    EntityMapping other = (EntityMapping) obj;
    if (attributeMappings == null) {
      if (other.attributeMappings != null) return false;
    } else if (!attributeMappings.equals(other.attributeMappings)) return false;
    if (identifier == null) {
      if (other.identifier != null) return false;
    } else if (!identifier.equals(other.identifier)) return false;
    if (sourceEntityType == null) {
      if (other.sourceEntityType != null) return false;
    } else if (!sourceEntityType.equals(other.sourceEntityType)) return false;
    if (targetEntityType == null) {
      if (other.targetEntityType != null) return false;
    } else if (!targetEntityType.equals(other.targetEntityType)) return false;
    return true;
  }

  @Override
  public String toString() {
    return "EntityMapping [identifier="
        + identifier
        + ", sourceEntityType="
        + sourceEntityType
        + ", targetEntityType="
        + targetEntityType
        + ", attributeMappings="
        + attributeMappings
        + "]";
  }

  public void deleteAttributeMapping(String attribute) {
    attributeMappings.remove(attribute);
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy