Mercurial > hg > openmind
annotate src/main/java/org/mpi/openmind/repository/bo/Relation.java @ 46:fc120c3c6ce4
better transaction logging.
author | casties |
---|---|
date | Fri, 21 Oct 2016 11:52:14 +0200 |
parents | c181cb6f1761 |
children | 2d669fdd0a8b |
rev | line source |
---|---|
1 | 1 package org.mpi.openmind.repository.bo; |
2 | |
3 import java.io.Serializable; | |
33
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
25
diff
changeset
|
4 import java.io.UnsupportedEncodingException; |
1 | 5 import java.util.ArrayList; |
6 import java.util.List; | |
7 | |
8 import javax.persistence.Column; | |
9 import javax.persistence.DiscriminatorValue; | |
10 import javax.persistence.Transient; | |
11 | |
33
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
25
diff
changeset
|
12 import org.apache.commons.codec.binary.Base64; |
1 | 13 import org.apache.commons.lang.StringUtils; |
14 import org.hibernate.annotations.Cache; | |
15 import org.hibernate.annotations.CacheConcurrencyStrategy; | |
16 import org.mpi.openmind.cache.WrapperService; | |
17 import org.mpi.openmind.repository.utils.OMUtils; | |
18 | |
19 import cl.maps.penta.PentaKey; | |
20 import cl.maps.utils.RelKey; | |
21 | |
22 /** | |
23 * | |
24 * @author jurzua | |
25 */ | |
26 @javax.persistence.Entity | |
27 @DiscriminatorValue("RELATION") | |
28 @Cache(usage=CacheConcurrencyStrategy.READ_ONLY) | |
29 public class Relation extends Node implements Serializable, Comparable<Relation> { | |
30 | |
31 public Relation(){ | |
32 this.setSystemStatus(SYS_STATUS_CURRENT_VERSION); | |
33 } | |
34 | |
35 | |
36 public static Relation defRelation(Relation other, WrapperService ws) throws Exception{ | |
37 | |
38 Relation rel = new Relation(); | |
39 | |
40 rel.setOwnValue(other.getOwnValue()); | |
41 rel.setObjectClass(other.getObjectClass()); | |
42 rel.setType(other.getType()); | |
43 | |
44 Entity src = ws.getDefinitionById(other.getSourceId()); | |
45 Entity tar = ws.getDefinitionById(other.getTargetId()); | |
46 if(src == null || tar == null){ | |
47 throw new Exception("src and/or target not found!"); | |
48 } | |
49 rel.setSource(src); | |
50 rel.setTarget(tar); | |
51 return rel; | |
52 } | |
53 | |
54 public static Relation entRelation(Relation other, WrapperService ws) throws Exception{ | |
55 | |
56 Relation rel = new Relation(); | |
57 | |
58 rel.setOwnValue(other.getOwnValue()); | |
59 rel.setObjectClass(other.getOwnValue()); | |
60 rel.setType(Node.TYPE_ABOX); | |
61 | |
62 Entity src = ws.getEntityById(other.getSourceId()); | |
63 Entity tar = ws.getEntityById(other.getTargetId()); | |
64 | |
65 if(src == null || tar == null){ | |
66 throw new Exception("src and/or target not found!"); | |
67 } | |
68 | |
69 rel.setSource(src); | |
70 rel.setTarget(tar); | |
71 return rel; | |
72 } | |
73 | |
74 public Relation(Entity source, Entity target){ | |
75 this.setSource(source); | |
76 this.setTarget(target); | |
77 source.addSourceRelation(this); | |
78 target.addTargetRelation(this); | |
79 this.setSystemStatus(Node.SYS_STATUS_CURRENT_VERSION); | |
80 } | |
81 | |
82 public Relation(Entity srcNonLW, Entity tarNonLW, String ownValue){ | |
83 this.setSource(srcNonLW); | |
84 this.setTarget(tarNonLW); | |
85 srcNonLW.addSourceRelation(this); | |
86 tarNonLW.addTargetRelation(this); | |
87 this.setOwnValue(ownValue); | |
88 this.setSystemStatus(Node.SYS_STATUS_CURRENT_VERSION); | |
89 } | |
90 | |
91 @Column(name="source_id") | |
92 private Long sourceId; | |
93 | |
94 @Column(name="source_modif") | |
95 private Long sourceModif; | |
96 | |
97 @Column(name="source_obj_class") | |
98 private String sourceObjectClass; | |
99 | |
100 @Column(name="target_id") | |
101 private Long targetId; | |
102 | |
103 @Column(name="target_modif") | |
104 private Long targetModif; | |
105 | |
106 @Column(name="target_obj_class") | |
107 private String targetObjectClass; | |
108 | |
109 @Transient | |
110 private Entity source; | |
111 | |
112 @Transient | |
113 private Entity target; | |
114 | |
115 @Transient | |
116 private List<Attribute> attributes = new ArrayList<Attribute>(); | |
117 | |
118 public Attribute getAttributeByName(String name) { | |
119 for (Attribute attribute : this.getAttributes()) { | |
120 if (attribute.getObjectClass().equals(name)) { | |
121 return attribute; | |
122 } | |
123 } | |
124 return null; | |
125 } | |
126 | |
127 public void addAttribute(Attribute att){ | |
128 if(att != null){ | |
129 att.setSourceId(this.getId()); | |
130 att.setSourceModif(this.getModificationTime()); | |
131 att.setSystemStatus(this.getSystemStatus()); | |
132 this.attributes.add(att); | |
133 } | |
134 } | |
135 | |
136 @Override | |
137 public void setSystemStatus(String status) { | |
138 super.setSystemStatus(status); | |
139 if(this.attributes != null){ | |
140 for(Attribute att : this.attributes){ | |
141 att.setSystemStatus(status); | |
142 } | |
143 } | |
144 | |
145 } | |
146 | |
147 public List<Attribute> getAttributes() { | |
148 return attributes; | |
149 } | |
150 | |
151 public void setAttributes(List<Attribute> attributes) { | |
152 this.attributes = attributes; | |
153 } | |
154 | |
155 public String getSourceObjectClass() { | |
156 return sourceObjectClass; | |
157 } | |
158 | |
159 public void setSourceObjectClass(String sourceObjectClass) { | |
160 this.sourceObjectClass = sourceObjectClass; | |
161 } | |
162 | |
163 public String getTargetObjectClass() { | |
164 return targetObjectClass; | |
165 } | |
166 | |
167 public void setTargetObjectClass(String targetObjectClass) { | |
168 this.targetObjectClass = targetObjectClass; | |
169 } | |
170 | |
171 public Long getSourceModif() { | |
172 return sourceModif; | |
173 } | |
174 | |
175 public void setSourceModif(Long sourceModif) { | |
176 this.sourceModif = sourceModif; | |
177 } | |
178 | |
179 public Long getTargetModif() { | |
180 return targetModif; | |
181 } | |
182 | |
183 public void setTargetModif(Long targetModif) { | |
184 this.targetModif = targetModif; | |
185 } | |
186 | |
187 public Entity getSource() { | |
188 return source; | |
189 } | |
190 | |
191 public void setSource(Entity src) { | |
192 this.source = src; | |
193 if(src != null){ | |
194 this.setSourceId(src.getId()); | |
195 this.setSourceModif(src.getModificationTime()); | |
196 this.setSourceObjectClass(src.getObjectClass()); | |
197 } | |
198 } | |
199 | |
200 public Entity getTarget() { | |
201 return target; | |
202 } | |
203 | |
204 public void setTarget(Entity target) { | |
205 this.target = target; | |
206 if(target != null){ | |
207 this.setTargetId(target.getId()); | |
208 this.setTargetModif(target.getModificationTime()); | |
209 this.setTargetObjectClass(target.getObjectClass()); | |
210 } | |
211 } | |
212 | |
213 | |
214 public Long getSourceId() { | |
215 return sourceId; | |
216 } | |
217 | |
218 public void setSourceId(Long sourceId) { | |
219 this.sourceId = sourceId; | |
220 } | |
221 | |
222 public Long getTargetId() { | |
223 return targetId; | |
224 } | |
225 | |
226 public void setTargetId(Long targetId) { | |
227 this.targetId = targetId; | |
228 } | |
229 | |
40
c181cb6f1761
first version of equalsStructure to test when saving entities.
casties
parents:
34
diff
changeset
|
230 /** |
c181cb6f1761
first version of equalsStructure to test when saving entities.
casties
parents:
34
diff
changeset
|
231 * Returns if this Relation is equal to another Relation. |
c181cb6f1761
first version of equalsStructure to test when saving entities.
casties
parents:
34
diff
changeset
|
232 * |
c181cb6f1761
first version of equalsStructure to test when saving entities.
casties
parents:
34
diff
changeset
|
233 * Compares ownvalue (=type), source id and target id. |
c181cb6f1761
first version of equalsStructure to test when saving entities.
casties
parents:
34
diff
changeset
|
234 * |
c181cb6f1761
first version of equalsStructure to test when saving entities.
casties
parents:
34
diff
changeset
|
235 * @param other |
c181cb6f1761
first version of equalsStructure to test when saving entities.
casties
parents:
34
diff
changeset
|
236 * @return |
c181cb6f1761
first version of equalsStructure to test when saving entities.
casties
parents:
34
diff
changeset
|
237 */ |
c181cb6f1761
first version of equalsStructure to test when saving entities.
casties
parents:
34
diff
changeset
|
238 public boolean equalsContent(Relation other) { |
c181cb6f1761
first version of equalsStructure to test when saving entities.
casties
parents:
34
diff
changeset
|
239 if (other == null) { |
c181cb6f1761
first version of equalsStructure to test when saving entities.
casties
parents:
34
diff
changeset
|
240 return false; |
c181cb6f1761
first version of equalsStructure to test when saving entities.
casties
parents:
34
diff
changeset
|
241 } |
c181cb6f1761
first version of equalsStructure to test when saving entities.
casties
parents:
34
diff
changeset
|
242 if (StringUtils.equals(getOwnValue(), other.getOwnValue()) |
c181cb6f1761
first version of equalsStructure to test when saving entities.
casties
parents:
34
diff
changeset
|
243 && OMUtils.equals(getSourceId(), other.getSourceId()) |
c181cb6f1761
first version of equalsStructure to test when saving entities.
casties
parents:
34
diff
changeset
|
244 && OMUtils.equals(getTargetId(), other.getTargetId())) { |
c181cb6f1761
first version of equalsStructure to test when saving entities.
casties
parents:
34
diff
changeset
|
245 return true; |
c181cb6f1761
first version of equalsStructure to test when saving entities.
casties
parents:
34
diff
changeset
|
246 } |
c181cb6f1761
first version of equalsStructure to test when saving entities.
casties
parents:
34
diff
changeset
|
247 return false; |
c181cb6f1761
first version of equalsStructure to test when saving entities.
casties
parents:
34
diff
changeset
|
248 } |
1 | 249 |
250 @Override | |
251 public String toString() { | |
252 String source = ", Src[id=" + sourceId; | |
253 if(getSource() != null) | |
254 source += ", src=" + getSource().getOwnValue(); | |
255 source +="] "; | |
256 String target = ", Tar[id=" + targetId; | |
257 if(getTarget() != null) | |
258 target += ", tar=" + getTarget().getOwnValue(); | |
259 target +="] "; | |
260 return "Relation[" + | |
261 "rowId=" + this.getRowId() + ", " + | |
262 "id=" + getId() +", " + | |
263 "ownValue=" + getOwnValue() + source + target + ", " + | |
264 "sysStatus=" + this.getSystemStatus() + "]"; | |
265 } | |
266 | |
33
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
25
diff
changeset
|
267 /** |
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
25
diff
changeset
|
268 * Returns a String representation with base64-encoded own-value. |
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
25
diff
changeset
|
269 * |
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
25
diff
changeset
|
270 * To be used for the transaction log. |
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
25
diff
changeset
|
271 * |
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
25
diff
changeset
|
272 * @return |
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
25
diff
changeset
|
273 */ |
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
25
diff
changeset
|
274 public String toEncString() { |
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
25
diff
changeset
|
275 try { |
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
25
diff
changeset
|
276 String es = "RELATION[" |
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
25
diff
changeset
|
277 + "id=\"" + this.getId() + "\", " |
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
25
diff
changeset
|
278 + "row-id=\"" + this.getRowId() + "\", " |
46 | 279 + "object-class=\"" + this.getObjectClass() + "\", " |
33
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
25
diff
changeset
|
280 + "source-id=\"" + this.getSourceId() + "\", " |
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
25
diff
changeset
|
281 + "source-mtime=\"" + this.getSourceModif() + "\", " |
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
25
diff
changeset
|
282 + "source-oc=\"" + this.getSourceObjectClass() + "\", " |
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
25
diff
changeset
|
283 + "target-id=\"" + this.getTargetId() + "\", " |
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
25
diff
changeset
|
284 + "target-mtime=\"" + this.getTargetModif() + "\", " |
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
25
diff
changeset
|
285 + "target-oc=\"" + this.getTargetObjectClass() + "\", " |
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
25
diff
changeset
|
286 + "mtime=\"" + this.getModificationTime() + "\", " |
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
25
diff
changeset
|
287 + "version=\"" + this.getVersion() + "\", " |
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
25
diff
changeset
|
288 + "user=\"" + this.getUser() + "\", " |
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
25
diff
changeset
|
289 + "public=\"" + this.getIsPublic() + "\", " |
34
5737ab564b94
more transaction logging. logs more attributes and logs nodes.
casties
parents:
33
diff
changeset
|
290 + "system-status=\"" + this.getSystemStatus() + "\", " |
5737ab564b94
more transaction logging. logs more attributes and logs nodes.
casties
parents:
33
diff
changeset
|
291 + "status=\"" + this.getStatus() + "\", " |
5737ab564b94
more transaction logging. logs more attributes and logs nodes.
casties
parents:
33
diff
changeset
|
292 + "type=\"" + this.getType() + "\", " |
33
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
25
diff
changeset
|
293 + "b64-value=\"" + ((this.getOwnValue() != null) ? Base64.encodeBase64String(this.getOwnValue().getBytes("UTF-8")) : "") + "\"]"; |
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
25
diff
changeset
|
294 return es; |
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
25
diff
changeset
|
295 } catch (UnsupportedEncodingException e) { |
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
25
diff
changeset
|
296 // this shouldn't happen |
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
25
diff
changeset
|
297 e.printStackTrace(); |
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
25
diff
changeset
|
298 } |
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
25
diff
changeset
|
299 return "!!!ENCODING-ERROR!!!"; |
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
25
diff
changeset
|
300 } |
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
25
diff
changeset
|
301 |
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
25
diff
changeset
|
302 |
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
25
diff
changeset
|
303 @Override |
1 | 304 public int compareTo(Relation e) { |
305 if(e == null) | |
306 return 1; | |
307 if(StringUtils.isNotEmpty(this.getOwnValue())) | |
308 return this.getOwnValue().compareTo(e.getOwnValue()); | |
309 else return 0; | |
310 } | |
311 | |
312 public PentaKey<RelKey, Long, Long, String, Long> getKey(){ | |
313 return new PentaKey<RelKey, Long, Long, String, Long>( | |
314 new RelKey(this.getSourceId(), this.getTargetId(), this.getOwnValue()), | |
315 this.getSourceId(), | |
316 this.getTargetId(), | |
317 this.getOwnValue(), | |
318 this.getId()); | |
319 } | |
320 } |