Salome HOME
Modifications done to respect PMD rules. Versioning a document is fixed. Validation...
[tools/siman.git] / Workspace / Siman-Common / src / org / splat / dal / bo / som / DocumentType.java
1 package org.splat.dal.bo.som;
2 /**
3  * 
4  * @author    Daniel Brunier-Coulin
5  * @copyright OPEN CASCADE 2012
6  */
7
8 import java.util.HashSet;
9 import java.util.Set;
10
11 import org.splat.dal.bo.kernel.Persistent;
12 import org.splat.kernel.InvalidPropertyException;
13 import org.splat.kernel.MissedPropertyException;
14 import org.splat.kernel.MultiplyDefinedException;
15 import org.splat.service.technical.ProjectSettingsService;
16
17
18 public class DocumentType extends Persistent {
19         
20 //  Persistent fields
21     private String            name;
22     private ProgressState     state;
23         private String            step;     // List of (dash separated) steps (numbers) containing this type
24     private String            result;   // Step (number ) having this type as result
25     private final Set<DocumentType> uses = new HashSet<DocumentType>();
26
27 //  ==============================================================================================================================
28 //  Construction
29 //  ==============================================================================================================================
30
31 //  Fields initialization class
32     public static class Properties extends Persistent.Properties {
33 //  ------------------------------------------------------------
34       private String         name   = null;
35       private String         step   = null;
36       private String         result = null;
37       private DocumentType[] uses   = null;
38
39 //  - Public services
40
41       @Override
42         public void clear () {
43         super.clear();
44         name    = null;
45         step    = null;
46         result  = null;
47         uses    = null;
48       }
49 //  - Setters of DocumentType properties
50       
51       public Properties setName (final String name) throws InvalidPropertyException
52       {
53         if (name.length() == 0) {
54                         throw new InvalidPropertyException("name");
55                 }
56         this.name = name;
57         return this;
58       }
59       public Properties setResult (final ProjectSettingsService.Step step)
60       {
61         this.result = String.valueOf(step.getNumber());
62         return this;
63       }
64       public Properties setStep (final ProjectSettingsService.Step... step)
65       {
66         this.step = "-";
67         for (int i=0; i<step.length; i++) {
68                         this.step = this.step + String.valueOf(step[i].getNumber()) + "-";
69                 }
70         return this;
71       }
72       public Properties setUses (final DocumentType... type)
73       {
74         this.uses = type;
75         return this;
76       }
77 //  - Global validity check
78         
79       public void checkValidity() throws MissedPropertyException, InvalidPropertyException, MultiplyDefinedException
80       {
81         if (name == null) {
82                         throw new MissedPropertyException("name");
83                 }
84         if (step == null) {
85                         throw new MissedPropertyException("path");
86                 }
87       }
88     }
89 //  Database fetch constructor
90     protected DocumentType () {
91 //  -------------------------
92     }
93 //  Initialization constructor
94     public DocumentType (final Properties dprop) throws MissedPropertyException, InvalidPropertyException, MultiplyDefinedException {
95 //  -----------------------------------------
96       super(dprop);              // Throws one of the above exception if not valid
97       name   = dprop.name;
98       state  = ProgressState.inCHECK;
99       step   = dprop.step;
100       result = dprop.result;     // May be null
101       if (dprop.uses != null) {
102                 for (int i=0; i<dprop.uses.length; i++) {
103                         getUses().add(dprop.uses[i]);
104                 }
105         }
106     }
107
108 //  ==============================================================================================================================
109 //  Public member functions
110 //  ==============================================================================================================================
111
112     @Override
113         public boolean equals(final Object entity) {
114 //  ------------------------------------
115       if (entity == null) {
116                 return false;
117         }
118       if (entity instanceof String) {
119         return this.name.equals(entity);   // Names are unique
120       } else if (entity instanceof DocumentType) {
121         DocumentType object = (DocumentType)entity;
122         long   he = object.getIndex();
123         long   me = this.getIndex();
124         if (me*he != 0) {
125                 return (he == me);
126         } else {
127                 return this.getName().equals(object.getName());
128         }
129       } else {
130         return false;
131       }
132     }
133
134     public String getName () {
135 //  ------------------------
136       return name;
137     }
138
139     public Set<DocumentType> getDefaultUses () {
140 //  -------------------------------------------
141       return getUses();
142     }
143
144     public boolean isApproved () {
145 //  ----------------------------
146       return (state == ProgressState.APPROVED);
147     }
148
149 /**
150  * Checks if documents of this type are attached to the given study step, either as result or content.
151  * 
152  * @param  step the involved study step
153  * @return true if documents of this type are attached to the given step.
154  * @see    #isResultOf(org.splat.service.technical.ProjectSettingsServiceImpl.Step)
155  */
156     public boolean isContentInto (final ProjectSettingsService.Step step) {
157 //  --------------------------------------------------------
158       String[] path = this.step.split("-");
159       for (int i=0; i<path.length; i++) {
160         String value = path[i];
161         if (value.length() == 0) {
162                         continue;
163                 }
164         if (Integer.valueOf(value) == step.getNumber()) {
165                         return true;
166                 }
167       }
168       return false;
169     }
170
171 /**
172  * Checks if documents of this type are result of any study step.
173  * 
174  * @return true if documents of this type are result of a step.
175  * @see    #isStudyResult()
176  * @see    #isResultOf(org.splat.service.technical.ProjectSettingsServiceImpl.Step)
177  */
178     public boolean isStepResult () {
179 //  ------------------------------
180       return (result != null);
181     }
182
183 /**
184  * Checks if documents of this type are result of the given study step.
185  * 
186  * @param  step the involved study step
187  * @return true if documents of this type are result of the given step.
188  * @see    #isContentInto(org.splat.service.technical.ProjectSettingsServiceImpl.Step)
189  * @see    #isStepResult()
190  * @see    #isStudyResult()
191  */
192     public boolean isResultOf (final ProjectSettingsService.Step step) {
193 //  -----------------------------------------------------
194       if (result == null) {
195                 return false;
196         }
197       return (Integer.valueOf(result) == step.getNumber());
198     }
199     
200     /**
201          * Get the state.
202          * @return the state
203          */
204         public ProgressState getState() {
205                 return state;
206         }
207         /**
208          * Set the state.
209          * @param state the state to set
210          */
211         public void setState(final ProgressState state) {
212                 this.state = state;
213         }
214         /**
215          * Get the uses.
216          * @return the uses
217          */
218         protected Set<DocumentType> getUses() {
219                 return uses;
220         }
221 }