1 package org.splat.dal.bo.som;
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.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;
24 public class KnowledgeElement extends Persistent {
26 private KnowledgeElementType type; // User extendable types
27 private Scenario owner;
28 private ProgressState state;
34 // ==============================================================================================================================
36 // ==============================================================================================================================
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
55 public void clear () {
67 context = new Vector<SimulationContext>(); // as clear() may generate side effects
69 public Properties copy () {
70 Properties copy = new Properties();
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;
84 // - Protected services
86 public User getActor () {
89 public User getAuthor () {
92 public ProgressState getProgressState () {
95 public String getReference () {
98 public List<SimulationContext> getSimulationContexts () {
101 public String getTitle () {
104 public KnowledgeElementType getType () {
107 public Visibility getVisibility () {
110 // - Property setters
112 // For building a search query
113 public Properties setActor (User actor)
118 public Properties setAuthor (User user)
123 public Properties setDate (Date date)
128 public Properties setOwnerScenario (Scenario owner)
133 // For building a search query
134 public Properties setReference (String kid) throws InvalidPropertyException
136 if (kid.length() == 0) throw new InvalidPropertyException("reference");
140 // For building a search query
141 public Properties setSimulationContexts (List<SimulationContext> context) {
142 this.context = context;
145 public Properties setState (ProgressState state) throws InvalidPropertyException
147 if (state != ProgressState.inWORK && state != ProgressState.inDRAFT && state != ProgressState.inCHECK && state != ProgressState.APPROVED) {
148 throw new InvalidPropertyException("state");
153 public Properties setTitle (String title) throws InvalidPropertyException
155 if (title.length() == 0) throw new InvalidPropertyException("title");
159 public Properties setType (KnowledgeElementType type)
164 public Properties setValue (String value) throws InvalidPropertyException
166 if (value.length() == 0) throw new InvalidPropertyException("value");
170 // For building a search query
171 public Properties setVisibility (Visibility area)
173 this.visibility = area;
176 // - Global validity check
178 public void checkValidity () throws MissedPropertyException, InvalidPropertyException, MultiplyDefinedException
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");
187 // Database fetch constructor
188 protected KnowledgeElement () {
190 // Internal constructor
191 public KnowledgeElement (Properties kprop) throws MissedPropertyException, InvalidPropertyException, MultiplyDefinedException {
192 super(kprop); // Throws one of the above exception if not valid
196 author = kprop.author;
200 Calendar current = Calendar.getInstance();
201 date = current.getTime(); // Today
205 if (type.isReserved()) state = ProgressState.inWORK;
206 else state = ProgressState.inDRAFT;
208 value = kprop.value.trim();
209 if (!value.startsWith("<p>")) {
210 StringBuffer text = new StringBuffer("<p>");
211 int index = value.indexOf("<p>");
213 value = text.append(value.substring(0, index)).append("</p>").append(value.substring(index)).toString();
215 value = text.append(value).append("</p>").toString();
220 // ==============================================================================================================================
221 // Public member functions
222 // ==============================================================================================================================
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;
232 public User getAuthor () {
233 // ------------------------
237 public Date getDate () {
238 // ----------------------
242 public Scenario getOwnerScenario () {
243 // -----------------------------------
247 public ProgressState getProgressState () {
248 // ----------------------------------------
252 public String getTitle () {
253 // -------------------------
257 public String getReference () {
258 // -----------------------------
259 DecimalFormat toString = new DecimalFormat("00000"); // Supports 99 999 knowledge elements
260 return "KE" + toString.format(this.getIndex());
263 public KnowledgeElementType getType () {
264 // --------------------------------------
268 public String getValue () {
269 // -------------------------
273 public Visibility getVisibility () {
274 // ----------------------------------
275 return getOwnerScenario().getOwnerStudy().getVisibility();
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>");
285 value = text.append(value.substring(0, index)).append("</p>").append(value.substring(index)).toString();
287 value = text.append(value).append("</p>").toString();
290 Database.getSession().update(this); // No need to update the Lucene index
293 // ==============================================================================================================================
295 // ==============================================================================================================================
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();
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();
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();
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();
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();
336 * @param aState knowledge element progress state to set
338 public void setProgressState(ProgressState aState) {
342 * @param aTitle a title to set
344 public void setTitle(String aTitle) {