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