Salome HOME
Refactoring continues: UserService is created instead of UserDirectory. Database...
[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.Set;
9 import java.util.HashSet;
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 Set<DocumentType> uses;
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       public void clear () {
42         super.clear();
43         name    = null;
44         step    = null;
45         result  = null;
46         uses    = null;
47       }
48 //  - Setters of DocumentType properties
49       
50       public Properties setName (String name) throws InvalidPropertyException
51       {
52         if (name.length() == 0) throw new InvalidPropertyException("name");
53         this.name = name;
54         return this;
55       }
56       public Properties setResult (ProjectSettingsService.Step step)
57       {
58         this.result = String.valueOf(step.getNumber());
59         return this;
60       }
61       public Properties setStep (ProjectSettingsService.Step... step)
62       {
63         this.step = "-";
64         for (int i=0; i<step.length; i++) this.step = this.step + String.valueOf(step[i].getNumber()) + "-";
65         return this;
66       }
67       public Properties setUses (DocumentType... type)
68       {
69         this.uses = type;
70         return this;
71       }
72 //  - Global validity check
73         
74       public void checkValidity() throws MissedPropertyException, InvalidPropertyException, MultiplyDefinedException
75       {
76         if (name == null)  throw new MissedPropertyException("name");
77         if (step == null)  throw new MissedPropertyException("path");
78       }
79     }
80 //  Database fetch constructor
81     protected DocumentType () {
82 //  -------------------------
83     }
84 //  Initialization constructor
85     public DocumentType (Properties dprop) throws MissedPropertyException, InvalidPropertyException, MultiplyDefinedException {
86 //  -----------------------------------------
87       super(dprop);              // Throws one of the above exception if not valid
88       name   = dprop.name;
89       state  = ProgressState.inCHECK;
90       step   = dprop.step;
91       result = dprop.result;     // May be null
92       uses   = new HashSet<DocumentType>();
93       if (dprop.uses != null) for (int i=0; i<dprop.uses.length; i++) uses.add(dprop.uses[i]);
94     }
95
96 //  ==============================================================================================================================
97 //  Public member functions
98 //  ==============================================================================================================================
99
100     public boolean equals(Object entity) {
101 //  ------------------------------------
102       if (entity == null) return false;
103       if (entity instanceof String) {
104         return this.name.equals((String)entity);   // Names are unique
105       } else
106       if (entity instanceof DocumentType) {
107         DocumentType object = (DocumentType)entity;
108         long   he = object.getIndex();
109         long   me = this.getIndex();
110         if (me*he != 0) return (he == me);
111         else            return this.getName().equals(object.getName());
112       } else {
113         return false;
114       }
115     }
116
117     public String getName () {
118 //  ------------------------
119       return name;
120     }
121
122     public Set<DocumentType> getDefaultUses () {
123 //  -------------------------------------------
124       return uses;
125     }
126
127     public boolean isApproved () {
128 //  ----------------------------
129       return (state == ProgressState.APPROVED);
130     }
131
132 /**
133  * Checks if documents of this type are attached to the given study step, either as result or content.
134  * 
135  * @param  step the involved study step
136  * @return true if documents of this type are attached to the given step.
137  * @see    #isResultOf(org.splat.service.technical.ProjectSettingsServiceImpl.Step)
138  */
139     public boolean isContentInto (ProjectSettingsService.Step step) {
140 //  --------------------------------------------------------
141       String[] path = this.step.split("-");
142       for (int i=0; i<path.length; i++) {
143         String value = path[i];
144         if (value.length() == 0) continue;
145         if (Integer.valueOf(value) == step.getNumber()) return true;
146       }
147       return false;
148     }
149
150 /**
151  * Checks if documents of this type are result of any study step.
152  * 
153  * @return true if documents of this type are result of a step.
154  * @see    #isStudyResult()
155  * @see    #isResultOf(org.splat.service.technical.ProjectSettingsServiceImpl.Step)
156  */
157     public boolean isStepResult () {
158 //  ------------------------------
159       return (result != null);
160     }
161
162 /**
163  * Checks if documents of this type are result of the given study step.
164  * 
165  * @param  step the involved study step
166  * @return true if documents of this type are result of the given step.
167  * @see    #isContentInto(org.splat.service.technical.ProjectSettingsServiceImpl.Step)
168  * @see    #isStepResult()
169  * @see    #isStudyResult()
170  */
171     public boolean isResultOf (ProjectSettingsService.Step step) {
172 //  -----------------------------------------------------
173       if (result == null) return false;
174       return (Integer.valueOf(result) == step.getNumber());
175     }
176     
177     /**
178          * Get the state.
179          * @return the state
180          */
181         public ProgressState getState() {
182                 return state;
183         }
184         /**
185          * Set the state.
186          * @param state the state to set
187          */
188         public void setState(ProgressState state) {
189                 this.state = state;
190         }
191 }