Salome HOME
Siman codebase is refactored. Spring beans are introduced in the context.
[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
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.hibernate.Session;
15
16 import org.splat.dal.bo.kernel.Persistent;
17 import org.splat.dal.bo.kernel.User;
18 import org.splat.dal.dao.som.Database;
19 import org.splat.kernel.InvalidPropertyException;
20 import org.splat.kernel.MissedPropertyException;
21 import org.splat.kernel.MultiplyDefinedException;
22
23
24 public class KnowledgeElement extends Persistent {
25
26         private KnowledgeElementType  type;     // User extendable types
27         private Scenario              owner;
28     private ProgressState         state;
29     private String                title;
30     private String                value;
31     private User                  author;
32     private Date                  date;
33
34 //  ==============================================================================================================================
35 //  Construction
36 //  ==============================================================================================================================
37
38 //  Fields initialization class
39     public static class Properties extends Persistent.Properties {
40 //  ------------------------------------------------------------
41       private String                  kid        = null;                             // Search criterion only
42       private KnowledgeElementType    type       = null;
43       private Scenario                owner      = null;
44       private Visibility              visibility = null;                             // Search criterion only
45       private ProgressState           state      = null;
46       private String                  title      = null;
47       private String                  value      = null;
48       private User                    author     = null;
49       private User                    actor      = null;                             // Search criterion only
50       private Date                    date       = null;
51       private List<SimulationContext> context = new Vector<SimulationContext>();     // Search criterion only
52
53 //  - Public services
54
55       public void clear () {
56         super.clear();
57         kid        = null;
58         type       = null;
59         owner      = null;
60         visibility = null;
61         state      = null;
62         title      = null;
63         value      = null;
64         author     = null;
65         actor      = null;
66         date       = null;
67         context    = new Vector<SimulationContext>();  // as clear() may generate side effects
68       }
69       public Properties copy () {
70         Properties copy = new Properties();
71         copy.kid        = this.kid;
72         copy.type       = this.type;
73         copy.owner      = this.owner;
74         copy.visibility = this.visibility;
75         copy.state      = this.state;
76         copy.title      = this.title;
77         copy.value      = this.value;
78         copy.author     = this.author;
79         copy.actor      = this.actor;
80         copy.date       = this.date;
81         copy.context    = this.context;
82         return copy;
83       }
84 //  - Protected services
85
86       public User getActor () {
87         return actor;
88       }
89       public User getAuthor () {
90             return author;
91           }
92       public ProgressState getProgressState () {
93         return state;
94       }
95       public String getReference () {
96         return kid;
97       }
98       public List<SimulationContext> getSimulationContexts () {
99         return context;
100       }
101       public String getTitle () {
102         return title;
103       }      
104       public KnowledgeElementType getType () {
105         return type;
106       }
107       public Visibility getVisibility () {
108         return visibility;
109       }
110 //  - Property setters
111
112 //    For building a search query
113       public Properties setActor (User actor)
114       {
115         this.actor = actor;
116         return this;
117       }
118       public Properties setAuthor (User user)
119       {
120         this.author = user;
121         return this;
122       }
123       public Properties setDate (Date date)
124       {
125         this.date = date;
126         return this;
127       }
128       public Properties setOwnerScenario (Scenario owner)
129       {
130         this.owner = owner;
131         return this;
132       }
133 //    For building a search query
134       public Properties setReference (String kid) throws InvalidPropertyException
135       {
136         if (kid.length() == 0) throw new InvalidPropertyException("reference");
137         this.kid = kid;
138         return this;
139       }
140 //    For building a search query
141       public Properties setSimulationContexts (List<SimulationContext> context) {
142         this.context = context;
143         return this;
144       }
145       public Properties setState (ProgressState state) throws InvalidPropertyException
146       {
147         if (state != ProgressState.inWORK && state != ProgressState.inDRAFT && state != ProgressState.inCHECK && state != ProgressState.APPROVED) {
148           throw new InvalidPropertyException("state");
149         }
150         this.state = state;
151         return this;
152       }
153       public Properties setTitle (String title) throws InvalidPropertyException
154       {
155         if (title.length() == 0) throw new InvalidPropertyException("title");
156         this.title = title;
157         return this;
158       }
159       public Properties setType (KnowledgeElementType type)
160       {
161         this.type = type;
162         return this;
163       }
164       public Properties setValue (String value) throws InvalidPropertyException
165       {
166         if (value.length() == 0) throw new InvalidPropertyException("value");
167         this.value = value;
168         return this;
169       }
170 //    For building a search query
171       public Properties setVisibility (Visibility area)
172       {
173         this.visibility = area;
174         return this;
175       }
176 //  - Global validity check
177       
178           public void checkValidity () throws MissedPropertyException, InvalidPropertyException, MultiplyDefinedException
179           {
180         if (type == null)   throw new MissedPropertyException("type");
181         if (owner == null)  throw new MissedPropertyException("owner");
182                 if (title == null)  throw new MissedPropertyException("title");
183                 if (value == null)  throw new MissedPropertyException("value");
184                 if (author == null) throw new MissedPropertyException("author");
185           }
186     }
187 //  Database fetch constructor
188     protected KnowledgeElement () {     
189     }
190 //  Internal constructor
191     public KnowledgeElement (Properties kprop) throws MissedPropertyException, InvalidPropertyException, MultiplyDefinedException {
192       super(kprop);   // Throws one of the above exception if not valid
193       type   = kprop.type;
194       owner  = kprop.owner;
195       title  = kprop.title;
196       author = kprop.author;
197
198       date = kprop.date;
199       if (date == null) {
200         Calendar current = Calendar.getInstance();
201         date = current.getTime();   // Today
202       }
203       state = kprop.state;
204       if (state == null) {
205         if (type.isReserved()) state = ProgressState.inWORK;
206         else                   state = ProgressState.inDRAFT;
207       }
208       value = kprop.value.trim();
209       if (!value.startsWith("<p>")) {
210         StringBuffer  text  = new StringBuffer("<p>");
211         int           index = value.indexOf("<p>");
212         if (index > 0) {
213           value = text.append(value.substring(0, index)).append("</p>").append(value.substring(index)).toString();
214         } else {
215           value = text.append(value).append("</p>").toString();
216         }
217       }
218     }
219     
220 //  ==============================================================================================================================
221 //  Public member functions
222 //  ==============================================================================================================================
223
224     public boolean equals (KnowledgeElement given) {
225 //  ----------------------------------------------
226       if (isSaved()) return (this.getIndex() == given.getIndex());
227       if (!this.getType().getName().equals(given.getType().getName())) return false;
228       if (this.getValue().equals(given.getValue())) return true;
229       return false;      
230     }
231
232     public User getAuthor () {
233 //  ------------------------
234       return author;
235     }
236
237     public Date getDate () {
238 //  ----------------------
239       return date;
240     }
241
242     public Scenario getOwnerScenario () {
243 //  -----------------------------------
244       return owner;
245     }
246
247     public ProgressState getProgressState () {
248 //  ----------------------------------------
249       return state;
250     }
251
252     public String getTitle () {
253 //  -------------------------
254       return title;
255     }
256
257     public String getReference () {
258 //  -----------------------------
259       DecimalFormat toString = new DecimalFormat("00000");   // Supports 99 999 knowledge elements
260       return "KE" + toString.format(this.getIndex());
261     }
262
263     public KnowledgeElementType getType () {
264 //  --------------------------------------
265       return type;
266     }
267
268     public String getValue () {
269 //  -------------------------
270       return value;
271     }
272
273     public Visibility getVisibility () {
274 //  ----------------------------------
275       return getOwnerScenario().getOwnerStudy().getVisibility();
276     }
277
278     public void update (String description) {
279 //  ---------------------------------------
280       value = description.trim();
281       if (!value.startsWith("<p>")) {
282         StringBuffer  text  = new StringBuffer("<p>");
283         int           index = value.indexOf("<p>");
284         if (index > 0) {
285           value = text.append(value.substring(0, index)).append("</p>").append(value.substring(index)).toString();
286         } else {
287           value = text.append(value).append("</p>").toString();
288         }
289       }
290       Database.getSession().update(this);   // No need to update the Lucene index
291     }
292
293 //  ==============================================================================================================================
294 //  Public services
295 //  ==============================================================================================================================
296
297     public static KnowledgeElementType createType (String name) throws RuntimeException {
298 //  -----------------------------------------------------------
299 //TODO: Check for duplicate definition
300       KnowledgeElementType kelt    = new KnowledgeElementType(name);
301       Session              session = Database.getSession();          
302       session.save(kelt);
303           
304       return kelt;
305     }
306
307     @SuppressWarnings("unchecked")
308         public static List<KnowledgeElementType> selectAllTypes () {
309 //  ----------------------------------------------------------
310           StringBuffer  query = new StringBuffer("from KnowledgeElementType");
311                     query = query.append(" order by rid asc");
312       return  Database.getSession().createQuery(query.toString()).list();
313     }
314
315     @SuppressWarnings("unchecked")
316         public static List<KnowledgeElementType> selectTypesWhere (ProgressState state) {
317 //  -------------------------------------------------------------------------------
318           StringBuffer  query = new StringBuffer("from KnowledgeElementType where state='").append(state).append("'");
319                     query = query.append(" order by rid asc");
320       return  Database.getSession().createQuery(query.toString()).list();
321         }
322
323     public static KnowledgeElementType selectType (String name) {
324 //  -----------------------------------------------------------         
325           StringBuffer  query = new StringBuffer("from KnowledgeElementType where name='").append(name).append("'");
326           return (KnowledgeElementType)Database.getSession().createQuery(query.toString()).uniqueResult();
327     }
328
329     public static KnowledgeElementType selectType (int index) {
330 //  ---------------------------------------------------------
331           StringBuffer  query = new StringBuffer("from KnowledgeElementType where rid='").append(index).append("'");
332           return (KnowledgeElementType)Database.getSession().createQuery(query.toString()).uniqueResult();
333     }
334     
335         /**
336          * @param aState knowledge element progress state to set
337          */
338         public void setProgressState(ProgressState aState) {
339                 state = aState;
340         }
341         /**
342          * @param aTitle a title to set
343          */
344         public void setTitle(String aTitle) {
345                 title = aTitle;
346         }
347 }