comparison src/main/java/org/mpi/openmind/cache/WrapperService.java @ 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 153a0232270b
comparison
equal deleted inserted replaced
54:7f9033d44a87 55:b7a8db041f68
836 } 836 }
837 837
838 return list; 838 return list;
839 } 839 }
840 840
841 /**
842 * Returns the Entities that are the sources of the target relations of the given Entity.
843 *
844 * Filters relations by relationName and tarObjClass.
845 *
846 * @param tar
847 * @param relationName
848 * @param srcObjClass
849 * @param maxResult
850 * @return
851 */
841 public List<Entity> getSourcesForTargetRelation(Entity tar, 852 public List<Entity> getSourcesForTargetRelation(Entity tar,
842 String relationName, String srcObjClass, int maxResult) { 853 String relationName, String srcObjClass, int maxResult) {
843 return getSourcesForTargetRelation(tar.getId(), relationName, 854 return getSourcesForTargetRelation(tar.getId(), relationName,
844 srcObjClass, maxResult); 855 srcObjClass, maxResult);
845 } 856 }
846 857
858 /**
859 * Returns the Entities that are the sources of the target relations of the Entity with the given tarId.
860 *
861 * Filters relations by relationName and tarObjClass.
862 *
863 * @param tarId
864 * @param relationName
865 * @param srcObjClass
866 * @param maxResult
867 * @return
868 */
847 public List<Entity> getSourcesForTargetRelation(Long tarId, 869 public List<Entity> getSourcesForTargetRelation(Long tarId,
848 String relationName, String srcObjClass, int maxResult) { 870 String relationName, String srcObjClass, int maxResult) {
849 List<Entity> rs = new ArrayList<Entity>(); 871 List<Entity> rs = new ArrayList<Entity>();
850 872
851 List<Relation> tarRelList = this.cache.getRelsByTarId(tarId); 873 List<Relation> tarRelList = this.cache.getRelsByTarId(tarId);
854 int count = 0; 876 int count = 0;
855 for (Relation rel : tarRelList) { 877 for (Relation rel : tarRelList) {
856 if (stringEquals(relationName, rel.getOwnValue()) 878 if (stringEquals(relationName, rel.getOwnValue())
857 && stringEquals(srcObjClass, rel.getSourceObjectClass())) { 879 && stringEquals(srcObjClass, rel.getSourceObjectClass())) {
858 Entity ent = getEntityByIdReadOnly(rel.getSourceId()); 880 Entity ent = getEntityByIdReadOnly(rel.getSourceId());
859 if (ent != null) { 881 if (ent == null) {
882 logger.error("Relation source id does not exist! id="+rel.getSourceId()+" relation: "+rel);
883 } else {
860 rs.add(ent); 884 rs.add(ent);
861 count++; 885 count++;
862 if (maxResult > 0 && count == maxResult) { 886 if (maxResult > 0 && count == maxResult) {
863 break; 887 break;
864 } 888 }
869 + (System.currentTimeMillis() - start)); 893 + (System.currentTimeMillis() - start));
870 Collections.sort(rs, new EntitySortByNormalizedOwnValue()); 894 Collections.sort(rs, new EntitySortByNormalizedOwnValue());
871 return rs; 895 return rs;
872 } 896 }
873 897
898 /**
899 * Returns the Entities that are the targets of the source relations of the given Entity.
900 *
901 * Filters relations by relationName and tarObjClass.
902 *
903 * @param src
904 * @param relationName
905 * @param tarObjClass
906 * @param maxResult
907 * @return
908 */
874 public List<Entity> getTargetsForSourceRelation(Entity src, 909 public List<Entity> getTargetsForSourceRelation(Entity src,
875 String relationName, String tarObjClass, int maxResult) { 910 String relationName, String tarObjClass, int maxResult) {
876 return getTargetsForSourceRelation(src.getId(), relationName, 911 return getTargetsForSourceRelation(src.getId(), relationName,
877 tarObjClass, maxResult); 912 tarObjClass, maxResult);
878 } 913 }
879 914
915 /**
916 * Returns the Entities that are the targets of the source relations of the Entity with the given srcId.
917 *
918 * Filters relations by relationName and tarObjClass.
919 *
920 * @param srcId
921 * @param relationName
922 * @param tarObjClass
923 * @param maxResult
924 * @return
925 */
880 public List<Entity> getTargetsForSourceRelation(Long srcId, 926 public List<Entity> getTargetsForSourceRelation(Long srcId,
881 String relationName, String tarObjClass, int maxResult) { 927 String relationName, String tarObjClass, int maxResult) {
882 List<Entity> rs = new ArrayList<Entity>(); 928 List<Entity> rs = new ArrayList<Entity>();
883 929
884 try { 930 try {
886 932
887 int count = 0; 933 int count = 0;
888 for (Relation rel : srcRelList) { 934 for (Relation rel : srcRelList) {
889 if (stringEquals(relationName, rel.getOwnValue()) 935 if (stringEquals(relationName, rel.getOwnValue())
890 && stringEquals(tarObjClass, rel.getTargetObjectClass())) { 936 && stringEquals(tarObjClass, rel.getTargetObjectClass())) {
891 rs.add(getEntityByIdReadOnly(rel.getTargetId())); 937 Entity ent = getEntityByIdReadOnly(rel.getTargetId());
892 count++; 938 if (ent == null) {
893 if (maxResult > 0 && count == maxResult) { 939 logger.error("Relation target id does not exist! id="+rel.getTargetId()+" relation: "+rel);
894 break; 940 } else {
895 } 941 rs.add(ent);
942 count++;
943 if (maxResult > 0 && count == maxResult) {
944 break;
945 }
946 }
896 } 947 }
897 } 948 }
898 Collections.sort(rs, new EntitySortByNormalizedOwnValue()); 949 Collections.sort(rs, new EntitySortByNormalizedOwnValue());
899 } catch (Exception e) { 950 } catch (Exception e) {
900 logger.error(e.getMessage(), e); 951 logger.error(e.getMessage(), e);
1084 ent = getEntityContentReadOnly(ent); 1135 ent = getEntityContentReadOnly(ent);
1085 } 1136 }
1086 return ent; 1137 return ent;
1087 } 1138 }
1088 1139
1140 /**
1141 * Returns if ow equals term if term is not empty. Returns true if term is empty.
1142 *
1143 * @param term
1144 * @param ow
1145 * @return
1146 */
1089 public static boolean stringEquals(String term, String ow) { 1147 public static boolean stringEquals(String term, String ow) {
1090 if (StringUtils.isEmpty(term)) 1148 if (StringUtils.isEmpty(term))
1091 return true; 1149 return true;
1092 return term.equals(ow); 1150 return term.equals(ow);
1093 } 1151 }