Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Op 125 #746

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,29 @@

package org.meveo.persistence.neo4j.helper;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import javax.inject.Inject;

import org.apache.commons.beanutils.BeanUtils;
import org.meveo.model.CustomEntity;
import org.meveo.model.persistence.DBStorageType;
import org.meveo.model.persistence.JacksonUtil;
import org.meveo.persistence.CrossStorageTransaction;
import org.meveo.service.storage.RepositoryService;
import org.neo4j.driver.Record;
import org.neo4j.driver.Result;
import org.neo4j.driver.Transaction;
import org.neo4j.driver.Value;
import org.neo4j.driver.internal.InternalNode;
import org.neo4j.driver.internal.types.TypeConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -86,6 +99,35 @@ public void execute(String neo4jConfiguration, String request, Map<String, Obje
public void execute(String neo4jConfiguration, String request, Map<String, Object> parameters){
execute(neo4jConfiguration, request, parameters, null, null);
}

public <T> List<T> execute(String neo4jConfiguration, String request, Map<String, Object> parameters, Class<T> entityClass){

CypherResultTransformer<List<T> > cypherResultTransformer = new CypherResultTransformer<List<T> >() {

@Override
public List<T> execute(Transaction transaction, Result result) {
List<T> output = new ArrayList<T>();
while(result.hasNext()) {
Record record = result.next();
List<Value> elements = record.values();
for(Value elem : elements) {
Map<String, Object> props = elem.asMap();
try {
T entity = entityClass.getConstructor().newInstance();
BeanUtils.populate(entity, props);
output.add(entity);
} catch (InstantiationException | IllegalAccessException | InvocationTargetException | IllegalArgumentException | NoSuchMethodException | SecurityException e) {
log.error("Couldn't populate entity " + entityClass.toString(), e);
continue;
}
}
}

return output;
}
};
return execute(neo4jConfiguration, request, parameters, cypherResultTransformer, null);
}

@SuppressWarnings("javadoc")
public void update(
Expand Down