// Path: net.locosoft.fold.neo4j/src/net/locosoft/fold/neo4j/ICypher.java
// public interface ICypher {
// 
// 	boolean allowParallelInvocation();
// 
// 	void allowParallelInvocation(boolean parallelInvocation);
// 
// 	String getStatementText();
// 
// 	void setStatementText(String statementText);
// 
// 	JsonObject getRequest();
// 
// 	void addStatement(String statementText);
// 
// 	void addParameter(String name, JsonValue value);
// 
// 	void addParameter(String name, int value);
// 
// 	void addParameter(String name, long value);
// 
// 	void addParameter(String name, float value);
// 
// 	void addParameter(String name, double value);
// 
// 	void addParameter(String name, boolean value);
// 
// 	void addParameter(String name, String value);
// 
// 	JsonObject getResponse();
// 
// 	JsonArray getResultData();
// 
// 	JsonArray getResultData(int resultSet);
// 
// 	int getResultDataRowCount();
// 
// 	int getResultDataRowCount(int resultSet);
// 
// 	JsonValue getResultDataRow(int index);
// 
// 	JsonValue getResultDataRow(int resultSet, int index);
// 
// 	JsonArray getErrors();
// 
// 	int getErrorCount();
// }
// 
// Path: net.locosoft.fold.neo4j/src/net/locosoft/fold/neo4j/INeo4jService.java
// public interface INeo4jService {
// 
// 	boolean isNeo4jReady();
// 
// 	int getNeo4jPID();
// 
// 	ICypher constructCypher();
// 
// 	ICypher constructCypher(String statement);
// 
// 	void invokeCypher(ICypher cypher);
// 
// 	void invokeCypher(ICypher cypher, boolean logErrors);
// 
// }
// 
// Path: net.locosoft.fold.sketch/src/net/locosoft/fold/sketch/AbstractNodeSketch.java
// public abstract class AbstractNodeSketch implements INodeSketch {
// 
// 	protected long _nodeId = -1;
// 
// 	public long getNodeId() {
// 		return _nodeId;
// 	}
// 
// 	private INeo4jService _neo4jService;
// 
// 	public INeo4jService getNeo4jService() {
// 		if (_neo4jService == null) {
// 			_neo4jService = Neo4jUtil.getNeo4jService();
// 		}
// 		return _neo4jService;
// 	}
// 
// }


import net.locosoft.fold.neo4j.INeo4jService;
import net.locosoft.fold.sketch.AbstractNodeSketch;
import com.eclipsesource.json.JsonValue;
import net.locosoft.fold.neo4j.ICypher;
/*****************************************************************************
 * Copyright (c) 2015 Chris J Daly (github user cjdaly)
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *   cjdaly - initial API and implementation
 ****************************************************************************/

package net.locosoft.fold.sketch.pad.neo4j;



public class CounterPropertyNode extends AbstractNodeSketch {

	public CounterPropertyNode(long nodeId) {
		_nodeId = nodeId;
	}

	private long crementCounter(String counterPropertyName, boolean isIncrement) {
		INeo4jService neo4jService = getNeo4jService();

		char crementOp = isIncrement ? '+' : '-';
		String cypherText = "MATCH node WHERE id(node)={nodeId}"
				+ " SET node.`" + counterPropertyName + "`=COALESCE(node.`"
				+ counterPropertyName + "`, 0) " + crementOp + " 1"
				+ " RETURN node.`" + counterPropertyName + "`";
		ICypher cypher = neo4jService.constructCypher(cypherText);
		cypher.addParameter("nodeId", getNodeId());

		neo4jService.invokeCypher(cypher);

		JsonValue jsonValue = cypher.getResultDataRow(0);
		return jsonValue.asLong();
	}

	public long incrementCounter(String counterPropertyName) {
		return crementCounter(counterPropertyName, true);
	}

	public long decrementCounter(String counterPropertyName) {
		return crementCounter(counterPropertyName, false);
	}

