changeset 36:63fe33172397

more comments, less log messages.
author casties
date Tue, 11 Oct 2016 20:55:36 +0200
parents 1cd9d9a67cad
children 3485498af8c3
files src/main/java/org/mpi/openmind/cache/WrapperService.java src/main/java/org/mpi/openmind/repository/services/AbstractPersistenceService.java
diffstat 2 files changed, 172 insertions(+), 83 deletions(-) [+]
line wrap: on
line diff
--- a/src/main/java/org/mpi/openmind/cache/WrapperService.java	Mon Aug 29 20:16:53 2016 +0200
+++ b/src/main/java/org/mpi/openmind/cache/WrapperService.java	Tue Oct 11 20:55:36 2016 +0200
@@ -608,6 +608,16 @@
 		return (Entity) entity.clone();
 	}
 
+	/**
+     * Save the given entity to the database.
+     * 
+     * Creates a new version of the entity.
+     * 
+	 * @param entity
+	 * @param user
+	 * @return
+	 * @throws Exception
+	 */
 	public Entity saveEntity(Entity entity, String user) throws Exception {
 		long start = System.currentTimeMillis();
 		if (StringUtils.isEmpty(entity.getType())) {
--- a/src/main/java/org/mpi/openmind/repository/services/AbstractPersistenceService.java	Mon Aug 29 20:16:53 2016 +0200
+++ b/src/main/java/org/mpi/openmind/repository/services/AbstractPersistenceService.java	Tue Oct 11 20:55:36 2016 +0200
@@ -593,14 +593,13 @@
 	 * @return
 	 */
 	public void saveEntity(Entity entity) throws Exception {
-
+	    // start transaction
 		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
 		session.getTransaction().begin();
-
+		// save entity
 		saveEntity0(session, entity);
-
+		// end transaction
 		session.getTransaction().commit();
-
 	}
 
 	/**
@@ -614,12 +613,13 @@
 	 */
 	private void saveEntity0(Session session, Entity entity) throws Exception {
 		if (entity.getId() != null) {
+		    // get the still current entity(s)
 			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!");
 				}
-
+				// set the first current entity as previous 
 				Entity previousEntity = previousEntityList.get(0);
 				logger.info("Saving previous entity: " + previousEntity);
 				this.persistEntityAsPrevious(session, previousEntity);
@@ -639,8 +639,7 @@
 	 * @param idSequence
 	 * @throws Exception
 	 */
-	private void saveCurrentEntity(Session session, Entity entity,
-			Sequence idSequence) throws Exception {
+	private void saveCurrentEntity(Session session, Entity entity, Sequence idSequence) throws Exception {
 		Long time = System.currentTimeMillis();
 		entity.setSystemStatus(Node.SYS_STATUS_CURRENT_VERSION);
 		entity.resetRowId();
@@ -656,7 +655,7 @@
 	}
 
 	/**
-	 * Update the entity to prepare it for persisting.
+	 * Update the entity to prepare it for persistence.
 	 * 
 	 * If the ID is null new IDs will be assigned for the entity and its 
 	 * attributes and relations.
@@ -668,9 +667,11 @@
 	 * @param entity
 	 * @return
 	 */
-	private Entity prepareEntity(Session session, Entity entity,
-			Sequence idSequence) {
+	private Entity prepareEntity(Session session, Entity entity, Sequence idSequence) {
 
+	    /*
+	     * update id
+	     */
 		if (entity.getId() == null) {
 			if (idSequence == null)
 				entity.setId(this.generateId(session));
@@ -678,6 +679,9 @@
 				entity.setId(idSequence.generateId());
 		}
 
+		/* 
+		 * update attributes
+		 */
 		for (Attribute att : entity.getAttributes()) {
 			if (att.getId() == null) {
 				if (idSequence == null)
@@ -685,14 +689,15 @@
 				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??
+		/*
+		 * update source relations
+		 */
 		for (Relation rel : entity.getSourceRelations()) {
 			if (rel.getId() == null) {
 				if (idSequence == null)
@@ -703,14 +708,17 @@
 			rel.setSourceId(entity.getId());
 			rel.setSourceModif(entity.getModificationTime());
 			rel.setSourceObjectClass(entity.getObjectClass());
-			rel.autoNormalize();
-			if(StringUtils.equals(entity.getType(), Node.TYPE_ABOX)){
+			// TODO: update relation target too?
+			rel.autoNormalize(); // TODO: normalize relation?
+			if(StringUtils.equals(entity.getType(), Node.TYPE_ABOX)) {
 				rel.setObjectClass(rel.getOwnValue());				
-			}else if(StringUtils.equals(entity.getType(), Node.TYPE_TBOX)){
+			} else if(StringUtils.equals(entity.getType(), Node.TYPE_TBOX)) {
 				rel.setObjectClass(Node.TYPE_TBOX);
 			}
 			
-			// new Attributes for relations
+			/*
+			 * update attributes for relations
+			 */
 			for (Attribute att : rel.getAttributes()) {
 				if (att.getId() == null) {
 					if (idSequence == null)
@@ -718,15 +726,16 @@
 					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();
 			}
 		}
 
+		/*
+		 * update target relations
+		 */
 		for (Relation rel : entity.getTargetRelations()) {
 			if (rel.getId() == null) {
 				if (idSequence == null)
@@ -737,13 +746,16 @@
 			rel.setTargetId(entity.getId());
 			rel.setTargetModif(entity.getModificationTime());
 			rel.setTargetObjectClass(entity.getObjectClass());
-			rel.autoNormalize();
+			// TODO: update relation source too?
+			rel.autoNormalize(); // TODO: normalize?
 			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
+			/*
+			 * update relation attributes
+			 */
 			for (Attribute att : rel.getAttributes()) {
 				if (att.getId() == null) {
 					if (idSequence == null)
@@ -751,14 +763,16 @@
 					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();
 			}
 		}
+		
+		/*
+		 * update views(?)
+		 */
 		for (View view : entity.getViews()) {
 			if (view.getId() == null) {
 				if (idSequence == null)
@@ -771,6 +785,7 @@
 			view.setSourceObjectClass(entity.getObjectClass());
 			view.autoNormalize();
 		}
+		
 		return entity;
 	}
 
@@ -810,7 +825,7 @@
 		for (View view : entity.getViews()) {
 			session.save(view);
 		}
-        txLog.debug("* End ...save previous entity");
+        txLog.debug("* END ...save previous entity");
 	}
 
 	/**
@@ -865,6 +880,7 @@
 			if (StringUtils.isNotEmpty(ownValue)) {
 				entity.setOwnValue(ownValue);
 				entity.autoNormalize();
+				txLog.info("save entity (new ov): "+entity.toEncString());
 				session.save(entity);
 			}
 		}
@@ -880,15 +896,17 @@
 	}
 
 	/**
-	 * <p>
-	 * Returns entities with their corresponding contents.
-	 * </p>
+	 * Returns entities with their content from the database.
+	 * 
+	 * If a parameter is null then the condition is omitted from the query
+	 * returning all entities matching the remaining conditions.
 	 * 
 	 * @param session
 	 * @param id
 	 * @param systemStatus
 	 * @param type
 	 * @param ownValue
+	 * @param considerTimeModif
 	 * @return
 	 */
 	private List<Entity> getEntities(Session session, Long id,
@@ -946,20 +964,19 @@
 	}
 
 	/**
-	 * <p>
 	 * Returns entities with their corresponding content.
-	 * </p>
 	 * 
+     * If a parameter is null then the condition is omitted from the query
+     * returning all entities matching the remaining conditions.
+     * 
 	 * @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 + "]");
+	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))) {
@@ -975,23 +992,20 @@
 		List<Entity> entities = null;
 		Session session = null;
 		try {
-			long start = System.currentTimeMillis();
+			//long start = System.currentTimeMillis();
 			session = HibernateUtil.getSessionFactory().getCurrentSession();
 			session.getTransaction().begin();
 
-			entities = this.getEntities(session, id, systemStatus, type,
-					ownValue, true);
+			entities = this.getEntities(session, id, systemStatus, type, ownValue, true);
 
-			long dif = System.currentTimeMillis() - start;
-
+			/* 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);
+			logger.debug(s); */
 
-			// session.getTransaction().commit();
 		} catch (Exception e) {
 			logger.error(e.getMessage(), e);
 			// e.printStackTrace();
@@ -1009,28 +1023,46 @@
 		return where;
 	}
 
+	/**
+	 * Returns a list of Nodes.
+     *
+     * If a parameter is null then the condition is omitted from the query
+     * returning all entities matching the remaining conditions.
+	 * 
+	 * @param session
+	 * @param id
+	 * @param systemStatus
+	 * @param sourceId
+	 * @param srcModif
+	 * @param targetId
+	 * @param tarModif
+	 * @return
+	 */
 	public List<Node> getNodes(Session session, Long id, String systemStatus,
 			Long sourceId, Long srcModif, Long targetId, Long tarModif) {
 		List<Node> nodes = null;
 
+		/*
+		 * create HQL query string
+		 */
 		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 ";
@@ -1040,34 +1072,53 @@
 			where = this.whereUpdate(where);
 			where += " targetId = :targetId ";
 		}
+		
 		if (tarModif != null) {
 			where = this.whereUpdate(where);
 			where += " targetModif = :targetModif ";
 		}
 
 		String hql = from + where;
+		// create query object
 		Query query = session.createQuery(hql);
 
+		/*
+		 * add query parameters
+		 */
 		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);
 
+		/*
+		 *  run the query
+		 */
 		nodes = query.list();
 
 		return nodes;
 	}
 
+	/**
+	 * Populate a lightweight entity with attributes and relations.
+	 *  
+	 * @param session
+	 * @param entity
+	 * @param considerTimeModif
+	 * @return
+	 */
 	private Entity getEntityContent(Session session, Entity entity, boolean considerTimeModif) {
 		if (entity != null && entity.isLightweight()) {
 			entity.setLightweight(false);
@@ -1084,25 +1135,30 @@
                 return entity;
             }
 			
-			// getting Attributes and SourceRelations
+			/*
+			 * get all nodes with source_id = entity.id
+			 * i.e. Attributes and SourceRelations.
+			 */
 			List<Node> nodes = null; 					
-			if(considerTimeModif){
+			if (considerTimeModif) {
 				nodes = this.getNodes(session, null,
 						entity.getSystemStatus(), entity.getId(),
 						entity.getModificationTime(), null, null);
-			}else{
+			} else {
 				nodes = this.getNodes(session, null,
 						entity.getSystemStatus(), entity.getId(),
 						null, null, null);
 			}
 					
-
+			/*
+			 * add Attributes and SourceRelations
+			 */
 			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
 		            /*
 		             * Danger: getNodes finds all attributes in the db and tries to attach them to 
 		             * this relation if id=null!
@@ -1111,6 +1167,10 @@
 		                logger.error("Relation with id=null! Abort loading attributes.");
 		                continue;
 		            }
+		            /*
+		             * get all nodes with source_id = rel.id
+		             * i.e. relation Attributes
+		             */
 					List<Node> attrs = this.getNodes(session, null,
 							rel.getSystemStatus(), rel.getId(),
 							rel.getModificationTime(), null, null);
@@ -1119,35 +1179,43 @@
 							rel.addAttribute((Attribute) attNode);
 						}
 					}
-					if(considerTimeModif){
+					// TODO: ???
+					if (considerTimeModif) {
 						entity.addSourceRelation(rel);	
-					}else{
+					} 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
+			/*
+			 * get all nodes with target_id = entity.id
+			 * i.e. TargetRelations
+			 */
 			List<Node> tarRels = null;
 			
-			if(considerTimeModif){
+			if (considerTimeModif) {
 				tarRels = this.getNodes(session, null,
 						entity.getSystemStatus(), null, null, entity.getId(),
 						entity.getModificationTime());
-			}else{
+			} else {
 				tarRels = this.getNodes(session, null,
 						entity.getSystemStatus(), null, null, entity.getId(),
 						null);
 			}
 			
+			/*
+			 * add TargetRelations
+			 */
 			for (Node node : tarRels) {
 				if (node instanceof Relation) {
 					Relation rel = (Relation) node;
-					// new attr for relations
 		            /*
 		             * Danger: getNodes finds all attributes in the db and tries to attach them to 
 		             * this relation if id=null!
@@ -1156,6 +1224,10 @@
 		                logger.error("Relation with id=null! Abort loading attributes.");
 		                continue;
 		            }
+                    /*
+                     * get all nodes with source_id = rel.id
+                     * i.e. relation Attributes
+                     */
 					List<Node> attrs = this.getNodes(session, null,
 							rel.getSystemStatus(), rel.getId(),
 							rel.getModificationTime(), null, null);
@@ -1164,45 +1236,53 @@
 							rel.addAttribute((Attribute) attNode);
 						}
 					}
-					if(considerTimeModif){
+					// TODO: ???
+					if (considerTimeModif) {
 						entity.addTargetRelation(rel);
-					}else{
+					} else {
 						entity.getTargetRelations().add(rel);
 					}
+					
 				} else {
-					throw new IllegalArgumentException("Invalid node found: "
-							+ node);
+					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();
+    /**
+     * Populate a lightweight entity with attributes and relations.
+     *  
+     * @param entity
+     * @return
+     */
+    public Entity getEntityContent(Entity entity) {
+        if (entity != null && entity.isLightweight()) {
+            Session session = null;
+            try {
+                /* logger.debug("GET ENTITY CONTENT [objClass=" + entity.getObjectClass() + ", id=" + entity.getId()
+                        + ", ownValue=" + entity.getOwnValue() + "]"); */
+                //long start = System.currentTimeMillis();
+                
+                // start transaction
+                session = HibernateUtil.getSessionFactory().getCurrentSession();
+                session.getTransaction().begin();
+                // get entity content
+                entity = this.getEntityContent(session, entity, true);
+                
+                /* long diff = System.currentTimeMillis() - start;
+                logger.debug("Time to get content=" + diff + "(s)"); */
+            } catch (Exception e) {
+                logger.error(e.getMessage(), e);
+            } finally {
+                session.getTransaction().commit();
+            }
+        }
+        return entity;
+    }
 
-				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) {
+	public List<String> getObjecClassSuggestion(String objectClass, Class nodeClass, int maxResults) {
 		objectClass += "%";
 		try {
 
@@ -1245,8 +1325,7 @@
 		return null;
 	}
 
-	public List<String> getOwnValueSuggestion(String ownValue, Class nodeClass,
-			int maxResults) {
+	public List<String> getOwnValueSuggestion(String ownValue, Class nodeClass, int maxResults) {
 		ownValue += "%";
 		try {