All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.lordofthejars.nosqlunit.graph.parser.Edge Maven / Gradle / Ivy

There is a newer version: 1.0.0
Show newest version
package com.lordofthejars.nosqlunit.graph.parser;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.neo4j.graphdb.DynamicRelationshipType;
import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.Relationship;
import org.neo4j.helpers.collection.MapUtil;

import com.google.common.base.Strings;

public class Edge {

	private String id;

	private String sourceNodeId;
	private String targetNodeId;

	private String label;

	private Map props = new HashMap();
	private List manualIndexes = new ArrayList();

	public Edge(String id, String sourceNodeId, String targetNodeId, String label) {
		this.id = id;
		this.sourceNodeId = sourceNodeId;
		this.targetNodeId = targetNodeId;
		this.label = label;
	}

	public Relationship createLink(Map nodes) {

		Node sourceNode = nodes.get(sourceNodeId);
		Node targetNode = nodes.get(targetNodeId);

		Relationship relationship = sourceNode
				.createRelationshipTo(targetNode, DynamicRelationshipType.withName(label));

		Set keys = props.keySet();

		for (String key : keys) {
			relationship.setProperty(key, props.get(key));
		}

		return relationship;

	}

	public void createManualIndexes(GraphDatabaseService graphDatabaseService, Relationship relationship) {
		for (ManualIndex manualIndex : manualIndexes) {
			if(manualIndex.getConfiguration() == null){
				graphDatabaseService.index().forRelationships(manualIndex.getIndexName())
						.add(relationship, manualIndex.getKey(), manualIndex.getValue());
			} else {
				graphDatabaseService.index().forRelationships(manualIndex.getIndexName(), manualIndex.getConfiguration())
						.add(relationship, manualIndex.getKey(), manualIndex.getValue());
			}
		}
	}

	public void putManualIndex(String indexName, String indexKey, String indexValue) {
		putManualIndex(indexName, indexKey, indexValue, null);
	}

	public void putManualIndex(String indexName, String indexKey, String indexValue, Map configuration) {
		this.manualIndexes.add(new ManualIndex(indexName, indexKey, indexValue, configuration));
	}

	public void putData(String key, Object data) {
		this.props.put(key, data);
	}

	public String getLabel() {
		return label;
	}

	public String getId() {
		return id;
	}

	public String getSourceNodeId() {
		return sourceNodeId;
	}

	public String getTargetNodeId() {
		return targetNodeId;
	}

	public Map getProps() {
		return props;
	}

	@Override
	public String toString() {
		return "Edge [id=" + id + ", sourceNodeId=" + sourceNodeId + ", targetNodeId=" + targetNodeId + ", label="
				+ label + "]";
	}

	private class ManualIndex {

		private String indexName;
		private String key;
		private String value;
		private Map configuration;

		public ManualIndex(String indexName, String key, String value) {
			super();
			this.indexName = indexName;
			this.key = key;
			this.value = value;
		}

		public ManualIndex(String indexName, String key, String value, Map configuration) {
			super();
			this.indexName = indexName;
			this.key = key;
			this.value = value;
			this.configuration = configuration;
		}

		public String getIndexName() {
			return indexName;
		}

		public String getKey() {
			return key;
		}

		public String getValue() {
			return value;
		}

		public Map getConfiguration() {
			return configuration;
		}

	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy