Search in sources :

Example 16 with Pair

use of eu.esdihumboldt.util.Pair in project hale by halestudio.

the class JacksonMapper method skipChoice.

/**
 * Handles skipping choice groups.
 *
 * @param propertyName the start property name
 * @param obj the object to inspect
 * @param reporter the reporter
 * @return a pair of property name and value to use
 */
private Pair<String, Object> skipChoice(String propertyName, Object obj, IOReporter reporter) {
    if (obj instanceof Group) {
        Group group = (Group) obj;
        // For choices search for the (only!) child and skip the choice.
        if (group.getDefinition() instanceof GroupPropertyDefinition) {
            if (((GroupPropertyDefinition) group.getDefinition()).getConstraint(ChoiceFlag.class).isEnabled()) {
                Iterator<QName> childPropertyNames = group.getPropertyNames().iterator();
                if (!childPropertyNames.hasNext()) {
                    reporter.warn(new IOMessageImpl("Found an empty choice.", null));
                    return null;
                }
                QName childPropertyName = childPropertyNames.next();
                Object[] values = group.getProperty(childPropertyName);
                Object value = values[0];
                if (childPropertyNames.hasNext() || values.length > 1)
                    reporter.warn(new IOMessageImpl("Found a choice with multiple children. Using first.", null));
                // delegate to only value
                return skipChoice(childPropertyName.getLocalPart(), value, reporter);
            }
        }
    }
    return new Pair<>(propertyName, obj);
}
Also used : GroupPropertyDefinition(eu.esdihumboldt.hale.common.schema.model.GroupPropertyDefinition) Group(eu.esdihumboldt.hale.common.instance.model.Group) QName(javax.xml.namespace.QName) IOMessageImpl(eu.esdihumboldt.hale.common.core.io.report.impl.IOMessageImpl) ChoiceFlag(eu.esdihumboldt.hale.common.schema.model.constraint.property.ChoiceFlag) Pair(eu.esdihumboldt.util.Pair)

Example 17 with Pair

use of eu.esdihumboldt.util.Pair in project hale by halestudio.

the class ShapeInstanceReader method getMostCompatibleShapeType.

/**
 * Determine the type out of the the mapping relevant types in the given
 * type index, that matches the given data type best.
 *
 * @param types the type index
 * @param dataType the Shapefile data type
 * @param preferredName the name of the preferred type
 * @return the most compatible type found together with is compatibility
 *         rating or <code>null</code> if there is no type that at least has
 *         one matching property
 *
 * @see #checkCompatibility(TypeDefinition, TypeDefinition)
 */
public static Pair<TypeDefinition, Integer> getMostCompatibleShapeType(TypeIndex types, TypeDefinition dataType, String preferredName) {
    int maxCompatibility = -1;
    TypeDefinition maxType = null;
    // check preferred name first
    TypeDefinition preferredType = types.getType(new QName(ShapefileConstants.SHAPEFILE_NS, preferredName));
    if (preferredType != null) {
        int comp = checkCompatibility(preferredType, dataType);
        if (comp >= 100) {
            // return an exact match directly
            return new Pair<TypeDefinition, Integer>(preferredType, 100);
        } else {
            maxType = preferredType;
            maxCompatibility = comp;
        }
    }
    for (TypeDefinition schemaType : types.getMappingRelevantTypes()) {
        if (ShapefileConstants.SHAPEFILE_NS.equals(schemaType.getName().getNamespaceURI())) {
            // is a shapefile type
            int comp = checkCompatibility(schemaType, dataType);
            if (comp >= 100) {
                // return an exact match directly
                return new Pair<TypeDefinition, Integer>(schemaType, 100);
            } else if (comp > maxCompatibility) {
                maxType = schemaType;
                maxCompatibility = comp;
            } else if (maxCompatibility > 0 && comp == maxCompatibility) {
            // TODO debug message? possible duplicate?
            }
        }
    }
    if (maxType != null && maxCompatibility > 0) {
        // return the type with the maximum compatibility rating
        return new Pair<TypeDefinition, Integer>(maxType, maxCompatibility);
    }
    return null;
}
Also used : QName(javax.xml.namespace.QName) TypeDefinition(eu.esdihumboldt.hale.common.schema.model.TypeDefinition) Pair(eu.esdihumboldt.util.Pair)

Example 18 with Pair

use of eu.esdihumboldt.util.Pair in project hale by halestudio.

the class TypeMetaPairContentProvider method getElements.

/**
 * @see TypeIndexContentProvider#getElements(Object)
 */
