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

Java tutorial

Introduction

Here is the source code for com.sysunite.weaver.nifi.CreateValueProperty.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.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 javax.validation.constraints.Null;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;
import java.util.concurrent.atomic.AtomicReference;

@Tags({ "weaver, createvalueproperty" })
@CapabilityDescription("creates a weaver entity of type VALUE_PROPERTY")
@SeeAlso({})
@ReadsAttributes({ @ReadsAttribute(attribute = "", description = "") })
@WritesAttributes({ @WritesAttribute(attribute = "", description = "") })
public class CreateValueProperty 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 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);
        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;
        }

        //read the flow file
        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);

                    //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");

                    //------------------check if root-object already exists

                    try {

                        Entity parentObject = weaver.get(valueOfNode);

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

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

                            //------------------create first triple
                            //the subject
                            String entityId = 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, entityId);

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

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

                            System.out.println(
                                    ".....SUBJECT { id: " + valueOfNode + ", type: " + EntityType.OBJECT + " }");
                            System.out.println(".....OBJECT { id: " + aCollection.getId() + ", type: "
                                    + EntityType.COLLECTION + " }");
                            System.out.println(".....PREDICATE { id " + PredicateType.PROPERTIES + " }");

                        }
                        //
                        //
                        //          //------------------create second triple
                        //
                        //          //value of object
                        //          //create an entity attributes-list
                        Map<String, Object> entityAttributes = new HashMap<>();
                        String firstAttributeKey = "value";
                        String firstAttributeValue = valueOfChildNode;//"2013-12-21";
                        String predicateKey = "predicate";
                        String predicateValue = "has" + childNodeToSearchFor;
                        entityAttributes.put(firstAttributeKey, firstAttributeValue);
                        entityAttributes.put(predicateKey, predicateValue);

                        //object
                        Entity anObjectWithValue = weaver.add(entityAttributes, EntityType.VALUE_PROPERTY,
                                weaver.createRandomUUID());

                        //get the collection object from parent
                        Entity aCollection = parentObject.getEntities().get(PredicateType.PROPERTIES);

                        //predicate
                        aCollection.linkEntity(anObjectWithValue.getId(), anObjectWithValue);

                        System.out.println("...creating from Node");
                        System.out.println("....attributes { " + firstAttributeKey + " : " + firstAttributeValue
                                + ", " + predicateKey + " : " + predicateValue + "}");

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

                } catch (IOException e) {
                    System.out.println("w00t");// + e.getMessage());
                } catch (IllegalArgumentException e) {
                    System.out.println("xml niet geldig?");
                } catch (IndexOutOfBoundsException e) {
                    System.out.println("de node waar gezocht naar moet worden is niet gevonden!");
                }

            }

        });

        session.transfer(flowFileIn, MY_RELATIONSHIP);

    }
}