Salome HOME
Copyrights update 2015.
[tools/siman.git] / Workspace / Siman-Common / src / org / splat / dal / bo / som / KnowledgeElement.java
1 package org.splat.dal.bo.som;
2 /**
3  * 
4  * @author    Daniel Brunier-Coulin
5  * @copyright OPEN CASCADE 2012-2015
6  */
7
8 import java.text.DecimalFormat;
9 import java.util.Calendar;
10 import java.util.Date;
11 import java.util.List;
12 import java.util.Vector;
13
14 import org.splat.dal.bo.kernel.Persistent;
15 import org.splat.dal.bo.kernel.User;
16 import org.splat.kernel.InvalidPropertyException;
17 import org.splat.kernel.MissedPropertyException;
18 import org.splat.kernel.MultiplyDefinedException;
19
20
21 public class KnowledgeElement extends Persistent {
22
23         private KnowledgeElementType  type;     // User extendable types
24         private Scenario              owner;
25     private ProgressState         state;
26     private String                title;
27     private String                value;
28     private User                  author;
29     private Date                  date;
30
31 //  ==============================================================================================================================
32 //  Construction
33 //  ==============================================================================================================================
34
35 //  Fields initialization class
36     public static class Properties extends Persistent.Properties {
37 //  ------------------------------------------------------------
38       private String                  kid        = null;                             // Search criterion only
39       private KnowledgeElementType    type       = null;
40       private Scenario                owner      = null;
41       private Visibility              visibility = null;                             // Search criterion only
42       private ProgressState           state      = null;
43       private String                  title      = null;
44       private String                  value      = null;
45       private User                    author     = null;
46       private User                    actor      = null;                             // Search criterion only
47       private Date                    date       = null;
48       private List<SimulationContext> context = new Vector<SimulationContext>();     // Search criterion only
49
50 //  - Public services
51
52       public void clear () {
53         super.clear();
54         kid        = null;
55         type       = null;
56         owner      = null;
57         visibility = null;
58         state      = null;
59         title      = null;
60         value      = null;
61         author     = null;
62         actor      = null;
63         date       = null;
64         context    = new Vector<SimulationContext>();  // as clear() may generate side effects
65       }
66       public Properties copy () {
67         Properties copy = new Properties();
68         copy.kid        = this.kid;
69         copy.type       = this.type;
70         copy.owner      = this.owner;
71         copy.visibility = this.visibility;
72         copy.state      = this.state;
73         copy.title      = this.title;
74         copy.value      = this.value;
75         copy.author     = this.author;
76         copy.actor      = this.actor;
77         copy.date       = this.date;
78         copy.context    = this.context;
79         return copy;
80       }
81 //  - Protected services
82
83       public User getActor () {
84         return actor;
85       }
86       public User getAuthor () {
87             return author;
88           }
89       public ProgressState getProgressState () {
90         return state;
91       }
92       public String getReference () {
93         return kid;
94       }
95       public List<SimulationContext> getSimulationContexts () {
96         return context;
97       }
98       public String getTitle () {
99         return title;
100       }      
101       public KnowledgeElementType getType () {
102         return type;
103       }
104       public Visibility getVisibility () {
105         return visibility;
106       }
107 //  - Property setters
108
109 //    For building a search query
110       public Properties setActor (User actor)
111       {
112         this.actor = actor;
113         return this;
114       }
115       public Properties setAuthor (User user)
116       {
117         this.author = user;
118         return this;
119       }
120       public Properties setDate (Date date)
121       {
122         this.date = date;
123         return this;
124       }
125       public Properties setOwnerScenario (Scenario owner)
126       {
127         this.owner = owner;
128         return this;
129       }
130 //    For building a search query
131       public Properties setReference (String kid) throws InvalidPropertyException
132       {
133         if (kid.length() == 0) throw new InvalidPropertyException("reference");
134         this.kid = kid;
135         return this;
136       }
137 //    For building a search query
138       public Properties setSimulationContexts (List<SimulationContext> context) {
139         this.context = context;
140         return this;
141       }
142       public Properties setState (ProgressState state) throws InvalidPropertyException
143       {
144         if (state != ProgressState.inWORK && state != ProgressState.inDRAFT && state != ProgressState.inCHECK && state != ProgressState.APPROVED) {
145           throw new InvalidPropertyException("state");
146         }
147         this.state = state;
148         return this;
149       }
150       public Properties setTitle (String title) throws InvalidPropertyException
151       {
152         if (title.length() == 0) throw new InvalidPropertyException("title");
153         this.title = title;
154         return this;
155       }
156       public Properties setType (KnowledgeElementType type)
157       {
158         this.type = type;
159         return this;
160       }
161       public Properties setValue (String value) throws InvalidPropertyException
162       {
163         if (value.length() == 0) throw new InvalidPropertyException("value");
164         this.value = value;
165         return this;
166       }
167 //    For building a search query
168       public Properties setVisibility (Visibility area)
169       {
170         this.visibility = area;
171         return this;
172       }
173 //  - Global validity check
174       
175           public void checkValidity () throws MissedPropertyException, InvalidPropertyException, MultiplyDefinedException
176           {
177         if (type == null)   throw new MissedPropertyException("type");
178         if (owner == null)  throw new MissedPropertyException("owner");
179                 if (title == null)  throw new MissedPropertyException("title");
180                 if (value == null)  throw new MissedPropertyException("value");
181                 if (author == null) throw new MissedPropertyException("author");
182           }
183     }
184 //  Database fetch constructor
185     protected KnowledgeElement () {     
186     }
187 //  Internal constructor
188     public KnowledgeElement (Properties kprop) throws MissedPropertyException, InvalidPropertyException, MultiplyDefinedException {
189       super(kprop);   // Throws one of the above exception if not valid
190       type   = kprop.type;
191       owner  = kprop.owner;
192       title  = kprop.title;
193       author = kprop.author;
194
195       date = kprop.date;
196       if (date == null) {
197         Calendar current = Calendar.getInstance();
198         date = current.getTime();   // Today
199       }
200       state = kprop.state;
201       if (state == null) {
202         if (type.isReserved()) state = ProgressState.inWORK;
203         else                   state = ProgressState.inDRAFT;
204       }
205       value = kprop.value.trim();
206       if (!value.startsWith("<p>")) {
207         StringBuffer  text  = new StringBuffer("<p>");
208         int           index = value.indexOf("<p>");
209         if (index > 0) {
210           value = text.append(value.substring(0, index)).append("</p>").append(value.substring(index)).toString();
211         } else {
212           value = text.append(value).append("</p>").toString();
213         }
214       }
215     }
216     
217 //  ==============================================================================================================================
218 //  Public member functions
219 //  ==============================================================================================================================
220
221     public boolean equals (KnowledgeElement given) {
222 //  ----------------------------------------------
223       if (isSaved()) return (this.getIndex() == given.getIndex());
224       if (!this.getType().getName().equals(given.getType().getName())) return false;
225       if (this.getValue().equals(given.getValue())) return true;
226       return false;      
227     }
228
229     public User getAuthor () {
230 //  ------------------------
231       return author;
232     }
233
234     public Date getDate () {
235 //  ----------------------
236       return date;
237     }
238
239     public Scenario getOwnerScenario () {
240 //  -----------------------------------
241       return owner;
242     }
243
244     public ProgressState getProgressState () {
245 //  ----------------------------------------
246       return state;
247     }
248
249     public String getTitle () {
250 //  -------------------------
251       return title;
252     }
253
254     public String getReference () {
255 //  -----------------------------
256       DecimalFormat toString = new DecimalFormat("00000");   // Supports 99 999 knowledge elements
257       return "KE" + toString.format(this.getIndex());
258     }
259
260     public KnowledgeElementType getType () {
261 //  --------------------------------------
262       return type;
263     }
264
265     public String getValue () {
266 //  -------------------------
267       return value;
268     }
269
270     public Visibility getVisibility () {
271 //  ----------------------------------
272       return getOwnerScenario().getOwnerStudy().getVisibility();
273     }
274
275         /**
276          * @param aState knowledge element progress state to set
277          */
278         public void setProgressState(ProgressState aState) {
279                 state = aState;
280         }
281         /**
282          * @param aTitle a title to set
283          */
284         public void setTitle(String aTitle) {
285                 title = aTitle;
286         }
287         /**
288          * Set the value.
289          * @param value the value to set
290          */
291         public void setValue(String value) {
292                 this.value = value;
293         }
294 }