changeset 75:e0be7c0030f5

cleanup and better comments.
author casties
date Thu, 23 Feb 2017 19:05:47 +0100
parents 7acd7564e394
children f04bd29d284d
files src/main/java/org/mpi/openmind/cache/CacheService.java src/main/java/org/mpi/openmind/repository/services/AbstractPersistenceService.java src/main/java/org/mpi/openmind/repository/utils/OM4StreamWriter.java
diffstat 3 files changed, 82 insertions(+), 31 deletions(-) [+]
line wrap: on
line diff
--- a/src/main/java/org/mpi/openmind/cache/CacheService.java	Fri Feb 03 12:56:28 2017 +0100
+++ b/src/main/java/org/mpi/openmind/cache/CacheService.java	Thu Feb 23 19:05:47 2017 +0100
@@ -529,14 +529,18 @@
 				diff.modifiedSrcRels.add(rel);
 			}
 			if (!cacheSrcRels.remove(rel)) {
+				// rel was not in cache
 				boolean found = false;
 				for (Relation cr : cacheSrcRels) {
+					// check all cached relations
 					if (cr.getId() == rel.getId()) {
+						// relation id is same
 						cacheSrcRels.remove(cr);
 						found = true;
 						break;
 					} else if (acceptNewIDs && cr.equalsContent(rel)) {
-						// same content is good enough
+						// same content is good enough 
+						// (includes checks for src and tar id)
 						cacheSrcRels.remove(cr);
 						found = true;
 						break;
@@ -564,14 +568,18 @@
 				diff.modifiedTarRels.add(rel);
 			}
 			if (!cacheTarRels.remove(rel)) {
+				// rel was not in cache
 				boolean found = false;
 				for (Relation cr : cacheTarRels) {
+					// check all cached relations
 					if (cr.getId() == rel.getId()) {
+						// relation id is same
 						cacheTarRels.remove(cr);
 						found = true;
 						break;
 					} else if (acceptNewIDs && cr.equalsContent(rel)) {
-						// same content is good enough
+						// same content is good enough 
+						// (includes checks for src and tar id)
 						cacheTarRels.remove(cr);
 						found = true;
 						break;
--- a/src/main/java/org/mpi/openmind/repository/services/AbstractPersistenceService.java	Fri Feb 03 12:56:28 2017 +0100
+++ b/src/main/java/org/mpi/openmind/repository/services/AbstractPersistenceService.java	Thu Feb 23 19:05:47 2017 +0100
@@ -1721,11 +1721,18 @@
 	// #################################################################
 	// #################################################################
 
+	/**
+	 * Returns the number of Entities of the given objectClass in the database.
+	 * 
+	 * Returns the number of all Entities if objectClass==null.
+	 * 
+	 * @param objectClass
+	 * @return
+	 */
 	public Long getEntityCount(String objectClass) {
 		Long count = null;
 		try {
-			Session session = HibernateUtil.getSessionFactory()
-					.getCurrentSession();
+			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
 			session.getTransaction().begin();
 
 			String hql = "select count(*) from Entity where ";
@@ -1748,55 +1755,61 @@
 
 			session.getTransaction().commit();
 		} catch (Exception e) {
-			logger.error(e.getMessage(), e);
+			logger.error(e);
 		}
 		return count;
 	}
 
 	/**
+	 * Returns all Entities of the given objectClass in the database 
+	 * between startRecord and endRecord.
 	 * 
 	 * @param objectClass
-	 *            if it is null, it will be returned all entities (no
-	 *            definition). To get the definitions objectClass should be:
+	 *            if it is null, all entities are returned (except
+	 *            definitions). 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) {
+	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 session = HibernateUtil.getSessionFactory().getCurrentSession();
 			session.getTransaction().begin();
 
 			String hql = "from Entity where ";
 
 			if (StringUtils.isNotEmpty(objectClass)) {
+				// real objectClass
 				hql += "objectClass = :objectClass AND ";
 			} else {
+				// objectClass == null -- match object_class != 'TBox'
 				hql += "objectClass != :objectClass AND ";
 			}
 
 			hql += "systemStatus = :systemStatus order by ownValue";
 
 			Query query = session.createQuery(hql);
+			// use Hibernate's result paging
 			query.setFirstResult(startRecord);
 			query.setMaxResults(endRecord);
+			// add query params
 			query.setString("systemStatus", Node.SYS_STATUS_CURRENT_VERSION);
 			if (StringUtils.isNotEmpty(objectClass)) {
+				// real objectClass
 				query.setString("objectClass", objectClass);
 			} else {
+				// objectClass == null -- match object_class != 'TBox'
 				query.setString("objectClass", Node.TYPE_TBOX);
 			}
+			// fetch Entities
 			entities = query.list();
 
 			session.getTransaction().commit();
 		} catch (Exception e) {
-			logger.error(e.getMessage(), e);
-			// e.printStackTrace();
+			logger.error(e);
 		}
 		return entities;
 	}
--- a/src/main/java/org/mpi/openmind/repository/utils/OM4StreamWriter.java	Fri Feb 03 12:56:28 2017 +0100
+++ b/src/main/java/org/mpi/openmind/repository/utils/OM4StreamWriter.java	Thu Feb 23 19:05:47 2017 +0100
@@ -22,7 +22,7 @@
 /**
  * Export all entities and relations and definitions to XML.
  * 
- * Saves (real) entities and relations (i.e. assertions) and definitions 
+ * Saves (content) entities and relations (i.e. assertions) and definitions 
  * (i.e. definition entities and relations) in separate files.
  * 
  * @author jurzua, casties
@@ -49,10 +49,22 @@
     }
     
     
+    /**
+     * Saves all content Entities with their Attributes and Relations in a XML file with the given fileName.
+     * 
+     * @param fileName
+     * @param ps
+     */
     public static void backupEntities(String fileName, PersistenceService ps) {
         writeEntsAndRels(fileName, ps, Node.TYPE_ABOX);
     }
 
+    /**
+     * Saves all definitions in a XML file with the given fileName.
+     * 
+     * @param fileName
+     * @param ps
+     */
     public static void backupDefinitions(String fileName, PersistenceService ps) {
         writeEntsAndRels(fileName, ps, Node.TYPE_TBOX);
     }
@@ -60,9 +72,11 @@
     /**
      * Writes all entities of the given type and their relations to the XML file at fileName.
      * 
+     * Type is either TYPE_TBOX or TYPE_ABOX.
+     * 
      * @param fileName
      * @param ps
-     * @param type
+     * @param type 
      */
     private static void writeEntsAndRels(String fileName, PersistenceService ps, String type) {
         OutputStreamWriter out;
@@ -79,10 +93,12 @@
             if (type.equals(Node.TYPE_ABOX)) {
                 writer.writeStartElement(XMLUtil.OPENMIND_DATA);
                 writer.writeAttribute("version", "4.3");
+                // get number of content Entities
                 entitiesCount = ps.getEntityCount(null).intValue();
             } else {
                 writer.writeStartElement(XMLUtil.META_DATA);
                 writer.writeAttribute("version", "4.3");
+                // get number of definition Entities
                 entitiesCount = ps.getEntityCount(Node.TYPE_TBOX).intValue();
             }
 
@@ -92,6 +108,7 @@
             long start = System.currentTimeMillis();
             DecimalFormat df = new DecimalFormat("#.##");
 
+            // list of Relations (filled from Entities)
             List<Relation> relList = new ArrayList<Relation>();
 
             /*
@@ -99,18 +116,21 @@
              */
             writer.writeStartElement((type.equals(Node.TYPE_TBOX)) ? XMLUtil.DEFINITIONS : XMLUtil.ENTITIES);
             writer.writeAttribute("number", Integer.toString(entitiesCount));
+            // go through all pages
             for (int currentPage = 0; currentPage <= numberOfPages; currentPage++) {
                 int startRecord = currentPage * itemsPerPage;
                 List<Entity> entities;
 
                 if (type.equals(Node.TYPE_ABOX)) {
+                	// get page of content Entities
                     entities = ps.getEntityPage(null, startRecord, itemsPerPage);
                 } else {
+                	// get page of definition Entities
                     entities = ps.getEntityPage(Node.TYPE_TBOX, startRecord, itemsPerPage);
                 }
 
                 for (Entity ent : entities) {
-                    // write entities to XML
+                    // write entity to XML
                     writeEntity(ent, writer, ps);
                     // add (source)relations to list
                     relList.addAll(ent.getSourceRelations());
@@ -147,10 +167,9 @@
 
             logger.info("END Stream Writer");
         } catch (IOException e) {
-            e.printStackTrace();
+            logger.error(e);
         } catch (XMLStreamException e) {
-            // TODO Auto-generated catch block
-            e.printStackTrace();
+        	logger.error(e);
         }
     }
 
@@ -170,16 +189,19 @@
         writer.writeAttribute(XMLUtil.OBJECT_CLASS, defaultString(rel.getObjectClass()));
         writer.writeAttribute(XMLUtil.ID, defaultString(rel.getId()));
         writer.writeAttribute(XMLUtil.ROW_ID, defaultString(rel.getRowId()));
-        if (StringUtils.isNotEmpty(rel.getContentType()))
+        if (StringUtils.isNotEmpty(rel.getContentType())) {
             writer.writeAttribute(XMLUtil.CONTENT_TYPE, rel.getContentType());
+        }
         writer.writeAttribute(XMLUtil.RELATION_SOURCE_ID, defaultString(rel.getSourceId()));
         writer.writeAttribute(XMLUtil.RELATION_TARGET_ID, defaultString(rel.getTargetId()));
         writer.writeAttribute(XMLUtil.VERSION, defaultString(rel.getVersion()));
         writer.writeAttribute(XMLUtil.MODIFICATION_TIME, defaultString(rel.getModificationTime()));
-        if (rel.getUser() != null)
+        if (rel.getUser() != null) {
             writer.writeAttribute(XMLUtil.USER, rel.getUser());
-        if (rel.getIsPublic())
+        }
+        if (rel.getIsPublic()) {
             writer.writeAttribute(XMLUtil.PUBLIC, "true");
+        }
 
         /*
          * write OpenMind attributes of this relation as XML tags
@@ -195,8 +217,9 @@
         /*
          *  write own value as content
          */
-        if (StringUtils.isNotEmpty(rel.getOwnValue()))
+        if (StringUtils.isNotEmpty(rel.getOwnValue())) {
             writer.writeCharacters(rel.getOwnValue());
+        }
 
         writer.writeEndElement();
     }
@@ -224,14 +247,17 @@
         writer.writeAttribute(XMLUtil.OBJECT_CLASS, defaultString(entity.getObjectClass()));
         writer.writeAttribute(XMLUtil.ID, defaultString(entity.getId()));
         writer.writeAttribute(XMLUtil.ROW_ID, defaultString(entity.getRowId()));
-        if (StringUtils.isNotEmpty(entity.getContentType()))
+        if (StringUtils.isNotEmpty(entity.getContentType())) {
             writer.writeAttribute(XMLUtil.CONTENT_TYPE, entity.getContentType());
+        }
         writer.writeAttribute(XMLUtil.VERSION, defaultString(entity.getVersion()));
         writer.writeAttribute(XMLUtil.MODIFICATION_TIME, defaultString(entity.getModificationTime()));
-        if (entity.getUser() != null)
+        if (entity.getUser() != null) {
             writer.writeAttribute(XMLUtil.USER, entity.getUser());
-        if (entity.getIsPublic())
+        }
+        if (entity.getIsPublic()) {
             writer.writeAttribute(XMLUtil.PUBLIC, "true");
+        }
 
         /*
          * write OpenMind attributes of this entity as XML tags
@@ -247,8 +273,9 @@
         /*
          * write own value
          */
-        if (StringUtils.isNotEmpty(entity.getOwnValue()))
+        if (StringUtils.isNotEmpty(entity.getOwnValue())) {
             writer.writeCharacters(entity.getOwnValue());
+        }
 
         writer.writeEndElement();
     }
@@ -265,16 +292,19 @@
         writer.writeAttribute(XMLUtil.CONTENT_TYPE, defaultString(att.getContentType()));
         writer.writeAttribute(XMLUtil.VERSION, defaultString(att.getVersion()));
         writer.writeAttribute(XMLUtil.MODIFICATION_TIME, defaultString(att.getModificationTime()));
-        if (att.getUser() != null)
+        if (att.getUser() != null) {
             writer.writeAttribute(XMLUtil.USER, att.getUser());
-        if (att.getIsPublic())
+        }
+        if (att.getIsPublic()) {
             writer.writeAttribute(XMLUtil.PUBLIC, "true");
-
+        }
+            
         /*
          * write value as content
          */
-        if (StringUtils.isNotEmpty(att.getValue()))
+        if (StringUtils.isNotEmpty(att.getValue())) {
             writer.writeCharacters(att.getValue());
+        }
         
         writer.writeEndElement();
     }