annotate src/main/java/org/mpi/openmind/cache/WrapperService.java @ 66:3e4b05a6cb47

new EditIntent for saveEntity().
author casties
date Mon, 30 Jan 2017 20:32:26 +0100
parents 74cd973f6ece
children ae732119447f
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)) {
66
3e4b05a6cb47 new EditIntent for saveEntity().
casties
parents: 65
diff changeset
671 logger.error("Possible collision: Entity to save not current!");
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 {
3e4b05a6cb47 new EditIntent for saveEntity().
casties
parents: 65
diff changeset
733 logger.warn("save without EditIntent!");
3e4b05a6cb47 new EditIntent for saveEntity().
casties
parents: 65
diff changeset
734 }
3e4b05a6cb47 new EditIntent for saveEntity().
casties
parents: 65
diff changeset
735 }
64
83b209aa4226 working on stale entity update problem.
casties
parents: 62
diff changeset
736 }
83b209aa4226 working on stale entity update problem.
casties
parents: 62
diff changeset
737
45
fd98c31371cb better transaction logging.
casties
parents: 39
diff changeset
738 // save in database
1
jurzua
parents:
diff changeset
739 this.getPS().saveEntity(entity);
45
fd98c31371cb better transaction logging.
casties
parents: 39
diff changeset
740 // save in cache
1
jurzua
parents:
diff changeset
741 cache.saveEntity(entity);
45
fd98c31371cb better transaction logging.
casties
parents: 39
diff changeset
742 txLog.debug("** END save entity: user="+user+" entity="+entity.toSmallString());
fd98c31371cb better transaction logging.
casties
parents: 39
diff changeset
743
fd98c31371cb better transaction logging.
casties
parents: 39
diff changeset
744 logger.debug("[U=" + user + "] SaveEntity - execution time[ms]: " + (System.currentTimeMillis() - start));
1
jurzua
parents:
diff changeset
745 Entity clone = (Entity) entity.clone();
jurzua
parents:
diff changeset
746 return clone;
jurzua
parents:
diff changeset
747 }
jurzua
parents:
diff changeset
748
jurzua
parents:
diff changeset
749 public void saveEntityList(List<Entity> list, String user) throws Exception{
jurzua
parents:
diff changeset
750 long start = System.currentTimeMillis();
jurzua
parents:
diff changeset
751 for(Entity entity : list){
jurzua
parents:
diff changeset
752 if (StringUtils.isEmpty(entity.getType())) {
jurzua
parents:
diff changeset
753 entity.setType(Node.TYPE_ABOX);
jurzua
parents:
diff changeset
754 }
jurzua
parents:
diff changeset
755 entity.setUser(user);
jurzua
parents:
diff changeset
756
jurzua
parents:
diff changeset
757 entity = removeWrongRelations(entity);
jurzua
parents:
diff changeset
758 entity = removeEmptyAttributes(entity);
jurzua
parents:
diff changeset
759 }
45
fd98c31371cb better transaction logging.
casties
parents: 39
diff changeset
760 txLog.debug("** START save entity list: user="+user+" list="+list);
1
jurzua
parents:
diff changeset
761 this.getPS().saveEntityList(list);
jurzua
parents:
diff changeset
762 for(Entity entity : list){
jurzua
parents:
diff changeset
763 cache.saveEntity(entity);
jurzua
parents:
diff changeset
764 }
45
fd98c31371cb better transaction logging.
casties
parents: 39
diff changeset
765 txLog.debug("** END save entity list: user="+user+" list="+list);
fd98c31371cb better transaction logging.
casties
parents: 39
diff changeset
766 logger.debug("[U=" + user + "] SaveEntityList - execution time[ms]: " + (System.currentTimeMillis() - start));
1
jurzua
parents:
diff changeset
767 }
jurzua
parents:
diff changeset
768
jurzua
parents:
diff changeset
769 private Entity removeEmptyAttributes(Entity ent){
jurzua
parents:
diff changeset
770
jurzua
parents:
diff changeset
771 for(Attribute att : new ArrayList<Attribute>(ent.getAttributes())){
jurzua
parents:
diff changeset
772 if(StringUtils.isEmpty(att.getValue())){
jurzua
parents:
diff changeset
773 ent.getAttributes().remove(att);
jurzua
parents:
diff changeset
774 }
jurzua
parents:
diff changeset
775 }
jurzua
parents:
diff changeset
776 return ent;
jurzua
parents:
diff changeset
777
jurzua
parents:
diff changeset
778 }
jurzua
parents:
diff changeset
779
33
e52f593f9e0d new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents: 7
diff changeset
780 /**
e52f593f9e0d new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents: 7
diff changeset
781 * Returns if this entity has wrong relations.
e52f593f9e0d new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents: 7
diff changeset
782 *
e52f593f9e0d new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents: 7
diff changeset
783 * Wrong are source relations whose target doesn't exist.
e52f593f9e0d new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents: 7
diff changeset
784 * Similarly for target relations.
e52f593f9e0d new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents: 7
diff changeset
785 *
e52f593f9e0d new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents: 7
diff changeset
786 * @param ent
e52f593f9e0d new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents: 7
diff changeset
787 * @return
e52f593f9e0d new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents: 7
diff changeset
788 */
1
jurzua
parents:
diff changeset
789 private boolean hasWrongRelations(Entity ent) {
jurzua
parents:
diff changeset
790 for (Relation srcRel : ent.getSourceRelations()) {
jurzua
parents:
diff changeset
791 if (getEntityById(srcRel.getTargetId()) == null) {
jurzua
parents:
diff changeset
792 return true;
jurzua
parents:
diff changeset
793 }
jurzua
parents:
diff changeset
794 }
jurzua
parents:
diff changeset
795
jurzua
parents:
diff changeset
796 for (Relation tarRel : ent.getTargetRelations()) {
jurzua
parents:
diff changeset
797 if (getEntityById(tarRel.getSourceId()) == null) {
jurzua
parents:
diff changeset
798 return true;
jurzua
parents:
diff changeset
799 }
jurzua
parents:
diff changeset
800 }
jurzua
parents:
diff changeset
801 return false;
jurzua
parents:
diff changeset
802 }
jurzua
parents:
diff changeset
803
33
e52f593f9e0d new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents: 7
diff changeset
804 /**
e52f593f9e0d new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents: 7
diff changeset
805 * Remove wrong relations from this entity.
e52f593f9e0d new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents: 7
diff changeset
806 *
e52f593f9e0d new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents: 7
diff changeset
807 * Wrong are source relations whose target is null or doesn't exist.
e52f593f9e0d new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents: 7
diff changeset
808 * Similarly for target relations.
e52f593f9e0d new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents: 7
diff changeset
809 *
e52f593f9e0d new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents: 7
diff changeset
810 * @param ent
e52f593f9e0d new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents: 7
diff changeset
811 * @return
e52f593f9e0d new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents: 7
diff changeset
812 */
e52f593f9e0d new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents: 7
diff changeset
813 private Entity removeWrongRelations(Entity ent) {
1
jurzua
parents:
diff changeset
814
33
e52f593f9e0d new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents: 7
diff changeset
815 for (Relation srcRel : new ArrayList<Relation>(ent.getSourceRelations())) {
e52f593f9e0d new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents: 7
diff changeset
816 if (srcRel.getTargetId() == null || getEntityByIdReadOnly(srcRel.getTargetId()) == null) {
e52f593f9e0d new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents: 7
diff changeset
817 ent.getSourceRelations().remove(srcRel);
e52f593f9e0d new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents: 7
diff changeset
818 logger.error("Inconsistency detected saving entity [" + ent.getId() + "] " + srcRel.toString());
e52f593f9e0d new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents: 7
diff changeset
819 }
e52f593f9e0d new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents: 7
diff changeset
820 }
e52f593f9e0d new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents: 7
diff changeset
821
e52f593f9e0d new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents: 7
diff changeset
822 for (Relation tarRel : new ArrayList<Relation>(ent.getTargetRelations())) {
e52f593f9e0d new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents: 7
diff changeset
823 if (tarRel.getSourceId() == null || getEntityByIdReadOnly(tarRel.getSourceId()) == null) {
e52f593f9e0d new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents: 7
diff changeset
824 ent.getTargetRelations().remove(tarRel);
e52f593f9e0d new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents: 7
diff changeset
825 logger.error("Inconsistency detected saving entity [" + ent.getId() + "] " + tarRel.toString());
e52f593f9e0d new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents: 7
diff changeset
826 }
e52f593f9e0d new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents: 7
diff changeset
827 }
e52f593f9e0d new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents: 7
diff changeset
828
e52f593f9e0d new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents: 7
diff changeset
829 return ent;
e52f593f9e0d new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents: 7
diff changeset
830 }
1
jurzua
parents:
diff changeset
831
jurzua
parents:
diff changeset
832 public Attribute getDefAttributeByOwnValue(String defOC, String attOW) {
jurzua
parents:
diff changeset
833 for (Attribute att : this.cache.getDefAttributes(defOC)) {
jurzua
parents:
diff changeset
834 if (att.getOwnValue().equals(attOW)) {
jurzua
parents:
diff changeset
835 return att;
jurzua
parents:
diff changeset
836 }
jurzua
parents:
diff changeset
837 }
jurzua
parents:
diff changeset
838 return null;
jurzua
parents:
diff changeset
839 }
jurzua
parents:
diff changeset
840
jurzua
parents:
diff changeset
841 public List<Attribute> getDefRelationAttributes(Long id) {
jurzua
parents:
diff changeset
842 return this.cache.getDefAttributesById(id);
jurzua
parents:
diff changeset
843 }
38
cb25e343e317 more comments and refactorings.
casties
parents: 37
diff changeset
844
cb25e343e317 more comments and refactorings.
casties
parents: 37
diff changeset
845 /**
cb25e343e317 more comments and refactorings.
casties
parents: 37
diff changeset
846 * Returns the attributes of the definition of the given type.
cb25e343e317 more comments and refactorings.
casties
parents: 37
diff changeset
847 *
cb25e343e317 more comments and refactorings.
casties
parents: 37
diff changeset
848 * @param objectClass
cb25e343e317 more comments and refactorings.
casties
parents: 37
diff changeset
849 * @return
cb25e343e317 more comments and refactorings.
casties
parents: 37
diff changeset
850 */
cb25e343e317 more comments and refactorings.
casties
parents: 37
diff changeset
851 public List<Attribute> getDefAttributes(String objectClass) {
cb25e343e317 more comments and refactorings.
casties
parents: 37
diff changeset
852 return this.cache.getDefAttributes(objectClass);
1
jurzua
parents:
diff changeset
853 }
jurzua
parents:
diff changeset
854
38
cb25e343e317 more comments and refactorings.
casties
parents: 37
diff changeset
855 /**
cb25e343e317 more comments and refactorings.
casties
parents: 37
diff changeset
856 * Returns the source relations of the definition of the given type.
cb25e343e317 more comments and refactorings.
casties
parents: 37
diff changeset
857 *
cb25e343e317 more comments and refactorings.
casties
parents: 37
diff changeset
858 * @param objectClass
cb25e343e317 more comments and refactorings.
casties
parents: 37
diff changeset
859 * @return
cb25e343e317 more comments and refactorings.
casties
parents: 37
diff changeset
860 */
cb25e343e317 more comments and refactorings.
casties
parents: 37
diff changeset
861 public List<Relation> getDefSourceRelations(String objectClass) {
cb25e343e317 more comments and refactorings.
casties
parents: 37
diff changeset
862 return this.cache.getDefSourceRelations(objectClass);
1
jurzua
parents:
diff changeset
863 }
jurzua
parents:
diff changeset
864
38
cb25e343e317 more comments and refactorings.
casties
parents: 37
diff changeset
865 /**
cb25e343e317 more comments and refactorings.
casties
parents: 37
diff changeset
866 * Returns the target relations of the definition of the given type.
cb25e343e317 more comments and refactorings.
casties
parents: 37
diff changeset
867 *
cb25e343e317 more comments and refactorings.
casties
parents: 37
diff changeset
868 * @param objectClass
cb25e343e317 more comments and refactorings.
casties
parents: 37
diff changeset
869 * @return
cb25e343e317 more comments and refactorings.
casties
parents: 37
diff changeset
870 */
cb25e343e317 more comments and refactorings.
casties
parents: 37
diff changeset
871 public List<Relation> getDefTargetRelations(String objectClass) {
cb25e343e317 more comments and refactorings.
casties
parents: 37
diff changeset
872 return this.cache.getDefTargetRelations(objectClass);
1
jurzua
parents:
diff changeset
873 }
jurzua
parents:
diff changeset
874
jurzua
parents:
diff changeset
875 public Entity getDefinitionById(Long id) {
jurzua
parents:
diff changeset
876 return this.cache.getLWDefinitionById(id);
jurzua
parents:
diff changeset
877 }
jurzua
parents:
diff changeset
878
38
cb25e343e317 more comments and refactorings.
casties
parents: 37
diff changeset
879 /**
cb25e343e317 more comments and refactorings.
casties
parents: 37
diff changeset
880 * Returns all (lightweight) definition entities.
cb25e343e317 more comments and refactorings.
casties
parents: 37
diff changeset
881 *
cb25e343e317 more comments and refactorings.
casties
parents: 37
diff changeset
882 * @return
cb25e343e317 more comments and refactorings.
casties
parents: 37
diff changeset
883 */
1
jurzua
parents:
diff changeset
884 public List<Entity> getLWDefinitions() {
jurzua
parents:
diff changeset
885 return this.cache.getLWDefinitions();
jurzua
parents:
diff changeset
886 }
jurzua
parents:
diff changeset
887
38
cb25e343e317 more comments and refactorings.
casties
parents: 37
diff changeset
888 /**
cb25e343e317 more comments and refactorings.
casties
parents: 37
diff changeset
889 * Returns the (lightweight) definition entity for the given type.
cb25e343e317 more comments and refactorings.
casties
parents: 37
diff changeset
890 *
cb25e343e317 more comments and refactorings.
casties
parents: 37
diff changeset
891 * @param objectClass
cb25e343e317 more comments and refactorings.
casties
parents: 37
diff changeset
892 * @return
cb25e343e317 more comments and refactorings.
casties
parents: 37
diff changeset
893 */
cb25e343e317 more comments and refactorings.
casties
parents: 37
diff changeset
894 public Entity getDefinition(String objectClass) {
cb25e343e317 more comments and refactorings.
casties
parents: 37
diff changeset
895 return this.cache.getLWDefinition(objectClass);
1
jurzua
parents:
diff changeset
896 }
jurzua
parents:
diff changeset
897
jurzua
parents:
diff changeset
898 public List<Relation> getRelation(String name, String srcOC, String tarOC)
jurzua
parents:
diff changeset
899 throws Exception {
jurzua
parents:
diff changeset
900 // TODO read only???
jurzua
parents:
diff changeset
901 List<Relation> list = new ArrayList<Relation>();
jurzua
parents:
diff changeset
902 if (StringUtils.isNotEmpty(name)) {
jurzua
parents:
diff changeset
903 List<Relation> list0 = cache.getRelsByName(name);
jurzua
parents:
diff changeset
904 if (StringUtils.isEmpty(srcOC) && StringUtils.isEmpty(tarOC)) {
jurzua
parents:
diff changeset
905 list = list0;
jurzua
parents:
diff changeset
906 } else {
jurzua
parents:
diff changeset
907 for (Relation rel : list0) {
jurzua
parents:
diff changeset
908 if ((StringUtils.isEmpty(srcOC) || srcOC.equals(rel
jurzua
parents:
diff changeset
909 .getSourceObjectClass()))
jurzua
parents:
diff changeset
910 && (StringUtils.isEmpty(tarOC) || tarOC.equals(rel
jurzua
parents:
diff changeset
911 .getTargetObjectClass()))) {
jurzua
parents:
diff changeset
912 list.add(rel);
jurzua
parents:
diff changeset
913 }
jurzua
parents:
diff changeset
914 }
jurzua
parents:
diff changeset
915 }
jurzua
parents:
diff changeset
916 }
jurzua
parents:
diff changeset
917
jurzua
parents:
diff changeset
918 return list;
jurzua
parents:
diff changeset
919 }
jurzua
parents:
diff changeset
920
55
b7a8db041f68 cleanup and fix when target_id doesn't exist.
casties
parents: 54
diff changeset
921 /**
b7a8db041f68 cleanup and fix when target_id doesn't exist.
casties
parents: 54
diff changeset
922 * 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
923 *
b7a8db041f68 cleanup and fix when target_id doesn't exist.
casties
parents: 54
diff changeset
924 * Filters relations by relationName and tarObjClass.
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 * @param tar
b7a8db041f68 cleanup and fix when target_id doesn't exist.
casties
parents: 54
diff changeset
927 * @param relationName
b7a8db041f68 cleanup and fix when target_id doesn't exist.
casties
parents: 54
diff changeset
928 * @param srcObjClass
b7a8db041f68 cleanup and fix when target_id doesn't exist.
casties
parents: 54
diff changeset
929 * @param maxResult
b7a8db041f68 cleanup and fix when target_id doesn't exist.
casties
parents: 54
diff changeset
930 * @return
b7a8db041f68 cleanup and fix when target_id doesn't exist.
casties
parents: 54
diff changeset
931 */
1
jurzua
parents:
diff changeset
932 public List<Entity> getSourcesForTargetRelation(Entity tar,
jurzua
parents:
diff changeset
933 String relationName, String srcObjClass, int maxResult) {
jurzua
parents:
diff changeset
934 return getSourcesForTargetRelation(tar.getId(), relationName,
jurzua
parents:
diff changeset
935 srcObjClass, maxResult);
jurzua
parents:
diff changeset
936 }
jurzua
parents:
diff changeset
937
55
b7a8db041f68 cleanup and fix when target_id doesn't exist.
casties
parents: 54
diff changeset
938 /**
b7a8db041f68 cleanup and fix when target_id doesn't exist.
casties
parents: 54
diff changeset
939 * 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
940 *
b7a8db041f68 cleanup and fix when target_id doesn't exist.
casties
parents: 54
diff changeset
941 * Filters relations by relationName and tarObjClass.
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 * @param tarId
b7a8db041f68 cleanup and fix when target_id doesn't exist.
casties
parents: 54
diff changeset
944 * @param relationName
b7a8db041f68 cleanup and fix when target_id doesn't exist.
casties
parents: 54
diff changeset
945 * @param srcObjClass
b7a8db041f68 cleanup and fix when target_id doesn't exist.
casties
parents: 54
diff changeset
946 * @param maxResult
b7a8db041f68 cleanup and fix when target_id doesn't exist.
casties
parents: 54
diff changeset
947 * @return
b7a8db041f68 cleanup and fix when target_id doesn't exist.
casties
parents: 54
diff changeset
948 */
1
jurzua
parents:
diff changeset
949 public List<Entity> getSourcesForTargetRelation(Long tarId,
jurzua
parents:
diff changeset
950 String relationName, String srcObjClass, int maxResult) {
jurzua
parents:
diff changeset
951 List<Entity> rs = new ArrayList<Entity>();
jurzua
parents:
diff changeset
952
jurzua
parents:
diff changeset
953 List<Relation> tarRelList = this.cache.getRelsByTarId(tarId);
jurzua
parents:
diff changeset
954
jurzua
parents:
diff changeset
955 long start = System.currentTimeMillis();
jurzua
parents:
diff changeset
956 int count = 0;
jurzua
parents:
diff changeset
957 for (Relation rel : tarRelList) {
jurzua
parents:
diff changeset
958 if (stringEquals(relationName, rel.getOwnValue())
jurzua
parents:
diff changeset
959 && stringEquals(srcObjClass, rel.getSourceObjectClass())) {
jurzua
parents:
diff changeset
960 Entity ent = getEntityByIdReadOnly(rel.getSourceId());
55
b7a8db041f68 cleanup and fix when target_id doesn't exist.
casties
parents: 54
diff changeset
961 if (ent == null) {
b7a8db041f68 cleanup and fix when target_id doesn't exist.
casties
parents: 54
diff changeset
962 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
963 } else {
1
jurzua
parents:
diff changeset
964 rs.add(ent);
jurzua
parents:
diff changeset
965 count++;
jurzua
parents:
diff changeset
966 if (maxResult > 0 && count == maxResult) {
jurzua
parents:
diff changeset
967 break;
jurzua
parents:
diff changeset
968 }
jurzua
parents:
diff changeset
969 }
jurzua
parents:
diff changeset
970 }
jurzua
parents:
diff changeset
971 }
64
83b209aa4226 working on stale entity update problem.
casties
parents: 62
diff changeset
972 //logger.debug("getSourcesForTargetRelation (loading sources) - execution time[ms]: "+(System.currentTimeMillis() - start));
1
jurzua
parents:
diff changeset
973 Collections.sort(rs, new EntitySortByNormalizedOwnValue());
jurzua
parents:
diff changeset
974 return rs;
jurzua
parents:
diff changeset
975 }
jurzua
parents:
diff changeset
976
55
b7a8db041f68 cleanup and fix when target_id doesn't exist.
casties
parents: 54
diff changeset
977 /**
b7a8db041f68 cleanup and fix when target_id doesn't exist.
casties
parents: 54
diff changeset
978 * 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
979 *
b7a8db041f68 cleanup and fix when target_id doesn't exist.
casties
parents: 54
diff changeset
980 * Filters relations by relationName and tarObjClass.
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 * @param src
b7a8db041f68 cleanup and fix when target_id doesn't exist.
casties
parents: 54
diff changeset
983 * @param relationName
b7a8db041f68 cleanup and fix when target_id doesn't exist.
casties
parents: 54
diff changeset
984 * @param tarObjClass
b7a8db041f68 cleanup and fix when target_id doesn't exist.
casties
parents: 54
diff changeset
985 * @param maxResult
b7a8db041f68 cleanup and fix when target_id doesn't exist.
casties
parents: 54
diff changeset
986 * @return
b7a8db041f68 cleanup and fix when target_id doesn't exist.
casties
parents: 54
diff changeset
987 */
1
jurzua
parents:
diff changeset
988 public List<Entity> getTargetsForSourceRelation(Entity src,
jurzua
parents:
diff changeset
989 String relationName, String tarObjClass, int maxResult) {
jurzua
parents:
diff changeset
990 return getTargetsForSourceRelation(src.getId(), relationName,
jurzua
parents:
diff changeset
991 tarObjClass, maxResult);
jurzua
parents:
diff changeset
992 }
jurzua
parents:
diff changeset
993
55
b7a8db041f68 cleanup and fix when target_id doesn't exist.
casties
parents: 54
diff changeset
994 /**
b7a8db041f68 cleanup and fix when target_id doesn't exist.
casties
parents: 54
diff changeset
995 * 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
996 *
b7a8db041f68 cleanup and fix when target_id doesn't exist.
casties
parents: 54
diff changeset
997 * Filters relations by relationName and tarObjClass.
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 * @param srcId
b7a8db041f68 cleanup and fix when target_id doesn't exist.
casties
parents: 54
diff changeset
1000 * @param relationName
b7a8db041f68 cleanup and fix when target_id doesn't exist.
casties
parents: 54
diff changeset
1001 * @param tarObjClass
b7a8db041f68 cleanup and fix when target_id doesn't exist.
casties
parents: 54
diff changeset
1002 * @param maxResult
b7a8db041f68 cleanup and fix when target_id doesn't exist.
casties
parents: 54
diff changeset
1003 * @return
b7a8db041f68 cleanup and fix when target_id doesn't exist.
casties
parents: 54
diff changeset
1004 */
1
jurzua
parents:
diff changeset
1005 public List<Entity> getTargetsForSourceRelation(Long srcId,
jurzua
parents:
diff changeset
1006 String relationName, String tarObjClass, int maxResult) {
jurzua
parents:
diff changeset
1007 List<Entity> rs = new ArrayList<Entity>();
jurzua
parents:
diff changeset
1008
jurzua
parents:
diff changeset
1009 try {
jurzua
parents:
diff changeset
1010 Collection<Relation> srcRelList = this.cache.getRelsBySrcId(srcId);
jurzua
parents:
diff changeset
1011
jurzua
parents:
diff changeset
1012 int count = 0;
jurzua
parents:
diff changeset
1013 for (Relation rel : srcRelList) {
jurzua
parents:
diff changeset
1014 if (stringEquals(relationName, rel.getOwnValue())
jurzua
parents:
diff changeset
1015 && stringEquals(tarObjClass, rel.getTargetObjectClass())) {
55
b7a8db041f68 cleanup and fix when target_id doesn't exist.
casties
parents: 54
diff changeset
1016 Entity ent = getEntityByIdReadOnly(rel.getTargetId());
b7a8db041f68 cleanup and fix when target_id doesn't exist.
casties
parents: 54
diff changeset
1017 if (ent == null) {
b7a8db041f68 cleanup and fix when target_id doesn't exist.
casties
parents: 54
diff changeset
1018 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
1019 } else {
b7a8db041f68 cleanup and fix when target_id doesn't exist.
casties
parents: 54
diff changeset
1020 rs.add(ent);
b7a8db041f68 cleanup and fix when target_id doesn't exist.
casties
parents: 54
diff changeset
1021 count++;
b7a8db041f68 cleanup and fix when target_id doesn't exist.
casties
parents: 54
diff changeset
1022 if (maxResult > 0 && count == maxResult) {
b7a8db041f68 cleanup and fix when target_id doesn't exist.
casties
parents: 54
diff changeset
1023 break;
b7a8db041f68 cleanup and fix when target_id doesn't exist.
casties
parents: 54
diff changeset
1024 }
b7a8db041f68 cleanup and fix when target_id doesn't exist.
casties
parents: 54
diff changeset
1025 }
1
jurzua
parents:
diff changeset
1026 }
jurzua
parents:
diff changeset
1027 }
jurzua
parents:
diff changeset
1028 Collections.sort(rs, new EntitySortByNormalizedOwnValue());
jurzua
parents:
diff changeset
1029 } catch (Exception e) {
jurzua
parents:
diff changeset
1030 logger.error(e.getMessage(), e);
jurzua
parents:
diff changeset
1031 }
jurzua
parents:
diff changeset
1032
jurzua
parents:
diff changeset
1033 return rs;
jurzua
parents:
diff changeset
1034 }
jurzua
parents:
diff changeset
1035
jurzua
parents:
diff changeset
1036 public List<Relation> getTargetRelations(Entity target,
jurzua
parents:
diff changeset
1037 String relationName, String srcObjClass, int maxResult) {
jurzua
parents:
diff changeset
1038 long start = System.currentTimeMillis();
jurzua
parents:
diff changeset
1039 List<Relation> list = new ArrayList<Relation>();
jurzua
parents:
diff changeset
1040
jurzua
parents:
diff changeset
1041 List<Relation> tarRelList = this.cache.getRelsByTarId(target.getId());
jurzua
parents:
diff changeset
1042 /*
jurzua
parents:
diff changeset
1043 * if(coll == null){ coll = this.getPS().getTargetRelations(target,
jurzua
parents:
diff changeset
1044 * null, null, -1, false); cache.setRelsForTar(target.getId(), coll); }
jurzua
parents:
diff changeset
1045 */
jurzua
parents:
diff changeset
1046
jurzua
parents:
diff changeset
1047 int count = 0;
jurzua
parents:
diff changeset
1048 for (Relation rel : tarRelList) {
jurzua
parents:
diff changeset
1049 if (stringEquals(relationName, rel.getOwnValue())
jurzua
parents:
diff changeset
1050 && stringEquals(srcObjClass, rel.getSourceObjectClass())) {
jurzua
parents:
diff changeset
1051 Entity source = getEntityById(rel.getSourceId());
jurzua
parents:
diff changeset
1052 if (source != null) {
jurzua
parents:
diff changeset
1053 rel.setTarget(target);
jurzua
parents:
diff changeset
1054 rel.setSource(source);
jurzua
parents:
diff changeset
1055 list.add(rel);
jurzua
parents:
diff changeset
1056 count++;
jurzua
parents:
diff changeset
1057 if (maxResult > 0 && count == maxResult) {
jurzua
parents:
diff changeset
1058 break;
jurzua
parents:
diff changeset
1059 }
jurzua
parents:
diff changeset
1060 } else {
jurzua
parents:
diff changeset
1061 logger.error("#########ERROR: Relation without source "
jurzua
parents:
diff changeset
1062 + rel.toString());
jurzua
parents:
diff changeset
1063 }
jurzua
parents:
diff changeset
1064
jurzua
parents:
diff changeset
1065 }
jurzua
parents:
diff changeset
1066 }
jurzua
parents:
diff changeset
1067 Collections.sort(list, new RelationSortBySourceOW());
64
83b209aa4226 working on stale entity update problem.
casties
parents: 62
diff changeset
1068 //logger.debug("getTargetRelations - execution time[ms]: "+ (System.currentTimeMillis() - start));
1
jurzua
parents:
diff changeset
1069 return list;
jurzua
parents:
diff changeset
1070 }
jurzua
parents:
diff changeset
1071
jurzua
parents:
diff changeset
1072 /**
jurzua
parents:
diff changeset
1073 * Returns a list of relations found by entity source.
jurzua
parents:
diff changeset
1074 *
jurzua
parents:
diff changeset
1075 * @param source
jurzua
parents:
diff changeset
1076 * @param relationName
jurzua
parents:
diff changeset
1077 * @param tarObjClass
jurzua
parents:
diff changeset
1078 * @param maxResult
jurzua
parents:
diff changeset
1079 * @return
jurzua
parents:
diff changeset
1080 */
jurzua
parents:
diff changeset
1081 public List<Relation> getSourceRelations(Entity source,
jurzua
parents:
diff changeset
1082 String relationName, String tarObjClass, int maxResult)
jurzua
parents:
diff changeset
1083 throws Exception {
jurzua
parents:
diff changeset
1084
jurzua
parents:
diff changeset
1085 long start = System.currentTimeMillis();
jurzua
parents:
diff changeset
1086 List<Relation> list = new ArrayList<Relation>();
jurzua
parents:
diff changeset
1087
jurzua
parents:
diff changeset
1088 // the collection coll should not be modified
jurzua
parents:
diff changeset
1089 // otherwise java.util.ConcurrentModificationException will be thrown.
jurzua
parents:
diff changeset
1090 // be carefully using getEntityContent, cause it changes the cache
jurzua
parents:
diff changeset
1091
jurzua
parents:
diff changeset
1092 List<Relation> srcRelList = this.cache.getRelsBySrcId(source.getId());
jurzua
parents:
diff changeset
1093
jurzua
parents:
diff changeset
1094 int count = 0;
jurzua
parents:
diff changeset
1095 for (Relation rel : srcRelList) {
jurzua
parents:
diff changeset
1096 if (stringEquals(relationName, rel.getOwnValue())
jurzua
parents:
diff changeset
1097 && stringEquals(tarObjClass, rel.getTargetObjectClass())) {
jurzua
parents:
diff changeset
1098 rel.setSource(source);
jurzua
parents:
diff changeset
1099 rel.setTarget(getEntityById(rel.getTargetId()));
jurzua
parents:
diff changeset
1100 list.add(rel);
jurzua
parents:
diff changeset
1101 count++;
jurzua
parents:
diff changeset
1102 if (maxResult > 0 && count == maxResult) {
jurzua
parents:
diff changeset
1103 break;
jurzua
parents:
diff changeset
1104 }
jurzua
parents:
diff changeset
1105 }
jurzua
parents:
diff changeset
1106 }
jurzua
parents:
diff changeset
1107 Collections.sort(list, new RelationSortByTargetOW());
64
83b209aa4226 working on stale entity update problem.
casties
parents: 62
diff changeset
1108 //logger.debug("getSourceRelations - execution time[ms]: "+ (System.currentTimeMillis() - start));
1
jurzua
parents:
diff changeset
1109
jurzua
parents:
diff changeset
1110 return list;
jurzua
parents:
diff changeset
1111 }
jurzua
parents:
diff changeset
1112
jurzua
parents:
diff changeset
1113 /**
54
7f9033d44a87 small cleanup.
casties
parents: 53
diff changeset
1114 * Load and attach the Attributes and Relations of the given Entity.
7f9033d44a87 small cleanup.
casties
parents: 53
diff changeset
1115 *
7f9033d44a87 small cleanup.
casties
parents: 53
diff changeset
1116 * Tries to loads from cache first and then from DB, saving to cache.
7f9033d44a87 small cleanup.
casties
parents: 53
diff changeset
1117 *
7f9033d44a87 small cleanup.
casties
parents: 53
diff changeset
1118 * Returns a cloned Entity.
7f9033d44a87 small cleanup.
casties
parents: 53
diff changeset
1119 *
1
jurzua
parents:
diff changeset
1120 * This method should not be used inside this Wrapper class, because it
54
7f9033d44a87 small cleanup.
casties
parents: 53
diff changeset
1121 * could throw a java.util.ConcurrentModificationException.
1
jurzua
parents:
diff changeset
1122 *
jurzua
parents:
diff changeset
1123 * @param ent
jurzua
parents:
diff changeset
1124 * @return
jurzua
parents:
diff changeset
1125 */
jurzua
parents:
diff changeset
1126 public Entity getEntityContent(Entity ent) {
jurzua
parents:
diff changeset
1127 try {
jurzua
parents:
diff changeset
1128 if (ent.isLightweight()) {
jurzua
parents:
diff changeset
1129 Entity e = this.cache.getEntityContent(ent);
jurzua
parents:
diff changeset
1130 if (e == null) {
jurzua
parents:
diff changeset
1131 e = this.getPS().getEntityContent(ent);
jurzua
parents:
diff changeset
1132 this.cache.saveEntity(e);
jurzua
parents:
diff changeset
1133 } else {
jurzua
parents:
diff changeset
1134 ent = e;
jurzua
parents:
diff changeset
1135 }
jurzua
parents:
diff changeset
1136 }
jurzua
parents:
diff changeset
1137 return (Entity) ent.clone();
jurzua
parents:
diff changeset
1138 } catch (Exception e) {
jurzua
parents:
diff changeset
1139 logger.error(e.getMessage(), e);
jurzua
parents:
diff changeset
1140 }
jurzua
parents:
diff changeset
1141 return null;
jurzua
parents:
diff changeset
1142 }
jurzua
parents:
diff changeset
1143
jurzua
parents:
diff changeset
1144 public Entity getEntityContentReadOnly(Entity ent) throws Exception {
jurzua
parents:
diff changeset
1145 if (ent.isLightweight()) {
jurzua
parents:
diff changeset
1146 Entity e = this.cache.getEntityContent(ent);
jurzua
parents:
diff changeset
1147 if (e == null) {
jurzua
parents:
diff changeset
1148 e = this.getPS().getEntityContent(ent);
jurzua
parents:
diff changeset
1149 this.cache.saveEntity(e);
jurzua
parents:
diff changeset
1150 } else {
jurzua
parents:
diff changeset
1151 ent = e;
jurzua
parents:
diff changeset
1152 }
jurzua
parents:
diff changeset
1153 }
jurzua
parents:
diff changeset
1154 return ent;
jurzua
parents:
diff changeset
1155 }
jurzua
parents:
diff changeset
1156
jurzua
parents:
diff changeset
1157 /**
39
86c343109257 more comments and cleanup.
casties
parents: 38
diff changeset
1158 * Return an entity with the given id from the cache.
86c343109257 more comments and cleanup.
casties
parents: 38
diff changeset
1159 *
1
jurzua
parents:
diff changeset
1160 * The entity returned could be LW or not, it depends on the cache.
jurzua
parents:
diff changeset
1161 * additionally, it will be always a clone of the version in cache,
jurzua
parents:
diff changeset
1162 * therefore it method is thought for editing's issues.
jurzua
parents:
diff changeset
1163 *
jurzua
parents:
diff changeset
1164 * @param id
jurzua
parents:
diff changeset
1165 * @return
jurzua
parents:
diff changeset
1166 */
jurzua
parents:
diff changeset
1167 public Entity getEntityById(Long id) {
jurzua
parents:
diff changeset
1168 Entity ent = this.cache.getEntityById(id);
jurzua
parents:
diff changeset
1169 return ent;
jurzua
parents:
diff changeset
1170 }
jurzua
parents:
diff changeset
1171
39
86c343109257 more comments and cleanup.
casties
parents: 38
diff changeset
1172 /**
86c343109257 more comments and cleanup.
casties
parents: 38
diff changeset
1173 * Return an entity with the given id from the cache.
86c343109257 more comments and cleanup.
casties
parents: 38
diff changeset
1174 *
86c343109257 more comments and cleanup.
casties
parents: 38
diff changeset
1175 * Do not modify the entity!
86c343109257 more comments and cleanup.
casties
parents: 38
diff changeset
1176 *
86c343109257 more comments and cleanup.
casties
parents: 38
diff changeset
1177 * @param id
86c343109257 more comments and cleanup.
casties
parents: 38
diff changeset
1178 * @return
86c343109257 more comments and cleanup.
casties
parents: 38
diff changeset
1179 */
1
jurzua
parents:
diff changeset
1180 public Entity getEntityByIdReadOnly(Long id) {
jurzua
parents:
diff changeset
1181 Entity ent = this.cache.getEntityByIdReadOnly(id);
jurzua
parents:
diff changeset
1182 return ent;
jurzua
parents:
diff changeset
1183 }
jurzua
parents:
diff changeset
1184
jurzua
parents:
diff changeset
1185 public Entity getClonedEntityById(Long id) {
jurzua
parents:
diff changeset
1186 return (Entity) getEntityById(id).clone();
jurzua
parents:
diff changeset
1187 }
jurzua
parents:
diff changeset
1188
jurzua
parents:
diff changeset
1189 /**
jurzua
parents:
diff changeset
1190 * This method should not be used inside this Wrapper class, because it
jurzua
parents:
diff changeset
1191 * could throws an java.util.ConcurrentModificationException.
jurzua
parents:
diff changeset
1192 *
jurzua
parents:
diff changeset
1193 * @param id
jurzua
parents:
diff changeset
1194 * @return
jurzua
parents:
diff changeset
1195 */
jurzua
parents:
diff changeset
1196 public Entity getEntityByIdWithContent(Long id) {
jurzua
parents:
diff changeset
1197 try {
jurzua
parents:
diff changeset
1198 Entity ent = getEntityById(id);
jurzua
parents:
diff changeset
1199 if (ent != null && ent.isLightweight()) {
jurzua
parents:
diff changeset
1200 ent = getEntityContent(ent);
jurzua
parents:
diff changeset
1201 }
jurzua
parents:
diff changeset
1202 return ent;
jurzua
parents:
diff changeset
1203 } catch (Exception e) {
jurzua
parents:
diff changeset
1204 logger.error(e.getMessage(), e);
jurzua
parents:
diff changeset
1205 }
jurzua
parents:
diff changeset
1206 return null;
jurzua
parents:
diff changeset
1207 }
jurzua
parents:
diff changeset
1208
jurzua
parents:
diff changeset
1209 public Entity getEntityByIdWithContentReadOnly(Long id) throws Exception {
jurzua
parents:
diff changeset
1210 Entity ent = getEntityByIdReadOnly(id);
jurzua
parents:
diff changeset
1211 if (ent.isLightweight()) {
jurzua
parents:
diff changeset
1212 ent = getEntityContentReadOnly(ent);
jurzua
parents:
diff changeset
1213 }
jurzua
parents:
diff changeset
1214 return ent;
jurzua
parents:
diff changeset
1215 }
jurzua
parents:
diff changeset
1216
55
b7a8db041f68 cleanup and fix when target_id doesn't exist.
casties
parents: 54
diff changeset
1217 /**
b7a8db041f68 cleanup and fix when target_id doesn't exist.
casties
parents: 54
diff changeset
1218 * 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
1219 *
b7a8db041f68 cleanup and fix when target_id doesn't exist.
casties
parents: 54
diff changeset
1220 * @param term
b7a8db041f68 cleanup and fix when target_id doesn't exist.
casties
parents: 54
diff changeset
1221 * @param ow
b7a8db041f68 cleanup and fix when target_id doesn't exist.
casties
parents: 54
diff changeset
1222 * @return
b7a8db041f68 cleanup and fix when target_id doesn't exist.
casties
parents: 54
diff changeset
1223 */
1
jurzua
parents:
diff changeset
1224 public static boolean stringEquals(String term, String ow) {
jurzua
parents:
diff changeset
1225 if (StringUtils.isEmpty(term))
jurzua
parents:
diff changeset
1226 return true;
jurzua
parents:
diff changeset
1227 return term.equals(ow);
jurzua
parents:
diff changeset
1228 }
jurzua
parents:
diff changeset
1229
jurzua
parents:
diff changeset
1230 // ////****
jurzua
parents:
diff changeset
1231 public Entity getLightweightEntityById(Long id) {
jurzua
parents:
diff changeset
1232 Entity entity = null;
jurzua
parents:
diff changeset
1233 if (id != null) {
jurzua
parents:
diff changeset
1234 List<Entity> list = this.getPS().getLightweightEntities(
jurzua
parents:
diff changeset
1235 Node.SYS_STATUS_CURRENT_VERSION, id, null, null, null,
jurzua
parents:
diff changeset
1236 true, -1);
jurzua
parents:
diff changeset
1237 if (list.size() > 0) {
jurzua
parents:
diff changeset
1238 entity = list.get(0);
jurzua
parents:
diff changeset
1239 }
jurzua
parents:
diff changeset
1240 }
jurzua
parents:
diff changeset
1241 return entity;
jurzua
parents:
diff changeset
1242 }
jurzua
parents:
diff changeset
1243
jurzua
parents:
diff changeset
1244 public List<Entity> getLightweightAssertions(String objectClass,
jurzua
parents:
diff changeset
1245 String ownValue, int maxResult) {
jurzua
parents:
diff changeset
1246 return getPS().getLightweightEntities(Node.SYS_STATUS_CURRENT_VERSION,
jurzua
parents:
diff changeset
1247 null, Node.TYPE_ABOX, objectClass, ownValue, true, maxResult);
jurzua
parents:
diff changeset
1248 }
jurzua
parents:
diff changeset
1249
38
cb25e343e317 more comments and refactorings.
casties
parents: 37
diff changeset
1250 /**
66
3e4b05a6cb47 new EditIntent for saveEntity().
casties
parents: 65
diff changeset
1251 * Save the entity. Calls {@link #saveEntity(String, String, EditIntent)}.
38
cb25e343e317 more comments and refactorings.
casties
parents: 37
diff changeset
1252 *
cb25e343e317 more comments and refactorings.
casties
parents: 37
diff changeset
1253 * @param entity
cb25e343e317 more comments and refactorings.
casties
parents: 37
diff changeset
1254 * @param user
cb25e343e317 more comments and refactorings.
casties
parents: 37
diff changeset
1255 * @throws Exception
cb25e343e317 more comments and refactorings.
casties
parents: 37
diff changeset
1256 */
1
jurzua
parents:
diff changeset
1257 public void saveAssertion(Entity entity, String user) throws Exception {
66
3e4b05a6cb47 new EditIntent for saveEntity().
casties
parents: 65
diff changeset
1258 this.saveEntity(entity, user, null);
1
jurzua
parents:
diff changeset
1259 }
jurzua
parents:
diff changeset
1260
jurzua
parents:
diff changeset
1261 /**
jurzua
parents:
diff changeset
1262 * <p>
jurzua
parents:
diff changeset
1263 * This method saves a non-persistent entity.
jurzua
parents:
diff changeset
1264 * </p>
jurzua
parents:
diff changeset
1265 * <p>
jurzua
parents:
diff changeset
1266 * The reason of this is that nodes are stored only in the DB and not in the
jurzua
parents:
diff changeset
1267 * cache.
jurzua
parents:
diff changeset
1268 * </p>
jurzua
parents:
diff changeset
1269 * <p>
jurzua
parents:
diff changeset
1270 * Later, when the nodes are required, they will be loaded from the BD to
jurzua
parents:
diff changeset
1271 * the cache automatically. However there are some structures like
jurzua
parents:
diff changeset
1272 * attributes by entId that become inconsistent using this method.
jurzua
parents:
diff changeset
1273 * </p>
jurzua
parents:
diff changeset
1274 *
jurzua
parents:
diff changeset
1275 * @param node
jurzua
parents:
diff changeset
1276 * non-persistent node.
jurzua
parents:
diff changeset
1277 * @param user
jurzua
parents:
diff changeset
1278 * @throws Exception
jurzua
parents:
diff changeset
1279 */
jurzua
parents:
diff changeset
1280 public void saveNodeOnlyForScripts(Node node, String user) throws Exception {
jurzua
parents:
diff changeset
1281 if (node.isPersistent()) {
jurzua
parents:
diff changeset
1282 throw new Exception(
jurzua
parents:
diff changeset
1283 "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
1284 }
jurzua
parents:
diff changeset
1285 if (StringUtils.isEmpty(node.getType())) {
jurzua
parents:
diff changeset
1286 throw new Exception("The type of the node can not be empty");
jurzua
parents:
diff changeset
1287 }
jurzua
parents:
diff changeset
1288 node.setUser(user);
jurzua
parents:
diff changeset
1289 this.getPS().saveNode(node);
jurzua
parents:
diff changeset
1290
jurzua
parents:
diff changeset
1291 // if(StringUtils.isNotEmpty(node.getType())){
jurzua
parents:
diff changeset
1292 // if(node.getType().equals(Node.TYPE_ABOX)){
jurzua
parents:
diff changeset
1293 //
jurzua
parents:
diff changeset
1294 // }else if(node.getType().equals(Node.TYPE_TBOX)){
jurzua
parents:
diff changeset
1295 //
jurzua
parents:
diff changeset
1296 // }
jurzua
parents:
diff changeset
1297 // }
jurzua
parents:
diff changeset
1298 }
jurzua
parents:
diff changeset
1299
jurzua
parents:
diff changeset
1300 public void removeNode(Node node) {
jurzua
parents:
diff changeset
1301 this.getPS().removeNode(node);
jurzua
parents:
diff changeset
1302 }
jurzua
parents:
diff changeset
1303
jurzua
parents:
diff changeset
1304 public Map<Long, Long> saveEntityListAsNew(List<Entity> entities, String user,
jurzua
parents:
diff changeset
1305 boolean testWrongRelations) throws Exception {
jurzua
parents:
diff changeset
1306 logger.info("\n ### Making persistent Entities size total "
jurzua
parents:
diff changeset
1307 + entities.size() + " ### \n");
jurzua
parents:
diff changeset
1308 int sizePart = 1000;
jurzua
parents:
diff changeset
1309 List<Entity> tmpList = null;
jurzua
parents:
diff changeset
1310
jurzua
parents:
diff changeset
1311 Map<Long, Long> idMap = new HashMap<Long, Long>();
jurzua
parents:
diff changeset
1312
jurzua
parents:
diff changeset
1313 for (int j = 0; j <= (entities.size() / sizePart); j++) {
jurzua
parents:
diff changeset
1314 tmpList = new ArrayList<Entity>(sizePart);
jurzua
parents:
diff changeset
1315 for (int i = (j * sizePart); i < ((j + 1) * sizePart); i++) {
jurzua
parents:
diff changeset
1316
jurzua
parents:
diff changeset
1317 if (i < entities.size()) {
jurzua
parents:
diff changeset
1318 if (StringUtils.isNotEmpty(user)) {
jurzua
parents:
diff changeset
1319 entities.get(i).setUser(user);
jurzua
parents:
diff changeset
1320 }
jurzua
parents:
diff changeset
1321
jurzua
parents:
diff changeset
1322 if (testWrongRelations
jurzua
parents:
diff changeset
1323 && hasWrongRelations(entities.get(i))) {
jurzua
parents:
diff changeset
1324 throw new Exception("Wrong relations "
jurzua
parents:
diff changeset
1325 + entities.get(i));
jurzua
parents:
diff changeset
1326 }
jurzua
parents:
diff changeset
1327
jurzua
parents:
diff changeset
1328 tmpList.add(entities.get(i));
jurzua
parents:
diff changeset
1329 } else {
jurzua
parents:
diff changeset
1330 break;
jurzua
parents:
diff changeset
1331 }
jurzua
parents:
diff changeset
1332 }
jurzua
parents:
diff changeset
1333
jurzua
parents:
diff changeset
1334 logger.info("\nMaking persistent Entities part " + (j + 1) + " of "
jurzua
parents:
diff changeset
1335 + ((entities.size() / sizePart) + 1) + " size="
jurzua
parents:
diff changeset
1336 + tmpList.size());
jurzua
parents:
diff changeset
1337 idMap = this.getPS().saveEntityListAsNew(tmpList, idMap);
jurzua
parents:
diff changeset
1338 }
jurzua
parents:
diff changeset
1339 return idMap;
jurzua
parents:
diff changeset
1340 }
jurzua
parents:
diff changeset
1341
jurzua
parents:
diff changeset
1342 /*
jurzua
parents:
diff changeset
1343 public void saveEntityList(List<Entity> entities, String user,
jurzua
parents:
diff changeset
1344 boolean testWrongRelations) throws Exception {
jurzua
parents:
diff changeset
1345 logger.info("\n ### Making persistent Entities size total "
jurzua
parents:
diff changeset
1346 + entities.size() + " ### \n");
jurzua
parents:
diff changeset
1347 int sizePart = 1000;
jurzua
parents:
diff changeset
1348 List<Entity> tmpList = null;
jurzua
parents:
diff changeset
1349
jurzua
parents:
diff changeset
1350 for (int j = 0; j <= (entities.size() / sizePart); j++) {
jurzua
parents:
diff changeset
1351 tmpList = new ArrayList<Entity>(sizePart);
jurzua
parents:
diff changeset
1352 for (int i = (j * sizePart); i < ((j + 1) * sizePart); i++) {
jurzua
parents:
diff changeset
1353
jurzua
parents:
diff changeset
1354 if (i < entities.size()) {
jurzua
parents:
diff changeset
1355 if (StringUtils.isNotEmpty(user)) {
jurzua
parents:
diff changeset
1356 entities.get(i).setUser(user);
jurzua
parents:
diff changeset
1357 }
jurzua
parents:
diff changeset
1358
jurzua
parents:
diff changeset
1359 if (testWrongRelations
jurzua
parents:
diff changeset
1360 && hasWrongRelations(entities.get(i))) {
jurzua
parents:
diff changeset
1361 throw new Exception("Wrong relations "
jurzua
parents:
diff changeset
1362 + entities.get(i));
jurzua
parents:
diff changeset
1363 }
jurzua
parents:
diff changeset
1364
jurzua
parents:
diff changeset
1365 tmpList.add(entities.get(i));
jurzua
parents:
diff changeset
1366 } else {
jurzua
parents:
diff changeset
1367 break;
jurzua
parents:
diff changeset
1368 }
jurzua
parents:
diff changeset
1369 }
jurzua
parents:
diff changeset
1370
jurzua
parents:
diff changeset
1371 logger.info("\nMaking persistent Entities part " + (j + 1) + " of "
jurzua
parents:
diff changeset
1372 + ((entities.size() / sizePart) + 1) + " size="
jurzua
parents:
diff changeset
1373 + tmpList.size());
jurzua
parents:
diff changeset
1374 this.getPS().saveEntityList(tmpList);
jurzua
parents:
diff changeset
1375
jurzua
parents:
diff changeset
1376 for(Entity ent : tmpList){
jurzua
parents:
diff changeset
1377 logger.info("[U=" + user + "] SaveEntity " + ent.toSmallString());
jurzua
parents:
diff changeset
1378 }
jurzua
parents:
diff changeset
1379
jurzua
parents:
diff changeset
1380 }
jurzua
parents:
diff changeset
1381 }*/
jurzua
parents:
diff changeset
1382
jurzua
parents:
diff changeset
1383 public void saveConcept(Entity entity) throws Exception {
jurzua
parents:
diff changeset
1384 entity.setType(Node.TYPE_TBOX);
jurzua
parents:
diff changeset
1385 entity.setObjectClass(Node.TYPE_TBOX);
jurzua
parents:
diff changeset
1386 this.getPS().saveEntity(entity);
jurzua
parents:
diff changeset
1387 }
jurzua
parents:
diff changeset
1388
jurzua
parents:
diff changeset
1389 public void saveNodeListOnlyForScripts(List<Node> nodeList, String user)
jurzua
parents:
diff changeset
1390 throws Exception {
jurzua
parents:
diff changeset
1391 logger.debug("### Making persistent Nodes size total "
jurzua
parents:
diff changeset
1392 + nodeList.size() + " ###");
jurzua
parents:
diff changeset
1393 List<Node> list = null;
jurzua
parents:
diff changeset
1394 int sizePart = 1000;
jurzua
parents:
diff changeset
1395 for (int j = 0; j <= (nodeList.size() / sizePart); j++) {
jurzua
parents:
diff changeset
1396 list = new ArrayList<Node>();
jurzua
parents:
diff changeset
1397 for (int i = (j * sizePart); i < ((j + 1) * sizePart); i++) {
jurzua
parents:
diff changeset
1398 if (i < nodeList.size()) {
jurzua
parents:
diff changeset
1399 nodeList.get(i).setUser(user);
jurzua
parents:
diff changeset
1400 list.add(nodeList.get(i));
jurzua
parents:
diff changeset
1401 }
jurzua
parents:
diff changeset
1402 }
jurzua
parents:
diff changeset
1403 logger.debug("Making persistent Nodes part " + (j + 1) + " of "
jurzua
parents:
diff changeset
1404 + ((nodeList.size() / sizePart) + 1) + " size="
jurzua
parents:
diff changeset
1405 + list.size());
jurzua
parents:
diff changeset
1406 this.getPS().saveNodeList(list);
jurzua
parents:
diff changeset
1407 }
jurzua
parents:
diff changeset
1408 }
jurzua
parents:
diff changeset
1409
jurzua
parents:
diff changeset
1410 public void saveNodeListOnlyForScripts(List<Node> nodeList)
jurzua
parents:
diff changeset
1411 throws Exception {
jurzua
parents:
diff changeset
1412 logger.info("\n ### Making persistent Nodes size total "
jurzua
parents:
diff changeset
1413 + nodeList.size() + " ### \n");
jurzua
parents:
diff changeset
1414 int sizePart = 1000;
jurzua
parents:
diff changeset
1415 List<Node> list = null;
jurzua
parents:
diff changeset
1416 for (int j = 0; j <= (nodeList.size() / sizePart); j++) {
jurzua
parents:
diff changeset
1417 list = new ArrayList<Node>();
jurzua
parents:
diff changeset
1418 for (int i = (j * sizePart); i < ((j + 1) * sizePart); i++) {
jurzua
parents:
diff changeset
1419 if (i < nodeList.size()) {
jurzua
parents:
diff changeset
1420 list.add(nodeList.get(i));
jurzua
parents:
diff changeset
1421 }
jurzua
parents:
diff changeset
1422 }
jurzua
parents:
diff changeset
1423 logger.info("Making persistent Nodes part " + (j + 1) + " of "
jurzua
parents:
diff changeset
1424 + ((nodeList.size() / sizePart) + 1) + " size="
jurzua
parents:
diff changeset
1425 + list.size());
jurzua
parents:
diff changeset
1426 this.getPS().saveNodeList(list);
jurzua
parents:
diff changeset
1427 }
jurzua
parents:
diff changeset
1428 // this.persistenceService.saveNodeList(nodeList);
jurzua
parents:
diff changeset
1429 }
jurzua
parents:
diff changeset
1430
jurzua
parents:
diff changeset
1431 public List<Entity> getConcepts() {
jurzua
parents:
diff changeset
1432 return this.getPS().getEntities(null, Node.SYS_STATUS_CURRENT_VERSION,
jurzua
parents:
diff changeset
1433 Node.TYPE_TBOX, null);
jurzua
parents:
diff changeset
1434 }
jurzua
parents:
diff changeset
1435
jurzua
parents:
diff changeset
1436 public List<Entity> getAssertion() {
jurzua
parents:
diff changeset
1437 return this.getPS().getEntities(null, Node.SYS_STATUS_CURRENT_VERSION,
jurzua
parents:
diff changeset
1438 Node.TYPE_ABOX, null);
jurzua
parents:
diff changeset
1439 }
jurzua
parents:
diff changeset
1440
jurzua
parents:
diff changeset
1441 public List<Entity> getLightweightAssertionsByExactOwnValue(
jurzua
parents:
diff changeset
1442 String objectClass, String ownValue, int maxResult) {
jurzua
parents:
diff changeset
1443 return getPS().getLightweightEntities(Node.SYS_STATUS_CURRENT_VERSION,
jurzua
parents:
diff changeset
1444 null, Node.TYPE_ABOX, objectClass, ownValue, false, maxResult);
jurzua
parents:
diff changeset
1445 }
jurzua
parents:
diff changeset
1446
jurzua
parents:
diff changeset
1447 public void removeNodeList(List<Node> nodeList) {
62
58659e865279 less System.out.println. less logging.
casties
parents: 58
diff changeset
1448 //System.out.println("\n ### Deleting Nodes size total "+ nodeList.size() + " ### \n");
1
jurzua
parents:
diff changeset
1449 this.getPS().removeNodeList(nodeList);
jurzua
parents:
diff changeset
1450 }
jurzua
parents:
diff changeset
1451
jurzua
parents:
diff changeset
1452 public void deleteAllConcepts() {
jurzua
parents:
diff changeset
1453 // persistenceService.deleteEntities(null, Node.TYPE_TBOX, true);
jurzua
parents:
diff changeset
1454 getPS().dropDefinitions();
jurzua
parents:
diff changeset
1455 }
jurzua
parents:
diff changeset
1456
jurzua
parents:
diff changeset
1457 public void importOM3Concepts(String fileName, Boolean dropConcepts) {
jurzua
parents:
diff changeset
1458 getPS().setImportModus(true);
jurzua
parents:
diff changeset
1459 ImportOM3Util.importConcepts(this, fileName, dropConcepts);
jurzua
parents:
diff changeset
1460 getPS().setImportModus(false);
jurzua
parents:
diff changeset
1461 }
jurzua
parents:
diff changeset
1462
jurzua
parents:
diff changeset
1463 // ///
jurzua
parents:
diff changeset
1464
jurzua
parents:
diff changeset
1465 public PersistenceService getPS() {
jurzua
parents:
diff changeset
1466 return this.cache.getPs();
jurzua
parents:
diff changeset
1467 }
jurzua
parents:
diff changeset
1468
jurzua
parents:
diff changeset
1469 public CacheService getCache() {
jurzua
parents:
diff changeset
1470 return cache;
jurzua
parents:
diff changeset
1471 }
jurzua
parents:
diff changeset
1472
jurzua
parents:
diff changeset
1473 public void setCache(CacheService cache) {
jurzua
parents:
diff changeset
1474 this.cache = cache;
jurzua
parents:
diff changeset
1475 }
jurzua
parents:
diff changeset
1476
jurzua
parents:
diff changeset
1477 public List<ViewerAttribute> getViewerAttributes(Long page){
jurzua
parents:
diff changeset
1478 List<ViewerAttribute> list = cache.getViewerAttributes(page);
jurzua
parents:
diff changeset
1479 Collections.sort(list);
jurzua
parents:
diff changeset
1480 return list;
jurzua
parents:
diff changeset
1481 }
jurzua
parents:
diff changeset
1482
jurzua
parents:
diff changeset
1483 public List<ViewerAttribute> getViewerAttributes4Edition(Long page) throws CloneNotSupportedException{
jurzua
parents:
diff changeset
1484 List<ViewerAttribute> list = new ArrayList<ViewerAttribute>();
jurzua
parents:
diff changeset
1485 for(ViewerAttribute att : cache.getViewerAttributes(page)){
jurzua
parents:
diff changeset
1486 list.add((ViewerAttribute)att.clone());
jurzua
parents:
diff changeset
1487 }
jurzua
parents:
diff changeset
1488 Collections.sort(list);
jurzua
parents:
diff changeset
1489 return list;
jurzua
parents:
diff changeset
1490 }
jurzua
parents:
diff changeset
1491
jurzua
parents:
diff changeset
1492 public Collection<ViewerPage> getViewerPages(){
jurzua
parents:
diff changeset
1493 return cache.getViewerPages();
jurzua
parents:
diff changeset
1494 }
jurzua
parents:
diff changeset
1495
jurzua
parents:
diff changeset
1496 public ViewerPage getViewerPage(Long id){
jurzua
parents:
diff changeset
1497 return cache.getViewerPageMap().get(id);
jurzua
parents:
diff changeset
1498 }
jurzua
parents:
diff changeset
1499
jurzua
parents:
diff changeset
1500 public ViewerPage getViewerPage(String definition){
jurzua
parents:
diff changeset
1501 for(ViewerPage page : cache.getViewerPages()){
jurzua
parents:
diff changeset
1502 if(page.getDefinition().equals(definition)){
jurzua
parents:
diff changeset
1503 return page;
jurzua
parents:
diff changeset
1504 }
jurzua
parents:
diff changeset
1505 }
jurzua
parents:
diff changeset
1506 return null;
jurzua
parents:
diff changeset
1507 }
jurzua
parents:
diff changeset
1508
jurzua
parents:
diff changeset
1509 public ViewerPage getViewerPage4Edition(Long id) throws CloneNotSupportedException{
jurzua
parents:
diff changeset
1510 ViewerPage page = cache.getViewerPageMap().get(id);
jurzua
parents:
diff changeset
1511 return (page != null) ? (ViewerPage) page.clone() : null;
jurzua
parents:
diff changeset
1512 }
jurzua
parents:
diff changeset
1513
jurzua
parents:
diff changeset
1514 public ViewerPage saveViewerPage(ViewerPage page, String user){
jurzua
parents:
diff changeset
1515 page.setUser(user);
jurzua
parents:
diff changeset
1516 return cache.saveViewerPage(page);
jurzua
parents:
diff changeset
1517
jurzua
parents:
diff changeset
1518 }
jurzua
parents:
diff changeset
1519
jurzua
parents:
diff changeset
1520 public ViewerAttribute saveViewerAttribute(ViewerPage page, ViewerAttribute att, String user) throws Exception{
jurzua
parents:
diff changeset
1521 att.setUser(user);
jurzua
parents:
diff changeset
1522 return cache.saveViewerAttribute(page, att);
jurzua
parents:
diff changeset
1523 }
jurzua
parents:
diff changeset
1524
jurzua
parents:
diff changeset
1525 /**
jurzua
parents:
diff changeset
1526 * Removes a page and all attributes associated with it.
jurzua
parents:
diff changeset
1527 * @param page
jurzua
parents:
diff changeset
1528 * @return
jurzua
parents:
diff changeset
1529 */
jurzua
parents:
diff changeset
1530 public int removeViewerPage(ViewerPage page){
jurzua
parents:
diff changeset
1531
jurzua
parents:
diff changeset
1532 return (page.getId() != null) ? cache.removeViewerPage(page.getId()) : 0;
jurzua
parents:
diff changeset
1533 }
jurzua
parents:
diff changeset
1534
jurzua
parents:
diff changeset
1535 public boolean removeViewerAnttribute(ViewerAttribute att){
jurzua
parents:
diff changeset
1536 return (att.getId() != null) ? cache.removeAtt(att.getId()) : false;
jurzua
parents:
diff changeset
1537 }
jurzua
parents:
diff changeset
1538 }