Mercurial > hg > openmind
annotate src/main/java/org/mpi/openmind/cache/WrapperService.java @ 35:1cd9d9a67cad
added regular expression match option to AttributeFilter and searchEntityByAttribute.
added regexp() function to mysql dialect to use mysql REGEXP.
author | casties |
---|---|
date | Mon, 29 Aug 2016 20:16:53 +0200 |
parents | e52f593f9e0d |
children | 63fe33172397 |
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 | |
77 List<Entity> entList = this.cache.getEntitiesByDef(oc); | |
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) { | |
207 List<Entity> list = this.cache.getEntitiesByDef(def); | |
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)) { | |
215 List<Entity> list = this.cache.getEntitiesByDef(def); | |
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 | |
611 public Entity saveEntity(Entity entity, String user) throws Exception { | |
612 long start = System.currentTimeMillis(); | |
613 if (StringUtils.isEmpty(entity.getType())) { | |
614 entity.setType(Node.TYPE_ABOX); | |
615 } | |
616 entity.setUser(user); | |
617 | |
618 entity = removeWrongRelations(entity); | |
619 entity = removeEmptyAttributes(entity); | |
620 | |
621 this.getPS().saveEntity(entity); | |
622 cache.saveEntity(entity); | |
623 System.gc(); | |
624 logger.info("[U=" + user + "] SaveEntity - execution time[ms]: " + (System.currentTimeMillis() - start)); | |
625 Entity clone = (Entity) entity.clone(); | |
626 // logger.info(clone.toString()); | |
627 return clone; | |
628 } | |
629 | |
630 public void saveEntityList(List<Entity> list, String user) throws Exception{ | |
631 long start = System.currentTimeMillis(); | |
632 for(Entity entity : list){ | |
633 if (StringUtils.isEmpty(entity.getType())) { | |
634 entity.setType(Node.TYPE_ABOX); | |
635 } | |
636 entity.setUser(user); | |
637 | |
638 entity = removeWrongRelations(entity); | |
639 entity = removeEmptyAttributes(entity); | |
640 } | |
641 this.getPS().saveEntityList(list); | |
642 for(Entity entity : list){ | |
643 cache.saveEntity(entity); | |
644 } | |
645 logger.info("[U=" + user + "] SaveEntityList - execution time[ms]: " + (System.currentTimeMillis() - start)); | |
646 } | |
647 | |
648 private Entity removeEmptyAttributes(Entity ent){ | |
649 | |
650 for(Attribute att : new ArrayList<Attribute>(ent.getAttributes())){ | |
651 if(StringUtils.isEmpty(att.getValue())){ | |
652 ent.getAttributes().remove(att); | |
653 } | |
654 } | |
655 return ent; | |
656 | |
657 } | |
658 | |
33
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
7
diff
changeset
|
659 /** |
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
7
diff
changeset
|
660 * 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
|
661 * |
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
7
diff
changeset
|
662 * 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
|
663 * Similarly for target relations. |
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
7
diff
changeset
|
664 * |
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
7
diff
changeset
|
665 * @param ent |
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
7
diff
changeset
|
666 * @return |
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
7
diff
changeset
|
667 */ |
1 | 668 private boolean hasWrongRelations(Entity ent) { |
669 for (Relation srcRel : ent.getSourceRelations()) { | |
670 if (getEntityById(srcRel.getTargetId()) == null) { | |
671 return true; | |
672 } | |
673 } | |
674 | |
675 for (Relation tarRel : ent.getTargetRelations()) { | |
676 if (getEntityById(tarRel.getSourceId()) == null) { | |
677 return true; | |
678 } | |
679 } | |
680 return false; | |
681 } | |
682 | |
33
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
7
diff
changeset
|
683 /** |
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
7
diff
changeset
|
684 * Remove wrong relations from this entity. |
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
7
diff
changeset
|
685 * |
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
7
diff
changeset
|
686 * 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
|
687 * Similarly for target relations. |
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
7
diff
changeset
|
688 * |
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
7
diff
changeset
|
689 * @param ent |
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
7
diff
changeset
|
690 * @return |
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 private Entity removeWrongRelations(Entity ent) { |
1 | 693 |
33
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
7
diff
changeset
|
694 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
|
695 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
|
696 ent.getSourceRelations().remove(srcRel); |
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
7
diff
changeset
|
697 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
|
698 } |
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
7
diff
changeset
|
699 } |
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
7
diff
changeset
|
700 |
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
7
diff
changeset
|
701 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
|
702 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
|
703 ent.getTargetRelations().remove(tarRel); |
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
7
diff
changeset
|
704 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
|
705 } |
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
7
diff
changeset
|
706 } |
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
7
diff
changeset
|
707 |
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
7
diff
changeset
|
708 return ent; |
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
7
diff
changeset
|
709 } |
1 | 710 |
711 public Attribute getDefAttributeByOwnValue(String defOC, String attOW) { | |
712 for (Attribute att : this.cache.getDefAttributes(defOC)) { | |
713 if (att.getOwnValue().equals(attOW)) { | |
714 return att; | |
715 } | |
716 } | |
717 return null; | |
718 } | |
719 | |
720 public List<Attribute> getDefRelationAttributes(Long id) { | |
721 return this.cache.getDefAttributesById(id); | |
722 } | |
723 | |
724 public List<Attribute> getDefAttributes(String ow) { | |
725 return this.cache.getDefAttributes(ow); | |
726 } | |
727 | |
728 public List<Relation> getDefSourceRelations(String ow) { | |
729 return this.cache.getDefSourceRelations(ow); | |
730 } | |
731 | |
732 public List<Relation> getDefTargetRelations(String ow) { | |
733 return this.cache.getDefTargetRelations(ow); | |
734 } | |
735 | |
736 public Entity getDefinitionById(Long id) { | |
737 return this.cache.getLWDefinitionById(id); | |
738 /* | |
739 * Entity def = cache.getDefinitionById(id); if(def == null){ | |
740 * List<Entity> list = this.searchServ.getEntities(id, | |
741 * Node.SYS_STATUS_CURRENT_VERSION, Node.TYPE_TBOX, null); if | |
742 * (list.size() > 0) { def = list.get(0); cache.saveDefinition(def); } } | |
743 * return def; | |
744 */ | |
745 } | |
746 | |
747 public List<Entity> getLWDefinitions() { | |
748 return this.cache.getLWDefinitions(); | |
749 /* | |
750 * List<Entity> defs = this.cache.getDefinitions(); | |
751 * Collections.sort(defs, new EntitySortByNormalizedOwnValue()); return | |
752 * defs; | |
753 */ | |
754 } | |
755 | |
756 public Entity getDefinition(String ownValue) { | |
757 return this.cache.getLWDefinition(ownValue); | |
758 } | |
759 | |
760 public List<Relation> getRelation(String name, String srcOC, String tarOC) | |
761 throws Exception { | |
762 // TODO read only??? | |
763 List<Relation> list = new ArrayList<Relation>(); | |
764 if (StringUtils.isNotEmpty(name)) { | |
765 List<Relation> list0 = cache.getRelsByName(name); | |
766 if (StringUtils.isEmpty(srcOC) && StringUtils.isEmpty(tarOC)) { | |
767 list = list0; | |
768 } else { | |
769 for (Relation rel : list0) { | |
770 if ((StringUtils.isEmpty(srcOC) || srcOC.equals(rel | |
771 .getSourceObjectClass())) | |
772 && (StringUtils.isEmpty(tarOC) || tarOC.equals(rel | |
773 .getTargetObjectClass()))) { | |
774 list.add(rel); | |
775 } | |
776 } | |
777 } | |
778 } | |
779 | |
780 return list; | |
781 } | |
782 | |
783 public List<Entity> getSourcesForTargetRelation(Entity tar, | |
784 String relationName, String srcObjClass, int maxResult) { | |
785 return getSourcesForTargetRelation(tar.getId(), relationName, | |
786 srcObjClass, maxResult); | |
787 } | |
788 | |
789 public List<Entity> getSourcesForTargetRelation(Long tarId, | |
790 String relationName, String srcObjClass, int maxResult) { | |
791 List<Entity> rs = new ArrayList<Entity>(); | |
792 | |
793 List<Relation> tarRelList = this.cache.getRelsByTarId(tarId); | |
794 | |
795 long start = System.currentTimeMillis(); | |
796 int count = 0; | |
797 for (Relation rel : tarRelList) { | |
798 if (stringEquals(relationName, rel.getOwnValue()) | |
799 && stringEquals(srcObjClass, rel.getSourceObjectClass())) { | |
800 Entity ent = getEntityByIdReadOnly(rel.getSourceId()); | |
801 if (ent != null) { | |
802 rs.add(ent); | |
803 count++; | |
804 if (maxResult > 0 && count == maxResult) { | |
805 break; | |
806 } | |
807 } | |
808 } | |
809 } | |
810 logger.debug("getSourcesForTargetRelation (loading sources) - execution time[ms]: " | |
811 + (System.currentTimeMillis() - start)); | |
812 Collections.sort(rs, new EntitySortByNormalizedOwnValue()); | |
813 return rs; | |
814 } | |
815 | |
816 public List<Entity> getTargetsForSourceRelation(Entity src, | |
817 String relationName, String tarObjClass, int maxResult) { | |
818 return getTargetsForSourceRelation(src.getId(), relationName, | |
819 tarObjClass, maxResult); | |
820 } | |
821 | |
822 public List<Entity> getTargetsForSourceRelation(Long srcId, | |
823 String relationName, String tarObjClass, int maxResult) { | |
824 List<Entity> rs = new ArrayList<Entity>(); | |
825 | |
826 try { | |
827 Collection<Relation> srcRelList = this.cache.getRelsBySrcId(srcId); | |
828 | |
829 int count = 0; | |
830 for (Relation rel : srcRelList) { | |
831 if (stringEquals(relationName, rel.getOwnValue()) | |
832 && stringEquals(tarObjClass, rel.getTargetObjectClass())) { | |
833 rs.add(getEntityByIdReadOnly(rel.getTargetId())); | |
834 count++; | |
835 if (maxResult > 0 && count == maxResult) { | |
836 break; | |
837 } | |
838 } | |
839 } | |
840 Collections.sort(rs, new EntitySortByNormalizedOwnValue()); | |
841 } catch (Exception e) { | |
842 logger.error(e.getMessage(), e); | |
843 } | |
844 | |
845 return rs; | |
846 } | |
847 | |
848 public List<Relation> getTargetRelations(Entity target, | |
849 String relationName, String srcObjClass, int maxResult) { | |
850 long start = System.currentTimeMillis(); | |
851 List<Relation> list = new ArrayList<Relation>(); | |
852 | |
853 List<Relation> tarRelList = this.cache.getRelsByTarId(target.getId()); | |
854 /* | |
855 * if(coll == null){ coll = this.getPS().getTargetRelations(target, | |
856 * null, null, -1, false); cache.setRelsForTar(target.getId(), coll); } | |
857 */ | |
858 | |
859 int count = 0; | |
860 for (Relation rel : tarRelList) { | |
861 if (stringEquals(relationName, rel.getOwnValue()) | |
862 && stringEquals(srcObjClass, rel.getSourceObjectClass())) { | |
863 Entity source = getEntityById(rel.getSourceId()); | |
864 if (source != null) { | |
865 rel.setTarget(target); | |
866 rel.setSource(source); | |
867 list.add(rel); | |
868 count++; | |
869 if (maxResult > 0 && count == maxResult) { | |
870 break; | |
871 } | |
872 } else { | |
873 logger.error("#########ERROR: Relation without source " | |
874 + rel.toString()); | |
875 } | |
876 | |
877 } | |
878 } | |
879 Collections.sort(list, new RelationSortBySourceOW()); | |
880 logger.debug("getTargetRelations - execution time[ms]: " | |
881 + (System.currentTimeMillis() - start)); | |
882 return list; | |
883 } | |
884 | |
885 /** | |
886 * Returns a list of relations found by entity source. | |
887 * | |
888 * @param source | |
889 * @param relationName | |
890 * @param tarObjClass | |
891 * @param maxResult | |
892 * @return | |
893 */ | |
894 public List<Relation> getSourceRelations(Entity source, | |
895 String relationName, String tarObjClass, int maxResult) | |
896 throws Exception { | |
897 | |
898 long start = System.currentTimeMillis(); | |
899 List<Relation> list = new ArrayList<Relation>(); | |
900 | |
901 // the collection coll should not be modified | |
902 // otherwise java.util.ConcurrentModificationException will be thrown. | |
903 // be carefully using getEntityContent, cause it changes the cache | |
904 | |
905 List<Relation> srcRelList = this.cache.getRelsBySrcId(source.getId()); | |
906 | |
907 int count = 0; | |
908 for (Relation rel : srcRelList) { | |
909 if (stringEquals(relationName, rel.getOwnValue()) | |
910 && stringEquals(tarObjClass, rel.getTargetObjectClass())) { | |
911 rel.setSource(source); | |
912 rel.setTarget(getEntityById(rel.getTargetId())); | |
913 list.add(rel); | |
914 count++; | |
915 if (maxResult > 0 && count == maxResult) { | |
916 break; | |
917 } | |
918 } | |
919 } | |
920 Collections.sort(list, new RelationSortByTargetOW()); | |
921 logger.debug("getSourceRelations - execution time[ms]: " | |
922 + (System.currentTimeMillis() - start)); | |
923 | |
924 return list; | |
925 } | |
926 | |
927 /** | |
928 * This method should not be used inside this Wrapper class, because it | |
929 * could throws an java.util.ConcurrentModificationException. | |
930 * | |
931 * @param ent | |
932 * @return | |
933 */ | |
934 public Entity getEntityContent(Entity ent) { | |
935 try { | |
936 if (ent.isLightweight()) { | |
937 Entity e = this.cache.getEntityContent(ent); | |
938 if (e == null) { | |
939 e = this.getPS().getEntityContent(ent); | |
940 this.cache.saveEntity(e); | |
941 } else { | |
942 ent = e; | |
943 } | |
944 } | |
945 return (Entity) ent.clone(); | |
946 } catch (Exception e) { | |
947 logger.error(e.getMessage(), e); | |
948 } | |
949 return null; | |
950 } | |
951 | |
952 public Entity getEntityContentReadOnly(Entity ent) throws Exception { | |
953 if (ent.isLightweight()) { | |
954 Entity e = this.cache.getEntityContent(ent); | |
955 if (e == null) { | |
956 e = this.getPS().getEntityContent(ent); | |
957 this.cache.saveEntity(e); | |
958 } else { | |
959 ent = e; | |
960 } | |
961 } | |
962 return ent; | |
963 } | |
964 | |
965 /** | |
966 * The entity returned could be LW or not, it depends on the cache. | |
967 * additionally, it will be always a clone of the version in cache, | |
968 * therefore it method is thought for editing's issues. | |
969 * | |
970 * @param id | |
971 * @return | |
972 */ | |
973 public Entity getEntityById(Long id) { | |
974 Entity ent = this.cache.getEntityById(id); | |
975 return ent; | |
976 } | |
977 | |
978 public Entity getEntityByIdReadOnly(Long id) { | |
979 Entity ent = this.cache.getEntityByIdReadOnly(id); | |
980 return ent; | |
981 } | |
982 | |
983 public Entity getClonedEntityById(Long id) { | |
984 return (Entity) getEntityById(id).clone(); | |
985 } | |
986 | |
987 /** | |
988 * This method should not be used inside this Wrapper class, because it | |
989 * could throws an java.util.ConcurrentModificationException. | |
990 * | |
991 * @param id | |
992 * @return | |
993 */ | |
994 public Entity getEntityByIdWithContent(Long id) { | |
995 try { | |
996 Entity ent = getEntityById(id); | |
997 if (ent != null && ent.isLightweight()) { | |
998 ent = getEntityContent(ent); | |
999 } | |
1000 return ent; | |
1001 } catch (Exception e) { | |
1002 logger.error(e.getMessage(), e); | |
1003 } | |
1004 return null; | |
1005 } | |
1006 | |
1007 public Entity getEntityByIdWithContentReadOnly(Long id) throws Exception { | |
1008 Entity ent = getEntityByIdReadOnly(id); | |
1009 if (ent.isLightweight()) { | |
1010 ent = getEntityContentReadOnly(ent); | |
1011 } | |
1012 return ent; | |
1013 } | |
1014 | |
1015 public static boolean stringEquals(String term, String ow) { | |
1016 if (StringUtils.isEmpty(term)) | |
1017 return true; | |
1018 return term.equals(ow); | |
1019 } | |
1020 | |
1021 // ////**** | |
1022 public Entity getLightweightEntityById(Long id) { | |
1023 Entity entity = null; | |
1024 if (id != null) { | |
1025 List<Entity> list = this.getPS().getLightweightEntities( | |
1026 Node.SYS_STATUS_CURRENT_VERSION, id, null, null, null, | |
1027 true, -1); | |
1028 if (list.size() > 0) { | |
1029 entity = list.get(0); | |
1030 } | |
1031 } | |
1032 return entity; | |
1033 } | |
1034 | |
1035 public List<Entity> getLightweightAssertions(String objectClass, | |
1036 String ownValue, int maxResult) { | |
1037 return getPS().getLightweightEntities(Node.SYS_STATUS_CURRENT_VERSION, | |
1038 null, Node.TYPE_ABOX, objectClass, ownValue, true, maxResult); | |
1039 } | |
1040 | |
1041 public void saveAssertion(Entity entity, String user) throws Exception { | |
1042 this.saveEntity(entity, user); | |
1043 } | |
1044 | |
1045 /** | |
1046 * <p> | |
1047 * This method saves a non-persistent entity. | |
1048 * </p> | |
1049 * <p> | |
1050 * The reason of this is that nodes are stored only in the DB and not in the | |
1051 * cache. | |
1052 * </p> | |
1053 * <p> | |
1054 * Later, when the nodes are required, they will be loaded from the BD to | |
1055 * the cache automatically. However there are some structures like | |
1056 * attributes by entId that become inconsistent using this method. | |
1057 * </p> | |
1058 * | |
1059 * @param node | |
1060 * non-persistent node. | |
1061 * @param user | |
1062 * @throws Exception | |
1063 */ | |
1064 public void saveNodeOnlyForScripts(Node node, String user) throws Exception { | |
1065 if (node.isPersistent()) { | |
1066 throw new Exception( | |
1067 "This method can save only non-persistent nodes. Because the nodes are stored only in the DB in not in the Cache."); | |
1068 } | |
1069 if (StringUtils.isEmpty(node.getType())) { | |
1070 throw new Exception("The type of the node can not be empty"); | |
1071 } | |
1072 node.setUser(user); | |
1073 this.getPS().saveNode(node); | |
1074 | |
1075 // if(StringUtils.isNotEmpty(node.getType())){ | |
1076 // if(node.getType().equals(Node.TYPE_ABOX)){ | |
1077 // | |
1078 // }else if(node.getType().equals(Node.TYPE_TBOX)){ | |
1079 // | |
1080 // } | |
1081 // } | |
1082 } | |
1083 | |
1084 public void removeNode(Node node) { | |
1085 this.getPS().removeNode(node); | |
1086 } | |
1087 | |
1088 public Map<Long, Long> saveEntityListAsNew(List<Entity> entities, String user, | |
1089 boolean testWrongRelations) throws Exception { | |
1090 logger.info("\n ### Making persistent Entities size total " | |
1091 + entities.size() + " ### \n"); | |
1092 int sizePart = 1000; | |
1093 List<Entity> tmpList = null; | |
1094 | |
1095 Map<Long, Long> idMap = new HashMap<Long, Long>(); | |
1096 | |
1097 for (int j = 0; j <= (entities.size() / sizePart); j++) { | |
1098 tmpList = new ArrayList<Entity>(sizePart); | |
1099 for (int i = (j * sizePart); i < ((j + 1) * sizePart); i++) { | |
1100 | |
1101 if (i < entities.size()) { | |
1102 if (StringUtils.isNotEmpty(user)) { | |
1103 entities.get(i).setUser(user); | |
1104 } | |
1105 | |
1106 if (testWrongRelations | |
1107 && hasWrongRelations(entities.get(i))) { | |
1108 throw new Exception("Wrong relations " | |
1109 + entities.get(i)); | |
1110 } | |
1111 | |
1112 tmpList.add(entities.get(i)); | |
1113 } else { | |
1114 break; | |
1115 } | |
1116 } | |
1117 | |
1118 logger.info("\nMaking persistent Entities part " + (j + 1) + " of " | |
1119 + ((entities.size() / sizePart) + 1) + " size=" | |
1120 + tmpList.size()); | |
1121 idMap = this.getPS().saveEntityListAsNew(tmpList, idMap); | |
1122 } | |
1123 return idMap; | |
1124 } | |
1125 | |
1126 /* | |
1127 public void saveEntityList(List<Entity> entities, String user, | |
1128 boolean testWrongRelations) throws Exception { | |
1129 logger.info("\n ### Making persistent Entities size total " | |
1130 + entities.size() + " ### \n"); | |
1131 int sizePart = 1000; | |
1132 List<Entity> tmpList = null; | |
1133 | |
1134 for (int j = 0; j <= (entities.size() / sizePart); j++) { | |
1135 tmpList = new ArrayList<Entity>(sizePart); | |
1136 for (int i = (j * sizePart); i < ((j + 1) * sizePart); i++) { | |
1137 | |
1138 if (i < entities.size()) { | |
1139 if (StringUtils.isNotEmpty(user)) { | |
1140 entities.get(i).setUser(user); | |
1141 } | |
1142 | |
1143 if (testWrongRelations | |
1144 && hasWrongRelations(entities.get(i))) { | |
1145 throw new Exception("Wrong relations " | |
1146 + entities.get(i)); | |
1147 } | |
1148 | |
1149 tmpList.add(entities.get(i)); | |
1150 } else { | |
1151 break; | |
1152 } | |
1153 } | |
1154 | |
1155 logger.info("\nMaking persistent Entities part " + (j + 1) + " of " | |
1156 + ((entities.size() / sizePart) + 1) + " size=" | |
1157 + tmpList.size()); | |
1158 this.getPS().saveEntityList(tmpList); | |
1159 | |
1160 for(Entity ent : tmpList){ | |
1161 logger.info("[U=" + user + "] SaveEntity " + ent.toSmallString()); | |
1162 } | |
1163 | |
1164 } | |
1165 }*/ | |
1166 | |
1167 public void saveConcept(Entity entity) throws Exception { | |
1168 entity.setType(Node.TYPE_TBOX); | |
1169 entity.setObjectClass(Node.TYPE_TBOX); | |
1170 this.getPS().saveEntity(entity); | |
1171 } | |
1172 | |
1173 public void saveNodeListOnlyForScripts(List<Node> nodeList, String user) | |
1174 throws Exception { | |
1175 logger.debug("### Making persistent Nodes size total " | |
1176 + nodeList.size() + " ###"); | |
1177 List<Node> list = null; | |
1178 int sizePart = 1000; | |
1179 for (int j = 0; j <= (nodeList.size() / sizePart); j++) { | |
1180 list = new ArrayList<Node>(); | |
1181 for (int i = (j * sizePart); i < ((j + 1) * sizePart); i++) { | |
1182 if (i < nodeList.size()) { | |
1183 nodeList.get(i).setUser(user); | |
1184 list.add(nodeList.get(i)); | |
1185 } | |
1186 } | |
1187 logger.debug("Making persistent Nodes part " + (j + 1) + " of " | |
1188 + ((nodeList.size() / sizePart) + 1) + " size=" | |
1189 + list.size()); | |
1190 this.getPS().saveNodeList(list); | |
1191 } | |
1192 } | |
1193 | |
1194 public void saveNodeListOnlyForScripts(List<Node> nodeList) | |
1195 throws Exception { | |
1196 logger.info("\n ### Making persistent Nodes size total " | |
1197 + nodeList.size() + " ### \n"); | |
1198 int sizePart = 1000; | |
1199 List<Node> list = null; | |
1200 for (int j = 0; j <= (nodeList.size() / sizePart); j++) { | |
1201 list = new ArrayList<Node>(); | |
1202 for (int i = (j * sizePart); i < ((j + 1) * sizePart); i++) { | |
1203 if (i < nodeList.size()) { | |
1204 list.add(nodeList.get(i)); | |
1205 } | |
1206 } | |
1207 logger.info("Making persistent Nodes part " + (j + 1) + " of " | |
1208 + ((nodeList.size() / sizePart) + 1) + " size=" | |
1209 + list.size()); | |
1210 this.getPS().saveNodeList(list); | |
1211 } | |
1212 // this.persistenceService.saveNodeList(nodeList); | |
1213 } | |
1214 | |
1215 public List<Entity> getConcepts() { | |
1216 return this.getPS().getEntities(null, Node.SYS_STATUS_CURRENT_VERSION, | |
1217 Node.TYPE_TBOX, null); | |
1218 } | |
1219 | |
1220 public List<Entity> getAssertion() { | |
1221 return this.getPS().getEntities(null, Node.SYS_STATUS_CURRENT_VERSION, | |
1222 Node.TYPE_ABOX, null); | |
1223 } | |
1224 | |
1225 public List<Entity> getLightweightAssertionsByExactOwnValue( | |
1226 String objectClass, String ownValue, int maxResult) { | |
1227 return getPS().getLightweightEntities(Node.SYS_STATUS_CURRENT_VERSION, | |
1228 null, Node.TYPE_ABOX, objectClass, ownValue, false, maxResult); | |
1229 } | |
1230 | |
1231 public void removeNodeList(List<Node> nodeList) { | |
1232 System.out.println("\n ### Deleting Nodes size total " | |
1233 + nodeList.size() + " ### \n"); | |
1234 this.getPS().removeNodeList(nodeList); | |
1235 } | |
1236 | |
1237 public void deleteAllConcepts() { | |
1238 // persistenceService.deleteEntities(null, Node.TYPE_TBOX, true); | |
1239 getPS().dropDefinitions(); | |
1240 } | |
1241 | |
1242 public void importOM3Concepts(String fileName, Boolean dropConcepts) { | |
1243 getPS().setImportModus(true); | |
1244 ImportOM3Util.importConcepts(this, fileName, dropConcepts); | |
1245 getPS().setImportModus(false); | |
1246 } | |
1247 | |
1248 // /// | |
1249 | |
1250 public PersistenceService getPS() { | |
1251 return this.cache.getPs(); | |
1252 } | |
1253 | |
1254 public CacheService getCache() { | |
1255 return cache; | |
1256 } | |
1257 | |
1258 public void setCache(CacheService cache) { | |
1259 this.cache = cache; | |
1260 } | |
1261 | |
1262 public List<ViewerAttribute> getViewerAttributes(Long page){ | |
1263 List<ViewerAttribute> list = cache.getViewerAttributes(page); | |
1264 Collections.sort(list); | |
1265 return list; | |
1266 } | |
1267 | |
1268 public List<ViewerAttribute> getViewerAttributes4Edition(Long page) throws CloneNotSupportedException{ | |
1269 List<ViewerAttribute> list = new ArrayList<ViewerAttribute>(); | |
1270 for(ViewerAttribute att : cache.getViewerAttributes(page)){ | |
1271 list.add((ViewerAttribute)att.clone()); | |
1272 } | |
1273 Collections.sort(list); | |
1274 return list; | |
1275 } | |
1276 | |
1277 public Collection<ViewerPage> getViewerPages(){ | |
1278 return cache.getViewerPages(); | |
1279 } | |
1280 | |
1281 public ViewerPage getViewerPage(Long id){ | |
1282 return cache.getViewerPageMap().get(id); | |
1283 } | |
1284 | |
1285 public ViewerPage getViewerPage(String definition){ | |
1286 for(ViewerPage page : cache.getViewerPages()){ | |
1287 if(page.getDefinition().equals(definition)){ | |
1288 return page; | |
1289 } | |
1290 } | |
1291 return null; | |
1292 } | |
1293 | |
1294 public ViewerPage getViewerPage4Edition(Long id) throws CloneNotSupportedException{ | |
1295 ViewerPage page = cache.getViewerPageMap().get(id); | |
1296 return (page != null) ? (ViewerPage) page.clone() : null; | |
1297 } | |
1298 | |
1299 public ViewerPage saveViewerPage(ViewerPage page, String user){ | |
1300 page.setUser(user); | |
1301 return cache.saveViewerPage(page); | |
1302 | |
1303 } | |
1304 | |
1305 public ViewerAttribute saveViewerAttribute(ViewerPage page, ViewerAttribute att, String user) throws Exception{ | |
1306 att.setUser(user); | |
1307 return cache.saveViewerAttribute(page, att); | |
1308 } | |
1309 | |
1310 /** | |
1311 * Removes a page and all attributes associated with it. | |
1312 * @param page | |
1313 * @return | |
1314 */ | |
1315 public int removeViewerPage(ViewerPage page){ | |
1316 | |
1317 return (page.getId() != null) ? cache.removeViewerPage(page.getId()) : 0; | |
1318 } | |
1319 | |
1320 public boolean removeViewerAnttribute(ViewerAttribute att){ | |
1321 return (att.getId() != null) ? cache.removeAtt(att.getId()) : false; | |
1322 } | |
1323 } |