@Override
public Object[] getElements(Object inputElement) {
    if (inputElement instanceof Pair<?, ?>) {
        Pair<?, ?> pair = (Pair<?, ?>) inputElement;
        Object type = pair.getFirst();
        if (type instanceof TypeDefinition) {
            type = new TypeEntityDefinition((TypeDefinition) type, getSchemaSpace(), null);
        }
        // second item will be a set of metadata keys
        return new Object[] { type, pair.getSecond() };
    } else
        return new Object[0];
}
Also used : TypeEntityDefinition(eu.esdihumboldt.hale.common.align.model.impl.TypeEntityDefinition) Pair(eu.esdihumboldt.util.Pair) TypeDefinition(eu.esdihumboldt.hale.common.schema.model.TypeDefinition)

Example 19 with Pair

use of eu.esdihumboldt.util.Pair in project hale by halestudio.

the class InstanceContentProvider method hasChildren.

/**
 * @see ITreeContentProvider#hasChildren(Object)
 */
@Override
public boolean hasChildren(Object element) {
    if (element instanceof Pair<?, ?>) {
        Pair<?, ?> pair = (Pair<?, ?>) element;
        if (pair.getFirst() == Metadata.METADATA && pair.getSecond() instanceof Instance) {
            return true;
        }
        element = pair.getSecond();
    }
    if (element instanceof Group) {
        Group group = (Group) element;
        return !Iterables.isEmpty(group.getPropertyNames());
    }
    return false;
}
Also used : Group(eu.esdihumboldt.hale.common.instance.model.Group) Instance(eu.esdihumboldt.hale.common.instance.model.Instance) Pair(eu.esdihumboldt.util.Pair)

Example 20 with Pair

use of eu.esdihumboldt.util.Pair in project hale by halestudio.

the class InstanceContentProvider method getChildren.

/**
 * @see ITreeContentProvider#getChildren(Object)
 */
@Override
public Object[] getChildren(Object parentElement) {
    boolean isMetaData = false;
    if (parentElement instanceof Pair<?, ?>) {
        Pair<?, ?> pair = (Pair<?, ?>) parentElement;
        parentElement = pair.getSecond();
        if (pair.getFirst() == Metadata.METADATA && pair.getSecond() instanceof Instance) {
            isMetaData = true;
        }
    }
    if (parentElement instanceof Group && isMetaData == false) {
        Group group = (Group) parentElement;
        List<Object> children = new ArrayList<Object>();
        for (QName name : group.getPropertyNames()) {
            Definition<?> childDef = group.getDefinition().getChild(name);
            for (Object value : group.getProperty(name)) {
                children.add(new Pair<Object, Object>(childDef, value));
            }
        }
        return children.toArray();
    }
    if (parentElement instanceof Group && isMetaData == true) {
        Instance inst = (Instance) parentElement;
        List<Object> metachildren = new ArrayList<Object>();
        for (String key : inst.getMetaDataNames()) {
            List<Object> values = inst.getMetaData(key);
            for (Object value : values) {
                metachildren.add(new Pair<String, Object>(key, value));
            }
        }
        return metachildren.toArray();
    }
    return new Object[0];
}
Also used : Group(eu.esdihumboldt.hale.common.instance.model.Group) Instance(eu.esdihumboldt.hale.common.instance.model.Instance) QName(javax.xml.namespace.QName) ArrayList(java.util.ArrayList) Pair(eu.esdihumboldt.util.Pair)

Aggregations

Pair (eu.esdihumboldt.util.Pair)33 ArrayList (java.util.ArrayList)11 TypeDefinition (eu.esdihumboldt.hale.common.schema.model.TypeDefinition)8 List (java.util.List)7 Instance (eu.esdihumboldt.hale.common.instance.model.Instance)6 Cell (eu.esdihumboldt.hale.common.align.model.Cell)4 Entity (eu.esdihumboldt.hale.common.align.model.Entity)4 EntityDefinition (eu.esdihumboldt.hale.common.align.model.EntityDefinition)4 Group (eu.esdihumboldt.hale.common.instance.model.Group)4 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)4 Point (org.eclipse.swt.graphics.Point)4 ParameterValue (eu.esdihumboldt.hale.common.align.model.ParameterValue)3 TypeEntityDefinition (eu.esdihumboldt.hale.common.align.model.impl.TypeEntityDefinition)3 IOMessageImpl (eu.esdihumboldt.hale.common.core.io.report.impl.IOMessageImpl)3 GroupPropertyDefinition (eu.esdihumboldt.hale.common.schema.model.GroupPropertyDefinition)3 IOException (java.io.IOException)3 URI (java.net.URI)3 Collection (java.util.Collection)3 QName (javax.xml.namespace.QName)3 SelectionAdapter (org.eclipse.swt.events.SelectionAdapter)3