/* * Copyright (c) [2011-2015] "Neo Technology" / "Graph Aware Ltd." * * This product is licensed to you under the Apache License, Version 2.0 (the "License"). * You may not use this product except in compliance with the License. * * This product may include a number of subcomponents with separate copyright notices and license terms. Your use of the source code for these subcomponents is subject to the terms and conditions of the subcomponent's license, as noted in the LICENSE file. * * */ package org.neo.services; import org.neo.Neo4jSessionFactory; import org.neo.models.Entity; import org.neo4j.ogm.session.Session; public abstract class GenericService<T> implements Service<T> { private static final int DEPTH_LIST = 0; private static final int DEPTH_ENTITY = 1; private Session session = Neo4jSessionFactory.getInstance().getNeo4jSession(); @Override public Iterable<T> findAll() { return session.loadAll(getEntityType(), DEPTH_LIST); } @Override public T find(Long id) { return session.load(getEntityType(), id, DEPTH_ENTITY); } @Override public void delete(Long id) { session.delete(session.load(getEntityType(), id)); } @Override public T createOrUpdate(T entity) { session.save(entity, DEPTH_ENTITY); Entity ent = (Entity) entity; if(ent != null) { return find(ent.getId()); } return null; } public abstract Class<T> getEntityType(); }