com.sysunite.weaver.nifi.CreateObjectProperty.java Source code

Java tutorial

Introduction

Here is the source code for com.sysunite.weaver.nifi.CreateObjectProperty.java

Source

/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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.
 */
package com.sysunite.weaver.nifi;

import com.jcabi.xml.XML;
import com.jcabi.xml.XMLDocument;
import com.weaverplatform.sdk.Entity;
import com.weaverplatform.sdk.EntityType;
import com.weaverplatform.sdk.PredicateType;
import com.weaverplatform.sdk.Weaver;
import org.apache.commons.io.IOUtils;
import org.apache.http.conn.HttpHostConnectException;
import org.apache.nifi.components.PropertyDescriptor;
import org.apache.nifi.components.PropertyValue;
import org.apache.nifi.flowfile.FlowFile;
import org.apache.nifi.processor.*;
import org.apache.nifi.annotation.behavior.ReadsAttribute;
import org.apache.nifi.annotation.behavior.ReadsAttributes;
import org.apache.nifi.annotation.behavior.WritesAttribute;
import org.apache.nifi.annotation.behavior.WritesAttributes;
import org.apache.nifi.annotation.lifecycle.OnScheduled;
import org.apache.nifi.annotation.documentation.CapabilityDescription;
import org.apache.nifi.annotation.documentation.SeeAlso;
import org.apache.nifi.annotation.documentation.Tags;
import org.apache.nifi.processor.exception.ProcessException;
import org.apache.nifi.processor.io.InputStreamCallback;
import org.apache.nifi.processor.util.StandardValidators;

import java.io.IOException;
import java.io.InputStream;
import java.util.*;

@Tags({ "weaver, createobjectproperty" })
@CapabilityDescription("Create a weaver object property.")
@SeeAlso({})
@ReadsAttributes({ @ReadsAttribute(attribute = "", description = "") })
@WritesAttributes({ @WritesAttribute(attribute = "", description = "") })
public class CreateObjectProperty extends AbstractProcessor {

    public static final PropertyDescriptor PROP_NODE = new PropertyDescriptor.Builder().name("prop_node")
            .description("Enter the tag-name of the root node, where the NODE_ATTRIBUTE lives.").required(true)
            .addValidator(StandardValidators.NON_EMPTY_VALIDATOR).build();

    public static final PropertyDescriptor PROP_NODE_ATTRIBUTE = new PropertyDescriptor.Builder()
            .name("prop_node_attribute").description("Setup node attribute to get its value, i.e. name.")
            .required(true).addValidator(StandardValidators.NON_EMPTY_VALIDATOR).build();

    public static final PropertyDescriptor PROP_CHILDNODE = new PropertyDescriptor.Builder().name("prop_childnode")
            .description("Enter the tag-name of a child node, where the CHILDNODE_ATTRIBUTE lives.").required(true)
            .addValidator(StandardValidators.NON_EMPTY_VALIDATOR).build();

    public static final PropertyDescriptor PROP_CHILDNODE_ATTRIBUTE = new PropertyDescriptor.Builder()
            .name("prop_childnode_attribute").description("Same setup as NODE_ATTRIBUTE").required(true)
            .addValidator(StandardValidators.NON_EMPTY_VALIDATOR).build();

    public static final PropertyDescriptor PROP_CHILDNODE_SUB = new PropertyDescriptor.Builder()
            .name("prop_childnode_sub").description("OPTIONAL: Name of node within childnode.").defaultValue("")
            .addValidator(StandardValidators.NON_EMPTY_VALIDATOR).build();

    public static final PropertyDescriptor PROP_CHILDNODE_SUB_ATTRIBUTE = new PropertyDescriptor.Builder()
            .name("prop_childnode_sub_attribute")
            .description("OPTIONAL: Attribute of node within childnode. i.e. name").defaultValue("")
            .addValidator(StandardValidators.NON_EMPTY_VALIDATOR).build();

    public static final Relationship MY_RELATIONSHIP = new Relationship.Builder().name("my_relationship")
            .description("Example relationship").build();

