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