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