4 * @author Daniel Brunier-Coulin
5 * @copyright OPEN CASCADE 2012
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;
14 import org.hibernate.Session;
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;
23 public class KnowledgeElement extends Persistent {
25 private KnowledgeElementType type; // User extendable types
26 private Scenario owner;
27 private ProgressState state;
33 // ==============================================================================================================================
35 // ==============================================================================================================================
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
54 public void clear () {
66 context = new Vector<SimulationContext>(); // as clear() may generate side effects
68 public Properties copy () {
69 Properties copy = new Properties();
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;
83 // - Protected services
85 protected User getActor () {
88 protected User getAuthor () {
91 protected ProgressState getProgressState () {
94 protected String getReference () {
97 protected List<SimulationContext> getSimulationContexts () {
100 protected String getTitle () {
103 protected KnowledgeElementType getType () {
106 protected Visibility getVisibility () {
109 // - Property setters
111 // For building a search query
112 public Properties setActor (User actor)
117 public Properties setAuthor (User user)
122 public Properties setDate (Date date)
127 protected Properties setOwnerScenario (Scenario owner)
132 // For building a search query
133 public Properties setReference (String kid) throws InvalidPropertyException
135 if (kid.length() == 0) throw new InvalidPropertyException("reference");
139 // For building a search query
140 public Properties setSimulationContexts (List<SimulationContext> context) {
141 this.context = context;
144 public Properties setState (ProgressState state) throws InvalidPropertyException
146 if (state != ProgressState.inWORK && state != ProgressState.inDRAFT && state != ProgressState.inCHECK && state != ProgressState.APPROVED) {
147 throw new InvalidPropertyException("state");
152 public Properties setTitle (String title) throws InvalidPropertyException
154 if (title.length() == 0) throw new InvalidPropertyException("title");
158 public Properties setType (KnowledgeElementType type)
163 public Properties setValue (String value) throws InvalidPropertyException
165 if (value.length() == 0) throw new InvalidPropertyException("value");
169 // For building a search query
170 public Properties setVisibility (Visibility area)
172 this.visibility = area;
175 // - Global validity check
177 public void checkValidity () throws MissedPropertyException, InvalidPropertyException, MultiplyDefinedException
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");
186 // Database fetch constructor
187 protected KnowledgeElement () {
189 // Internal constructor
190 protected KnowledgeElement (Properties kprop) throws MissedPropertyException, InvalidPropertyException, MultiplyDefinedException {
191 super(kprop); // Throws one of the above exception if not valid
195 author = kprop.author;
199 Calendar current = Calendar.getInstance();
200 date = current.getTime(); // Today
204 if (type.isReserved()) state = ProgressState.inWORK;
205 else state = ProgressState.inDRAFT;
207 value = kprop.value.trim();
208 if (!value.startsWith("<p>")) {
209 StringBuffer text = new StringBuffer("<p>");
210 int index = value.indexOf("<p>");
212 value = text.append(value.substring(0, index)).append("</p>").append(value.substring(index)).toString();
214 value = text.append(value).append("</p>").toString();
219 // ==============================================================================================================================
220 // Public member functions
221 // ==============================================================================================================================
223 public boolean approve () {
224 // -------------------------
225 if (state != ProgressState.inCHECK) return false;
226 state = ProgressState.APPROVED;
230 public boolean demote () {
231 // ------------------------
232 if (state != ProgressState.APPROVED && state != ProgressState.inCHECK) return false;
233 state = ProgressState.inDRAFT;
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;
245 public User getAuthor () {
246 // ------------------------
250 public Date getDate () {
251 // ----------------------
255 public Scenario getOwnerScenario () {
256 // -----------------------------------
260 public ProgressState getProgressState () {
261 // ----------------------------------------
265 public String getTitle () {
266 // -------------------------
270 public String getReference () {
271 // -----------------------------
272 DecimalFormat toString = new DecimalFormat("00000"); // Supports 99 999 knowledge elements
273 return "KE" + toString.format(this.getIndex());
276 public KnowledgeElementType getType () {
277 // --------------------------------------
281 public String getValue () {
282 // -------------------------
286 public Visibility getVisibility () {
287 // ----------------------------------
288 return getOwnerScenario().getOwnerStudy().getVisibility();
291 public boolean promote () {
292 // -------------------------
293 if (state != ProgressState.inDRAFT) return false;
294 state = ProgressState.inCHECK;
298 public void rename (String title) throws InvalidPropertyException {
299 // ---------------------------------
300 if (title.length() == 0) throw new InvalidPropertyException("name");
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>");
312 value = text.append(value.substring(0, index)).append("</p>").append(value.substring(index)).toString();
314 value = text.append(value).append("</p>").toString();
317 Database.getSession().update(this); // No need to update the Lucene index
320 // ==============================================================================================================================
322 // ==============================================================================================================================
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();
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();
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();
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();
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();
362 // ==============================================================================================================================
363 // Protected services
364 // ==============================================================================================================================
366 protected boolean updateMe () {
367 // -----------------------------
369 Database.getSession().update(this);
370 Database.getIndex().update(this);
373 catch (Exception error) {
374 // logger.error("Unable to re-index the knowledge '" + getIndex() + "', reason:", error);