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