    private List<PropertyDescriptor> descriptors;

    private Set<Relationship> relationships;

    @Override
    protected void init(final ProcessorInitializationContext context) {
        final List<PropertyDescriptor> descriptors = new ArrayList<PropertyDescriptor>();
        descriptors.add(PROP_NODE);
        descriptors.add(PROP_NODE_ATTRIBUTE);
        descriptors.add(PROP_CHILDNODE);
        descriptors.add(PROP_CHILDNODE_ATTRIBUTE);
        descriptors.add(PROP_CHILDNODE_SUB);
        descriptors.add(PROP_CHILDNODE_SUB_ATTRIBUTE);
        this.descriptors = Collections.unmodifiableList(descriptors);

        final Set<Relationship> relationships = new HashSet<Relationship>();
        relationships.add(MY_RELATIONSHIP);
        this.relationships = Collections.unmodifiableSet(relationships);
    }

    @Override
    public Set<Relationship> getRelationships() {
        return this.relationships;
    }

    @Override
    public final List<PropertyDescriptor> getSupportedPropertyDescriptors() {
        return descriptors;
    }

    @OnScheduled
    public void onScheduled(final ProcessContext context) {

    }

    @Override
    public void onTrigger(final ProcessContext context, final ProcessSession session) throws ProcessException {
        FlowFile flowFileIn = session.get();
        if (flowFileIn == null) {
            return;
        }

        session.read(flowFileIn, new InputStreamCallback() {

            @Override
            public void process(InputStream isIn) throws IOException {

                System.out.println("contact!");

                try {

                    String contents = IOUtils.toString(isIn);

                    XML xmlFile = new XMLDocument(contents);

                    //--------getting node
                    //get attribute value of root-node
                    String nodeToSearchFor = context.getProperty(PROP_NODE).getValue();
                    String xpathToNode = "/" + nodeToSearchFor;
                    String nodeAttributeToSearchFor = context.getProperty(PROP_NODE_ATTRIBUTE).getValue();
                    String xpathToNodeAttribute = xpathToNode + "/@" + nodeAttributeToSearchFor;

                    String valueOfNode = xmlFile.xpath(xpathToNodeAttribute).get(0);

                    System.out.println("RootNode: " + nodeToSearchFor);
                    System.out.println("RootNode (xpath): " + xpathToNode);
                    System.out.println("RootNode-Attribute: " + nodeAttributeToSearchFor);
                    System.out.println("RootNode-Attribute (xpath): " + xpathToNodeAttribute);
                    System.out.println("RootNode-Attribute (value): " + valueOfNode);

                    //get attribute value of child-node
                    String childNodeToSearchFor = context.getProperty(PROP_CHILDNODE).getValue();
                    String xPathToChildNode = xpathToNode + "/" + childNodeToSearchFor;
                    String childNodeAttributeToSearchFor = context.getProperty(PROP_CHILDNODE_ATTRIBUTE).getValue();
                    String xPathToChildNodeAttribute = xPathToChildNode + "/@" + childNodeAttributeToSearchFor;

                    String valueOfChildNode = xmlFile.xpath(xPathToChildNodeAttribute).get(0);

                    System.out.println("ChildNode: " + childNodeToSearchFor);
                    System.out.println("ChildNode (xpath): " + xPathToChildNode);
                    System.out.println("ChildNode-Attribute: " + childNodeAttributeToSearchFor);
                    System.out.println("Child-Attribute (xpath): " + xPathToChildNodeAttribute);
                    System.out.println("Child-Attribute (value): " + valueOfChildNode);

                    //------------weaver
                    Weaver weaver = new Weaver();
                    weaver.connect("http://localhost:9487");

                    //------------------create first triple

                    try {

                        //if connection fails (weaver-server), this creates a nullpointer exception
                        Entity parentObject = weaver.get(valueOfNode);

                        if (parentObject.getType() == null) {

                            System.out.println("...creating new parentObj");

                            //the subject
                            String subjectId = valueOfNode; //"1";
                            // replaced: new HashMap<>() to new HashMap<String, Object>(), for incompatible type error with expected Map<String, Object>
                            parentObject = weaver.add(new HashMap<String, Object>(), EntityType.OBJECT, subjectId);

                            //object
                            Entity aCollection = weaver.add(new HashMap<String, Object>(), EntityType.COLLECTION,
                                    weaver.createRandomUUID());

                            //predicate
                            parentObject.linkEntity(PredicateType.PROPERTIES, aCollection);

                            System.out.println("..OBJECT (SUBJECT): { id: " + subjectId + "}");
                            System.out.println("..OBJECT (OBJECT): { id: " + aCollection.getId() + "}");
                            System.out.println("..PREDICATE (PREDICATE): { " + PredicateType.PROPERTIES + "}");

                        }

                        //fetch collection
                        Entity aCollection = parentObject.getEntities().get(PredicateType.PROPERTIES);
                        System.out.println("..fetching COLLECTION");

                        //-----------------------create third triple

                        String childNodeSubToSearchFor = context.getProperty(PROP_CHILDNODE_SUB).getValue();
                        String childNodeSubAttributeToSearchFor = context.getProperty(PROP_CHILDNODE_SUB_ATTRIBUTE)
                                .getValue();
                        String searchWithinSub = childNodeSubToSearchFor + "/@" + childNodeSubAttributeToSearchFor;

                        //----check for multiples

                        List<XML> xmlObjectProperties = xmlFile.nodes(xPathToChildNode);

                        System.out.println("gevonden: " + xmlObjectProperties.size());

                        for (XML xmlObjectProperty : xmlObjectProperties) {

                            //            System.out.println(xmlObjectProperty.toString());
                            System.out.println("...creating from node");

                            Map<String, Object> entityAttributes = new HashMap<>();

                            //create new entity attribute
                            String predicateKey = "predicate";
                            String predicateValue = "has" + childNodeSubToSearchFor; //hasGEOMETRY
                            entityAttributes.put(predicateKey, predicateValue);

                            Entity anObjectProperty = weaver.add(entityAttributes, EntityType.OBJECT_PROPERTY,
                                    weaver.createRandomUUID());

                            System.out.println("....OBJECT (SUBJECT): { id: " + anObjectProperty.getId()
                                    + ", type: " + EntityType.OBJECT_PROPERTY + " }");
                            System.out.println(".....ATTRIBUTES: { " + predicateKey + " : " + predicateValue + "}");
                            System.out.println("....PREDICATE (PREDICATE): { " + anObjectProperty.getId() + "}");

                            System.out.println("..linking OBJECT_PROPERTY TO COLLECTION");
                            aCollection.linkEntity(anObjectProperty.getId(), anObjectProperty);

                            //GEOMETRY
                            try {

                                //System.out.println(searchWithinSub);

                                String valueOfChildNodeSubAttribute = xmlObjectProperty.xpath(searchWithinSub)
                                        .get(0);

                                System.out.println("....creating from subnode {" + childNodeSubToSearchFor + "}");
                                System.out.println(".....attribute {" + childNodeSubAttributeToSearchFor + " : "
                                        + valueOfChildNodeSubAttribute + "}");

                                String idOfObject = valueOfChildNodeSubAttribute; //5;
                                Entity aObject = weaver.add(entityAttributes, EntityType.OBJECT, idOfObject);

                                System.out
                                        .println(".....{ id: " + idOfObject + ", type: " + EntityType.OBJECT + "}");

                                anObjectProperty.linkEntity(PredicateType.OBJECT, aObject);

                                System.out.println(".....linking sub to OBJECT_PROPERTY");
                                System.out.println(".....PREDICATE { type: " + PredicateType.OBJECT + " }");

                            } catch (IndexOutOfBoundsException e) {
                                System.out.println("xpath: niet geldig!");
                            }

                        }
                        //----

                    } catch (NullPointerException e) {
                        System.out.println("weaver-connection error!");
                    }

                } catch (IllegalArgumentException e) {
                    System.out.println("Node not found");
                }

            }
        });

        session.transfer(flowFileIn, MY_RELATIONSHIP);

    }
}