	public long getCounter(String counterPropertyName) {
		PropertyAccessNode sketch = new PropertyAccessNode(getNodeId());
		JsonValue jsonValue = sketch.getValue(counterPropertyName);
// Path: net.locosoft.fold.neo4j/src/net/locosoft/fold/neo4j/ICypher.java
// public interface ICypher {
// 
// 	boolean allowParallelInvocation();
// 
// 	void allowParallelInvocation(boolean parallelInvocation);
// 
// 	String getStatementText();
// 
// 	void setStatementText(String statementText);
// 
// 	JsonObject getRequest();
// 
// 	void addStatement(String statementText);
// 
// 	void addParameter(String name, JsonValue value);
// 
// 	void addParameter(String name, int value);
// 
// 	void addParameter(String name, long value);
// 
// 	void addParameter(String name, float value);
// 
// 	void addParameter(String name, double value);
// 
// 	void addParameter(String name, boolean value);
// 
// 	void addParameter(String name, String value);
// 
// 	JsonObject getResponse();
// 
// 	JsonArray getResultData();
// 
// 	JsonArray getResultData(int resultSet);
// 
// 	int getResultDataRowCount();
// 
// 	int getResultDataRowCount(int resultSet);
// 
// 	JsonValue getResultDataRow(int index);
// 
// 	JsonValue getResultDataRow(int resultSet, int index);
// 
// 	JsonArray getErrors();
// 
// 	int getErrorCount();
// }
// 
// Path: net.locosoft.fold.neo4j/src/net/locosoft/fold/neo4j/INeo4jService.java
// public interface INeo4jService {
// 
// 	boolean isNeo4jReady();
// 
// 	int getNeo4jPID();
// 
// 	ICypher constructCypher();
// 
// 	ICypher constructCypher(String statement);
// 
// 	void invokeCypher(ICypher cypher);
// 
// 	void invokeCypher(ICypher cypher, boolean logErrors);
// 
// }
// 
// Path: net.locosoft.fold.sketch/src/net/locosoft/fold/sketch/AbstractNodeSketch.java
// public abstract class AbstractNodeSketch implements INodeSketch {
// 
// 	protected long _nodeId = -1;
// 
// 	public long getNodeId() {
// 		return _nodeId;
// 	}
// 
// 	private INeo4jService _neo4jService;
// 
// 	public INeo4jService getNeo4jService() {
// 		if (_neo4jService == null) {
// 			_neo4jService = Neo4jUtil.getNeo4jService();
// 		}
// 		return _neo4jService;
// 	}
// 
// }


// Path: net.locosoft.fold.sketch.pad/src/net/locosoft/fold/sketch/pad/neo4j/CounterPropertyNode.java
import net.locosoft.fold.neo4j.INeo4jService;
import net.locosoft.fold.sketch.AbstractNodeSketch;
import com.eclipsesource.json.JsonValue;
import net.locosoft.fold.neo4j.ICypher;

/*****************************************************************************
 * Copyright (c) 2015 Chris J Daly (github user cjdaly)
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *   cjdaly - initial API and implementation
 ****************************************************************************/

package net.locosoft.fold.sketch.pad.neo4j;



public class CounterPropertyNode extends AbstractNodeSketch {

	public CounterPropertyNode(long nodeId) {
		_nodeId = nodeId;
	}

	private long crementCounter(String counterPropertyName, boolean isIncrement) {
		INeo4jService neo4jService = getNeo4jService();

		char crementOp = isIncrement ? '+' : '-';
		String cypherText = "MATCH node WHERE id(node)={nodeId}"
				+ " SET node.`" + counterPropertyName + "`=COALESCE(node.`"
				+ counterPropertyName + "`, 0) " + crementOp + " 1"
				+ " RETURN node.`" + counterPropertyName + "`";
		ICypher cypher = neo4jService.constructCypher(cypherText);
		cypher.addParameter("nodeId", getNodeId());

		neo4jService.invokeCypher(cypher);

		JsonValue jsonValue = cypher.getResultDataRow(0);
		return jsonValue.asLong();
	}

	public long incrementCounter(String counterPropertyName) {
		return crementCounter(counterPropertyName, true);
	}

	public long decrementCounter(String counterPropertyName) {
		return crementCounter(counterPropertyName, false);
	}

	public long getCounter(String counterPropertyName) {
		PropertyAccessNode sketch = new PropertyAccessNode(getNodeId());
		JsonValue jsonValue = sketch.getValue(counterPropertyName);
		if ((jsonValue == null) || !jsonValue.isNumber())