1
2
3
4
5
6
7 package ch.colabproject.colab.api.model.document;
8
9 import ch.colabproject.colab.api.exceptions.ColabMergeException;
10 import ch.colabproject.colab.api.model.ColabEntity;
11 import ch.colabproject.colab.api.model.tools.EntityHelper;
12 import java.util.ArrayList;
13 import java.util.List;
14 import javax.json.bind.annotation.JsonbTransient;
15 import javax.persistence.DiscriminatorValue;
16 import javax.persistence.Entity;
17 import javax.persistence.FetchType;
18 import javax.persistence.Index;
19 import javax.persistence.ManyToOne;
20 import javax.persistence.NamedQuery;
21 import javax.persistence.Table;
22 import javax.persistence.Transient;
23 import javax.validation.constraints.NotNull;
24
25
26
27
28
29
30
31
32
33
34
35
36
37 @Entity
38 @Table(
39 indexes = {
40 @Index(columnList = "target_id"),
41 }
42 )
43 @DiscriminatorValue("RESOURCE_REF")
44 @NamedQuery(name = "ResourceRef.findDirectReferences",
45 query = "SELECT ref FROM ResourceRef ref "
46 + "WHERE ref.target IS NOT NULL AND ref.target.id = :targetId")
47 public class ResourceRef extends AbstractResource {
48
49 private static final long serialVersionUID = 1L;
50
51
52
53
54
55
56
57
58
59 @NotNull
60 private boolean refused;
61
62
63
64
65
66
67
68
69
70
71 @NotNull
72 private boolean residual;
73
74
75
76
77 @ManyToOne(fetch = FetchType.LAZY)
78 @NotNull
79 @JsonbTransient
80 private AbstractResource target;
81
82
83
84
85 @Transient
86 private Long targetId;
87
88
89
90
91
92
93
94
95
96 public boolean isRefused() {
97 return refused;
98 }
99
100
101
102
103
104 public void setRefused(boolean refused) {
105 this.refused = refused;
106 }
107
108
109
110
111
112 public boolean isResidual() {
113 return residual;
114 }
115
116
117
118
119
120 public void setResidual(boolean residual) {
121 this.residual = residual;
122 }
123
124
125
126
127 public AbstractResource getTarget() {
128 return target;
129 }
130
131
132
133
134 public void setTarget(AbstractResource target) {
135 this.target = target;
136 }
137
138
139
140
141
142
143
144 public Long getTargetId() {
145 if (target != null) {
146 return target.getId();
147 } else {
148 return targetId;
149 }
150 }
151
152
153
154
155
156
157
158 public void setTargetId(Long targetId) {
159 this.targetId = targetId;
160 }
161
162
163
164
165
166 @Override
167 public Resource resolve() {
168 if (this.target != null) {
169 return this.target.resolve();
170 }
171
172 return null;
173 }
174
175 @Override
176 public List<AbstractResource> expand() {
177 List<AbstractResource> list = new ArrayList<>();
178
179 list.add(this);
180 if (this.target != null) {
181 list.addAll(this.target.expand());
182 }
183 return list;
184 }
185
186
187
188
189
190 @Override
191 public void mergeToUpdate(ColabEntity other) throws ColabMergeException {
192 super.mergeToUpdate(other);
193
194
195
196
197 if (!(other instanceof ResourceRef)) {
198 throw new ColabMergeException(this, other);
199 }
200 }
201
202 @Override
203 public void mergeToDuplicate(ColabEntity other) throws ColabMergeException {
204 super.mergeToDuplicate(other);
205
206 if (other instanceof ResourceRef) {
207 ResourceRef o = (ResourceRef) other;
208 this.setRefused(o.isRefused());
209 this.setResidual(o.isResidual());
210 } else {
211 throw new ColabMergeException(this, other);
212 }
213 }
214
215 @Override
216 public int hashCode() {
217 return EntityHelper.hashCode(this);
218 }
219
220 @Override
221 @SuppressWarnings("EqualsWhichDoesntCheckParameterClass")
222 public boolean equals(Object obj) {
223 return EntityHelper.equals(this, obj);
224 }
225
226 @Override
227 public String toString() {
228 return "ResourceRef{" + toPartialString() + ", refused=" + refused
229 + ", residual=" + residual + ", targetId=" + targetId + "}";
230 }
231
232 }