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