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 / Scenario.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.io.IOException;
9 import java.util.Calendar;
10 import java.util.Collections;
11 import java.util.Date;
12 import java.util.HashMap;
13 import java.util.Iterator;
14 import java.util.List;
15 import java.util.Set;
16 import java.util.HashSet;
17 import java.util.Vector;
18
19 import org.hibernate.HibernateException;
20 import org.hibernate.Session;
21 import org.hibernate.Transaction;
22 import org.splat.dal.bo.kernel.Persistent;
23 import org.splat.dal.bo.kernel.User;
24 import org.splat.dal.dao.som.Database;
25 import org.splat.kernel.InvalidPropertyException;
26 import org.splat.kernel.MissedPropertyException;
27 import org.splat.kernel.MultiplyDefinedException;
28 import org.splat.service.StepService;
29 import org.splat.service.technical.IndexService;
30 import org.splat.som.Step;
31
32
33 public class Scenario extends ProjectElement {
34
35 //  Persistent fields
36     private Study                 owner;
37         private int                   sid;               // Identifier unique in the scope of owner study
38         private User                  cuser;             // User having checked-out the scenario, if done
39     private Set<KnowledgeElement> kelms;
40
41 //  Transient fields
42     private HashMap<Integer, List<KnowledgeElement>> known;
43     private List<KnowledgeElement>                   knowl;  // Copy of kelms excluding the internal Knowledge Element (ucase below)
44         private KnowledgeElement                         ucase;  // Internal Knowledge Element for accessing to all used simulation contexts
45
46
47 //  ==============================================================================================================================
48 //  Construction
49 //  ==============================================================================================================================
50
51 //  Fields initialization class
52     public static class Properties extends Persistent.Properties {
53 //  ------------------------------------------------------------
54       private Study    owner    = null;
55       private Scenario previous = null;
56       private Step     base     = null;
57       private String   title    = null;
58       private String   summary  = null;
59       private User     manager  = null;
60       private Date     date     = null;
61
62 //  - Public services
63
64       public void clear () {
65         super.clear();
66         owner    = null;
67         previous = null;
68         base     = null;
69         title    = null;
70         summary  = null;
71         manager  = null;
72         date     = null;
73       }
74 //  - Protected services
75
76       public Step getBaseStep () {
77         return base;         // May be null
78       }
79       public Scenario getInsertAfter () {
80         return previous;     // May be null
81       }
82           public User getManager () {
83                 return manager;
84           }
85       public Properties setOwnerStudy (Study owner)
86       {
87         this.owner = owner;
88         return this;
89       }
90 //  - Setters of Scenario properties
91
92       public Properties setBaseStep (Step base) throws InvalidPropertyException
93       {
94         if (!(base.getOwner() instanceof Scenario)) throw new InvalidPropertyException("step");
95         this.base = base;
96         return this;
97       }
98       public Properties setDate (Date date)
99       {
100         this.date = date;
101         return this;
102       }
103       public Properties setDescription (String summary)
104       {
105         if (summary.length() > 0) this.summary = summary;
106         return this;
107       }
108       public Properties setInsertAfter (Scenario previous)
109       {
110         this.previous = previous;
111         return this;
112       }
113       public Properties setManager (User user)
114       {
115         this.manager = user;
116         return this;
117       }
118       public Properties setTitle (String title) throws InvalidPropertyException
119       {
120         if (title.length() == 0) throw new InvalidPropertyException("title");
121         this.title = title;
122         return this;
123       }
124 //  - Global validity check
125       
126       public void checkValidity() throws MissedPropertyException, InvalidPropertyException, MultiplyDefinedException
127       {
128         if (owner == null)   throw new MissedPropertyException("owner");
129         if (title == null)   throw new MissedPropertyException("title");
130         if (manager == null) throw new MissedPropertyException("manager");
131       }
132     }
133 //  Database fetch constructor
134     protected Scenario () {
135 //  ---------------------
136       known = null;
137       knowl = null;
138       ucase = null;
139     }
140 //  Internal constructor
141     public Scenario (Properties sprop) throws MissedPropertyException, InvalidPropertyException, MultiplyDefinedException {
142 //  -------------------------------------
143       super(sprop);                // Throws one of the above exception if not valid
144       owner   = sprop.owner;
145       sid     = 0;
146       cuser   = null;
147       title   = sprop.title;       // Inherited attribute
148       known   = null;
149       knowl   = null;              // Initialized when getting all Knowledge Elements
150       ucase   = null;
151       kelms   = new HashSet<KnowledgeElement>();
152
153       manager = sprop.manager;
154       if (!owner.isStaffedBy(manager)) throw new InvalidPropertyException("manager");
155
156       credate = sprop.date;        // Inherited attribute
157       if (credate == null) {
158         Calendar current = Calendar.getInstance();
159         credate = current.getTime();  // Today
160       }
161       lasdate = credate;           // Inherited attribute
162
163       if (sprop.summary != null) this.setAttribute( new DescriptionAttribute(this, sprop.summary) );
164
165       Scenario[] scene = owner.getScenarii();
166       for (int i=0; i<scene.length; i++) if (scene[i].sid > this.sid) this.sid = scene[i].sid;
167       sid += 1;
168     }
169
170 //  ==============================================================================================================================
171 //  Public member functions
172 //  ==============================================================================================================================
173
174     public List<KnowledgeElement> getAllKnowledgeElements () {
175 //  --------------------------------------------------------
176       if (knowl == null) {
177         knowl = new Vector<KnowledgeElement>(kelms.size());
178         for (Iterator<KnowledgeElement> i=kelms.iterator(); i.hasNext(); ) {
179           KnowledgeElement  kelm = i.next();
180           if (kelm.getType().equals("usecase")) ucase = kelm;
181           else                                  knowl.add(kelm);
182         }
183       }
184       return  Collections.unmodifiableList(knowl);
185     }
186
187     public KnowledgeElement getKnowledgeElement (int index) {
188 //  -------------------------------------------------------
189       for (Iterator<KnowledgeElement> i=kelms.iterator(); i.hasNext();) {
190             KnowledgeElement mykelm = i.next();
191         if (mykelm.getIndex() == index) return mykelm;
192       }
193       return null;
194     }
195
196     public List<KnowledgeElement> getKnowledgeElementsOf (KnowledgeElementType type) {
197 //  --------------------------------------------------------------------------------
198       if (kelms.isEmpty()) return  new Vector<KnowledgeElement>();   // Smarter than returning null
199       if (known == null)   known = new HashMap<Integer, List<KnowledgeElement>>();
200
201       int                    numtype = type.getIndex();
202       List<KnowledgeElement> listype = known.get(numtype);
203       if (listype == null) {
204         listype = new Vector<KnowledgeElement>();
205         for (Iterator<KnowledgeElement> i=kelms.iterator(); i.hasNext();) {
206           KnowledgeElement kelm = i.next();
207           if (kelm.getType().getIndex() == numtype) listype.add(kelm);
208         }
209         known.put(numtype, listype);
210       }
211       return listype;   // No protection against this object corruption as it would not corrupt the database
212     }
213
214     public Study getOwnerStudy () {
215 //  -----------------------------
216       return  owner;
217     }
218 /**
219  * Returns the local reference of this scenario. This reference is unique in the scope of the owner study.
220  */
221     public String getReference () {
222 //  -----------------------------
223       return  String.valueOf(sid);
224     }
225
226     public User getUser () {
227       return  cuser;    // Null if the scenario has not been checked-out
228     }
229
230     public void setUser (User aUser) {
231       cuser = aUser;    // Null if the scenario has not been checked-out
232     }
233
234     public boolean removeKnowledgeElement (KnowledgeElement kelm) {
235 //  -------------------------------------------------------------
236           KnowledgeElement torem = getKnowledgeElement(kelm.getIndex());
237       if (torem == null) return false;
238       boolean done = kelms.remove(torem);
239       if (done) {
240 //      Update of my transient data
241         List<KnowledgeElement> kelms = known.get(kelm.getType().getIndex());
242         kelms.remove(torem);
243         if (knowl != null) knowl.remove(torem);
244         Database.getSession().update(this);
245 //TODO: If the owner study is not private, remove the knowledge from the Lucene index
246         return true;
247       } else {
248         return false;
249       }
250     }
251
252     public boolean isCheckedout () {
253 //  ------------------------------
254       return (cuser != null);
255     }
256
257     
258
259 //  ==============================================================================================================================
260 //  Protected member function
261 //  ==============================================================================================================================
262
263     public void updateMyIndex (IndexService lucin) throws IOException {
264 //  ------------------------------------------
265       if (ucase == null) for (Iterator<KnowledgeElement> i=kelms.iterator(); i.hasNext(); ) {
266         KnowledgeElement  kelm = i.next();
267         if (!kelm.getType().equals("usecase")) continue;
268         ucase = kelm;
269         break;
270       }
271       lucin.update(ucase);
272     }
273
274 //  ==============================================================================================================================
275 //  Private services
276 //  ==============================================================================================================================
277         /**
278          * @return
279          */
280         public Set<KnowledgeElement> getKnowledgeElements() {
281                 return kelms;
282         }
283         /**
284          * @param kelm
285          */
286         public void setUcase(KnowledgeElement kelm) {
287                 ucase = kelm;
288         }
289         /**
290          * @return
291          */
292         public List<KnowledgeElement> getKnowledgeElementsList() {
293                 return knowl;
294         }
295 }