de.fhg.iais.cortex.services.binding.IdHandler.java Source code

Java tutorial

Introduction

Here is the source code for de.fhg.iais.cortex.services.binding.IdHandler.java

Source

package de.fhg.iais.cortex.services.binding;

/******************************************************************************
 * Copyright 2011 (c) Fraunhofer IAIS Netmedia  http://www.iais.fraunhofer.de *
 * ************************************************************************** *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may    *
 * not use this file except in compliance with the License.                   *
 * You may obtain a copy of the License at                                    *
 * http://www.apache.org/licenses/LICENSE-2.0                                 *
 * Unless required by applicable law or agreed to in writing,                 *
 * software distributed under the License is distributed on an "AS IS" BASIS, *
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   *
 * See the License for the specific language governing permissions and        *
 * limitations under the License.                                             *
 ******************************************************************************/

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.jdom.Attribute;
import org.jdom.CDATA;
import org.jdom.Comment;
import org.jdom.DocType;
import org.jdom.Element;
import org.jdom.EntityRef;
import org.jdom.Text;

import com.google.common.base.Strings;

import de.fhg.iais.commons.functional.Method1;
import de.fhg.iais.commons.xml.JDomAction;
import de.fhg.iais.commons.xml.JDomVisitor;
import de.fhg.iais.cortex.model.aip.AipObject;
import de.fhg.iais.cortex.model.aip.AipObject.NewSchemaPaths;
import de.fhg.iais.cortex.model.aip.AipObject.OldSchemaPaths;
import de.fhg.iais.cortex.model.aip.IdGenerator;
import de.fhg.iais.cortex.model.graph.Graph;
import de.fhg.iais.cortex.model.graph.Vertex;
import de.fhg.iais.cortex.model.util.GlobalConstants;
import de.fhg.iais.cortex.model.util.VocTerms.RDF;
import de.fhg.iais.cortex.report.common.ReportSections;
import de.fhg.iais.cortex.report.common.revision.RevisionReport;
import de.fhg.iais.cortex.services.IIngestContext;
import de.fhg.iais.cortex.services.ingest.IIdHandler;
import de.fhg.iais.cortex.services.ingest.worker.parser.Parser;

/**
 * Translate local id's into persistent id's. Store items and pseudo item in node store.
 *
 * @author
 */
public class IdHandler implements IIdHandler {

    public static final String GLOBAL_HTTP_PREFIX = "http://";
    public static final String GLOBAL_HTTPS_PREFIX = "https://";

    @Override
    public String doBinding(AipObject aipObject, IIngestContext context) {

        final String providerId = aipObject.getObjectFromFirstAvailablePathOrNull(String.class,
                OldSchemaPaths.PATH_PROVIDER_ID.path(), NewSchemaPaths.PATH_PROVIDER_ID.path());
        final String providerItemId = aipObject.getObjectFromFirstAvailablePathOrNull(String.class,
                OldSchemaPaths.PATH_PROVIDER_ITEM_ID.path(), NewSchemaPaths.PATH_PROVIDER_ITEM_ID.path());

        String itemId = IdGenerator.item(providerId, providerItemId).generate();

        if (Strings.isNullOrEmpty(itemId)) {
            context.reportAndThrowDataError(RevisionReport.INGEST_PART, ReportSections.ID_HANDLER,
                    "Invalid XML Input, " + "" + "Item ID is missing.", null);
        }

        context.setItemId(itemId);

        if (context.getAipObject().isNewFormat()) {
            aipObject.setObjectForPath(NewSchemaPaths.PATH_CORTEX_ITEM_ID.path(), itemId);
        } else {
            aipObject.setObjectForPath(OldSchemaPaths.PATH_CORTEX_ITEM_ID.path(), itemId);
        }

        final Map<String, String> idMap = new HashMap<String, String>();

        Element model = aipObject.getModelElement();
        Parser parser = Parser.createParser(model);
        Graph graph = parser.load(model);

        graph.dfs(new Method1<Vertex>() {
            @Override
            public void call(Vertex vertex) {
                String localSubjectId = vertex.getId();
                if (vertex.outgoing().isEmpty() && !localSubjectId.startsWith(IdGenerator.GLOBAL_PREFIX)) {
                    return;
                }
                if (localSubjectId.startsWith(IdGenerator.GLOBAL_PREFIX)) {
                    String globalId = IdGenerator.global(providerId, localSubjectId).generate();
                    idMap.put(localSubjectId, GlobalConstants.CORTEX_ITEM_IDENTIFIER_PREFIX + globalId);
                } else if (localSubjectId.startsWith(IdGenerator.UID_PREFIX)) {
                    String uniqueId = StringUtils.substringAfter(localSubjectId, IdGenerator.UID_PREFIX);
                    String ddbId = IdGenerator.item(providerId, uniqueId).generate();
                    idMap.put(localSubjectId, IdGenerator.UID_PREFIX + ddbId);
                } else if (!localSubjectId.startsWith(GLOBAL_HTTP_PREFIX)
                        && !localSubjectId.startsWith(GLOBAL_HTTPS_PREFIX)) {
                    String localId = IdGenerator.local(providerId, vertex).generate();
                    idMap.put(localSubjectId, localId);
                }
            }
        });

        Vertex primary = graph.getPrimaryItemVertex();
        String localIdThis = primary.getId();
        idMap.put(localIdThis, GlobalConstants.CORTEX_ITEM_IDENTIFIER_PREFIX + itemId);

        JDomVisitor idRewriteVisitor = new JDomVisitor(new IdRewriteAction(idMap, aipObject.isNewFormat()));
        idRewriteVisitor.visit(model);

        if (model != null) {
            context.getAipObject().setModelElement(model);
        }

        return itemId;
    }

    private static class IdRewriteAction implements JDomAction {

        private final Map<String, String> idMapping;
        private final boolean isNewFormat;

        public IdRewriteAction(Map<String, String> idMap, boolean newFormat) {
            this.idMapping = idMap;
            this.isNewFormat = newFormat;
        }

        @Override
        public void text(Text text) {
            // do nothing
        }

        @Override
        public void attribute(Attribute attribute) {
            String localId = attribute.getValue();
            String mappedId = this.idMapping.get(localId);
            String attributeName = attribute.getName();

            if ((RDF.Attributes.RESOURCE.equals(attributeName) || RDF.Attributes.ABOUT.equals(attributeName))
                    && attribute.getNamespace().equals(GlobalConstants.RDF_NAMESPACE)) {
                Element attributeParent = attribute.getParent();
                if (this.isNewFormat) {
                    if (mappedId != null) {
                        attribute.setValue(mappedId);
                    }
                } else {
                    if (!(RDF.Properties.TYPE.equals(attributeParent.getName())
                            && attributeParent.getNamespace().equals(GlobalConstants.RDF_NAMESPACE))) {
                        if (mappedId != null) {
                            attribute.setValue(mappedId);
                        }
                    }
                }
            }
        }

        @Override
        public void comment(Comment comment) {
            // do nothing
        }

        @Override
        public void docType(DocType docType) {
            // do nothing
        }

        @Override
        public void entityRef(EntityRef entityRef) {
            // do nothing
        }

        @Override
        public void cdata(CDATA cdata) {
            // do nothing
        }

        @Override
        public void element(Element element) {
            // do nothing
        }
    }
}