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