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