Salome HOME
Mapping is fixed. Relation in the mapping is inherited from Persistent now. Versionin...
[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 = 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       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       if (dprop.uses != null) for (int i=0; i<dprop.uses.length; i++) getUses().add(dprop.uses[i]);
93     }
94
95 //  ==============================================================================================================================
96 //  Public member functions
97 //  ==============================================================================================================================
98
99     public boolean equals(Object entity) {
100 //  ------------------------------------
101       if (entity == null) return false;
102       if (entity instanceof String) {
103         return this.name.equals((String)entity);   // Names are unique
104       } else
105       if (entity instanceof DocumentType) {
106         DocumentType object = (DocumentType)entity;
107         long   he = object.getIndex();
108         long   me = this.getIndex();
109         if (me*he != 0) return (he == me);
110         else            return this.getName().equals(object.getName());
111       } else {
112         return false;
113       }
114     }
115
116     public String getName () {
117 //  ------------------------
118       return name;
119     }
120
121     public Set<DocumentType> getDefaultUses () {
122 //  -------------------------------------------
123       return getUses();
124     }
125
126     public boolean isApproved () {
127 //  ----------------------------
128       return (state == ProgressState.APPROVED);
129     }
130
131 /**
132  * Checks if documents of this type are attached to the given study step, either as result or content.
133  * 
134  * @param  step the involved study step
135  * @return true if documents of this type are attached to the given step.
136  * @see    #isResultOf(org.splat.service.technical.ProjectSettingsServiceImpl.Step)
137  */
138     public boolean isContentInto (ProjectSettingsService.Step step) {
139 //  --------------------------------------------------------
140       String[] path = this.step.split("-");
141       for (int i=0; i<path.length; i++) {
142         String value = path[i];
143         if (value.length() == 0) continue;
144         if (Integer.valueOf(value) == step.getNumber()) return true;
145       }
146       return false;
147     }
148
149 /**
150  * Checks if documents of this type are result of any study step.
151  * 
152  * @return true if documents of this type are result of a step.
153  * @see    #isStudyResult()
154  * @see    #isResultOf(org.splat.service.technical.ProjectSettingsServiceImpl.Step)
155  */
156     public boolean isStepResult () {
157 //  ------------------------------
158       return (result != null);
159     }
160
161 /**
162  * Checks if documents of this type are result of the given study step.
163  * 
164  * @param  step the involved study step
165  * @return true if documents of this type are result of the given step.
166  * @see    #isContentInto(org.splat.service.technical.ProjectSettingsServiceImpl.Step)
167  * @see    #isStepResult()
168  * @see    #isStudyResult()
169  */
170     public boolean isResultOf (ProjectSettingsService.Step step) {
171 //  -----------------------------------------------------
172       if (result == null) return false;
173       return (Integer.valueOf(result) == step.getNumber());
174     }
175     
176     /**
177          * Get the state.
178          * @return the state
179          */
180         public ProgressState getState() {
181                 return state;
182         }
183         /**
184          * Set the state.
185          * @param state the state to set
186          */
187         public void setState(ProgressState state) {
188                 this.state = state;
189         }
190         /**
191          * Get the uses.
192          * @return the uses
193          */
194         protected Set<DocumentType> getUses() {
195                 return uses;
196         }
197 }