1
|
1 package org.mpi.openmind.repository.bo;
|
|
2
|
|
3 import java.io.Serializable;
|
|
4 import java.util.ArrayList;
|
|
5 import java.util.List;
|
|
6
|
|
7 import javax.persistence.Column;
|
|
8 import javax.persistence.DiscriminatorValue;
|
|
9 import javax.persistence.Transient;
|
|
10
|
|
11 import org.apache.commons.lang.StringUtils;
|
|
12 import org.hibernate.annotations.Cache;
|
|
13 import org.hibernate.annotations.CacheConcurrencyStrategy;
|
|
14 import org.mpi.openmind.cache.WrapperService;
|
|
15 import org.mpi.openmind.repository.utils.OMUtils;
|
|
16
|
|
17 import cl.maps.penta.PentaKey;
|
|
18 import cl.maps.utils.RelKey;
|
|
19
|
|
20 /**
|
|
21 *
|
|
22 * @author jurzua
|
|
23 */
|
|
24 @javax.persistence.Entity
|
|
25 @DiscriminatorValue("RELATION")
|
|
26 @Cache(usage=CacheConcurrencyStrategy.READ_ONLY)
|
|
27 public class Relation extends Node implements Serializable, Comparable<Relation> {
|
|
28
|
|
29 public Relation(){
|
|
30 this.setSystemStatus(SYS_STATUS_CURRENT_VERSION);
|
|
31 }
|
|
32
|
|
33
|
|
34 public static Relation defRelation(Relation other, WrapperService ws) throws Exception{
|
|
35
|
|
36 Relation rel = new Relation();
|
|
37
|
|
38 rel.setOwnValue(other.getOwnValue());
|
|
39 rel.setObjectClass(other.getObjectClass());
|
|
40 rel.setType(other.getType());
|
|
41
|
|
42 Entity src = ws.getDefinitionById(other.getSourceId());
|
|
43 Entity tar = ws.getDefinitionById(other.getTargetId());
|
|
44 if(src == null || tar == null){
|
|
45 throw new Exception("src and/or target not found!");
|
|
46 }
|
|
47 rel.setSource(src);
|
|
48 rel.setTarget(tar);
|
|
49 return rel;
|
|
50 }
|
|
51
|
|
52 public static Relation entRelation(Relation other, WrapperService ws) throws Exception{
|
|
53
|
|
54 Relation rel = new Relation();
|
|
55
|
|
56 rel.setOwnValue(other.getOwnValue());
|
|
57 rel.setObjectClass(other.getOwnValue());
|
|
58 rel.setType(Node.TYPE_ABOX);
|
|
59
|
|
60 Entity src = ws.getEntityById(other.getSourceId());
|
|
61 Entity tar = ws.getEntityById(other.getTargetId());
|
|
62
|
|
63 if(src == null || tar == null){
|
|
64 throw new Exception("src and/or target not found!");
|
|
65 }
|
|
66
|
|
67 rel.setSource(src);
|
|
68 rel.setTarget(tar);
|
|
69 return rel;
|
|
70 }
|
|
71
|
|
72 public Relation(Entity source, Entity target){
|
|
73 this.setSource(source);
|
|
74 this.setTarget(target);
|
|
75 source.addSourceRelation(this);
|
|
76 target.addTargetRelation(this);
|
|
77 this.setSystemStatus(Node.SYS_STATUS_CURRENT_VERSION);
|
|
78 }
|
|
79
|
|
80 public Relation(Entity srcNonLW, Entity tarNonLW, String ownValue){
|
|
81 this.setSource(srcNonLW);
|
|
82 this.setTarget(tarNonLW);
|
|
83 srcNonLW.addSourceRelation(this);
|
|
84 tarNonLW.addTargetRelation(this);
|
|
85 this.setOwnValue(ownValue);
|
|
86 this.setSystemStatus(Node.SYS_STATUS_CURRENT_VERSION);
|
|
87 }
|
|
88
|
|
89 @Column(name="source_id")
|
|
90 private Long sourceId;
|
|
91
|
|
92 @Column(name="source_modif")
|
|
93 private Long sourceModif;
|
|
94
|
|
95 @Column(name="source_obj_class")
|
|
96 private String sourceObjectClass;
|
|
97
|
|
98 @Column(name="target_id")
|
|
99 private Long targetId;
|
|
100
|
|
101 @Column(name="target_modif")
|
|
102 private Long targetModif;
|
|
103
|
|
104 @Column(name="target_obj_class")
|
|
105 private String targetObjectClass;
|
|
106
|
|
107 @Transient
|
|
108 private Entity source;
|
|
109
|
|
110 @Transient
|
|
111 private Entity target;
|
|
112
|
|
113 @Transient
|
|
114 private List<Attribute> attributes = new ArrayList<Attribute>();
|
|
115
|
|
116 public Attribute getAttributeByName(String name) {
|
|
117 for (Attribute attribute : this.getAttributes()) {
|
|
118 if (attribute.getObjectClass().equals(name)) {
|
|
119 return attribute;
|
|
120 }
|
|
121 }
|
|
122 return null;
|
|
123 }
|
|
124
|
|
125 public void addAttribute(Attribute att){
|
|
126 if(att != null){
|
|
127 att.setSourceId(this.getId());
|
|
128 att.setSourceModif(this.getModificationTime());
|
|
129 att.setSystemStatus(this.getSystemStatus());
|
|
130 this.attributes.add(att);
|
|
131 }
|
|
132 }
|
|
133
|
|
134 @Override
|
|
135 public void setSystemStatus(String status) {
|
|
136 super.setSystemStatus(status);
|
|
137 if(this.attributes != null){
|
|
138 for(Attribute att : this.attributes){
|
|
139 att.setSystemStatus(status);
|
|
140 }
|
|
141 }
|
|
142
|
|
143 }
|
|
144
|
|
145 public List<Attribute> getAttributes() {
|
|
146 return attributes;
|
|
147 }
|
|
148
|
|
149 public void setAttributes(List<Attribute> attributes) {
|
|
150 this.attributes = attributes;
|
|
151 }
|
|
152
|
|
153 public String getSourceObjectClass() {
|
|
154 return sourceObjectClass;
|
|
155 }
|
|
156
|
|
157 public void setSourceObjectClass(String sourceObjectClass) {
|
|
158 this.sourceObjectClass = sourceObjectClass;
|
|
159 }
|
|
160
|
|
161 public String getTargetObjectClass() {
|
|
162 return targetObjectClass;
|
|
163 }
|
|
164
|
|
165 public void setTargetObjectClass(String targetObjectClass) {
|
|
166 this.targetObjectClass = targetObjectClass;
|
|
167 }
|
|
168
|
|
169 public Long getSourceModif() {
|
|
170 return sourceModif;
|
|
171 }
|
|
172
|
|
173 public void setSourceModif(Long sourceModif) {
|
|
174 this.sourceModif = sourceModif;
|
|
175 }
|
|
176
|
|
177 public Long getTargetModif() {
|
|
178 return targetModif;
|
|
179 }
|
|
180
|
|
181 public void setTargetModif(Long targetModif) {
|
|
182 this.targetModif = targetModif;
|
|
183 }
|
|
184
|
|
185 public Entity getSource() {
|
|
186 return source;
|
|
187 }
|
|
188
|
|
189 public void setSource(Entity src) {
|
|
190 this.source = src;
|
|
191 if(src != null){
|
|
192 this.setSourceId(src.getId());
|
|
193 this.setSourceModif(src.getModificationTime());
|
|
194 this.setSourceObjectClass(src.getObjectClass());
|
|
195 }
|
|
196 }
|
|
197
|
|
198 public Entity getTarget() {
|
|
199 return target;
|
|
200 }
|
|
201
|
|
202 public void setTarget(Entity target) {
|
|
203 this.target = target;
|
|
204 if(target != null){
|
|
205 this.setTargetId(target.getId());
|
|
206 this.setTargetModif(target.getModificationTime());
|
|
207 this.setTargetObjectClass(target.getObjectClass());
|
|
208 }
|
|
209 }
|
|
210
|
|
211
|
|
212 public Long getSourceId() {
|
|
213 return sourceId;
|
|
214 }
|
|
215
|
|
216 public void setSourceId(Long sourceId) {
|
|
217 this.sourceId = sourceId;
|
|
218 }
|
|
219
|
|
220 public Long getTargetId() {
|
|
221 return targetId;
|
|
222 }
|
|
223
|
|
224 public void setTargetId(Long targetId) {
|
|
225 this.targetId = targetId;
|
|
226 }
|
|
227
|
|
228 public boolean equalsContent(Relation other){
|
|
229 if(other == null){
|
|
230 return false;
|
|
231 }
|
|
232
|
|
233 if(StringUtils.equals(getOwnValue(), other.getOwnValue()) &&
|
|
234 OMUtils.equals(getSourceId(), other.getSourceId()) &&
|
|
235 OMUtils.equals(getTargetId(), other.getTargetId())){
|
|
236 return true;
|
|
237 }
|
|
238
|
|
239 return false;
|
|
240 }
|
|
241
|
|
242
|
|
243 @Override
|
|
244 public String toString() {
|
|
245 String source = ", Src[id=" + sourceId;
|
|
246 if(getSource() != null)
|
|
247 source += ", src=" + getSource().getOwnValue();
|
|
248 source +="] ";
|
|
249 String target = ", Tar[id=" + targetId;
|
|
250 if(getTarget() != null)
|
|
251 target += ", tar=" + getTarget().getOwnValue();
|
|
252 target +="] ";
|
|
253 return "Relation[" +
|
|
254 "rowId=" + this.getRowId() + ", " +
|
|
255 "id=" + getId() +", " +
|
|
256 "ownValue=" + getOwnValue() + source + target + ", " +
|
|
257 "sysStatus=" + this.getSystemStatus() + "]";
|
|
258 }
|
|
259
|
|
260 @Override
|
|
261 public int compareTo(Relation e) {
|
|
262 if(e == null)
|
|
263 return 1;
|
|
264 if(StringUtils.isNotEmpty(this.getOwnValue()))
|
|
265 return this.getOwnValue().compareTo(e.getOwnValue());
|
|
266 else return 0;
|
|
267 }
|
|
268
|
|
269 public PentaKey<RelKey, Long, Long, String, Long> getKey(){
|
|
270 return new PentaKey<RelKey, Long, Long, String, Long>(
|
|
271 new RelKey(this.getSourceId(), this.getTargetId(), this.getOwnValue()),
|
|
272 this.getSourceId(),
|
|
273 this.getTargetId(),
|
|
274 this.getOwnValue(),
|
|
275 this.getId());
|
|
276 }
|
|
277 }
|