use of org.neo4j.ogm.metadata.ClassInfo in project neo4j-ogm by neo4j.
the class DeleteDelegate method delete.
public <T> Object delete(Class<T> clazz, Iterable<Filter> filters, boolean listResults) {
ClassInfo classInfo = session.metaData().classInfo(clazz.getSimpleName());
if (classInfo != null) {
resolvePropertyAnnotations(clazz, filters);
CypherQuery query;
if (classInfo.isRelationshipEntity()) {
query = new RelationshipDeleteStatements().deleteAndList(classInfo.neo4jName(), filters);
} else {
query = new NodeDeleteStatements().deleteAndList(classInfo.neo4jName(), filters);
}
if (listResults) {
return list(query, classInfo.isRelationshipEntity());
}
return count(query, classInfo.isRelationshipEntity());
}
throw new RuntimeException(clazz.getName() + " is not a persistable class");
}
use of org.neo4j.ogm.metadata.ClassInfo in project neo4j-ogm by neo4j.
the class DeleteDelegate method deleteAll.
/**
* Deletes all nodes of a given type. They will get discovered by using the matching label for that type.
* To avoid a delete of every node in the database the method will abort the delete operation if no label
* can be determined.
*
* @param type The type of the nodes/objects to be deleted.
* @param <T> The type to work with
*/
public <T> void deleteAll(Class<T> type) {
ClassInfo classInfo = session.metaData().classInfo(type.getName());
if (classInfo != null) {
String entityLabel = classInfo.neo4jName();
if (entityLabel == null) {
session.warn("Unable to find database label for entity " + type.getName() + " : no results will be returned. Make sure the class is registered, " + "and not abstract without @NodeEntity annotation");
return;
}
Statement request = getDeleteStatementsBasedOnType(type).delete(entityLabel);
RowModelRequest query = new DefaultRowModelRequest(request.getStatement(), request.getParameters());
session.notifyListeners(new PersistenceEvent(type, Event.TYPE.PRE_DELETE));
session.doInTransaction(() -> {
try (Response<RowModel> response = session.requestHandler().execute(query)) {
session.context().removeType(type);
if (session.eventsEnabled()) {
session.notifyListeners(new PersistenceEvent(type, Event.TYPE.POST_DELETE));
}
}
}, Transaction.Type.READ_WRITE);
} else {
session.warn(type.getName() + " is not a persistable class");
}
}
use of org.neo4j.ogm.metadata.ClassInfo in project neo4j-ogm by neo4j.
the class DeleteDelegate method deleteOneOrMoreObjects.
// TODO : this is being done in multiple requests at the moment, one per object. Why not put them in a single request?
private void deleteOneOrMoreObjects(List<?> objects, Set<Object> neighbours) {
Set<Object> notified = new HashSet<>();
if (session.eventsEnabled()) {
for (Object affectedObject : neighbours) {
if (!notified.contains(affectedObject)) {
session.notifyListeners(new PersistenceEvent(affectedObject, Event.TYPE.PRE_SAVE));
notified.add(affectedObject);
}
}
}
for (Object object : objects) {
MetaData metaData = session.metaData();
ClassInfo classInfo = metaData.classInfo(object);
if (classInfo == null) {
session.warn(object.getClass().getName() + " is not an instance of a persistable class");
} else {
MappingContext mappingContext = session.context();
Long id = mappingContext.optionalNativeId(object).filter(possibleId -> possibleId >= 0).orElseGet(() -> {
session.warn(String.format("Instance of class %s has to be reloaded to be deleted. This can happen if the session has " + "been cleared between loading and deleting or using an object from a different transaction.", object.getClass()));
return classInfo.getPrimaryIndexOrIdReader().apply(object).map(primaryIndexOrId -> session.load(object.getClass(), (Serializable) primaryIndexOrId)).flatMap(reloadedObject -> mappingContext.optionalNativeId(reloadedObject)).orElse(-1L);
});
if (id >= 0) {
Statement request = getDeleteStatement(object, id, classInfo);
if (session.eventsEnabled() && !notified.contains(object)) {
session.notifyListeners(new PersistenceEvent(object, Event.TYPE.PRE_DELETE));
notified.add(object);
}
RowModelRequest query = new DefaultRowModelRequest(request.getStatement(), request.getParameters());
session.doInTransaction(() -> {
try (Response<RowModel> response = session.requestHandler().execute(query)) {
if (request.optimisticLockingConfig().isPresent()) {
List<RowModel> rowModels = response.toList();
session.optimisticLockingChecker().checkResultsCount(rowModels, request);
}
if (metaData.isRelationshipEntity(classInfo.name())) {
session.detachRelationshipEntity(id);
} else {
session.detachNodeEntity(id);
}
// enabled in the first place.
if (notified.contains(object)) {
session.notifyListeners(new PersistenceEvent(object, Event.TYPE.POST_DELETE));
}
}
}, Transaction.Type.READ_WRITE);
}
}
}
if (session.eventsEnabled()) {
for (Object affectedObject : neighbours) {
if (notified.contains(affectedObject)) {
session.notifyListeners(new PersistenceEvent(affectedObject, Event.TYPE.POST_SAVE));
}
}
}
}
use of org.neo4j.ogm.metadata.ClassInfo in project neo4j-ogm by neo4j.
the class ExecuteQueriesDelegate method countEntitiesOfType.
public long countEntitiesOfType(Class<?> entity) {
ClassInfo classInfo = session.metaData().classInfo(entity.getName());
if (classInfo == null) {
return 0;
}
CypherQuery countStatement;
if (classInfo.isRelationshipEntity()) {
ClassInfo startNodeInfo = null;
ClassInfo endNodeInfo = null;
for (FieldInfo fieldInfo : classInfo.fieldsInfo().fields()) {
if (fieldInfo.hasAnnotation(StartNode.class)) {
startNodeInfo = session.metaData().classInfo(DescriptorMappings.getType(fieldInfo.getTypeDescriptor()).getName());
} else if (fieldInfo.hasAnnotation(EndNode.class)) {
endNodeInfo = session.metaData().classInfo(DescriptorMappings.getType(fieldInfo.getTypeDescriptor()).getName());
}
if (endNodeInfo != null && startNodeInfo != null) {
break;
}
}
String start = startNodeInfo.neo4jName();
String end = endNodeInfo.neo4jName();
String type = classInfo.neo4jName();
countStatement = new CountStatements().countEdges(start, type, end);
} else {
Collection<String> labels = classInfo.staticLabels();
if (labels.isEmpty()) {
return 0;
}
countStatement = new CountStatements().countNodes(labels);
}
return session.doInTransaction(() -> {
try (Response<RowModel> response = session.requestHandler().execute((RowModelRequest) countStatement)) {
RowModel queryResult = response.next();
return queryResult == null ? 0 : ((Number) queryResult.getValues()[0]).longValue();
}
}, Transaction.Type.READ_ONLY);
}
use of org.neo4j.ogm.metadata.ClassInfo in project neo4j-ogm by neo4j.
the class LoadByIdsDelegate method loadAll.
public <T, ID extends Serializable> Collection<T> loadAll(Class<T> type, Collection<ID> ids, SortOrder sortOrder, Pagination pagination, int depth) {
Optional<String> labelsOrType = session.determineLabelsOrTypeForLoading(type);
if (!labelsOrType.isPresent()) {
LOG.warn("Unable to find database label for entity " + type.getName() + " : no results will be returned. Make sure the class is registered, " + "and not abstract without @NodeEntity annotation");
return Collections.emptyList();
}
QueryStatements<ID> queryStatements = session.queryStatementsFor(type, depth);
ClassInfo classInfo = session.metaData().classInfo(type.getName());
PagingAndSortingQuery qry = queryStatements.findAllByType(labelsOrType.get(), convertIfNeeded(classInfo, ids), depth).setSortOrder(sortOrder).setPagination(pagination);
GraphModelRequest request = new DefaultGraphModelRequest(qry.getStatement(), qry.getParameters());
return session.doInTransaction(() -> {
try (Response<GraphModel> response = session.requestHandler().execute(request)) {
Iterable<T> mapped = new GraphRowModelMapper(session.metaData(), session.context(), session.getEntityInstantiator()).map(type, response);
if (sortOrder.sortClauses().isEmpty()) {
return sortResultsByIds(type, ids, mapped);
}
Set<T> results = new LinkedHashSet<>();
for (T entity : mapped) {
if (includeMappedEntity(ids, entity)) {
results.add(entity);
}
}
return results;
}
}, Transaction.Type.READ_ONLY);
}
Aggregations