view src/main/java/org/mpi/openmind/repository/services/AbstractPersistenceService.java @ 26:5e24413d355b

Fixed bug that deleted all attributes in the system if a relation had id=null :-(
author Robert Casties <casties@mpiwg-berlin.mpg.de>
date Fri, 05 Aug 2016 22:02:18 +0200
parents c23ae718fdd3
children 8ea716da439f
line wrap: on
line source

package org.mpi.openmind.repository.services;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.hibernate.Query;
import org.hibernate.Session;
import org.mpi.openmind.configuration.ConfigurationService;
import org.mpi.openmind.repository.bo.Attribute;
import org.mpi.openmind.repository.bo.Entity;
import org.mpi.openmind.repository.bo.Node;
import org.mpi.openmind.repository.bo.Relation;
import org.mpi.openmind.repository.bo.View;
import org.mpi.openmind.repository.bo.utils.Sequence;
import org.mpi.openmind.repository.utils.HibernateUtil;
import org.mpi.openmind.repository.utils.OwnValueGenerator;

/**
 * 
 * @author jurzua
 */
public abstract class AbstractPersistenceService {

	private ConfigurationService configurationService;

	private OwnValueGenerator ownValueGenerator;

	private final static String NODE_SEQUENCE = "nodeSequence";

	private static Logger logger = Logger
			.getLogger(AbstractPersistenceService.class);

	private boolean importModus = false;

	/*
	 * static { logger.setLevel(Level.DEBUG); PatternLayout layout = new
	 * PatternLayout( "%d{ABSOLUTE} %5p %c{1}:%L - %m%n"); Appender stdout = new
	 * ConsoleAppender(layout, "System.out"); logger.addAppender(stdout); }
	 */

	public AbstractPersistenceService() {
	}

	public String generateOwnValue(Entity entity) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		String ownValue = null;

		try {
			session.getTransaction().begin();
			ownValue = this.ownValueGenerator.generateOwnValue(entity, session);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			// e.printStackTrace();
		} finally {
			session.getTransaction().commit();
		}

		return ownValue;
	}

	/**
	 * Performs the actual disabling of the Hibernate second-level cache.
	 */
	/*
	protected void disableSecondLevelCache() {
		Map<Object, Cache> cacheRegionsMap = ((SessionFactoryImpl) HibernateUtil
				.getSessionFactory()).getAllSecondLevelCacheRegions();
		Collection<Cache> cacheRegions = cacheRegionsMap.values();
		for (Cache cache : cacheRegions) {
			cache.clear();
		}
	}*/

	public int dropAssertions() {
		int row = 0;
		try {
			Session session = HibernateUtil.getSessionFactory()
					.getCurrentSession();
			session.beginTransaction();
			String sql = "DELETE FROM Node WHERE type = 'ABox'";
			Query query = session.createQuery(sql);
			row = query.executeUpdate();

			logger.info("Drop assertions - rows deleted= " + row);

			session.getTransaction().commit();
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			// e.printStackTrace();
		}
		return row;
	}

	public int dropDefinitions() {
		int row = 0;
		try {
			Session session = HibernateUtil.getSessionFactory()
					.getCurrentSession();
			session.beginTransaction();
			String sql = "DELETE FROM Node WHERE type = 'TBox'";
			Query query = session.createQuery(sql);
			row = query.executeUpdate();

			logger.info("Drop definitions - rows deleted= " + row);

			session.getTransaction().commit();
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			// e.printStackTrace();
		}
		return row;
	}

	public void saveOrUpdateObject(Object obj) {
		try {
			Session session = HibernateUtil.getSessionFactory()
					.getCurrentSession();
			session.getTransaction().begin();
			session.saveOrUpdate(obj);
			session.getTransaction().commit();
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
	}
	
	private void saveNode0(Session session, Node node, Sequence idSequence){
		node.autoNormalize();
		node.setSystemStatus(Node.SYS_STATUS_CURRENT_VERSION);
		
		if(node.getModificationTime() == null){
			node.setModificationTime(System.currentTimeMillis());
		}	
		if(node.getVersion() == null){
			node.increaseVersion();
		}
		
		if (node.getId() == null){
			node.setId(idSequence.generateId());
		}	
		
		if (node.getRowId() == null){
			session.save(node);
		}else{
			session.merge(node);
		}
			
		logger.info("Saved node\t" + node);
	}

	public void saveNode(Node node) throws Exception {

		logger.info("Saving node\t" + node);

		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.getTransaction().begin();

		Sequence idSequence = this.getIdSequence(session);

		this.saveNode0(session, node, idSequence);
		
		session.save(idSequence);

		session.getTransaction().commit();
		logger.info("Node saved\t" + node);
	}

	public void saveNodeList(List<Node> nodeList) throws Exception{

		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.getTransaction().begin();

		Sequence idSequence = this.getIdSequence(session);
		/*
		long start = System.currentTimeMillis();
		DecimalFormat df = new DecimalFormat("#.##");
		int counter = 0;
		*/
		logger.info("##### Saving Node List: " + nodeList.size() + " #####");

		for (Node node : nodeList) {
			this.saveNode0(session, node, idSequence);
			/*
			double percent = ((double) counter / (double) nodeList.size()) * 100.0;
			long diff = System.currentTimeMillis() - start;
			double min = (double) diff / (double) (60 * 1000);

			if ((percent % 10) < 0.005) {
				logger.debug("\n[" + df.format(percent) + " %] counter: "
						+ counter + " / " + nodeList.size());

				logger.debug("Tempo: " + (double) counter / min);
			}
			counter++;
			*/
		}

		session.save(idSequence);
		session.getTransaction().commit();
	}

	/**
	 * <p>
	 * This method delete the nodes and its history.
	 * </p>
	 * 
	 * @param nodeList
	 */
	public void removeNode(Node node) {
		try {
			Session session = HibernateUtil.getSessionFactory()
					.getCurrentSession();
			session.getTransaction().begin();

			this.removeNodesById(node.getId(), session);

			session.getTransaction().commit();
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			// e.printStackTrace();
		}
	}

	/**
	 * <p>
	 * This method delete the nodes and its history.
	 * </p>
	 * 
	 * @param nodeList
	 */
	public void removeNodeList(List<Node> nodeList) {
		try {
			Session session = HibernateUtil.getSessionFactory()
					.getCurrentSession();
			session.getTransaction().begin();

			logger.debug("##### Deleting Node List: " + nodeList.size()
					+ " #####");

			for (Node node : nodeList) {
				this.removeNodesById(node.getId(), session);
			}

			session.getTransaction().commit();
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			// e.printStackTrace();
		}
	}

	/**
	 * Save every part of the entity but not generate new versions nor ownValue
	 * 
	 * @param nodeList
	 */
	public void saveEntityListAsNode(List<Entity> entList) {
		int entitiesSize = 0;
		int nodeSize = 0;
		long start = System.currentTimeMillis();
		try {
			Session session = HibernateUtil.getSessionFactory()
					.getCurrentSession();
			session.getTransaction().begin();

			Sequence idSequence = this.getIdSequence(session);

			DecimalFormat df = new DecimalFormat("#.##");
			int counter = 0;

			List<Node> nodeList = generateNodeList(entList);
			logger.debug("##### Saving Node List #####");
			logger.debug("Entities: " + entList.size() + " #####");
			logger.debug("Nodes: " + nodeList.size() + " #####");

			entitiesSize = entList.size();
			nodeSize = nodeList.size();

			for (Node node : nodeList) {

				node.autoNormalize();
				
				if (node.getId() == null)
					node.setId(idSequence.generateId());
				if (node.getRowId() == null)
					session.save(node);
				else
					session.merge(node);

				double percent = ((double) counter / (double) nodeList.size()) * 100.0;
				long diff = System.currentTimeMillis() - start;
				double min = (double) diff / (double) (60 * 1000);

				if ((percent % 10) < 0.005) {
					logger.debug("\n[" + df.format(percent) + " %] counter: "
							+ counter + " / " + nodeList.size());

					logger.debug("Tempo: " + (double) counter / min);
				}
				counter++;
			}

			session.save(idSequence);
			session.getTransaction().commit();

			StringBuilder sb = new StringBuilder();
			sb.append("\n\t#### saveEntityListAsNode ####\n");
			sb.append("\tentitiesSize=\t" + entitiesSize + "\n");
			sb.append("\tnodeSize=\t" + nodeSize + "\n");
			sb.append("\ttime[ms]=\t" + (System.currentTimeMillis() - start)
					+ "\n");
			logger.info(sb.toString());

		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			e.printStackTrace();
		}
	}

	private List<Node> generateNodeList(List<Entity> list) {
		List<Node> list0 = new ArrayList<Node>();
		for (Entity ent : list) {
			list0.add(ent);
			for (Attribute att : ent.getAttributes()) {
				list0.add(att);
			}
			for (Relation rel : ent.getSourceRelations()) {
				list0.add(rel);
			}
			for (Relation rel : ent.getTargetRelations()) {
				list0.add(rel);
			}
		}
		return list0;
	}

	/**
	 * @param nodeList
	 */
	public void saveEntityListAsNodeWithoutContent(List<Entity> nodeList)
			throws Exception {
		long start = System.currentTimeMillis();

		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.getTransaction().begin();

		Sequence idSequence = this.getIdSequence(session);
		DecimalFormat df = new DecimalFormat("#.##");
		int counter = 0;

		logger.debug("##### Saving Node List: " + nodeList.size() + " #####");

		for (Entity node : nodeList) {
			node.autoNormalize();
			if (node.getId() == null)
				node.setId(idSequence.generateId());
			if (node.getRowId() == null)
				session.save(node);
			else
				session.merge(node);
			double percent = ((double) counter / (double) nodeList.size()) * 100.0;
			long diff = System.currentTimeMillis() - start;
			double min = (double) diff / (double) (60 * 1000);

			if ((percent % 10) < 0.005) {
				logger.debug("\n[" + df.format(percent) + " %] counter: "
						+ counter + " / " + nodeList.size());

				logger.debug("Tempo: " + (double) counter / min);
			}
			counter++;
		}

		session.save(idSequence);
		session.getTransaction().commit();
	}

	public void deleteEntityList(List<Entity> entities) {
		try {
			Session session = HibernateUtil.getSessionFactory()
					.getCurrentSession();
			session.getTransaction().begin();

			for (Entity ent : entities) {
				this.removePersistenceEntity(ent, session);
			}

			session.getTransaction().commit();
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			// e.printStackTrace();
		}
	}

	public void saveEntityList(List<Entity> entities) throws Exception {		
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.getTransaction().begin();

		for (Entity entity : entities) {
			saveEntity0(session, entity);
		}

		session.getTransaction().commit();		
	}
	
	public Map<Long, Long> saveEntityListAsNew(List<Entity> entities,
			Map<Long, Long> idMap) throws Exception {

		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.getTransaction().begin();
		Sequence idSequence = this.getIdSequence(session);

		for (Entity entity : entities) {

			Long oldId = entity.getId();
			entity.resetId();
			entity.resetRowId();
			this.saveEntity0(session, entity);
			idMap.put(oldId, entity.getId());
		}

		session.save(idSequence);
		session.getTransaction().commit();
		return idMap;
	}

	/**
	 * <p>
	 * This method deletes all entities by type (TBox/ABox), it means the method
	 * deletes either all concepts or all assertions.
	 * </p>
	 * <p>
	 * The history of every deleted entity will be removed too.
	 * </p>
	 * 
	 * @param type
	 *            Node.TYPE_TBOX or Node.TYPE_ABOX
	 */
	@Deprecated
	public void deleteEntities(Long id, String type, Boolean deleteHistory) {
		if (!(Node.TYPE_ABOX.equals(type) || Node.TYPE_TBOX.equals(type))) {
			throw new IllegalArgumentException(
					"The parameter 'type' should be either: " + Node.TYPE_ABOX
							+ " or " + Node.TYPE_TBOX + ", but was: " + type);
		}

		List<Entity> entList = this.getEntities(id,
				Node.SYS_STATUS_CURRENT_VERSION, type, null);

		// loading previous versions
		List<Entity> historyEntList = new ArrayList<Entity>();
		if (deleteHistory) {
			for (Entity ent : entList) {
				historyEntList.addAll(this.getEntities(ent.getId(),
						Node.SYS_STATUS_PREVIOUS_VERSION, type, null));
			}
		}

		try {
			Session session = HibernateUtil.getSessionFactory()
					.getCurrentSession();
			session.getTransaction().begin();

			for (Entity ent : entList) {
				this.removePersistenceEntity(ent, session);
			}
			if (deleteHistory) {
				for (Entity ent : historyEntList) {
					this.removePersistenceEntity(ent, session);
				}
			}

			session.getTransaction().commit();
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			// e.printStackTrace();
		}
	}

	/**
	 * This deleting is made using JDBC and not hibernate. The history is too
	 * removed by this method. TODO test what could happen with the hibernate
	 * cache
	 * 
	 * @param ent
	 * @param session
	 */
	private void removePersistenceEntity(Entity ent, Session session) {
		for (Attribute att : ent.getAttributes()) {
			// session.delete(att);
			removeNodesById(att.getId(), session);
			for (View view : att.getViews()) {
				// session.delete(view);
				removeNodesById(view.getId(), session);
			}
		}
		for (Relation rel : ent.getSourceRelations()) {
			// session.delete(rel);
			removeNodesById(rel.getId(), session);
			for (View view : rel.getViews()) {
				// session.delete(view);
				removeNodesById(view.getId(), session);
			}
		}
		for (Relation rel : ent.getTargetRelations()) {
			// session.delete(rel);
			removeNodesById(rel.getId(), session);
			for (View view : rel.getViews()) {
				// session.delete(view);
				removeNodesById(view.getId(), session);
			}
		}
		for (View view : ent.getViews()) {
			// session.delete(view);
			removeNodesById(view.getId(), session);
		}
		// session.delete(ent);
		removeNodesById(ent.getId(), session);
	}

	private int removeNodesById(Long id, Session session) {
		String sql = "DELETE FROM Node WHERE id = :id";
		Query query = session.createQuery(sql);
		query.setLong("id", id);
		return query.executeUpdate();
	}

	/**
	 * <p>
	 * Changes the system status of the entity from CURRENT_VERSION to
	 * PREVIOUS_VERSION.
	 * </p>
	 * 
	 * <p>
	 * It means, the entity will stay in the DB, but the it will not be visible
	 * by the ordinary methods.
	 * </p>
	 * 
	 * @param entity
	 */
	public void removeEntCurrentVersion(Long entId, String type)
			throws Exception {
		logger.info("Deleting entity [ID=" + entId + ", type=" + type
				+ "]. But keeping history in DB.");
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.getTransaction().begin();

		if (entId != null) {
			//this method get the current version. Related to the relation, the time_modification is not consider,
			//because it it producing problems. 
			List<Entity> previousEntityList = 
					this.getEntities(session, entId,
							Node.SYS_STATUS_CURRENT_VERSION, 
							type, null, false);
			if (previousEntityList.size() > 0) {
				if (previousEntityList.size() > 1) {
					System.err
							.println("[PersistenceService.saveEntity] found more than one current entities!");
				}

				Entity previousEntity = previousEntityList.get(0);
				logger.info("Saving previous entity: " + previousEntity);
				this.savePreviousEntity(session, previousEntity);
			}
		}
		session.getTransaction().commit();
	}

	/*
	 * public void removeEntity(Long entId, String type) {
	 * logger.info("Deleting entity [ID=" + entId + ", type=" + type +
	 * "]. But keeping history in DB."); Session session =
	 * HibernateUtil.getSessionFactory().getCurrentSession();
	 * session.getTransaction().begin();
	 * 
	 * if (entId != null) { List<Entity> previousEntityList =
	 * this.getEntities(session, entId, Node.SYS_STATUS_CURRENT_VERSION, type,
	 * null); if (previousEntityList.size() > 0) { if (previousEntityList.size()
	 * > 1) { System.err .println(
	 * "[PersistenceService.saveEntity] found more than one current entities!");
	 * }
	 * 
	 * Entity previousEntity = previousEntityList.get(0);
	 * logger.info("Saving previous entity: " + previousEntity);
	 * this.savePreviousEntity(session, previousEntity); } } }
	 */

	public void savePreviousEntity(Entity entity) throws Exception {

		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.getTransaction().begin();

		if (entity.getId() != null) {
			List<Entity> previousEntityList = this.getEntities(session,
					entity.getId(), Node.SYS_STATUS_CURRENT_VERSION,
					entity.getType(), null, false);
			if (previousEntityList.size() > 0) {
				if (previousEntityList.size() > 1) {
					logger.error("[PersistenceService.saveEntity] found more than one current entities!");
				}
				Entity previousEntity = previousEntityList.get(0);
				logger.debug("Saving previous entity: " + previousEntity);
				this.savePreviousEntity(session, previousEntity);
			}
		}
		this.saveCurrentEntity(session, entity, null);
		session.getTransaction().commit();
	}

	/**
	 * 
	 * @param entity
	 * @return
	 */
	public void saveEntity(Entity entity) throws Exception {

		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.getTransaction().begin();

		saveEntity0(session, entity);

		session.getTransaction().commit();

	}

	private void saveEntity0(Session session, Entity entity) throws Exception {
		if (entity.getId() != null) {
			List<Entity> previousEntityList = this.getEntities(session, entity.getId(), Node.SYS_STATUS_CURRENT_VERSION, entity.getType(), null, false);
			if (previousEntityList.size() > 0) {
				if (previousEntityList.size() > 1) {
					logger.error("[PersistenceService.saveEntity] found more than one current entities!");
				}

				Entity previousEntity = previousEntityList.get(0);
				logger.info("Saving previous entity: " + previousEntity);
				this.savePreviousEntity(session, previousEntity);
			}
		}
		this.saveCurrentEntity(session, entity, null);
	}

	private void saveCurrentEntity(Session session, Entity entity,
			Sequence idSequence) throws Exception {
		Long time = System.currentTimeMillis();
		entity.setSystemStatus(Node.SYS_STATUS_CURRENT_VERSION);
		entity.resetRowId();
		entity.increaseVersion();
		entity.setObjectClass(entity.getObjectClass());
		entity.setModificationTime(time);
		entity.setType(entity.getType());
		entity.setUser(entity.getUser());
		entity.autoNormalize();
		// generating of id, connecting rels, atts and views to the entity
		this.prepareEntity(session, entity, idSequence);
		this.saveEntity(session, entity);
	}

	/**
	 * Here the modification time will be propagated from the entity to its
	 * relations an attributes.
	 * 
	 * @param session
	 * @param entity
	 * @return
	 */
	private Entity prepareEntity(Session session, Entity entity,
			Sequence idSequence) {

		if (entity.getId() == null) {
			if (idSequence == null)
				entity.setId(this.generateId(session));
			else
				entity.setId(idSequence.generateId());
		}

		for (Attribute att : entity.getAttributes()) {
			if (att.getId() == null) {
				if (idSequence == null)
					att.setId(this.generateId(session));
				else
					att.setId(idSequence.generateId());
			}
			logger.debug("setting att source to entity: "+entity);
			att.setSourceId(entity.getId());
			att.setSourceModif(entity.getModificationTime());
			att.setSourceObjectClass(entity.getObjectClass());
			att.autoNormalize();
		}

		// TODO normalize the name of the relations??
		for (Relation rel : entity.getSourceRelations()) {
			if (rel.getId() == null) {
				if (idSequence == null)
					rel.setId(this.generateId(session));
				else
					rel.setId(idSequence.generateId());
			}
			rel.setSourceId(entity.getId());
			rel.setSourceModif(entity.getModificationTime());
			rel.setSourceObjectClass(entity.getObjectClass());
			rel.autoNormalize();
			if(StringUtils.equals(entity.getType(), Node.TYPE_ABOX)){
				rel.setObjectClass(rel.getOwnValue());				
			}else if(StringUtils.equals(entity.getType(), Node.TYPE_TBOX)){
				rel.setObjectClass(Node.TYPE_TBOX);
			}
			
			// new Attributes for relations
			for (Attribute att : rel.getAttributes()) {
				if (att.getId() == null) {
					if (idSequence == null)
						att.setId(this.generateId(session));
					else
						att.setId(idSequence.generateId());
				}
	            logger.debug("setting att source to (source)relation: "+rel);
				att.setSourceId(rel.getId());
				att.setSourceModif(rel.getModificationTime());
				// TODO this should be analyzed
				att.setSourceObjectClass(rel.getOwnValue());
				att.autoNormalize();
			}
		}

		for (Relation rel : entity.getTargetRelations()) {
			if (rel.getId() == null) {
				if (idSequence == null)
					rel.setId(this.generateId(session));
				else
					rel.setId(idSequence.generateId());
			}
			rel.setTargetId(entity.getId());
			rel.setTargetModif(entity.getModificationTime());
			rel.setTargetObjectClass(entity.getObjectClass());
			rel.autoNormalize();
			if(StringUtils.equals(entity.getType(), Node.TYPE_ABOX)){
				rel.setObjectClass(rel.getOwnValue());				
			}else if(StringUtils.equals(entity.getType(), Node.TYPE_TBOX)){
				rel.setObjectClass(Node.TYPE_TBOX);
			}
			// new Attributes for relations
			for (Attribute att : rel.getAttributes()) {
				if (att.getId() == null) {
					if (idSequence == null)
						att.setId(this.generateId(session));
					else
						att.setId(idSequence.generateId());
				}
                logger.debug("setting att source to (target)relation: "+rel);
				att.setSourceId(rel.getId());
				att.setSourceModif(rel.getModificationTime());
				// TODO this should be analyzed
				att.setSourceObjectClass(rel.getOwnValue());
				att.autoNormalize();
			}
		}
		for (View view : entity.getViews()) {
			if (view.getId() == null) {
				if (idSequence == null)
					view.setId(this.generateId(session));
				else
					view.setId(idSequence.generateId());
			}
			view.setSourceId(entity.getId());
			view.setSourceModif(entity.getModificationTime());
			view.setSourceObjectClass(entity.getObjectClass());
			view.autoNormalize();
		}
		return entity;
	}

	/**
	 * Saves an entity setting its system state as previous_version
	 * 
	 * @param session
	 * @param entity
	 */
	private void savePreviousEntity(Session session, Entity entity) {
		entity.setSystemStatus(Node.SYS_STATUS_PREVIOUS_VERSION);
		session.save(entity);
		for (Attribute attribute : entity.getAttributes()) {
            logger.debug("  saving prev entity attribute: "+attribute);
			session.save(attribute);
		}
		for (Relation rel : entity.getSourceRelations()) {
			session.save(rel);
			for (Attribute att : rel.getAttributes()) {
	            logger.debug("  saving prev (source)relation attribute: "+rel);
				session.save(att);
			}
		}
		for (Relation rel : entity.getTargetRelations()) {
			session.save(rel);
            for (Attribute att : rel.getAttributes()) {
                logger.debug("  saving prev (target)relation attribute: "+rel);
                session.save(att);
            }
		}
		for (View view : entity.getViews()) {
			session.save(view);
		}
	}

	private void saveEntity(Session session, Entity entity) throws Exception {
		logger.info("Saving current entity: " + entity);
		session.save(entity);
		for (Attribute attribute : entity.getAttributes()) {
		    logger.debug("  saving entity attribute: "+attribute);
			session.save(attribute);
		}
		for (Relation rel : entity.getSourceRelations()) {
			rel.setSource(entity);
			if (rel.getSourceId() == null || rel.getTargetId() == null) {
				throw new IllegalStateException(
						"Either the sourceId or the targetId was not initialized to the source relation: "
								+ rel.getOwnValue());
			}
			session.save(rel);
			for (Attribute att : rel.getAttributes()) {
	            logger.debug("  saving (source)relation attribute: "+att);
				session.save(att);
			}
		}
		for (Relation rel : entity.getTargetRelations()) {
			rel.setTarget(entity);
			if (rel.getSourceId() == null || rel.getTargetId() == null) {
				throw new IllegalStateException(
						"Either the sourceId or the targetId was not initialized to the target relation: "
								+ rel.getOwnValue());
			}
			session.save(rel);
            for (Attribute att : rel.getAttributes()) {
                logger.debug("  saving (target)relation attribute: "+att);
                session.save(att);
            }
		}

		// Call of ownValue Generator.
		// TODO: the method should be used always? what would happen if the
		// ownValue returns null?
		if (!isImportModus()) {
			String ownValue = this.getOwnValueGenerator().generateOwnValue(
					entity, session);
			if (StringUtils.isNotEmpty(ownValue)) {
				entity.setOwnValue(ownValue);
				entity.autoNormalize();
				session.save(entity);
			}
		}
	}

	public OwnValueGenerator getOwnValueGenerator() {
		return ownValueGenerator;
	}

	public void setOwnValueGenerator(OwnValueGenerator ownValueGenerator) {
		this.ownValueGenerator = ownValueGenerator;
	}

	/**
	 * <p>
	 * Returns entities with their corresponded contain.
	 * </p>
	 * 
	 * @param session
	 * @param id
	 * @param systemStatus
	 * @param type
	 * @param ownValue
	 * @return
	 */
	private List<Entity> getEntities(Session session, Long id,
			String systemStatus, String type, String ownValue, boolean considerTimeModif) {

		if (!(systemStatus.equals(Node.SYS_STATUS_PREVIOUS_VERSION) || systemStatus
				.equals(Node.SYS_STATUS_CURRENT_VERSION))) {
			throw new IllegalArgumentException("Invalid input systemStatus: "
					+ systemStatus);
		}

		if (StringUtils.isNotEmpty(type)
				&& !(type.equals(Node.TYPE_ABOX) || type.equals(Node.TYPE_TBOX))) {
			throw new IllegalArgumentException("Invalid input type: " + type);
		}

		List<Entity> entities;

		String hqlEntities = "from Entity where systemStatus = :systemStatus";

		if (StringUtils.isNotEmpty(type)) {
			hqlEntities += " AND type = :type";
		}

		if (id != null) {
			hqlEntities += " AND id = :id";
		}

		if (StringUtils.isNotEmpty(ownValue)) {
			hqlEntities += " AND ownValue = :ownValue";
		}

		Query queryEntities = session.createQuery(hqlEntities);
		queryEntities.setString("systemStatus", systemStatus);

		if (StringUtils.isNotEmpty(type)) {
			queryEntities.setString("type", type);
		}

		if (StringUtils.isNotEmpty(ownValue)) {
			queryEntities.setString("ownValue", ownValue);
		}

		if (id != null) {
			queryEntities.setLong("id", id);
		}
		entities = queryEntities.list();

		for (Entity entity : entities) {
			entity.setLightweight(true);
			entity = this.getEntityContent(session, entity, considerTimeModif);
		}

		return entities;
	}

	/**
	 * <p>
	 * Returns entities with their corresponded contain.
	 * </p>
	 * 
	 * @param id
	 * @param systemStatus
	 * @param type
	 * @param ownValue
	 * @return
	 */
	public List<Entity> getEntities(Long id, String systemStatus, String type,
			String ownValue) {
		logger.debug("GET ENTITIES Entities [id=" + id + ", type=" + type
				+ ", ownValue=" + ownValue + "]");

		if (!(systemStatus.equals(Node.SYS_STATUS_PREVIOUS_VERSION) || systemStatus
				.equals(Node.SYS_STATUS_CURRENT_VERSION))) {
			throw new IllegalArgumentException("Invalid input systemStatus: "
					+ systemStatus);
		}

		if (StringUtils.isNotEmpty(type)
				&& !(type.equals(Node.TYPE_ABOX) || type.equals(Node.TYPE_TBOX))) {
			throw new IllegalArgumentException("Invalid input type: " + type);
		}

		List<Entity> entities = null;
		Session session = null;
		try {
			long start = System.currentTimeMillis();
			session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.getTransaction().begin();

			entities = this.getEntities(session, id, systemStatus, type,
					ownValue, true);

			long dif = System.currentTimeMillis() - start;

			String s = "Found=\n";
			for (Entity e : entities) {
				s += e.toString() + "\n";
			}
			s += "time used= " + dif + "[ms]\n\n";
			logger.debug(s);

			// session.getTransaction().commit();
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			// e.printStackTrace();
		} finally {
			session.getTransaction().commit();
		}
		return entities;
	}

	private String whereUpdate(String where) {
		if (StringUtils.isEmpty(where))
			where = " where ";
		else
			where += " AND ";
		return where;
	}

	public List<Node> getNodes(Session session, Long id, String systemStatus,
			Long sourceId, Long srcModif, Long targetId, Long tarModif) {
		List<Node> nodes = null;

		String from = "from Node ";
		String where = "";

		if (id != null) {
			where = this.whereUpdate(where);
			where += " id = :id ";

		}
		if (StringUtils.isNotEmpty(systemStatus)) {
			where = this.whereUpdate(where);
			where += " systemStatus = :systemStatus ";
		}

		// source's attributes
		if (sourceId != null) {
			where = this.whereUpdate(where);
			where += " sourceId = :sourceId ";
		}
		if (srcModif != null) {
			where = this.whereUpdate(where);
			where += " sourceModif = :sourceModif ";
		}

		if (targetId != null) {
			where = this.whereUpdate(where);
			where += " targetId = :targetId ";
		}
		if (tarModif != null) {
			where = this.whereUpdate(where);
			where += " targetModif = :targetModif ";
		}

		String hql = from + where;
		Query query = session.createQuery(hql);

		if (id != null)
			query.setLong("id", id);
		if (StringUtils.isNotEmpty(systemStatus))
			query.setString("systemStatus", systemStatus);

		if (sourceId != null)
			query.setLong("sourceId", sourceId);
		if (srcModif != null)
			query.setLong("sourceModif", srcModif);

		if (targetId != null)
			query.setLong("targetId", targetId);
		if (tarModif != null)
			query.setLong("targetModif", tarModif);

		nodes = query.list();

		return nodes;
	}

	private Entity getEntityContent(Session session, Entity entity, boolean considerTimeModif) {
		if (entity != null && entity.isLightweight()) {
			entity.setLightweight(false);
			entity.setAttributes(new ArrayList<Attribute>());
			entity.setSourceRelations(new ArrayList<Relation>());
			entity.setTargetRelations(new ArrayList<Relation>());

            // FIXME: getNodes finds all attributes if id=null!
            if (entity.getId() == null) {
                logger.error("Relation with id=null! Abort loading attributes.");
                return entity;
            }
			
			// getting Attributes and SourceRelations
			List<Node> nodes = null; 					
			if(considerTimeModif){
				nodes = this.getNodes(session, null,
						entity.getSystemStatus(), entity.getId(),
						entity.getModificationTime(), null, null);
			}else{
				nodes = this.getNodes(session, null,
						entity.getSystemStatus(), entity.getId(),
						null, null, null);
			}
					

			for (Node node : nodes) {
				if (node instanceof Attribute) {
					entity.addAttribute((Attribute) node);
				} else if (node instanceof Relation) {
					Relation rel = (Relation) node;
					// new attr for relations
					// FIXME: getNodes finds all attributes if id=null!
		            if (rel.getId() == null) {
		                logger.error("Relation with id=null! Abort loading attributes.");
		                continue;
		            }
					List<Node> attrs = this.getNodes(session, null,
							rel.getSystemStatus(), rel.getId(),
							rel.getModificationTime(), null, null);
					for (Node attNode : attrs) {
						if (attNode instanceof Attribute) {
							rel.addAttribute((Attribute) attNode);
						}
					}
					if(considerTimeModif){
						entity.addSourceRelation(rel);	
					}else{
						entity.getSourceRelations().add(rel);
					}
				} else if (node instanceof View) {
					entity.getViews().add((View) node);
				} else {
					throw new IllegalArgumentException("Invalid node found: "
							+ node);
				}
			}

			// getting TargetRelations
			List<Node> tarRels = null;
			
			if(considerTimeModif){
				tarRels = this.getNodes(session, null,
						entity.getSystemStatus(), null, null, entity.getId(),
						entity.getModificationTime());
			}else{
				tarRels = this.getNodes(session, null,
						entity.getSystemStatus(), null, null, entity.getId(),
						null);
			}
			
			for (Node node : tarRels) {
				if (node instanceof Relation) {
					Relation rel = (Relation) node;
					// new attr for relations
		            // FIXME: getNodes finds all attributes if id=null!
		            if (rel.getId() == null) {
		                logger.error("Relation with id=null! Abort loading attributes.");
		                continue;
		            }
					List<Node> attrs = this.getNodes(session, null,
							rel.getSystemStatus(), rel.getId(),
							rel.getModificationTime(), null, null);
					for (Node attNode : attrs) {
						if (attNode instanceof Attribute) {
							rel.addAttribute((Attribute) attNode);
						}
					}
					if(considerTimeModif){
						entity.addTargetRelation(rel);
					}else{
						entity.getTargetRelations().add(rel);
					}
				} else {
					throw new IllegalArgumentException("Invalid node found: "
							+ node);
				}
			}
		}
		return entity;
	}

	public Entity getEntityContent(Entity entity) {
		if (entity != null && entity.isLightweight()) {
			try {
				logger.debug("GET ENTITY CONTENT [objClass="
						+ entity.getObjectClass() + ", id=" + entity.getId()
						+ ", ownValue=" + entity.getOwnValue() + "]");
				long start = System.currentTimeMillis();
				Session session = HibernateUtil.getSessionFactory()
						.getCurrentSession();
				session.getTransaction().begin();

				entity = this.getEntityContent(session, entity, true);
				long diff = System.currentTimeMillis() - start;
				logger.debug("Time to get content=" + diff + "(s)");
				session.getTransaction().commit();
			} catch (Exception e) {
				logger.error(e.getMessage(), e);
				// e.printStackTrace();
			}
		}
		return entity;
	}

	public List<String> getObjecClassSuggestion(String objectClass,
			Class nodeClass, int maxResults) {
		objectClass += "%";
		try {

			List<String> suggestions = new ArrayList<String>();
			Session session = HibernateUtil.getSessionFactory()
					.getCurrentSession();
			session.getTransaction().begin();
			String hql = "from ";
			if (nodeClass.equals(Entity.class)) {
				hql += "Entity ";
			}
			if (nodeClass.equals(Relation.class)) {
				hql += "Relation ";
			}
			if (nodeClass.equals(Attribute.class)) {
				hql += "Attribute ";
			}
			if (nodeClass.equals(Node.class)) {
				hql += "Node ";
			}
			if (nodeClass.equals(View.class)) {
				hql += "View ";
			}
			hql += " where objectClass like :objectClass "
					+ "group by objectClass";

			Query query = session.createQuery(hql);
			query.setString("objectClass", objectClass);
			query.setMaxResults(maxResults);
			List<Node> list = query.list();
			for (Node node : list) {
				suggestions.add(node.getObjectClass());
			}
			return suggestions;

		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			// e.printStackTrace();
		}
		return null;
	}

	public List<String> getOwnValueSuggestion(String ownValue, Class nodeClass,
			int maxResults) {
		ownValue += "%";
		try {

			List<String> suggestions = new ArrayList<String>();
			Session session = HibernateUtil.getSessionFactory()
					.getCurrentSession();
			session.getTransaction().begin();
			String hql = "from ";
			if (nodeClass.equals(Entity.class)) {
				hql += "Entity ";
			}
			if (nodeClass.equals(Relation.class)) {
				hql += "Relation ";
			}
			if (nodeClass.equals(Attribute.class)) {
				hql += "Attribute ";
			}
			if (nodeClass.equals(Node.class)) {
				hql += "Node ";
			}
			if (nodeClass.equals(View.class)) {
				hql += "View ";
			}
			hql += " where ownValue like :ownValue " + "group by ownValue";

			Query query = session.createQuery(hql);
			query.setString("ownValue", ownValue);
			query.setMaxResults(maxResults);
			List<Node> list = query.list();
			for (Node node : list) {
				suggestions.add(node.getOwnValue());
			}
			return suggestions;

		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			// e.printStackTrace();
		}
		return null;
	}

	public Entity loadEntitiesForTargetRelation(Entity entity) {
		try {
			// where parent_id = :parent_id AND type = :type
			Session session = HibernateUtil.getSessionFactory()
					.getCurrentSession();
			session.getTransaction().begin();

			for (Relation rel : entity.getTargetRelations()) {
				List<Entity> entities = this.getEntities(session,
						rel.getSourceId(), Node.SYS_STATUS_CURRENT_VERSION,
						null, null, true);
				if (entities.size() > 0) {
					Entity source = entities.get(0);
					rel.setSource(source);
				}
			}

			session.getTransaction().commit();
			return entity;
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			// e.printStackTrace();
		}
		return entity;
	}

	protected List<Entity> getLightweightEntities(Session session,
			String systemStatus, Long id, String type, String objectClass,
			String ownValue, boolean ownValueSubString, int maxResult) {

		String hqlEntities = "from Entity where ";
		if (StringUtils.isNotEmpty(ownValue)) {
			if (ownValueSubString)
				hqlEntities += "ownValue like :ownValue AND ";
			else
				hqlEntities += "ownValue = :ownValue AND ";
		}
		if (id != null) {
			hqlEntities += "id = :id AND ";
		}
		if (StringUtils.isNotEmpty(objectClass)) {
			hqlEntities += "objectClass = :objectClass AND ";
		}
		if (StringUtils.isNotEmpty(type)) {
			hqlEntities += "type = :type AND ";
		}

		hqlEntities += "systemStatus = :systemStatus ";

		// it will be now sorted by openmind
		// + "order by ownValue";

		Query queryEntities = session.createQuery(hqlEntities);
		queryEntities.setString("systemStatus", systemStatus);
		if (StringUtils.isNotEmpty(ownValue)) {
			if (ownValueSubString) {
				queryEntities.setString("ownValue", "%" + ownValue + "%");
				logger.info("ownValue=%" + ownValue + "%");
			} else {
				queryEntities.setString("ownValue", ownValue);
				logger.info("ownValue=" + ownValue);
			}
		}
		if (maxResult > 0) {
			queryEntities.setMaxResults(maxResult);
		}

		if (StringUtils.isNotEmpty(type)) {
			queryEntities.setString("type", type);
		}
		if (id != null) {
			queryEntities.setLong("id", id);
		}
		if (StringUtils.isNotEmpty(objectClass)) {
			queryEntities.setString("objectClass", objectClass);
		}
		List<Entity> entities = queryEntities.list();
		
		for(Entity ent : entities){
			ent.setLightweight(true);
		}
		
		return entities;
	}

	/**
	 * Own value is searched as substring
	 * 
	 * @param systemStatus
	 * @param id
	 * @param type
	 * @param objectClass
	 * @param ownValue
	 * @param maxResult
	 * @return
	 */
	public List<Entity> getLightweightEntities(String systemStatus, Long id,
			String type, String objectClass, String ownValue,
			boolean ownValueSubString, int maxResult) {
		logger.debug("GET LW ENTITIES [type=" + type + " id=" + id
				+ ", objectClass=" + objectClass + ", ownValue=" + ownValue
				+ "]");

		List<Entity> entities = new ArrayList<Entity>();
		if (!(systemStatus.equals(Node.SYS_STATUS_PREVIOUS_VERSION) || systemStatus
				.equals(Node.SYS_STATUS_CURRENT_VERSION))) {
			throw new IllegalArgumentException("Invalid input systemStatus: "
					+ systemStatus);
		}

		if (StringUtils.isNotEmpty(type)
				&& !(type.equals(Node.TYPE_ABOX) || type.equals(Node.TYPE_TBOX))) {
			throw new IllegalArgumentException("Invalid input type: " + type);
		}

		try {
			// where parent_id = :parent_id AND type = :type
			Session session = HibernateUtil.getSessionFactory()
					.getCurrentSession();
			session.getTransaction().begin();

			entities = this.getLightweightEntities(session, systemStatus, id,
					type, objectClass, ownValue, ownValueSubString, maxResult);


			session.getTransaction().commit();
			return entities;
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
		return entities;
	}

	private Sequence getIdSequence(Session session) {
		Sequence sequence = null;
		String hqlJoin = "from Sequence where name = :name";
		Query query = session.createQuery(hqlJoin);
		query.setString("name", NODE_SEQUENCE);
		List<Sequence> sequences = query.list();
		if (sequences.size() > 0)
			sequence = sequences.get(0);
		if (sequence == null) {
			sequence = new Sequence(NODE_SEQUENCE, new Long(0));
		}
		return sequence;
	}

	protected Long generateId(Session session) {
		Long id = null;
		Sequence sequence = null;
		String hqlJoin = "from Sequence where name = :name";
		Query query = session.createQuery(hqlJoin);
		query.setString("name", NODE_SEQUENCE);
		List<Sequence> sequences = query.list();
		if (sequences.size() > 0) {
			sequence = sequences.get(0);
		} else {
			sequence = new Sequence(NODE_SEQUENCE, new Long(0));
		}
		id = sequence.generateId();
		session.save(sequence);
		return id;
	}

	public ConfigurationService getConfigurationService() {
		return configurationService;
	}

	public void setConfigurationService(
			ConfigurationService configurationService) {
		this.configurationService = configurationService;
	}

	public boolean isImportModus() {
		return importModus;
	}

	public void setImportModus(boolean importModus) {
		this.importModus = importModus;
	}

	// #################################################################
	// #################################################################
	// #################################################################

	public List<Relation> getDefRelations() {
		List<Relation> list = new ArrayList<Relation>();
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		try {
			session.getTransaction().begin();

			String hql = "from Relation where systemStatus = :systemStatus and type = :type";
			Query query = session.createQuery(hql);

			query.setString("systemStatus", Node.SYS_STATUS_CURRENT_VERSION);
			query.setString("type", Node.TYPE_TBOX);

			list = query.list();
			/*
			 * for(Relation rel : list){ List<Node> attrs =
			 * this.getNodes(session, null, rel.getSystemStatus(), rel.getId(),
			 * rel.getModificationTime(), null, null); for(Node attNode :
			 * attrs){ if(attNode instanceof Attribute){
			 * rel.addAttribute((Attribute)attNode); } } }
			 */

		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		} finally {
			session.getTransaction().commit();
		}
		return list;
	}

	public List<Attribute> getDefAttributes() {
		List<Attribute> list = new ArrayList<Attribute>();
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		try {
			session.getTransaction().begin();

			String hql = "from Attribute where systemStatus = :systemStatus and type = :type";
			Query query = session.createQuery(hql);

			query.setString("systemStatus", Node.SYS_STATUS_CURRENT_VERSION);
			query.setString("type", Node.TYPE_TBOX);

			list = query.list();

		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		} finally {
			session.getTransaction().commit();
		}
		return list;
	}

	public List<Entity> getLWDefinitions() throws Exception {
		List<Entity> list = new ArrayList<Entity>();
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();

		session.getTransaction().begin();

		String hql = "from Entity where systemStatus = :systemStatus and type = :type";
		Query query = session.createQuery(hql);

		query.setString("systemStatus", Node.SYS_STATUS_CURRENT_VERSION);
		query.setString("type", Node.TYPE_TBOX);

		list = query.list();
		for (Entity def : list) {
			def.setLightweight(true);
		}
		session.getTransaction().commit();

		return list;
	}

	// #################################################################
	// #################################################################
	// #################################################################

	public Long getEntityCount(String objectClass) {
		Long count = null;
		try {
			Session session = HibernateUtil.getSessionFactory()
					.getCurrentSession();
			session.getTransaction().begin();

			String hql = "select count(*) from Entity where ";

			if (StringUtils.isNotEmpty(objectClass)) {
				hql += "objectClass = :objectClass AND ";
			} else {
				hql += "objectClass != :objectClass AND ";
			}
			hql += "systemStatus = :systemStatus ";

			Query query = session.createQuery(hql);
			query.setString("systemStatus", Node.SYS_STATUS_CURRENT_VERSION);
			if (StringUtils.isNotEmpty(objectClass)) {
				query.setString("objectClass", objectClass);
			} else {
				query.setString("objectClass", Node.TYPE_TBOX);
			}
			count = (Long) query.uniqueResult();

			session.getTransaction().commit();
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
		return count;
	}

	/**
	 * 
	 * @param objectClass
	 *            if it is null, it will be returned all entities (no
	 *            definition). To get the definitions objectClass should be:
	 *            Node.TYPE_TBOX
	 * @param startRecord
	 * @param endRecord
	 * @return
	 */
	public List<Entity> getEntityPage(String objectClass,
			final int startRecord, final int endRecord) {

		List<Entity> entities = new ArrayList<Entity>();
		try {
			Session session = HibernateUtil.getSessionFactory()
					.getCurrentSession();
			session.getTransaction().begin();

			String hql = "from Entity where ";

			if (StringUtils.isNotEmpty(objectClass)) {
				hql += "objectClass = :objectClass AND ";
			} else {
				hql += "objectClass != :objectClass AND ";
			}

			hql += "systemStatus = :systemStatus order by ownValue";

			Query query = session.createQuery(hql);
			query.setFirstResult(startRecord);
			query.setMaxResults(endRecord);
			query.setString("systemStatus", Node.SYS_STATUS_CURRENT_VERSION);
			if (StringUtils.isNotEmpty(objectClass)) {
				query.setString("objectClass", objectClass);
			} else {
				query.setString("objectClass", Node.TYPE_TBOX);
			}
			entities = query.list();

			session.getTransaction().commit();
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			// e.printStackTrace();
		}
		return entities;
	}

	public static void main(String[] args) {
		/*
		 * ServiceRegistry sr = new ServiceRegistry(); Long count =
		 * sr.getPersistenceService().getEntityCount("CODEX");
		 * logger.info("count codex " + count);
		 * 
		 * List<Entity> list = sr.getPersistenceService().getEntityPage("CODEX",
		 * count.intValue() - 100, count.intValue() - 50); for(Entity e : list){
		 * logger.info(e.toString()); }
		 */
	}

}