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.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;
21 public class KnowledgeElement extends Persistent {
23 private KnowledgeElementType type; // User extendable types
24 private Scenario owner;
25 private ProgressState state;
31 // ==============================================================================================================================
33 // ==============================================================================================================================
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
52 public void clear () {
64 context = new Vector<SimulationContext>(); // as clear() may generate side effects
66 public Properties copy () {
67 Properties copy = new Properties();
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;
81 // - Protected services
83 public User getActor () {
86 public User getAuthor () {
89 public ProgressState getProgressState () {
92 public String getReference () {
95 public List<SimulationContext> getSimulationContexts () {
98 public String getTitle () {
101 public KnowledgeElementType getType () {
104 public Visibility getVisibility () {
107 // - Property setters
109 // For building a search query
110 public Properties setActor (User actor)
115 public Properties setAuthor (User user)
120 public Properties setDate (Date date)
125 public Properties setOwnerScenario (Scenario owner)
130 // For building a search query
131 public Properties setReference (String kid) throws InvalidPropertyException
133 if (kid.length() == 0) throw new InvalidPropertyException("reference");
137 // For building a search query
138 public Properties setSimulationContexts (List<SimulationContext> context) {
139 this.context = context;
142 public Properties setState (ProgressState state) throws InvalidPropertyException
144 if (state != ProgressState.inWORK && state != ProgressState.inDRAFT && state != ProgressState.inCHECK && state != ProgressState.APPROVED) {
145 throw new InvalidPropertyException("state");
150 public Properties setTitle (String title) throws InvalidPropertyException
152 if (title.length() == 0) throw new InvalidPropertyException("title");
156 public Properties setType (KnowledgeElementType type)
161 public Properties setValue (String value) throws InvalidPropertyException
163 if (value.length() == 0) throw new InvalidPropertyException("value");
167 // For building a search query
168 public Properties setVisibility (Visibility area)
170 this.visibility = area;
173 // - Global validity check
175 public void checkValidity () throws MissedPropertyException, InvalidPropertyException, MultiplyDefinedException
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");
184 // Database fetch constructor
185 protected KnowledgeElement () {
187 // Internal constructor
188 public KnowledgeElement (Properties kprop) throws MissedPropertyException, InvalidPropertyException, MultiplyDefinedException {
189 super(kprop); // Throws one of the above exception if not valid
193 author = kprop.author;
197 Calendar current = Calendar.getInstance();
198 date = current.getTime(); // Today
202 if (type.isReserved()) state = ProgressState.inWORK;
203 else state = ProgressState.inDRAFT;
205 value = kprop.value.trim();
206 if (!value.startsWith("<p>")) {
207 StringBuffer text = new StringBuffer("<p>");
208 int index = value.indexOf("<p>");
210 value = text.append(value.substring(0, index)).append("</p>").append(value.substring(index)).toString();
212 value = text.append(value).append("</p>").toString();
217 // ==============================================================================================================================
218 // Public member functions
219 // ==============================================================================================================================
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;
229 public User getAuthor () {
230 // ------------------------
234 public Date getDate () {
235 // ----------------------
239 public Scenario getOwnerScenario () {
240 // -----------------------------------
244 public ProgressState getProgressState () {
245 // ----------------------------------------
249 public String getTitle () {
250 // -------------------------
254 public String getReference () {
255 // -----------------------------
256 DecimalFormat toString = new DecimalFormat("00000"); // Supports 99 999 knowledge elements
257 return "KE" + toString.format(this.getIndex());
260 public KnowledgeElementType getType () {
261 // --------------------------------------
265 public String getValue () {
266 // -------------------------
270 public Visibility getVisibility () {
271 // ----------------------------------
272 return getOwnerScenario().getOwnerStudy().getVisibility();
276 * @param aState knowledge element progress state to set
278 public void setProgressState(ProgressState aState) {
282 * @param aTitle a title to set
284 public void setTitle(String aTitle) {
289 * @param value the value to set
291 public void setValue(String value) {