Search in sources :

Example 1 with EntryTransformer

use of com.google.common.collect.Maps.EntryTransformer in project hale by halestudio.

the class KVPUtil method addTypeNameParameter.

/**
 * Add typename and namespace parameters for a WFS request to the given URI
 * builder.
 *
 * @param builder the builder for the WFS request URI
 * @param selected the type names to include
 * @param version the targeted WFS version
 */
public static void addTypeNameParameter(URIBuilder builder, Iterable<QName> selected, WFSVersion version) {
    // namespaces mapped to prefixes
    Map<String, String> namespaces = new HashMap<>();
    // type names with updated prefix
    Set<QName> typeNames = new HashSet<>();
    for (QName type : selected) {
        String prefix;
        if (type.getNamespaceURI() != null && !type.getNamespaceURI().isEmpty()) {
            prefix = namespaces.get(type.getNamespaceURI());
            if (prefix == null) {
                // no mapping yet for namespace
                String candidate = type.getPrefix();
                prefix = addPrefix(candidate, type.getNamespaceURI(), namespaces);
            }
        } else {
            // default namespace
            prefix = XMLConstants.DEFAULT_NS_PREFIX;
        }
        // add updated type
        typeNames.add(new QName(type.getNamespaceURI(), type.getLocalPart(), prefix));
    }
    final String paramNamespaces;
    final String paramTypenames;
    final String prefixNamespaceDelim;
    switch(version) {
        case V1_1_0:
            paramNamespaces = "NAMESPACE";
            paramTypenames = "TYPENAME";
            prefixNamespaceDelim = "=";
            break;
        case V2_0_0:
        case V2_0_2:
            /*
			 * XXX below are the values as defined in the WFS 2 specification.
			 * There have been problems with some GeoServer instances if used in
			 * that manner.
			 */
            paramNamespaces = "NAMESPACES";
            paramTypenames = "TYPENAMES";
            prefixNamespaceDelim = ",";
            break;
        default:
            // fall-back to WFS 1.1
            paramNamespaces = "NAMESPACE";
            paramTypenames = "TYPENAME";
            prefixNamespaceDelim = "=";
    }
    // add namespace prefix definitions
    if (!namespaces.isEmpty()) {
        builder.addParameter(paramNamespaces, Joiner.on(',').join(Maps.transformEntries(namespaces, new EntryTransformer<String, String, String>() {

            @Override
            public String transformEntry(String namespace, String prefix) {
                StringBuilder sb = new StringBuilder();
                sb.append("xmlns(");
                sb.append(prefix);
                sb.append(prefixNamespaceDelim);
                sb.append(namespace);
                sb.append(")");
                return sb.toString();
            }
        }).values()));
    }
    // add type names
    if (!typeNames.isEmpty()) {
        builder.addParameter(paramTypenames, Joiner.on(',').join(Iterables.transform(typeNames, new Function<QName, String>() {

            @Override
            public String apply(QName typeName) {
                String prefix = typeName.getPrefix();
                if (prefix == null || prefix.isEmpty()) {
                    return typeName.getLocalPart();
                }
                return prefix + ":" + typeName.getLocalPart();
            }
        })));
    }
}
Also used : HashMap(java.util.HashMap) EntryTransformer(com.google.common.collect.Maps.EntryTransformer) QName(javax.xml.namespace.QName) HashSet(java.util.HashSet)

Example 2 with EntryTransformer

use of com.google.common.collect.Maps.EntryTransformer in project hale by halestudio.

the class DefaultCellMigrator method updateCell.

@Override
public MutableCell updateCell(final Cell originalCell, final AlignmentMigration migration, final MigrationOptions options, SimpleLog log) {
    MutableCell result = new DefaultCell(originalCell);
    SimpleLog cellLog = SimpleLog.all(log, new CellLog(result, CELL_LOG_CATEGORY));
    final AtomicBoolean replacedEntities = new AtomicBoolean(false);
    EntryTransformer<String, Entity, Entity> entityTransformer = new EntryTransformer<String, Entity, Entity>() {

        @Override
        public Entity transformEntry(String key, Entity value) {
            EntityDefinition org = value.getDefinition();
            Optional<EntityDefinition> replace = migration.entityReplacement(org, cellLog);
            EntityDefinition entity = replace.orElse(org);
            if (!Objects.equal(entity, org)) {
                replacedEntities.set(true);
            }
            if (entity instanceof PropertyEntityDefinition) {
                return new DefaultProperty((PropertyEntityDefinition) entity);
            } else if (entity instanceof TypeEntityDefinition) {
                return new DefaultType((TypeEntityDefinition) entity);
            } else {
                throw new IllegalStateException("Invalid entity definition for creating entity");
            }
        }
    };
    // update source entities
    if (options.updateSource() && result.getSource() != null && !result.getSource().isEmpty()) {
        result.setSource(ArrayListMultimap.create(Multimaps.transformEntries(result.getSource(), entityTransformer)));
    }
    // update target entities
    if (options.updateTarget() && result.getTarget() != null && !result.getTarget().isEmpty()) {
        result.setTarget(ArrayListMultimap.create(Multimaps.transformEntries(result.getTarget(), entityTransformer)));
    }
    // TODO anything else?
    postUpdateCell(result, options, replacedEntities.get(), cellLog);
    return result;
}
Also used : SimpleLog(eu.esdihumboldt.hale.common.core.report.SimpleLog) Entity(eu.esdihumboldt.hale.common.align.model.Entity) MutableCell(eu.esdihumboldt.hale.common.align.model.MutableCell) EntryTransformer(com.google.common.collect.Maps.EntryTransformer) DefaultType(eu.esdihumboldt.hale.common.align.model.impl.DefaultType) DefaultProperty(eu.esdihumboldt.hale.common.align.model.impl.DefaultProperty) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) PropertyEntityDefinition(eu.esdihumboldt.hale.common.align.model.impl.PropertyEntityDefinition) TypeEntityDefinition(eu.esdihumboldt.hale.common.align.model.impl.TypeEntityDefinition) EntityDefinition(eu.esdihumboldt.hale.common.align.model.EntityDefinition) TypeEntityDefinition(eu.esdihumboldt.hale.common.align.model.impl.TypeEntityDefinition) PropertyEntityDefinition(eu.esdihumboldt.hale.common.align.model.impl.PropertyEntityDefinition) DefaultCell(eu.esdihumboldt.hale.common.align.model.impl.DefaultCell) CellLog(eu.esdihumboldt.hale.common.align.model.annotations.messages.CellLog)

Aggregations

EntryTransformer (com.google.common.collect.Maps.EntryTransformer)2 Entity (eu.esdihumboldt.hale.common.align.model.Entity)1 EntityDefinition (eu.esdihumboldt.hale.common.align.model.EntityDefinition)1 MutableCell (eu.esdihumboldt.hale.common.align.model.MutableCell)1 CellLog (eu.esdihumboldt.hale.common.align.model.annotations.messages.CellLog)1 DefaultCell (eu.esdihumboldt.hale.common.align.model.impl.DefaultCell)1 DefaultProperty (eu.esdihumboldt.hale.common.align.model.impl.DefaultProperty)1 DefaultType (eu.esdihumboldt.hale.common.align.model.impl.DefaultType)1 PropertyEntityDefinition (eu.esdihumboldt.hale.common.align.model.impl.PropertyEntityDefinition)1 TypeEntityDefinition (eu.esdihumboldt.hale.common.align.model.impl.TypeEntityDefinition)1 SimpleLog (eu.esdihumboldt.hale.common.core.report.SimpleLog)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 QName (javax.xml.namespace.QName)1