changeset 55:b7a8db041f68

cleanup and fix when target_id doesn't exist.
author casties
date Mon, 07 Nov 2016 20:01:51 +0100
parents 7f9033d44a87
children 467843399e70
files src/main/java/org/mpi/openmind/cache/AbstractCacheService.java src/main/java/org/mpi/openmind/cache/WrapperService.java
diffstat 2 files changed, 67 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/src/main/java/org/mpi/openmind/cache/AbstractCacheService.java	Fri Nov 04 18:16:04 2016 +0100
+++ b/src/main/java/org/mpi/openmind/cache/AbstractCacheService.java	Mon Nov 07 20:01:51 2016 +0100
@@ -88,12 +88,14 @@
 	/**
 	 * Returns an entity from the cache by id.
 	 * 
+	 * Loads the entity from the database if its not in the cache.
+	 * 
 	 * Do not modify the entity!
 	 * 
 	 * @param id
 	 * @return
 	 */
-	public Entity getEntityByIdReadOnly(Long id){
+	public Entity getEntityByIdReadOnly(Long id) {
 		if (id == null) {
 		    // TODO: WTF?
 			try {
--- a/src/main/java/org/mpi/openmind/cache/WrapperService.java	Fri Nov 04 18:16:04 2016 +0100
+++ b/src/main/java/org/mpi/openmind/cache/WrapperService.java	Mon Nov 07 20:01:51 2016 +0100
@@ -838,12 +838,34 @@
 		return list;
 	}
 
+	/**
+     * Returns the Entities that are the sources of the target relations of the given Entity.
+     * 
+     * Filters relations by relationName and tarObjClass.
+     * 
+	 * @param tar
+	 * @param relationName
+	 * @param srcObjClass
+	 * @param maxResult
+	 * @return
+	 */
 	public List<Entity> getSourcesForTargetRelation(Entity tar,
 			String relationName, String srcObjClass, int maxResult) {
 		return getSourcesForTargetRelation(tar.getId(), relationName,
 				srcObjClass, maxResult);
 	}
 
+	/**
+     * Returns the Entities that are the sources of the target relations of the Entity with the given tarId.
+     * 
+     * Filters relations by relationName and tarObjClass.
+     * 
+	 * @param tarId
+	 * @param relationName
+	 * @param srcObjClass
+	 * @param maxResult
+	 * @return
+	 */
 	public List<Entity> getSourcesForTargetRelation(Long tarId,
 			String relationName, String srcObjClass, int maxResult) {
 		List<Entity> rs = new ArrayList<Entity>();
@@ -856,7 +878,9 @@
 			if (stringEquals(relationName, rel.getOwnValue())
 					&& stringEquals(srcObjClass, rel.getSourceObjectClass())) {
 				Entity ent = getEntityByIdReadOnly(rel.getSourceId());
-				if (ent != null) {
+				if (ent == null) {
+                    logger.error("Relation source id does not exist! id="+rel.getSourceId()+" relation: "+rel);
+                } else {
 					rs.add(ent);
 					count++;
 					if (maxResult > 0 && count == maxResult) {
@@ -871,12 +895,34 @@
 		return rs;
 	}
 
+	/**
+	 * Returns the Entities that are the targets of the source relations of the given Entity.
+	 * 
+	 * Filters relations by relationName and tarObjClass.
+	 * 
+	 * @param src
+	 * @param relationName
+	 * @param tarObjClass
+	 * @param maxResult
+	 * @return
+	 */
 	public List<Entity> getTargetsForSourceRelation(Entity src,
 			String relationName, String tarObjClass, int maxResult) {
 		return getTargetsForSourceRelation(src.getId(), relationName,
 				tarObjClass, maxResult);
 	}
 
+	/**
+	 * Returns the Entities that are the targets of the source relations of the Entity with the given srcId.
+	 * 
+	 * Filters relations by relationName and tarObjClass.
+	 * 
+	 * @param srcId
+	 * @param relationName
+	 * @param tarObjClass
+	 * @param maxResult
+	 * @return
+	 */
 	public List<Entity> getTargetsForSourceRelation(Long srcId,
 			String relationName, String tarObjClass, int maxResult) {
 		List<Entity> rs = new ArrayList<Entity>();
@@ -888,11 +934,16 @@
 			for (Relation rel : srcRelList) {
 				if (stringEquals(relationName, rel.getOwnValue())
 						&& stringEquals(tarObjClass, rel.getTargetObjectClass())) {
-					rs.add(getEntityByIdReadOnly(rel.getTargetId()));
-					count++;
-					if (maxResult > 0 && count == maxResult) {
-						break;
-					}
+					Entity ent = getEntityByIdReadOnly(rel.getTargetId());
+                    if (ent == null) {
+                        logger.error("Relation target id does not exist! id="+rel.getTargetId()+" relation: "+rel);
+                    } else {
+                        rs.add(ent);
+                        count++;
+                        if (maxResult > 0 && count == maxResult) {
+                            break;
+                        }
+                    }
 				}
 			}
 			Collections.sort(rs, new EntitySortByNormalizedOwnValue());
@@ -1086,6 +1137,13 @@
 		return ent;
 	}
 
+	/**
+	 * Returns if ow equals term if term is not empty. Returns true if term is empty.
+	 * 
+	 * @param term
+	 * @param ow
+	 * @return
+	 */
 	public static boolean stringEquals(String term, String ow) {
 		if (StringUtils.isEmpty(term))
 			return true;