Salome HOME
d86176c0a018ab574c305764532524189d0238a2
[tools/siman.git] / Workspace / Siman-Common / src / org / splat / dal / bo / som / DocumentType.java
1 package org.splat.dal.bo.som;
2
3 /**
4  * 
5  * @author    Daniel Brunier-Coulin
6  * @copyright OPEN CASCADE 2012
7  */
8
9 import java.util.Arrays;
10 import java.util.HashSet;
11 import java.util.Set;
12
13 import org.splat.dal.bo.kernel.Persistent;
14 import org.splat.kernel.InvalidPropertyException;
15 import org.splat.kernel.MissedPropertyException;
16 import org.splat.kernel.MultiplyDefinedException;
17 import org.splat.service.technical.ProjectSettingsService;
18
19 /**
20  * Persistent document type.
21  */
22 public class DocumentType extends Persistent {
23
24         // Persistent fields
25         /**
26          * Type name.
27          */
28         private String name;
29         /**
30          * Type progress state.
31          */
32         private ProgressState state;
33         /**
34          * List of (dash separated) steps (numbers) containing this type.
35          */
36         private String step;
37         /**
38          * Step (number) having this type as result.
39          */
40         private String result;
41         /**
42          * Set of "parent" document types, which are used by this type.
43          */
44         private final Set<DocumentType> uses = new HashSet<DocumentType>();
45
46         // ==============================================================================================================================
47         // Construction
48         // ==============================================================================================================================
49
50         /**
51          * Fields initialization class.
52          */
53         public static class Properties extends Persistent.Properties {
54                 /**
55                  * {@inheritDoc}
56                  * 
57                  * @see DocumentType#name
58                  */
59                 private String name = null;
60                 /**
61                  * {@inheritDoc}
62                  * 
63                  * @see DocumentType#step
64                  */
65                 private String step = null;
66                 /**
67                  * {@inheritDoc}
68                  * 
69                  * @see DocumentType#result
70                  */
71                 private String result = null;
72                 /**
73                  * {@inheritDoc}
74                  * 
75                  * @see DocumentType#uses
76                  */
77                 private DocumentType[] uses = null;
78
79                 // - Public services
80
81                 /**
82                  * {@inheritDoc}
83                  * 
84                  * @see org.splat.dal.bo.kernel.Persistent.Properties#clear()
85                  */
86                 @Override
87                 public void clear() {
88                         super.clear();
89                         name = null;
90                         step = null;
91                         result = null;
92                         uses = null;
93                 }
94
95                 // - Setters of DocumentType properties
96
97                 public Properties setName(final String name)
98                                 throws InvalidPropertyException {
99                         if (name.length() == 0) {
100                                 throw new InvalidPropertyException("name");
101                         }
102                         this.name = name;
103                         return this;
104                 }
105
106                 public Properties setResult(final ProjectSettingsService.Step step) {
107                         this.result = String.valueOf(step.getNumber());
108                         return this;
109                 }
110
111                 public Properties setStep(final ProjectSettingsService.Step... step) {
112                         this.step = "-";
113                         for (int i = 0; i < step.length; i++) {
114                                 this.step = this.step + String.valueOf(step[i].getNumber())
115                                                 + "-";
116                         }
117                         return this;
118                 }
119
120                 public Properties setUses(final DocumentType... type) {
121                         this.uses = type;
122                         return this;
123                 }
124
125                 // - Global validity check
126
127                 public void checkValidity() throws MissedPropertyException,
128                                 InvalidPropertyException, MultiplyDefinedException {
129                         if (name == null) {
130                                 throw new MissedPropertyException("name");
131                         }
132                         if (step == null) {
133                                 throw new MissedPropertyException("path");
134                         }
135                 }
136
137                 /**
138                  * Get the name.
139                  * 
140                  * @return the name
141                  */
142                 public String getName() {
143                         return name;
144                 }
145         }
146
147         /**
148          * Database fetch constructor.
149          */
150         protected DocumentType() {
151                 super();
152         }
153
154         /**
155          * Initialization constructor.
156          * 
157          * @param dprop
158          *            type properties
159          * @throws MissedPropertyException
160          *             if some mandatory property is missed
161          * @throws InvalidPropertyException
162          *             if some property has invalid value
163          * @throws MultiplyDefinedException
164          *             if some property is defined several times
165          */
166         public DocumentType(final Properties dprop) throws MissedPropertyException,
167                         InvalidPropertyException, MultiplyDefinedException {
168                 super(dprop); // Throws one of the above exception if not valid
169                 name = dprop.name;
170                 state = ProgressState.inCHECK;
171                 step = dprop.step;
172                 result = dprop.result; // May be null
173                 if (dprop.uses != null) {
174                         for (int i = 0; i < dprop.uses.length; i++) {
175                                 getUses().add(dprop.uses[i]);
176                         }
177                 }
178         }
179
180         // ==============================================================================================================================
181         // Public member functions
182         // ==============================================================================================================================
183
184         @Override
185         public boolean equals(final Object entity) {
186                 if (entity == null) {
187                         return false;
188                 }
189                 if (entity instanceof String) {
190                         return this.name.equals(entity); // Names are unique
191                 } else if (entity instanceof DocumentType) {
192                         DocumentType object = (DocumentType) entity;
193                         long he = object.getIndex();
194                         long me = this.getIndex();
195                         if (me * he == 0) {
196                                 return this.getName().equals(object.getName());
197                         } else {
198                                 return (he == me);
199                         }
200                 } else {
201                         return false;
202                 }
203         }
204
205         public String getName() {
206                 return name;
207         }
208
209         public Set<DocumentType> getDefaultUses() {
210                 return getUses();
211         }
212
213         public boolean isApproved() {
214                 return (state == ProgressState.APPROVED);
215         }
216
217         /**
218          * Checks if documents of this type are attached to the given study step, either as result or content.
219          * 
220          * @param step
221          *            the involved study step
222          * @return true if documents of this type are attached to the given step.
223          * @see #isResultOf(org.splat.service.technical.ProjectSettingsServiceImpl.Step)
224          */
225         public boolean isContentInto(final ProjectSettingsService.Step step) {
226                 String[] path = this.step.split("-");
227                 for (int i = 0; i < path.length; i++) {
228                         String value = path[i];
229                         if (value.length() == 0) {
230                                 continue;
231                         }
232                         if (Integer.valueOf(value) == step.getNumber()) {
233                                 return true;
234                         }
235                 }
236                 return false;
237         }
238
239         /**
240          * Checks if documents of this type are result of any study step.
241          * 
242          * @return true if documents of this type are result of a step.
243          * @see #isStudyResult()
244          * @see #isResultOf(org.splat.service.technical.ProjectSettingsServiceImpl.Step)
245          */
246         public boolean isStepResult() {
247                 return (result != null);
248         }
249
250         /**
251          * Checks if documents of this type are result of the given study step.
252          * 
253          * @param step
254          *            the involved study step
255          * @return true if documents of this type are result of the given step.
256          * @see #isContentInto(org.splat.service.technical.ProjectSettingsServiceImpl.Step)
257          * @see #isStepResult()
258          * @see #isStudyResult()
259          */
260         public boolean isResultOf(final ProjectSettingsService.Step step) {
261                 if (result == null) {
262                         return false;
263                 }
264                 return (Integer.valueOf(result) == step.getNumber());
265         }
266
267         /**
268          * Get the state.
269          * 
270          * @return the state
271          */
272         public ProgressState getState() {
273                 return state;
274         }
275
276         /**
277          * Set the state.
278          * 
279          * @param state
280          *            the state to set
281          */
282         public void setState(final ProgressState state) {
283                 this.state = state;
284         }
285
286         /**
287          * Get the uses.
288          * 
289          * @return the uses
290          */
291         protected Set<DocumentType> getUses() {
292                 return uses;
293         }
294
295         /**
296          * Set properties of document type except persistent id and name.
297          * 
298          * @param tprop
299          *            type properties
300          */
301         public void setProperties(final Properties tprop) {
302                 this.step = tprop.step;
303                 this.result = tprop.result;
304                 this.uses.clear();
305                 if (tprop.uses != null) {
306                         this.uses.addAll(Arrays.asList(tprop.uses));
307                 }
308         }
309 }