annotate src/main/java/org/mpi/openmind/cache/WrapperService.java @ 67:ae732119447f

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