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