Mercurial > hg > openmind
annotate src/main/java/org/mpi/openmind/cache/WrapperService.java @ 69:bde6212babfd
106: translit normalization should ignore all '-equivalent chars
Task-Url: https://it-dev.mpiwg-berlin.mpg.de/tracs/ismi/ticket/106
author | casties |
---|---|
date | Wed, 01 Feb 2017 19:54:12 +0100 |
parents | 677492395dc0 |
children | aeb29e362a67 |
rev | line source |
---|---|
1 | 1 package org.mpi.openmind.cache; |
2 | |
3 import java.io.Serializable; | |
4 import java.lang.management.ManagementFactory; | |
5 import java.util.ArrayList; | |
6 import java.util.Collection; | |
7 import java.util.Collections; | |
8 import java.util.HashMap; | |
9 import java.util.List; | |
10 import java.util.Map; | |
11 | |
12 import org.apache.commons.lang.StringUtils; | |
13 import org.apache.log4j.Logger; | |
65
74cd973f6ece
more work on stale entity update problem. cache compare function works now.
casties
parents:
64
diff
changeset
|
14 import org.mpi.openmind.cache.CacheService.EntityDiff; |
1 | 15 import org.mpi.openmind.repository.bo.Attribute; |
16 import org.mpi.openmind.repository.bo.Entity; | |
17 import org.mpi.openmind.repository.bo.Node; | |
18 import org.mpi.openmind.repository.bo.Relation; | |
19 import org.mpi.openmind.repository.bo.ViewerAttribute; | |
20 import org.mpi.openmind.repository.bo.ViewerPage; | |
21 import org.mpi.openmind.repository.bo.utils.EntitySortByNormalizedOwnValue; | |
22 import org.mpi.openmind.repository.bo.utils.EntitySortByVersion; | |
23 import org.mpi.openmind.repository.bo.utils.RelationSortBySourceOW; | |
24 import org.mpi.openmind.repository.bo.utils.RelationSortByTargetOW; | |
25 import org.mpi.openmind.repository.services.PersistenceService; | |
26 import org.mpi.openmind.repository.services.utils.AttributeFilter; | |
66 | 27 import org.mpi.openmind.repository.services.utils.EditIntent; |
69
bde6212babfd
106: translit normalization should ignore all '-equivalent chars
casties
parents:
68
diff
changeset
|
28 import org.mpi.openmind.repository.utils.ArabicTranslitNormalizer; |
1 | 29 import org.mpi.openmind.repository.utils.ImportOM3Util; |
30 import org.mpi.openmind.repository.utils.NormalizerUtils; | |
7 | 31 import org.mpi.openmind.repository.utils.RomanizationLoC; |
1 | 32 |
33 public class WrapperService implements Serializable{ | |
34 | |
35 private static final long serialVersionUID = 2578074607841626396L; | |
36 | |
37 public static final String IS_TYPE_OF = "is_type_of"; | |
38 | |
39 private static Logger logger = Logger.getLogger(WrapperService.class); | |
40 | |
45 | 41 private static Logger txLog = Logger.getLogger(PersistenceService.TRANSACTION_LOGGER); |
42 | |
43 private transient CacheService cache; | |
1 | 44 |
45 public long getSourceRelationsCount(Entity entity, String relationName, | |
46 String tarObjClass) { | |
47 return this.getPS().getSourceRelationsCount(entity, relationName, | |
48 tarObjClass); | |
49 } | |
50 | |
51 public Long getTargetRelationsCount(Entity entity, String relationName, | |
52 String srcObjClass) { | |
53 return this.getPS().getTargetRelationsCount(entity, relationName, | |
54 srcObjClass); | |
55 } | |
56 | |
35
1cd9d9a67cad
added regular expression match option to AttributeFilter and searchEntityByAttribute.
casties
parents:
33
diff
changeset
|
57 /** |
1cd9d9a67cad
added regular expression match option to AttributeFilter and searchEntityByAttribute.
casties
parents:
33
diff
changeset
|
58 * Search entities matching a list of attribute filters. |
1cd9d9a67cad
added regular expression match option to AttributeFilter and searchEntityByAttribute.
casties
parents:
33
diff
changeset
|
59 * |
1cd9d9a67cad
added regular expression match option to AttributeFilter and searchEntityByAttribute.
casties
parents:
33
diff
changeset
|
60 * Returns a Map of Entities and Attributes. |
1cd9d9a67cad
added regular expression match option to AttributeFilter and searchEntityByAttribute.
casties
parents:
33
diff
changeset
|
61 * |
1cd9d9a67cad
added regular expression match option to AttributeFilter and searchEntityByAttribute.
casties
parents:
33
diff
changeset
|
62 * @param filters |
1cd9d9a67cad
added regular expression match option to AttributeFilter and searchEntityByAttribute.
casties
parents:
33
diff
changeset
|
63 * @param maxResult |
1cd9d9a67cad
added regular expression match option to AttributeFilter and searchEntityByAttribute.
casties
parents:
33
diff
changeset
|
64 * @return |
1cd9d9a67cad
added regular expression match option to AttributeFilter and searchEntityByAttribute.
casties
parents:
33
diff
changeset
|
65 */ |
1cd9d9a67cad
added regular expression match option to AttributeFilter and searchEntityByAttribute.
casties
parents:
33
diff
changeset
|
66 public Map<Entity, Attribute> searchEntityByAttributeFilter ( |
1 | 67 List<AttributeFilter> filters, int maxResult) { |
68 return this.getPS().searchEntityByAttributeFilter(filters, maxResult); | |
69 } | |
70 | |
71 public Map<Attribute, Entity> searchAttEntityByAttributeFilter( | |
72 List<AttributeFilter> filters, int maxResult) { | |
73 return this.getPS() | |
74 .searchAttEntityByAttributeFilter(filters, maxResult); | |
75 } | |
76 | |
77 public List<Entity> searchEntityByOwnValue(String oc, String term){ | |
78 List<Entity> rs = new ArrayList<Entity>(); | |
79 if(StringUtils.isEmpty(term)) | |
80 return rs; | |
81 | |
37 | 82 List<Entity> entList = this.cache.loadEntitiesByDef(oc); |
1 | 83 term = NormalizerUtils.normalize(term); |
84 | |
85 for(Entity ent : entList){ | |
86 if(StringUtils.isNotEmpty(ent.getNormalizedOwnValue()) && | |
87 StringUtils.contains(ent.getNormalizedOwnValue(), term)){ | |
88 rs.add(ent); | |
89 } | |
90 } | |
91 return rs; | |
92 } | |
93 | |
94 // ************************************************************ | |
95 // ************************************************************ | |
96 // ************************************************************ | |
97 // ************************************************************ | |
98 | |
99 public List<Entity> getPreviousEntitiesById(Long id) { | |
100 List<Entity> list = this.getPS().getEntities(id, | |
101 Node.SYS_STATUS_PREVIOUS_VERSION, null, null); | |
102 Collections.sort(list, new EntitySortByVersion()); | |
103 return list; | |
104 } | |
105 | |
106 public void initCache() { | |
107 logger.info("##### Initializing Cache #####"); | |
108 logger.info(ManagementFactory.getRuntimeMXBean().getName()); | |
109 | |
110 try { | |
111 int mb = 1024 * 1024; | |
112 | |
113 // Getting the runtime reference from system | |
114 Runtime runtime = Runtime.getRuntime(); | |
115 | |
116 logger.info("##### Heap utilization statistics [MB] #####"); | |
117 | |
118 // Print used memory | |
119 logger.info("Used Memory:" | |
120 + (runtime.totalMemory() - runtime.freeMemory()) / mb); | |
121 | |
122 // Print free memory | |
123 logger.info("Free Memory:" + runtime.freeMemory() / mb); | |
124 | |
125 // Print total available memory | |
126 logger.info("Total Memory:" + runtime.totalMemory() / mb); | |
127 | |
128 // Print Maximum available memory | |
129 logger.info("Max Memory:" + runtime.maxMemory() / mb + "\n"); | |
130 } catch (Exception e) { | |
38 | 131 logger.error(e); |
1 | 132 } |
133 | |
134 if (!this.cache.areDefsLoaded()) { | |
135 try { | |
38 | 136 this.cache.initDefinitions(getPS().getLWDefinitions(), |
137 getPS().getDefAttributes(), | |
138 getPS().getDefRelations()); | |
1 | 139 } catch (Exception e) { |
38 | 140 logger.error(e); |
1 | 141 } |
142 } | |
143 } | |
144 | |
51 | 145 public void removeCurrentVersionEntity(Entity entity, String user) throws Exception { |
146 logger.info("removeCurrentVersionEntity ("+ user + ") " + entity); | |
147 txLog.debug("** START remove entity: user="+user+" entity="+entity.toSmallString()); | |
148 // delete entity from cache | |
1 | 149 this.cache.deleteEntity(entity.getId(), entity.getObjectClass()); |
51 | 150 // delete entity from DB |
1 | 151 this.getPS().removeEntCurrentVersion(entity.getId(), entity.getType()); |
51 | 152 txLog.debug("** END remove entity: user="+user+" entity="+entity.toSmallString()); |
1 | 153 } |
154 | |
69
bde6212babfd
106: translit normalization should ignore all '-equivalent chars
casties
parents:
68
diff
changeset
|
155 public Map<Entity, Attribute> searchEntityByAttributeFilter(String term, List<AttributeFilter> filters, |
bde6212babfd
106: translit normalization should ignore all '-equivalent chars
casties
parents:
68
diff
changeset
|
156 int maxResults) { |
1 | 157 Map<Entity, Attribute> map = new HashMap<Entity, Attribute>(); |
158 List<Long> usedIds = new ArrayList<Long>(); | |
159 | |
160 boolean mustBreak = false; | |
161 int count = 0; | |
162 if (StringUtils.isNotEmpty(term)) { | |
69
bde6212babfd
106: translit normalization should ignore all '-equivalent chars
casties
parents:
68
diff
changeset
|
163 // TODO: better normalization |
bde6212babfd
106: translit normalization should ignore all '-equivalent chars
casties
parents:
68
diff
changeset
|
164 String normalizedTerm = ArabicTranslitNormalizer.normalize(term); |
1 | 165 for (AttributeFilter filter : filters) { |
166 if (mustBreak) { | |
167 break; | |
168 } | |
69
bde6212babfd
106: translit normalization should ignore all '-equivalent chars
casties
parents:
68
diff
changeset
|
169 for (Attribute att : getAttributesByDefByAttName(filter.getEntObjectClass(), filter.getName(), -1)) { |
bde6212babfd
106: translit normalization should ignore all '-equivalent chars
casties
parents:
68
diff
changeset
|
170 if (!usedIds.contains(att.getSourceId()) && StringUtils.isNotEmpty(att.getNormalizedOwnValue()) |
bde6212babfd
106: translit normalization should ignore all '-equivalent chars
casties
parents:
68
diff
changeset
|
171 && att.getNormalizedOwnValue().contains(normalizedTerm)) { |
1 | 172 map.put(getEntityById(att.getSourceId()), att); |
173 usedIds.add(att.getSourceId()); | |
174 count++; | |
175 if (maxResults > 0 && count == maxResults) { | |
176 mustBreak = true; | |
177 break; | |
178 } | |
179 } | |
180 } | |
181 } | |
182 } | |
183 return map; | |
184 } | |
185 | |
186 public int getEntitiesCount(String def) { | |
187 int count = this.cache.getEntitiesCount(def); | |
188 return count; | |
189 } | |
190 | |
53 | 191 /** |
192 * Returns a List of Entities of the given definition (object_class). | |
193 * | |
194 * All Entities are sorted by normalized own-value first. Then the | |
195 * part between fromIndex and toIndex is returned. | |
196 * | |
197 * @param def | |
198 * @param fromIndex | |
199 * @param toIndex | |
200 * @return | |
201 */ | |
202 public List<Entity> getEntityByDefSubList(String def, int fromIndex, int toIndex) { | |
203 List<Entity> list = new ArrayList<Entity>(); | |
204 List<Entity> all = getEntitiesByDef(def); | |
205 if (all != null && all.size() >= toIndex) { | |
1 | 206 |
53 | 207 long start = System.currentTimeMillis(); |
208 Collections.sort(list, new EntitySortByNormalizedOwnValue()); | |
209 long diff = System.currentTimeMillis() - start; | |
210 logger.debug("Sorting entities time[ms] " + diff); | |
211 | |
212 for (Entity ent : all.subList(fromIndex, toIndex)) { | |
213 list.add(ent); | |
214 } | |
215 } | |
216 return list; | |
217 } | |
218 | |
219 /** | |
220 * Returns a List of all Entities of the given definition (object_class). | |
221 * | |
222 * @param def | |
223 * @return | |
224 */ | |
1 | 225 public List<Entity> getEntitiesByDef(String def) { |
37 | 226 List<Entity> list = this.cache.loadEntitiesByDef(def); |
1 | 227 return list; |
228 } | |
229 | |
230 public List<Entity> getEntityByDefAndOW(String def, String ow, | |
231 int maxResults) { | |
232 List<Entity> result = new ArrayList<Entity>(); | |
233 if (StringUtils.isNotEmpty(ow)) { | |
37 | 234 List<Entity> list = this.cache.loadEntitiesByDef(def); |
1 | 235 |
236 int count = 0; | |
237 ow = NormalizerUtils.normalize(ow); | |
238 for (Entity e : list) { | |
239 // String eow = (StringUtils.isNotEmpty(e.getOwnValue())) ? | |
240 // e.getOwnValue().toLowerCase() : ""; | |
241 if (StringUtils.isNotBlank(e.getNormalizedOwnValue()) | |
242 && e.getNormalizedOwnValue().contains(ow)) { | |
243 result.add((Entity) e.clone()); | |
244 count++; | |
245 } | |
246 | |
247 if (maxResults > 0 && maxResults == count) { | |
248 break; | |
249 } | |
250 } | |
251 } | |
252 return result; | |
253 } | |
254 | |
255 /** | |
256 * this method does not execute: - ownValue generation - new version | |
257 * generation This method only save the entity in the current state. | |
258 * | |
259 * @param nodeList | |
260 * @param user | |
261 */ | |
262 public void saveEntityListAsNode(List<Entity> entList, String user) { | |
263 for (Entity ent : entList) { | |
264 ent.setUser(user); | |
265 } | |
266 getPS().saveEntityListAsNode(entList); | |
267 for (Entity ent : entList) { | |
268 cache.saveEntity(ent); | |
269 } | |
270 } | |
271 | |
272 public void saveEntityListAsNodeWithoutContent(List<Entity> nodeList, | |
273 String user) throws Exception { | |
274 this.cache.saveEntityListAsNodeWithoutContent(nodeList); | |
275 } | |
276 | |
277 public boolean existEntity(Long entId) { | |
278 return getCache().getEntityByIdReadOnly(entId) != null; | |
279 } | |
280 | |
281 public boolean existRelation(Long srcId, Long tarId, String relName) { | |
282 | |
283 try { | |
284 if (srcId == null || tarId == null) { | |
285 throw new Exception("srcId and tarId can not be null."); | |
286 } | |
287 | |
288 List<Relation> relList = this.cache.getRelsBySrcId(srcId); | |
289 | |
290 for (Relation rel : relList) { | |
291 if (rel.getTargetId().equals(tarId) | |
292 && rel.getOwnValue().equals(relName)) | |
293 return true; | |
294 } | |
295 } catch (Exception e) { | |
296 e.printStackTrace(); | |
297 } | |
298 | |
299 return false; | |
300 } | |
301 | |
302 public void saveRelationAsNode(Relation rel, String user) throws Exception { | |
303 | |
304 if (rel.getAttributes() != null && rel.getAttributes().size() > 0) { | |
305 throw new Exception( | |
306 "This method can be only used if the relation does not contains attributes, this is not the case."); | |
307 } | |
308 | |
309 rel.setUser(user); | |
310 this.getPS().saveNode(rel); | |
311 this.cache.updateRelationAsNode(rel); | |
312 } | |
313 | |
314 public List<Entity> searchEntityByAttributeOfTarRelation(String objClass, | |
315 String relName, String objClassSrc, String attName, | |
316 String attValue, int maxResults) throws Exception { | |
317 List<Entity> list = new ArrayList<Entity>(); | |
318 | |
319 List<Attribute> attList = getAttributesByDefByAttName(objClassSrc, | |
320 attName, attValue, -1); | |
321 int count = 0; | |
322 for (Attribute att : attList) { | |
323 Relation rel = getFirstRelationByTargetOCByName( | |
324 cache.getRelsBySrcId(att.getSourceId()), objClass, relName); | |
325 if (rel != null) { | |
326 list.add(getEntityById(rel.getTargetId())); | |
327 count++; | |
328 if (maxResults > 0 && maxResults == count) { | |
329 break; | |
330 } | |
331 } | |
332 } | |
333 | |
334 return list; | |
335 } | |
336 | |
337 private static Relation getFirstRelationByTargetOCByName( | |
338 List<Relation> list, String tarOC, String name) { | |
339 if (StringUtils.isNotEmpty(name)) { | |
340 name = name.toLowerCase(); | |
341 for (Relation rel : list) { | |
342 if (rel.getTargetObjectClass().equals(tarOC)) { | |
343 if (StringUtils.isNotEmpty(rel.getOwnValue()) | |
344 && name.equals(rel.getOwnValue().toLowerCase())) { | |
345 return rel; | |
346 } | |
347 } | |
348 } | |
349 } | |
350 return null; | |
351 } | |
352 | |
353 public List<Attribute> getAttributeByEntId(Long entId) { | |
354 List<Attribute> list = cache.getAttsBySrcId(entId); | |
355 return list; | |
356 } | |
357 | |
358 public Attribute getAttributeByName(Long entId, String name) { | |
359 List<Attribute> list = cache.getAttsBySrcId(entId); | |
360 | |
361 for (Attribute att : list) { | |
362 if (att.getName().equals(name)) { | |
363 return (Attribute) att.clone(); | |
364 } | |
365 } | |
366 return null; | |
367 } | |
368 | |
7 | 369 public String getAttributeOVByName(Long entId, String name, boolean useRomanization){ |
370 Attribute att = getAttributeByName(entId, name); | |
371 if(att != null){ | |
372 return (useRomanization) ? RomanizationLoC.convert(att.getOwnValue()) : att.getOwnValue(); | |
373 } | |
374 return ""; | |
375 } | |
376 | |
1 | 377 public List<Attribute> searchAttribute(String firstName, String firstValue, |
378 String secondName, String secondValue, String def, int maxResults) { | |
379 List<Attribute> list = new ArrayList<Attribute>(); | |
380 | |
381 List<Attribute> firstAttList = cache.getAttsByOCAndName(def, firstName); | |
382 | |
383 firstValue = StringUtils.isNotEmpty(firstValue) ? NormalizerUtils | |
384 .normalize(firstValue) : null; | |
385 secondValue = StringUtils.isNotEmpty(secondValue) ? NormalizerUtils | |
386 .normalize(secondValue) : null; | |
387 | |
388 if (StringUtils.isNotEmpty(firstValue)) { | |
389 for (Attribute firstAtt : firstAttList) { | |
390 String attValue1 = (StringUtils.isNotEmpty(firstAtt | |
391 .getNormalizedOwnValue())) ? firstAtt | |
392 .getNormalizedOwnValue() : ""; | |
393 // (StringUtils.isNotEmpty(firstAtt.getValue())) ? | |
394 // firstAtt.getValue().toLowerCase() : ""; | |
395 if (StringUtils.isNotEmpty(firstValue) | |
396 && attValue1.contains(firstValue)) { | |
397 Attribute secondAtt = getAttributeByName( | |
398 firstAtt.getSourceId(), secondName); | |
399 if (secondAtt != null) { | |
400 String attValue2 = (StringUtils.isNotEmpty(secondAtt | |
401 .getNormalizedOwnValue())) ? secondAtt | |
402 .getNormalizedOwnValue() : ""; | |
403 // (StringUtils.isNotEmpty(secondAtt.getValue())) ? | |
404 // secondAtt.getValue().toLowerCase() : ""; | |
405 if (StringUtils.isNotEmpty(secondValue) | |
406 && attValue2.contains(attValue2)) { | |
407 list.add((Attribute) firstAtt.clone()); | |
408 } | |
409 } | |
410 } | |
411 } | |
412 } | |
413 | |
414 return list; | |
415 } | |
416 | |
417 private List<Attribute> getAttributesByDefByAttName(String def, | |
418 String attName, int maxResults) { | |
419 List<Attribute> list = cache.getAttsByOCAndName(def, attName); | |
420 return list; | |
421 } | |
422 | |
423 /** | |
58 | 424 * Returns a sorted list of attributes by def (=objectClass) of the entity, the att Name |
425 * and its (normalized) value. | |
1 | 426 * |
427 * @param def | |
428 * @param attName | |
429 * @param attValue | |
58 | 430 * case insensitive sub string match. |
1 | 431 * @param maxResults |
432 * @return | |
433 */ | |
434 public List<Attribute> getAttributesByDefByAttName(String def, | |
435 String attName, String attValue, int maxResults) { | |
436 | |
437 List<Attribute> list = getAttributesByDefByAttName0(def, attName, attValue, maxResults); | |
438 Collections.sort(list); | |
439 return list; | |
440 } | |
441 | |
442 public List<Attribute> getAttributes(String oc, String attName){ | |
443 return cache.getAttsByOCAndName(oc, attName); | |
444 } | |
445 | |
446 /** | |
447 * | |
448 * @param attName | |
449 * @param attValue | |
450 * @return | |
451 */ | |
452 public List<Attribute> getAttributesByExactValue(String attName, String attValue){ | |
453 List<Attribute> list = new ArrayList<Attribute>(); | |
454 | |
455 if(StringUtils.isNotEmpty(attValue)){ | |
456 for(Entity def : cache.getLWDefinitions()){ | |
457 | |
458 List<Attribute> attList = cache.getAttsByOCAndName(def.getOwnValue(), attName); | |
459 attValue = NormalizerUtils.normalize(attValue); | |
460 | |
461 for(Attribute att : attList){ | |
462 if(StringUtils.equals(attValue, att.getNormalizedOwnValue())){ | |
463 list.add(att); | |
464 } | |
465 } | |
466 | |
467 } | |
468 Collections.sort(list); | |
469 } | |
470 return list; | |
471 } | |
472 | |
58 | 473 /** |
474 * Returns a list of attributes by def (=objectClass) of the entity, the att Name | |
475 * and its (normalized) value. | |
476 * | |
477 * @param def | |
478 * @param attName | |
479 * @param attValue | |
480 * @param maxResults | |
481 * @return | |
482 */ | |
1 | 483 private List<Attribute> getAttributesByDefByAttName0(String def, |
484 String attName, String attValue, int maxResults) { | |
485 List<Attribute> list = new ArrayList<Attribute>(); | |
486 List<Attribute> attList = cache.getAttsByOCAndName(def, attName); | |
487 attValue = NormalizerUtils.normalize(attValue); | |
488 int count = 0; | |
489 if (StringUtils.isEmpty(attValue)) { | |
490 for (Attribute att : attList) { | |
491 list.add((Attribute) att.clone()); | |
492 count++; | |
493 if (maxResults > 0 && maxResults == count) { | |
494 break; | |
495 } | |
496 } | |
497 } else { | |
498 for (Attribute att : attList) { | |
499 String attValue0 = StringUtils.isNotEmpty(att | |
500 .getNormalizedOwnValue()) ? att.getNormalizedOwnValue() | |
501 : ""; | |
502 if (StringUtils.isNotEmpty(attValue) | |
503 && attValue0.contains(attValue)) { | |
504 list.add((Attribute) att.clone()); | |
505 count++; | |
506 if (maxResults > 0 && maxResults == count) { | |
507 break; | |
508 } | |
509 } | |
510 } | |
511 } | |
512 return list; | |
513 } | |
514 | |
515 /** | |
516 * Returns a list of entities. | |
517 * | |
518 * @param def | |
519 * @param attName | |
520 * @param attValue | |
521 * ignores case sensitive | |
522 * @param maxResults | |
523 * @param subString | |
524 * if true the attValue is searching as substring | |
525 * @return | |
526 */ | |
527 public List<Entity> getEntitiesByAtt(String def, String attName, | |
528 String attValue, int maxResults, boolean subString) { | |
529 List<Entity> list = new ArrayList<Entity>(); | |
530 | |
531 List<Attribute> attList = cache.getAttsByOCAndName(def, attName); | |
532 | |
533 int count = 0; | |
534 if (StringUtils.isEmpty(attValue)) { | |
535 for (Attribute att : attList) { | |
536 list.add(getEntityById(att.getSourceId())); | |
537 count++; | |
538 if (maxResults > 0 && maxResults == count) { | |
539 break; | |
540 } | |
541 } | |
542 } else { | |
543 attValue = (StringUtils.isNotEmpty(attValue)) ? NormalizerUtils | |
544 .normalize(attValue) : ""; | |
545 for (Attribute att : attList) { | |
546 String attValue0 = (StringUtils.isNotEmpty(att | |
547 .getNormalizedOwnValue())) ? att | |
548 .getNormalizedOwnValue() : ""; | |
549 if ((subString && attValue0.contains(attValue)) | |
550 || (!subString && attValue.equals(attValue0))) { | |
551 list.add(getEntityById(att.getSourceId())); | |
552 count++; | |
553 if (maxResults > 0 && maxResults == count) { | |
554 break; | |
555 } | |
556 } | |
557 } | |
558 } | |
559 return list; | |
560 } | |
561 | |
562 public void removeDefAttribute(Attribute att) throws Exception { | |
563 this.cache.deleteDefAttribute(att); | |
564 att.setSystemStatus(Node.SYS_STATUS_PREVIOUS_VERSION); | |
565 this.getPS().saveNode(att); | |
566 } | |
567 | |
568 public void removeDefRelation(Relation rel) throws Exception { | |
569 this.cache.deleteDefRelation(rel); | |
570 rel.setSystemStatus(Node.SYS_STATUS_PREVIOUS_VERSION); | |
571 this.getPS().saveNode(rel); | |
572 } | |
573 | |
574 public Entity saveLWDefinition(Entity def, String user) throws Exception { | |
575 boolean isNew = !def.isPersistent(); | |
576 def.setUser(user); | |
577 def.increaseVersion(); | |
578 def.setModificationTime(System.currentTimeMillis()); | |
579 def.setType(Node.TYPE_TBOX); | |
580 def.setObjectClass(Node.TYPE_TBOX); | |
581 def.setSystemStatus(Node.SYS_STATUS_CURRENT_VERSION); | |
582 this.getPS().saveNode(def); | |
583 | |
584 if (isNew) { | |
585 this.cache.createLWDefinition(def); | |
586 } else { | |
587 this.cache.saveLWDefinition(def); | |
588 } | |
589 | |
590 return def; | |
591 } | |
592 | |
593 public Attribute saveDefAttribute(Attribute att, String user) | |
594 throws Exception { | |
595 att.setUser(user); | |
596 att.increaseVersion(); | |
597 att.setModificationTime(System.currentTimeMillis()); | |
598 att.setObjectClass(Node.TYPE_TBOX); | |
599 att.setType(Node.TYPE_TBOX); | |
600 att.getPossibleValues(); | |
601 this.getPS().saveNode(att); | |
602 this.cache.saveDefAttribute(att); | |
603 return att; | |
604 } | |
605 | |
606 public Relation saveDefRelation(Relation rel, String user) throws Exception { | |
607 rel.setUser(user); | |
608 rel.increaseVersion(); | |
609 rel.setModificationTime(System.currentTimeMillis()); | |
610 rel.setObjectClass(Node.TYPE_TBOX); | |
611 rel.setType(Node.TYPE_TBOX); | |
612 this.getPS().saveNode(rel); | |
613 this.cache.saveDefRelation(rel); | |
614 return rel; | |
615 } | |
616 | |
33
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
7
diff
changeset
|
617 /** |
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
7
diff
changeset
|
618 * Save the given entity as a separate new entity with a new ID. |
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
7
diff
changeset
|
619 * |
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
7
diff
changeset
|
620 * Keeps attributes and relations as copies. |
1 | 621 * |
33
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
7
diff
changeset
|
622 * @param entity |
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
7
diff
changeset
|
623 * @param user |
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
7
diff
changeset
|
624 * @return |
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
7
diff
changeset
|
625 * @throws Exception |
1 | 626 */ |
627 public Entity saveEntityAsNew(Entity entity, String user) throws Exception { | |
628 long start = System.currentTimeMillis(); | |
629 entity.resetRowId(); | |
630 entity.resetId(); | |
631 entity.setType(Node.TYPE_ABOX); | |
632 entity.setUser(user); | |
45 | 633 |
634 txLog.debug("** START save entity as new: user="+user+" entity="+entity.toSmallString()); | |
635 // save in database | |
1 | 636 this.getPS().saveEntity(entity); |
45 | 637 // save in cache |
1 | 638 cache.saveEntity(entity); |
45 | 639 txLog.debug("** END save entity as new: user="+user+" entity="+entity.toSmallString()); |
640 | |
641 logger.debug("[U=" + user + "] SaveEntityAsNew - execution time[ms]: " + (System.currentTimeMillis() - start)); | |
1 | 642 return (Entity) entity.clone(); |
643 } | |
644 | |
36 | 645 /** |
646 * Save the given entity to the database. | |
647 * | |
648 * Creates a new version of the entity. | |
649 * | |
650 * @param entity | |
651 * @param user | |
66 | 652 * @param intent TODO |
36 | 653 * @return |
654 * @throws Exception | |
655 */ | |
66 | 656 public Entity saveEntity(Entity entity, String user, EditIntent intent) throws Exception { |
1 | 657 long start = System.currentTimeMillis(); |
658 if (StringUtils.isEmpty(entity.getType())) { | |
659 entity.setType(Node.TYPE_ABOX); | |
660 } | |
661 entity.setUser(user); | |
662 | |
663 entity = removeWrongRelations(entity); | |
664 entity = removeEmptyAttributes(entity); | |
665 | |
45 | 666 txLog.debug("** START save entity: user="+user+" entity="+entity.toSmallString()); |
64 | 667 // check if entity is up to date with cache |
668 if (!cache.isEntityCurrent(entity)) { | |
67 | 669 logger.warn("Possible collision: Entity to save not current!"); |
66 | 670 // create differences from cache |
65
74cd973f6ece
more work on stale entity update problem. cache compare function works now.
casties
parents:
64
diff
changeset
|
671 EntityDiff diff = cache.diffEntityCache(entity, true); |
66 | 672 if (diff != null) { |
673 if (intent != null) { | |
674 /* | |
675 * check if diff was intended | |
676 */ | |
677 if (!intent.isEntModificationIntended(entity.getObjectClass())) { | |
678 logger.error("unintended modification of entity: "+entity); | |
679 } | |
680 for (Attribute a : diff.addedAttributes) { | |
681 if (!intent.isAttModificationIntended(a.getName())) { | |
682 logger.error("unintended addition: "+a); | |
683 } | |
684 } | |
685 for (Attribute a : diff.modifiedAttributes) { | |
686 if (!intent.isAttModificationIntended(a.getName())) { | |
687 logger.error("unintended modification: "+a); | |
688 } | |
689 } | |
690 for (Attribute a : diff.removedAttributes) { | |
691 if (!intent.isAttModificationIntended(a.getName())) { | |
692 logger.error("unintended removal prevented: "+a); | |
693 entity.addAttribute(a); | |
694 } | |
695 } | |
696 for (Relation r : diff.addedSrcRels) { | |
697 if (!intent.isSrcRelModificationIntended(r.getObjectClass())) { | |
698 logger.error("unintended addition: "+r); | |
699 } | |
700 } | |
701 for (Relation r : diff.modifiedSrcRels) { | |
702 if (!intent.isSrcRelModificationIntended(r.getObjectClass())) { | |
703 logger.error("unintended modification: "+r); | |
704 } | |
705 } | |
706 for (Relation r : diff.removedSrcRels) { | |
707 if (!intent.isSrcRelModificationIntended(r.getObjectClass())) { | |
708 logger.error("unintended removal prevented: "+r); | |
709 // re-add | |
710 entity.addSourceRelation(r); | |
711 } | |
712 } | |
713 for (Relation r : diff.addedTarRels) { | |
714 if (!intent.isTarRelModificationIntended(r.getObjectClass())) { | |
715 logger.error("unintended addition: "+r); | |
716 } | |
717 } | |
718 for (Relation r : diff.modifiedTarRels) { | |
719 if (!intent.isTarRelModificationIntended(r.getObjectClass())) { | |
720 logger.error("unintended modification: "+r); | |
721 } | |
722 } | |
723 for (Relation r : diff.removedTarRels) { | |
724 if (!intent.isTarRelModificationIntended(r.getObjectClass())) { | |
725 logger.error("unintended removal prevented: "+r); | |
726 // re-add | |
727 entity.addTargetRelation(r); | |
728 } | |
729 } | |
730 } else { | |
67 | 731 logger.warn("Save with missing EditIntent!"); |
66 | 732 } |
67 | 733 } else { |
68 | 734 logger.warn("No difference to cache found."); |
66 | 735 } |
64 | 736 } |
737 | |
45 | 738 // save in database |
1 | 739 this.getPS().saveEntity(entity); |
45 | 740 // save in cache |
1 | 741 cache.saveEntity(entity); |
45 | 742 txLog.debug("** END save entity: user="+user+" entity="+entity.toSmallString()); |
743 | |
744 logger.debug("[U=" + user + "] SaveEntity - execution time[ms]: " + (System.currentTimeMillis() - start)); | |
1 | 745 Entity clone = (Entity) entity.clone(); |
746 return clone; | |
747 } | |
748 | |
749 public void saveEntityList(List<Entity> list, String user) throws Exception{ | |
750 long start = System.currentTimeMillis(); | |
751 for(Entity entity : list){ | |
752 if (StringUtils.isEmpty(entity.getType())) { | |
753 entity.setType(Node.TYPE_ABOX); | |
754 } | |
755 entity.setUser(user); | |
756 | |
757 entity = removeWrongRelations(entity); | |
758 entity = removeEmptyAttributes(entity); | |
759 } | |
45 | 760 txLog.debug("** START save entity list: user="+user+" list="+list); |
1 | 761 this.getPS().saveEntityList(list); |
762 for(Entity entity : list){ | |
763 cache.saveEntity(entity); | |
764 } | |
45 | 765 txLog.debug("** END save entity list: user="+user+" list="+list); |
766 logger.debug("[U=" + user + "] SaveEntityList - execution time[ms]: " + (System.currentTimeMillis() - start)); | |
1 | 767 } |
768 | |
769 private Entity removeEmptyAttributes(Entity ent){ | |
770 | |
771 for(Attribute att : new ArrayList<Attribute>(ent.getAttributes())){ | |
772 if(StringUtils.isEmpty(att.getValue())){ | |
773 ent.getAttributes().remove(att); | |
774 } | |
775 } | |
776 return ent; | |
777 | |
778 } | |
779 | |
33
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
7
diff
changeset
|
780 /** |
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
7
diff
changeset
|
781 * Returns if this entity has wrong relations. |
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
7
diff
changeset
|
782 * |
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
7
diff
changeset
|
783 * Wrong are source relations whose target doesn't exist. |
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
7
diff
changeset
|
784 * Similarly for target relations. |
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
7
diff
changeset
|
785 * |
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
7
diff
changeset
|
786 * @param ent |
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
7
diff
changeset
|
787 * @return |
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
7
diff
changeset
|
788 */ |
1 | 789 private boolean hasWrongRelations(Entity ent) { |
790 for (Relation srcRel : ent.getSourceRelations()) { | |
791 if (getEntityById(srcRel.getTargetId()) == null) { | |
792 return true; | |
793 } | |
794 } | |
795 | |
796 for (Relation tarRel : ent.getTargetRelations()) { | |
797 if (getEntityById(tarRel.getSourceId()) == null) { | |
798 return true; | |
799 } | |
800 } | |
801 return false; | |
802 } | |
803 | |
33
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
7
diff
changeset
|
804 /** |
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
7
diff
changeset
|
805 * Remove wrong relations from this entity. |
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
7
diff
changeset
|
806 * |
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
7
diff
changeset
|
807 * Wrong are source relations whose target is null or doesn't exist. |
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
7
diff
changeset
|
808 * Similarly for target relations. |
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
7
diff
changeset
|
809 * |
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
7
diff
changeset
|
810 * @param ent |
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
7
diff
changeset
|
811 * @return |
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
7
diff
changeset
|
812 */ |
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
7
diff
changeset
|
813 private Entity removeWrongRelations(Entity ent) { |
1 | 814 |
33
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
7
diff
changeset
|
815 for (Relation srcRel : new ArrayList<Relation>(ent.getSourceRelations())) { |
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
7
diff
changeset
|
816 if (srcRel.getTargetId() == null || getEntityByIdReadOnly(srcRel.getTargetId()) == null) { |
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
7
diff
changeset
|
817 ent.getSourceRelations().remove(srcRel); |
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
7
diff
changeset
|
818 logger.error("Inconsistency detected saving entity [" + ent.getId() + "] " + srcRel.toString()); |
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
7
diff
changeset
|
819 } |
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
7
diff
changeset
|
820 } |
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
7
diff
changeset
|
821 |
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
7
diff
changeset
|
822 for (Relation tarRel : new ArrayList<Relation>(ent.getTargetRelations())) { |
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
7
diff
changeset
|
823 if (tarRel.getSourceId() == null || getEntityByIdReadOnly(tarRel.getSourceId()) == null) { |
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
7
diff
changeset
|
824 ent.getTargetRelations().remove(tarRel); |
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
7
diff
changeset
|
825 logger.error("Inconsistency detected saving entity [" + ent.getId() + "] " + tarRel.toString()); |
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
7
diff
changeset
|
826 } |
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
7
diff
changeset
|
827 } |
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
7
diff
changeset
|
828 |
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
7
diff
changeset
|
829 return ent; |
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
7
diff
changeset
|
830 } |
1 | 831 |
832 public Attribute getDefAttributeByOwnValue(String defOC, String attOW) { | |
833 for (Attribute att : this.cache.getDefAttributes(defOC)) { | |
834 if (att.getOwnValue().equals(attOW)) { | |
835 return att; | |
836 } | |
837 } | |
838 return null; | |
839 } | |
840 | |
841 public List<Attribute> getDefRelationAttributes(Long id) { | |
842 return this.cache.getDefAttributesById(id); | |
843 } | |
38 | 844 |
845 /** | |
846 * Returns the attributes of the definition of the given type. | |
847 * | |
848 * @param objectClass | |
849 * @return | |
850 */ | |
851 public List<Attribute> getDefAttributes(String objectClass) { | |
852 return this.cache.getDefAttributes(objectClass); | |
1 | 853 } |
854 | |
38 | 855 /** |
856 * Returns the source relations of the definition of the given type. | |
857 * | |
858 * @param objectClass | |
859 * @return | |
860 */ | |
861 public List<Relation> getDefSourceRelations(String objectClass) { | |
862 return this.cache.getDefSourceRelations(objectClass); | |
1 | 863 } |
864 | |
38 | 865 /** |
866 * Returns the target relations of the definition of the given type. | |
867 * | |
868 * @param objectClass | |
869 * @return | |
870 */ | |
871 public List<Relation> getDefTargetRelations(String objectClass) { | |
872 return this.cache.getDefTargetRelations(objectClass); | |
1 | 873 } |
874 | |
875 public Entity getDefinitionById(Long id) { | |
876 return this.cache.getLWDefinitionById(id); | |
877 } | |
878 | |
38 | 879 /** |
880 * Returns all (lightweight) definition entities. | |
881 * | |
882 * @return | |
883 */ | |
1 | 884 public List<Entity> getLWDefinitions() { |
885 return this.cache.getLWDefinitions(); | |
886 } | |
887 | |
38 | 888 /** |
889 * Returns the (lightweight) definition entity for the given type. | |
890 * | |
891 * @param objectClass | |
892 * @return | |
893 */ | |
894 public Entity getDefinition(String objectClass) { | |
895 return this.cache.getLWDefinition(objectClass); | |
1 | 896 } |
897 | |
898 public List<Relation> getRelation(String name, String srcOC, String tarOC) | |
899 throws Exception { | |
900 // TODO read only??? | |
901 List<Relation> list = new ArrayList<Relation>(); | |
902 if (StringUtils.isNotEmpty(name)) { | |
903 List<Relation> list0 = cache.getRelsByName(name); | |
904 if (StringUtils.isEmpty(srcOC) && StringUtils.isEmpty(tarOC)) { | |
905 list = list0; | |
906 } else { | |
907 for (Relation rel : list0) { | |
908 if ((StringUtils.isEmpty(srcOC) || srcOC.equals(rel | |
909 .getSourceObjectClass())) | |
910 && (StringUtils.isEmpty(tarOC) || tarOC.equals(rel | |
911 .getTargetObjectClass()))) { | |
912 list.add(rel); | |
913 } | |
914 } | |
915 } | |
916 } | |
917 | |
918 return list; | |
919 } | |
920 | |
55 | 921 /** |
922 * Returns the Entities that are the sources of the target relations of the given Entity. | |
923 * | |
924 * Filters relations by relationName and tarObjClass. | |
925 * | |
926 * @param tar | |
927 * @param relationName | |
928 * @param srcObjClass | |
929 * @param maxResult | |
930 * @return | |
931 */ | |
1 | 932 public List<Entity> getSourcesForTargetRelation(Entity tar, |
933 String relationName, String srcObjClass, int maxResult) { | |
934 return getSourcesForTargetRelation(tar.getId(), relationName, | |
935 srcObjClass, maxResult); | |
936 } | |
937 | |
55 | 938 /** |
939 * Returns the Entities that are the sources of the target relations of the Entity with the given tarId. | |
940 * | |
941 * Filters relations by relationName and tarObjClass. | |
942 * | |
943 * @param tarId | |
944 * @param relationName | |
945 * @param srcObjClass | |
946 * @param maxResult | |
947 * @return | |
948 */ | |
1 | 949 public List<Entity> getSourcesForTargetRelation(Long tarId, |
950 String relationName, String srcObjClass, int maxResult) { | |
951 List<Entity> rs = new ArrayList<Entity>(); | |
952 | |
953 List<Relation> tarRelList = this.cache.getRelsByTarId(tarId); | |
954 | |
67 | 955 //long start = System.currentTimeMillis(); |
1 | 956 int count = 0; |
957 for (Relation rel : tarRelList) { | |
958 if (stringEquals(relationName, rel.getOwnValue()) | |
959 && stringEquals(srcObjClass, rel.getSourceObjectClass())) { | |
960 Entity ent = getEntityByIdReadOnly(rel.getSourceId()); | |
55 | 961 if (ent == null) { |
962 logger.error("Relation source id does not exist! id="+rel.getSourceId()+" relation: "+rel); | |
963 } else { | |
1 | 964 rs.add(ent); |
965 count++; | |
966 if (maxResult > 0 && count == maxResult) { | |
967 break; | |
968 } | |
969 } | |
970 } | |
971 } | |
64 | 972 //logger.debug("getSourcesForTargetRelation (loading sources) - execution time[ms]: "+(System.currentTimeMillis() - start)); |
1 | 973 Collections.sort(rs, new EntitySortByNormalizedOwnValue()); |
974 return rs; | |
975 } | |
976 | |
55 | 977 /** |
978 * Returns the Entities that are the targets of the source relations of the given Entity. | |
979 * | |
980 * Filters relations by relationName and tarObjClass. | |
981 * | |
982 * @param src | |
983 * @param relationName | |
984 * @param tarObjClass | |
985 * @param maxResult | |
986 * @return | |
987 */ | |
1 | 988 public List<Entity> getTargetsForSourceRelation(Entity src, |
989 String relationName, String tarObjClass, int maxResult) { | |
990 return getTargetsForSourceRelation(src.getId(), relationName, | |
991 tarObjClass, maxResult); | |
992 } | |
993 | |
55 | 994 /** |
995 * Returns the Entities that are the targets of the source relations of the Entity with the given srcId. | |
996 * | |
997 * Filters relations by relationName and tarObjClass. | |
998 * | |
999 * @param srcId | |
1000 * @param relationName | |
1001 * @param tarObjClass | |
1002 * @param maxResult | |
1003 * @return | |
1004 */ | |
1 | 1005 public List<Entity> getTargetsForSourceRelation(Long srcId, |
1006 String relationName, String tarObjClass, int maxResult) { | |
1007 List<Entity> rs = new ArrayList<Entity>(); | |
1008 | |
1009 try { | |
1010 Collection<Relation> srcRelList = this.cache.getRelsBySrcId(srcId); | |
1011 | |
1012 int count = 0; | |
1013 for (Relation rel : srcRelList) { | |
1014 if (stringEquals(relationName, rel.getOwnValue()) | |
1015 && stringEquals(tarObjClass, rel.getTargetObjectClass())) { | |
55 | 1016 Entity ent = getEntityByIdReadOnly(rel.getTargetId()); |
1017 if (ent == null) { | |
1018 logger.error("Relation target id does not exist! id="+rel.getTargetId()+" relation: "+rel); | |
1019 } else { | |
1020 rs.add(ent); | |
1021 count++; | |
1022 if (maxResult > 0 && count == maxResult) { | |
1023 break; | |
1024 } | |
1025 } | |
1 | 1026 } |
1027 } | |
1028 Collections.sort(rs, new EntitySortByNormalizedOwnValue()); | |
1029 } catch (Exception e) { | |
1030 logger.error(e.getMessage(), e); | |
1031 } | |
1032 | |
1033 return rs; | |
1034 } | |
1035 | |
1036 public List<Relation> getTargetRelations(Entity target, | |
1037 String relationName, String srcObjClass, int maxResult) { | |
67 | 1038 //long start = System.currentTimeMillis(); |
1 | 1039 List<Relation> list = new ArrayList<Relation>(); |
1040 | |
1041 List<Relation> tarRelList = this.cache.getRelsByTarId(target.getId()); | |
1042 | |
1043 int count = 0; | |
1044 for (Relation rel : tarRelList) { | |
1045 if (stringEquals(relationName, rel.getOwnValue()) | |
1046 && stringEquals(srcObjClass, rel.getSourceObjectClass())) { | |
1047 Entity source = getEntityById(rel.getSourceId()); | |
1048 if (source != null) { | |
1049 rel.setTarget(target); | |
1050 rel.setSource(source); | |
1051 list.add(rel); | |
1052 count++; | |
1053 if (maxResult > 0 && count == maxResult) { | |
1054 break; | |
1055 } | |
1056 } else { | |
67 | 1057 logger.error("Relation without source: "+ rel.toString()); |
1 | 1058 } |
1059 | |
1060 } | |
1061 } | |
1062 Collections.sort(list, new RelationSortBySourceOW()); | |
64 | 1063 //logger.debug("getTargetRelations - execution time[ms]: "+ (System.currentTimeMillis() - start)); |
1 | 1064 return list; |
1065 } | |
1066 | |
1067 /** | |
1068 * Returns a list of relations found by entity source. | |
1069 * | |
1070 * @param source | |
1071 * @param relationName | |
1072 * @param tarObjClass | |
1073 * @param maxResult | |
1074 * @return | |
1075 */ | |
1076 public List<Relation> getSourceRelations(Entity source, | |
1077 String relationName, String tarObjClass, int maxResult) | |
1078 throws Exception { | |
1079 | |
67 | 1080 //long start = System.currentTimeMillis(); |
1 | 1081 List<Relation> list = new ArrayList<Relation>(); |
1082 | |
1083 // the collection coll should not be modified | |
1084 // otherwise java.util.ConcurrentModificationException will be thrown. | |
1085 // be carefully using getEntityContent, cause it changes the cache | |
1086 | |
1087 List<Relation> srcRelList = this.cache.getRelsBySrcId(source.getId()); | |
1088 | |
1089 int count = 0; | |
1090 for (Relation rel : srcRelList) { | |
1091 if (stringEquals(relationName, rel.getOwnValue()) | |
1092 && stringEquals(tarObjClass, rel.getTargetObjectClass())) { | |
1093 rel.setSource(source); | |
1094 rel.setTarget(getEntityById(rel.getTargetId())); | |
1095 list.add(rel); | |
1096 count++; | |
1097 if (maxResult > 0 && count == maxResult) { | |
1098 break; | |
1099 } | |
1100 } | |
1101 } | |
1102 Collections.sort(list, new RelationSortByTargetOW()); | |
64 | 1103 //logger.debug("getSourceRelations - execution time[ms]: "+ (System.currentTimeMillis() - start)); |
1 | 1104 |
1105 return list; | |
1106 } | |
1107 | |
1108 /** | |
54 | 1109 * Load and attach the Attributes and Relations of the given Entity. |
1110 * | |
1111 * Tries to loads from cache first and then from DB, saving to cache. | |
1112 * | |
1113 * Returns a cloned Entity. | |
1114 * | |
1 | 1115 * This method should not be used inside this Wrapper class, because it |
54 | 1116 * could throw a java.util.ConcurrentModificationException. |
1 | 1117 * |
1118 * @param ent | |
1119 * @return | |
1120 */ | |
1121 public Entity getEntityContent(Entity ent) { | |
1122 try { | |
1123 if (ent.isLightweight()) { | |
1124 Entity e = this.cache.getEntityContent(ent); | |
1125 if (e == null) { | |
1126 e = this.getPS().getEntityContent(ent); | |
1127 this.cache.saveEntity(e); | |
1128 } else { | |
1129 ent = e; | |
1130 } | |
1131 } | |
1132 return (Entity) ent.clone(); | |
1133 } catch (Exception e) { | |
1134 logger.error(e.getMessage(), e); | |
1135 } | |
1136 return null; | |
1137 } | |
1138 | |
1139 public Entity getEntityContentReadOnly(Entity ent) throws Exception { | |
1140 if (ent.isLightweight()) { | |
1141 Entity e = this.cache.getEntityContent(ent); | |
1142 if (e == null) { | |
1143 e = this.getPS().getEntityContent(ent); | |
1144 this.cache.saveEntity(e); | |
1145 } else { | |
1146 ent = e; | |
1147 } | |
1148 } | |
1149 return ent; | |
1150 } | |
1151 | |
1152 /** | |
39 | 1153 * Return an entity with the given id from the cache. |
1154 * | |
1 | 1155 * The entity returned could be LW or not, it depends on the cache. |
1156 * additionally, it will be always a clone of the version in cache, | |
1157 * therefore it method is thought for editing's issues. | |
1158 * | |
1159 * @param id | |
1160 * @return | |
1161 */ | |
1162 public Entity getEntityById(Long id) { | |
1163 Entity ent = this.cache.getEntityById(id); | |
1164 return ent; | |
1165 } | |
1166 | |
39 | 1167 /** |
1168 * Return an entity with the given id from the cache. | |
1169 * | |
1170 * Do not modify the entity! | |
1171 * | |
1172 * @param id | |
1173 * @return | |
1174 */ | |
1 | 1175 public Entity getEntityByIdReadOnly(Long id) { |
1176 Entity ent = this.cache.getEntityByIdReadOnly(id); | |
1177 return ent; | |
1178 } | |
1179 | |
1180 public Entity getClonedEntityById(Long id) { | |
1181 return (Entity) getEntityById(id).clone(); | |
1182 } | |
1183 | |
1184 /** | |
1185 * This method should not be used inside this Wrapper class, because it | |
1186 * could throws an java.util.ConcurrentModificationException. | |
1187 * | |
1188 * @param id | |
1189 * @return | |
1190 */ | |
1191 public Entity getEntityByIdWithContent(Long id) { | |
1192 try { | |
1193 Entity ent = getEntityById(id); | |
1194 if (ent != null && ent.isLightweight()) { | |
1195 ent = getEntityContent(ent); | |
1196 } | |
1197 return ent; | |
1198 } catch (Exception e) { | |
1199 logger.error(e.getMessage(), e); | |
1200 } | |
1201 return null; | |
1202 } | |
1203 | |
1204 public Entity getEntityByIdWithContentReadOnly(Long id) throws Exception { | |
1205 Entity ent = getEntityByIdReadOnly(id); | |
1206 if (ent.isLightweight()) { | |
1207 ent = getEntityContentReadOnly(ent); | |
1208 } | |
1209 return ent; | |
1210 } | |
1211 | |
55 | 1212 /** |
1213 * Returns if ow equals term if term is not empty. Returns true if term is empty. | |
1214 * | |
1215 * @param term | |
1216 * @param ow | |
1217 * @return | |
1218 */ | |
1 | 1219 public static boolean stringEquals(String term, String ow) { |
1220 if (StringUtils.isEmpty(term)) | |
1221 return true; | |
1222 return term.equals(ow); | |
1223 } | |
1224 | |
1225 // ////**** | |
1226 public Entity getLightweightEntityById(Long id) { | |
1227 Entity entity = null; | |
1228 if (id != null) { | |
1229 List<Entity> list = this.getPS().getLightweightEntities( | |
1230 Node.SYS_STATUS_CURRENT_VERSION, id, null, null, null, | |
1231 true, -1); | |
1232 if (list.size() > 0) { | |
1233 entity = list.get(0); | |
1234 } | |
1235 } | |
1236 return entity; | |
1237 } | |
1238 | |
1239 public List<Entity> getLightweightAssertions(String objectClass, | |
1240 String ownValue, int maxResult) { | |
1241 return getPS().getLightweightEntities(Node.SYS_STATUS_CURRENT_VERSION, | |
1242 null, Node.TYPE_ABOX, objectClass, ownValue, true, maxResult); | |
1243 } | |
1244 | |
38 | 1245 /** |
66 | 1246 * Save the entity. Calls {@link #saveEntity(String, String, EditIntent)}. |
38 | 1247 * |
1248 * @param entity | |
1249 * @param user | |
1250 * @throws Exception | |
1251 */ | |
1 | 1252 public void saveAssertion(Entity entity, String user) throws Exception { |
66 | 1253 this.saveEntity(entity, user, null); |
1 | 1254 } |
1255 | |
1256 /** | |
1257 * <p> | |
1258 * This method saves a non-persistent entity. | |
1259 * </p> | |
1260 * <p> | |
1261 * The reason of this is that nodes are stored only in the DB and not in the | |
1262 * cache. | |
1263 * </p> | |
1264 * <p> | |
1265 * Later, when the nodes are required, they will be loaded from the BD to | |
1266 * the cache automatically. However there are some structures like | |
1267 * attributes by entId that become inconsistent using this method. | |
1268 * </p> | |
1269 * | |
1270 * @param node | |
1271 * non-persistent node. | |
1272 * @param user | |
1273 * @throws Exception | |
1274 */ | |
1275 public void saveNodeOnlyForScripts(Node node, String user) throws Exception { | |
1276 if (node.isPersistent()) { | |
1277 throw new Exception( | |
1278 "This method can save only non-persistent nodes. Because the nodes are stored only in the DB in not in the Cache."); | |
1279 } | |
1280 if (StringUtils.isEmpty(node.getType())) { | |
1281 throw new Exception("The type of the node can not be empty"); | |
1282 } | |
1283 node.setUser(user); | |
1284 this.getPS().saveNode(node); | |
1285 | |
1286 // if(StringUtils.isNotEmpty(node.getType())){ | |
1287 // if(node.getType().equals(Node.TYPE_ABOX)){ | |
1288 // | |
1289 // }else if(node.getType().equals(Node.TYPE_TBOX)){ | |
1290 // | |
1291 // } | |
1292 // } | |
1293 } | |
1294 | |
1295 public void removeNode(Node node) { | |
1296 this.getPS().removeNode(node); | |
1297 } | |
1298 | |
1299 public Map<Long, Long> saveEntityListAsNew(List<Entity> entities, String user, | |
1300 boolean testWrongRelations) throws Exception { | |
1301 logger.info("\n ### Making persistent Entities size total " | |
1302 + entities.size() + " ### \n"); | |
1303 int sizePart = 1000; | |
1304 List<Entity> tmpList = null; | |
1305 | |
1306 Map<Long, Long> idMap = new HashMap<Long, Long>(); | |
1307 | |
1308 for (int j = 0; j <= (entities.size() / sizePart); j++) { | |
1309 tmpList = new ArrayList<Entity>(sizePart); | |
1310 for (int i = (j * sizePart); i < ((j + 1) * sizePart); i++) { | |
1311 | |
1312 if (i < entities.size()) { | |
1313 if (StringUtils.isNotEmpty(user)) { | |
1314 entities.get(i).setUser(user); | |
1315 } | |
1316 | |
1317 if (testWrongRelations | |
1318 && hasWrongRelations(entities.get(i))) { | |
1319 throw new Exception("Wrong relations " | |
1320 + entities.get(i)); | |
1321 } | |
1322 | |
1323 tmpList.add(entities.get(i)); | |
1324 } else { | |
1325 break; | |
1326 } | |
1327 } | |
1328 | |
1329 logger.info("\nMaking persistent Entities part " + (j + 1) + " of " | |
1330 + ((entities.size() / sizePart) + 1) + " size=" | |
1331 + tmpList.size()); | |
1332 idMap = this.getPS().saveEntityListAsNew(tmpList, idMap); | |
1333 } | |
1334 return idMap; | |
1335 } | |
1336 | |
1337 /* | |
1338 public void saveEntityList(List<Entity> entities, String user, | |
1339 boolean testWrongRelations) throws Exception { | |
1340 logger.info("\n ### Making persistent Entities size total " | |
1341 + entities.size() + " ### \n"); | |
1342 int sizePart = 1000; | |
1343 List<Entity> tmpList = null; | |
1344 | |
1345 for (int j = 0; j <= (entities.size() / sizePart); j++) { | |
1346 tmpList = new ArrayList<Entity>(sizePart); | |
1347 for (int i = (j * sizePart); i < ((j + 1) * sizePart); i++) { | |
1348 | |
1349 if (i < entities.size()) { | |
1350 if (StringUtils.isNotEmpty(user)) { | |
1351 entities.get(i).setUser(user); | |
1352 } | |
1353 | |
1354 if (testWrongRelations | |
1355 && hasWrongRelations(entities.get(i))) { | |
1356 throw new Exception("Wrong relations " | |
1357 + entities.get(i)); | |
1358 } | |
1359 | |
1360 tmpList.add(entities.get(i)); | |
1361 } else { | |
1362 break; | |
1363 } | |
1364 } | |
1365 | |
1366 logger.info("\nMaking persistent Entities part " + (j + 1) + " of " | |
1367 + ((entities.size() / sizePart) + 1) + " size=" | |
1368 + tmpList.size()); | |
1369 this.getPS().saveEntityList(tmpList); | |
1370 | |
1371 for(Entity ent : tmpList){ | |
1372 logger.info("[U=" + user + "] SaveEntity " + ent.toSmallString()); | |
1373 } | |
1374 | |
1375 } | |
1376 }*/ | |
1377 | |
1378 public void saveConcept(Entity entity) throws Exception { | |
1379 entity.setType(Node.TYPE_TBOX); | |
1380 entity.setObjectClass(Node.TYPE_TBOX); | |
1381 this.getPS().saveEntity(entity); | |
1382 } | |
1383 | |
1384 public void saveNodeListOnlyForScripts(List<Node> nodeList, String user) | |
1385 throws Exception { | |
1386 logger.debug("### Making persistent Nodes size total " | |
1387 + nodeList.size() + " ###"); | |
1388 List<Node> list = null; | |
1389 int sizePart = 1000; | |
1390 for (int j = 0; j <= (nodeList.size() / sizePart); j++) { | |
1391 list = new ArrayList<Node>(); | |
1392 for (int i = (j * sizePart); i < ((j + 1) * sizePart); i++) { | |
1393 if (i < nodeList.size()) { | |
1394 nodeList.get(i).setUser(user); | |
1395 list.add(nodeList.get(i)); | |
1396 } | |
1397 } | |
1398 logger.debug("Making persistent Nodes part " + (j + 1) + " of " | |
1399 + ((nodeList.size() / sizePart) + 1) + " size=" | |
1400 + list.size()); | |
1401 this.getPS().saveNodeList(list); | |
1402 } | |
1403 } | |
1404 | |
1405 public void saveNodeListOnlyForScripts(List<Node> nodeList) | |
1406 throws Exception { | |
1407 logger.info("\n ### Making persistent Nodes size total " | |
1408 + nodeList.size() + " ### \n"); | |
1409 int sizePart = 1000; | |
1410 List<Node> list = null; | |
1411 for (int j = 0; j <= (nodeList.size() / sizePart); j++) { | |
1412 list = new ArrayList<Node>(); | |
1413 for (int i = (j * sizePart); i < ((j + 1) * sizePart); i++) { | |
1414 if (i < nodeList.size()) { | |
1415 list.add(nodeList.get(i)); | |
1416 } | |
1417 } | |
1418 logger.info("Making persistent Nodes part " + (j + 1) + " of " | |
1419 + ((nodeList.size() / sizePart) + 1) + " size=" | |
1420 + list.size()); | |
1421 this.getPS().saveNodeList(list); | |
1422 } | |
1423 // this.persistenceService.saveNodeList(nodeList); | |
1424 } | |
1425 | |
1426 public List<Entity> getConcepts() { | |
1427 return this.getPS().getEntities(null, Node.SYS_STATUS_CURRENT_VERSION, | |
1428 Node.TYPE_TBOX, null); | |
1429 } | |
1430 | |
1431 public List<Entity> getAssertion() { | |
1432 return this.getPS().getEntities(null, Node.SYS_STATUS_CURRENT_VERSION, | |
1433 Node.TYPE_ABOX, null); | |
1434 } | |
1435 | |
1436 public List<Entity> getLightweightAssertionsByExactOwnValue( | |
1437 String objectClass, String ownValue, int maxResult) { | |
1438 return getPS().getLightweightEntities(Node.SYS_STATUS_CURRENT_VERSION, | |
1439 null, Node.TYPE_ABOX, objectClass, ownValue, false, maxResult); | |
1440 } | |
1441 | |
1442 public void removeNodeList(List<Node> nodeList) { | |
62 | 1443 //System.out.println("\n ### Deleting Nodes size total "+ nodeList.size() + " ### \n"); |
1 | 1444 this.getPS().removeNodeList(nodeList); |
1445 } | |
1446 | |
1447 public void deleteAllConcepts() { | |
1448 // persistenceService.deleteEntities(null, Node.TYPE_TBOX, true); | |
1449 getPS().dropDefinitions(); | |
1450 } | |
1451 | |
1452 public void importOM3Concepts(String fileName, Boolean dropConcepts) { | |
1453 getPS().setImportModus(true); | |
1454 ImportOM3Util.importConcepts(this, fileName, dropConcepts); | |
1455 getPS().setImportModus(false); | |
1456 } | |
1457 | |
1458 // /// | |
1459 | |
1460 public PersistenceService getPS() { | |
1461 return this.cache.getPs(); | |
1462 } | |
1463 | |
1464 public CacheService getCache() { | |
1465 return cache; | |
1466 } | |
1467 | |
1468 public void setCache(CacheService cache) { | |
1469 this.cache = cache; | |
1470 } | |
1471 | |
1472 public List<ViewerAttribute> getViewerAttributes(Long page){ | |
1473 List<ViewerAttribute> list = cache.getViewerAttributes(page); | |
1474 Collections.sort(list); | |
1475 return list; | |
1476 } | |
1477 | |
1478 public List<ViewerAttribute> getViewerAttributes4Edition(Long page) throws CloneNotSupportedException{ | |
1479 List<ViewerAttribute> list = new ArrayList<ViewerAttribute>(); | |
1480 for(ViewerAttribute att : cache.getViewerAttributes(page)){ | |
1481 list.add((ViewerAttribute)att.clone()); | |
1482 } | |
1483 Collections.sort(list); | |
1484 return list; | |
1485 } | |
1486 | |
1487 public Collection<ViewerPage> getViewerPages(){ | |
1488 return cache.getViewerPages(); | |
1489 } | |
1490 | |
1491 public ViewerPage getViewerPage(Long id){ | |
1492 return cache.getViewerPageMap().get(id); | |
1493 } | |
1494 | |
1495 public ViewerPage getViewerPage(String definition){ | |
1496 for(ViewerPage page : cache.getViewerPages()){ | |
1497 if(page.getDefinition().equals(definition)){ | |
1498 return page; | |
1499 } | |
1500 } | |
1501 return null; | |
1502 } | |
1503 | |
1504 public ViewerPage getViewerPage4Edition(Long id) throws CloneNotSupportedException{ | |
1505 ViewerPage page = cache.getViewerPageMap().get(id); | |
1506 return (page != null) ? (ViewerPage) page.clone() : null; | |
1507 } | |
1508 | |
1509 public ViewerPage saveViewerPage(ViewerPage page, String user){ | |
1510 page.setUser(user); | |
1511 return cache.saveViewerPage(page); | |
1512 | |
1513 } | |
1514 | |
1515 public ViewerAttribute saveViewerAttribute(ViewerPage page, ViewerAttribute att, String user) throws Exception{ | |
1516 att.setUser(user); | |
1517 return cache.saveViewerAttribute(page, att); | |
1518 } | |
1519 | |
1520 /** | |
1521 * Removes a page and all attributes associated with it. | |
1522 * @param page | |
1523 * @return | |
1524 */ | |
1525 public int removeViewerPage(ViewerPage page){ | |
1526 | |
1527 return (page.getId() != null) ? cache.removeViewerPage(page.getId()) : 0; | |
1528 } | |
1529 | |
1530 public boolean removeViewerAnttribute(ViewerAttribute att){ | |
1531 return (att.getId() != null) ? cache.removeAtt(att.getId()) : false; | |
1532 } | |
1533 } |