# HG changeset patch # User casties # Date 1478545311 -3600 # Node ID b7a8db041f68feb92e9aee97fca4165fb367b6a0 # Parent 7f9033d44a875df36386cb4302b269edfe494094 cleanup and fix when target_id doesn't exist. diff -r 7f9033d44a87 -r b7a8db041f68 src/main/java/org/mpi/openmind/cache/AbstractCacheService.java --- 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 { diff -r 7f9033d44a87 -r b7a8db041f68 src/main/java/org/mpi/openmind/cache/WrapperService.java --- 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 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 getSourcesForTargetRelation(Long tarId, String relationName, String srcObjClass, int maxResult) { List rs = new ArrayList(); @@ -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 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 getTargetsForSourceRelation(Long srcId, String relationName, String tarObjClass, int maxResult) { List rs = new ArrayList(); @@ -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;