Salome HOME
6b83936e84ea8b541941c1ee1dfedf40fe43dc4d
[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.List;
9 import java.util.Set;
10 import java.util.HashSet;
11
12 import org.splat.dal.bo.kernel.Persistent;
13 import org.splat.dal.dao.som.Database;
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 import org.splat.service.technical.ProjectSettingsService.Step;
19
20
21 public class DocumentType extends Persistent {
22         
23 //  Persistent fields
24     private String            name;
25     private ProgressState     state;
26     private String            step;     // List of (dash separated) steps (numbers) containing this type
27     private String            result;   // Step (number ) having this type as result
28     private Set<DocumentType> uses;
29
30 //  ==============================================================================================================================
31 //  Construction
32 //  ==============================================================================================================================
33
34 //  Fields initialization class
35     public static class Properties extends Persistent.Properties {
36 //  ------------------------------------------------------------
37       private String         name   = null;
38       private String         step   = null;
39       private String         result = null;
40       private DocumentType[] uses   = null;
41
42 //  - Public services
43
44       public void clear () {
45         super.clear();
46         name    = null;
47         step    = null;
48         result  = null;
49         uses    = null;
50       }
51 //  - Setters of DocumentType properties
52       
53       public Properties setName (String name) throws InvalidPropertyException
54       {
55         if (name.length() == 0) throw new InvalidPropertyException("name");
56         this.name = name;
57         return this;
58       }
59       public Properties setResult (ProjectSettingsService.Step step)
60       {
61         this.result = String.valueOf(step.getNumber());
62         return this;
63       }
64       public Properties setStep (ProjectSettingsService.Step... step)
65       {
66         this.step = "-";
67         for (int i=0; i<step.length; i++) this.step = this.step + String.valueOf(step[i].getNumber()) + "-";
68         return this;
69       }
70       public Properties setUses (DocumentType... type)
71       {
72         this.uses = type;
73         return this;
74       }
75 //  - Global validity check
76         
77       public void checkValidity() throws MissedPropertyException, InvalidPropertyException, MultiplyDefinedException
78       {
79         if (name == null)  throw new MissedPropertyException("name");
80         if (step == null)  throw new MissedPropertyException("path");
81       }
82     }
83 //  Database fetch constructor
84     protected DocumentType () {
85 //  -------------------------
86     }
87 //  Initialization constructor
88     protected DocumentType (Properties dprop) throws MissedPropertyException, InvalidPropertyException, MultiplyDefinedException {
89 //  -----------------------------------------
90       super(dprop);              // Throws one of the above exception if not valid
91       name   = dprop.name;
92       state  = ProgressState.inCHECK;
93       step   = dprop.step;
94       result = dprop.result;     // May be null
95       uses   = new HashSet<DocumentType>();
96       if (dprop.uses != null) for (int i=0; i<dprop.uses.length; i++) uses.add(dprop.uses[i]);
97     }
98
99 //  ==============================================================================================================================
100 //  Public member functions
101 //  ==============================================================================================================================
102
103     public boolean approve () {
104 //  -------------------------
105       if  (state != ProgressState.inCHECK) return false;      
106       this.state =  ProgressState.APPROVED;        // The type name is supposed being localized
107       Database.getSession().update(this);
108           return true;
109     }
110
111     public boolean equals(Object entity) {
112 //  ------------------------------------
113       if (entity == null) return false;
114       if (entity instanceof String) {
115         return this.name.equals((String)entity);   // Names are unique
116       } else
117       if (entity instanceof DocumentType) {
118         DocumentType object = (DocumentType)entity;
119         long   he = object.getIndex();
120         long   me = this.getIndex();
121         if (me*he != 0) return (he == me);
122         else            return this.getName().equals(object.getName());
123       } else {
124         return false;
125       }
126     }
127
128     public String getName () {
129 //  ------------------------
130       return name;
131     }
132
133     public Set<DocumentType> getDefaultUses () {
134 //  -------------------------------------------
135       return uses;
136     }
137
138     public boolean isApproved () {
139 //  ----------------------------
140       return (state == ProgressState.APPROVED);
141     }
142
143 /**
144  * Checks if documents of this type are attached to the given study step, either as result or content.
145  * 
146  * @param  step the involved study step
147  * @return true if documents of this type are attached to the given step.
148  * @see    #isResultOf(org.splat.service.technical.ProjectSettingsServiceImpl.Step)
149  */
150     public boolean isContentInto (ProjectSettingsService.Step step) {
151 //  --------------------------------------------------------
152       String[] path = this.step.split("-");
153       for (int i=0; i<path.length; i++) {
154         String value = path[i];
155         if (value.length() == 0) continue;
156         if (Integer.valueOf(value) == step.getNumber()) return true;
157       }
158       return false;
159     }
160
161 /**
162  * Checks if documents of this type are result of any study step.
163  * 
164  * @return true if documents of this type are result of a step.
165  * @see    #isStudyResult()
166  * @see    #isResultOf(org.splat.service.technical.ProjectSettingsServiceImpl.Step)
167  */
168     public boolean isStepResult () {
169 //  ------------------------------
170       return (result != null);
171     }
172
173 /**
174  * Checks if documents of this type are result of the given study step.
175  * 
176  * @param  step the involved study step
177  * @return true if documents of this type are result of the given step.
178  * @see    #isContentInto(org.splat.service.technical.ProjectSettingsServiceImpl.Step)
179  * @see    #isStepResult()
180  * @see    #isStudyResult()
181  */
182     public boolean isResultOf (ProjectSettingsService.Step step) {
183 //  -----------------------------------------------------
184       if (result == null) return false;
185       return (Integer.valueOf(result) == step.getNumber());
186     }
187 }