Now I am trying to generalize the apoc.cypher.run function :
unwind ['p.reorderLevel','null'] as ReordLevel
unwind ['s.supplierId', 'null'] as Supplier
call apoc.cypher.run('
MATCH (p:Product)-[:SUPPLIERS]->(s:Supplier)
RETURN '+ReordLevel+' as ReordLevel, '+Supplier+' as Supplier, sum(p.unitsInStock) as SommeUnits ORDER BY ReordLevel', {} )
yield value as row
return row. ReordLevel as ReordLevel, row.Supplier as Supplier, row.SommeUnits as SommeUnits
order by ReordLevel, Supplier
by creating my own function in Java.
As first approach I tried to generalize every row of the apoc.cypher.run function and I wrote this :
package example;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.Result;
import org.neo4j.procedure.Context;
import org.neo4j.procedure.Description;
import org.neo4j.procedure.Name;
import org.neo4j.procedure.UserFunction;
/**
* This is an example how you can create a simple user-defined function for Neo4j.
*/
public class Agreggate
{
@Context public GraphDatabaseService graphDb;
@UserFunction
@Description("example.cube(['s1','s2',...], operation)")
public String cube(
@Name("strings") List<String> strings,
@Name("operation") String operation,
@Name("aggregateName") String aggregateName,
@Name("table") String table) {
Map<String,Object> parameters = new HashMap<String, Object>();
String str1, str3, str4;
str1 = "unwind ['p.strings.get(0)', 'null'] as strings.get(0)";
str3 = "RETURN '+strings.get(0)+' as strings.get(0) ";
str4 = "RETURN row.strings.get(0) as strings.get(0) ";
parameters.put("strings.get(0)", strings.get(0));
for(int i = 1; i<strings.size(); i++) {
parameters.put("strings.get(i)",strings.get(i));
str1 = str1 + "\n" + "unwind ['strings.get(i)', 'null'] as strings.get(i)";
str3 = str3 + ", '+strings.get(i)+' as strings.get(i) ";
str4 = str4 + ", row.strings.get(0) as strings.get(0) ";
}
str3 = str3 + " ORDER BY " + strings.get(0);
str4 = str4 + ", row.aggregateName as aggregateName";
parameters.put("operation", operation);
parameters.put("aggregateName", aggregateName);
parameters.put("table", table);
Result result = graphDb.execute(str1 +
"call apoc.cypher.run('\n" +
"MATCH (p:table)\n" +
str3 + ", {} ) \n" +
"yield value as row\n" +
str4, parameters);
return result.toString();
}
The function doesn't work, because I have some problems with what should I return and also because I don't know if this is the best approach in order to generalize the apoc.cypher.run function. Could you help me with a suggestion ?