Salome HOME
Rename the scenario functionality is implemented
[tools/siman.git] / Workspace / Siman / src / org / splat / simer / AbstractOpenObject.java
1 package org.splat.simer;
2
3 import java.util.ArrayList;
4 import java.util.HashMap;
5 import java.util.Iterator;
6 import java.util.List;
7 import java.util.Map;
8
9 import org.splat.dal.bo.kernel.User;
10 import org.splat.dal.bo.som.Document;
11 import org.splat.dal.bo.som.DocumentType;
12 import org.splat.dal.bo.som.KnowledgeElement;
13 import org.splat.dal.bo.som.KnowledgeElementType;
14 import org.splat.dal.bo.som.ProgressState;
15 import org.splat.dal.bo.som.Publication;
16 import org.splat.dal.bo.som.Scenario;
17 import org.splat.service.KnowledgeElementTypeService;
18 import org.splat.service.ProjectElementService;
19 import org.splat.service.PublicationService;
20 import org.splat.service.dto.KnowledgeElementDTO;
21 import org.splat.service.dto.Proxy;
22 import org.splat.service.technical.ProjectSettingsService;
23 import org.splat.som.Step;
24 import org.splat.util.BeanHelper;
25 import org.splat.wapp.Menu;
26 import org.splat.wapp.PopupMenu;
27
28 /**
29  * Open study or knowledge.
30  */
31 public abstract class AbstractOpenObject implements Proxy {
32
33         /**
34          * Current open study step cache for presented documents.
35          */
36         protected Map<Long, DocumentFacade> docpres = null;
37         /**
38          * Current open study step cache for presented knowledges.
39          */
40         protected Map<Long, KnowledgeElementFacade> knowpres = null;
41
42         /**
43          * Connected user.
44          */
45         protected transient User _cuser = null;
46         /**
47          * Menu selected by the user.
48          */
49         protected transient String _selection = null;
50         /**
51          * Corresponding selected step.
52          */
53         protected transient Step _ustep = null;
54         /**
55          * Object description (rich text).
56          */
57         protected transient String _description = null;
58         protected transient List<Step> _involving = new ArrayList<Step>();
59         /**
60          * Simulation Context display representations.
61          */
62         protected transient List<SimulationContextFacade> _context = new ArrayList<SimulationContextFacade>();
63         /**
64          * Document display representations.
65          */
66         protected transient List<DocumentFacade> _contents = null;
67         /**
68          * Knowledge Element display representations structured by knowledge types.
69          */
70         protected transient List<KnowledgeIterator> _knowledge = null;
71
72         /**
73          * Left pane menu of this object.
74          */
75         protected Menu _menu = null;
76         /**
77          * Pop-up menu of this object, if the user has write access.
78          */
79         protected transient PopupMenu _popup = null;
80         /**
81          * Injected project settings service.
82          */
83         private ProjectSettingsService _projectSettings;
84         /**
85          * Injected application settings bean.
86          */
87         private ApplicationSettings _applicationSettings;
88         /**
89          * Injected publication service.
90          */
91         private PublicationService _publicationService;
92         /**
93          * Injected knowledge element service.
94          */
95         private KnowledgeElementTypeService _knowledgeElementTypeService;
96         /**
97          * Injected project element service.
98          */
99         private ProjectElementService _projectElementService;
100
101         public class KnowledgeIterator {
102                 protected transient KnowledgeElementType _type;
103                 protected transient List<KnowledgeElementFacade> _list;
104
105                 public KnowledgeIterator(final KnowledgeElementType type,
106                                 final List<KnowledgeElementFacade> list) {
107                         this._type = type;
108                         this._list = list;
109                 }
110
111                 public String getIndex() {
112                         return String.valueOf(_type.getIndex());
113                 }
114
115                 public String getType() {
116                         return _type.getName();
117                 }
118
119                 public List<KnowledgeElementFacade> getKnowledgeElements() {
120                         return _list;
121                 }
122         }
123
124         // ==============================================================================================================================
125         // Constructor
126         // ==============================================================================================================================
127
128         protected AbstractOpenObject() {
129                 // All member fields are supposed initialized by subclasses
130                 if (docpres == null) {
131                         docpres = new HashMap<Long, DocumentFacade>();
132                 }
133                 if (knowpres == null) {
134                         knowpres = new HashMap<Long, KnowledgeElementFacade>();
135                 }
136         }
137
138         // ==============================================================================================================================
139         // Public member functions
140         // ==============================================================================================================================
141
142         public void developDocument(final String index) {
143                 for (Iterator<DocumentFacade> i = _contents.iterator(); i.hasNext();) {
144                         DocumentFacade doc = i.next();
145                         if (!doc.getIndex().equals(index)) {
146                                 continue;
147                         }
148                         doc.develop();
149                         return;
150                 }
151         }
152
153         public void developKnowledge(final String index) {
154                 for (Iterator<KnowledgeIterator> i = _knowledge.iterator(); i.hasNext();) {
155                         List<KnowledgeElementFacade> knowelms = i.next()
156                                         .getKnowledgeElements();
157                         for (Iterator<KnowledgeElementFacade> j = knowelms.iterator(); j
158                                         .hasNext();) {
159                                 KnowledgeElementFacade kelm = j.next();
160                                 if (!kelm.getIndex().equals(index)) {
161                                         continue;
162                                 }
163                                 kelm.develop();
164                                 return;
165                         }
166                 }
167         }
168
169         public void clearFacades() {
170                 docpres.clear(); // For eventually reopening the knowledge from a fresh context
171                 knowpres.clear(); // For eventually reopening the knowledge from a fresh context
172         }
173
174         public List<Document> collectInvolvedDocuments(final DocumentType type) {
175                 List<Document> found = new ArrayList<Document>();
176                 for (Iterator<Step> i = _involving.iterator(); i.hasNext();) {
177                         Step step = i.next();
178                         List<Publication> exist = step.getAllDocuments();
179                         for (Iterator<Publication> j = exist.iterator(); j.hasNext();) {
180                                 Document doc = j.next().value();
181                                 if (doc.getType().equals(type)) {
182                                         found.add(doc);
183                                 }
184                         }
185                 }
186                 return found;
187         }
188
189         public String getDisplayedDescription() {
190                 return _description;
191         }
192
193         public List<DocumentFacade> getDisplayedDocuments() {
194                 return _contents;
195         }
196
197         public List<SimulationContextFacade> getDisplayedSimulationContexts() {
198                 return _context;
199         }
200
201         public List<KnowledgeIterator> getDisplayedKnowledges() {
202                 return _knowledge;
203         }
204
205         public List<Step> getInvolvedSteps() {
206                 return _involving;
207         }
208
209         public Menu getMenu() {
210                 return _menu;
211         }
212
213         public PopupMenu getPopup() {
214                 return _popup;
215         }
216
217         public Step getSelectedStep() {
218                 return _ustep;
219         }
220
221         public String getSelection() {
222                 return _selection;
223         }
224
225         public String getSelectedScenarioId() {
226                 String res;
227                 if (_selection == null) {
228                         res = "0";
229                 } else {
230                         res = _selection.substring(0, _selection.indexOf('.'));
231                 }
232                 return res;
233         }
234
235         public User getUser() {
236                 return _cuser;
237         }
238
239         public boolean isOpenForWriting() {
240                 return (_popup != null); // The pop-up is supposed existed when the user is staffed on the study
241         }
242
243         public void reduceDocument(final String index) {
244                 for (Iterator<DocumentFacade> i = _contents.iterator(); i.hasNext();) {
245                         DocumentFacade doc = i.next();
246                         if (!doc.getIndex().equals(index)) {
247                                 continue;
248                         }
249                         doc.reduceAll();
250                         return;
251                 }
252         }
253
254         public void reduceHistory(final String index) {
255                 for (Iterator<DocumentFacade> i = _contents.iterator(); i.hasNext();) {
256                         DocumentFacade doc = i.next();
257                         if (!doc.getIndex().equals(index)) {
258                                 continue;
259                         }
260                         doc.reduce();
261                         return;
262                 }
263         }
264
265         public void reduceKnowledge(final String index) {
266                 for (Iterator<KnowledgeIterator> i = _knowledge.iterator(); i.hasNext();) {
267                         List<KnowledgeElementFacade> knowelms = i.next()
268                                         .getKnowledgeElements();
269                         for (Iterator<KnowledgeElementFacade> j = knowelms.iterator(); j
270                                         .hasNext();) {
271                                 KnowledgeElementFacade kelm = j.next();
272                                 if (!kelm.getIndex().equals(index)) {
273                                         continue;
274                                 }
275                                 kelm.reduce();
276                                 return;
277                         }
278                 }
279         }
280
281         // ==============================================================================================================================
282         // Protected services
283         // ==============================================================================================================================
284
285         protected void setupContents() {
286                 // Description
287                 // Initialized in subclasses
288
289                 // Knowledge elements supposed ordered by type
290                 if (_ustep.mayContain(KnowledgeElement.class)) {
291                         Scenario scene = (Scenario) _ustep.getOwner();
292                         List<KnowledgeElementType> types = getKnowledgeElementTypeService()
293                                         .selectTypesWhere(ProgressState.APPROVED);
294                         List<KnowledgeElement> kelms = scene.getAllKnowledgeElements();
295                         Iterator<KnowledgeElement> more = kelms.iterator();
296                         KnowledgeElement current = null;
297                         if (more.hasNext()) {
298                                 current = more.next();
299                         }
300
301                         _knowledge = new ArrayList<KnowledgeIterator>(types.size());
302                         for (Iterator<KnowledgeElementType> i = types.iterator(); i
303                                         .hasNext();) {
304                                 KnowledgeElementType type = i.next();
305                                 List<KnowledgeElementFacade> display = new ArrayList<KnowledgeElementFacade>(
306                                                 kelms.size());
307                                 while (current != null && current.getType().equals(type)) {
308                                         KnowledgeElementFacade facade = knowpres.get(current
309                                                         .getIndex());
310                                         if (facade == null) {
311                                                 facade = new KnowledgeElementFacade(BeanHelper
312                                                                 .copyBean(current, KnowledgeElementDTO.class),
313                                                                 getApplicationSettings());
314                                                 knowpres.put(current.getIndex(), facade);
315                                         }
316                                         display.add(facade);
317                                         if (more.hasNext()) {
318                                                 current = more.next();
319                                         } else {
320                                                 current = null;
321                                         }
322                                 }
323                                 _knowledge.add(new KnowledgeIterator(type, display));
324                         }
325                 } else {
326                         _knowledge = null;
327                 }
328                 // Documents
329                 if (_ustep.mayContain(Document.class)) {
330                         List<Publication> list = _ustep.getAllDocuments();
331
332                         _contents = new ArrayList<DocumentFacade>(list.size());
333                         for (Iterator<Publication> i = list.iterator(); i.hasNext();) {
334                                 Publication present = i.next();
335                                 Long index = present.getIndex();
336                                 DocumentFacade facade = docpres.get(index);
337                                 if (facade == null) {
338                                         facade = new DocumentFacade(this, present,
339                                                         getProjectSettings(), getPublicationService(),
340                                                         getApplicationSettings());
341                                         docpres.put(index, facade);
342                                 }
343                                 _contents.add(facade);
344                         }
345                 } else {
346                         _contents = null;
347                 }
348         }
349
350         /**
351          * Get project settings.
352          * 
353          * @return Project settings service
354          */
355         public ProjectSettingsService getProjectSettings() {
356                 return _projectSettings;
357         }
358
359         /**
360          * Get the publicationService.
361          * 
362          * @return the publicationService
363          */
364         public PublicationService getPublicationService() {
365                 return _publicationService;
366         }
367
368         /**
369          * Set project settings service.
370          * 
371          * @param projectSettingsService
372          *            project settings service
373          */
374         public void setProjectSettings(
375                         final ProjectSettingsService projectSettingsService) {
376                 _projectSettings = projectSettingsService;
377         }
378
379         /**
380          * Set the publicationService.
381          * 
382          * @param publicationService
383          *            the publicationService to set
384          */
385         public void setPublicationService(
386                         final PublicationService publicationService) {
387                 _publicationService = publicationService;
388         }
389
390         /**
391          * Set the menu.
392          * 
393          * @param menu
394          *            the menu to set
395          */
396         public void setMenu(final Menu menu) {
397                 this._menu = menu;
398         }
399
400         /**
401          * Get the projectElementService.
402          * 
403          * @return the projectElementService
404          */
405         public ProjectElementService getProjectElementService() {
406                 return _projectElementService;
407         }
408
409         /**
410          * Set the projectElementService.
411          * 
412          * @param projectElementService
413          *            the projectElementService to set
414          */
415         public void setProjectElementService(
416                         final ProjectElementService projectElementService) {
417                 _projectElementService = projectElementService;
418         }
419
420         /**
421          * Get the knowledgeElementTypeService.
422          * 
423          * @return the knowledgeElementTypeService
424          */
425         public KnowledgeElementTypeService getKnowledgeElementTypeService() {
426                 return _knowledgeElementTypeService;
427         }
428
429         /**
430          * Set the knowledgeElementTypeService.
431          * 
432          * @param knowledgeElementTypeService
433          *            the knowledgeElementTypeService to set
434          */
435         public void setKnowledgeElementTypeService(
436                         final KnowledgeElementTypeService knowledgeElementTypeService) {
437                 _knowledgeElementTypeService = knowledgeElementTypeService;
438         }
439
440         /**
441          * Get the applicationSettings.
442          * 
443          * @return the applicationSettings
444          */
445         public ApplicationSettings getApplicationSettings() {
446                 return _applicationSettings;
447         }
448
449         /**
450          * Set the applicationSettings.
451          * 
452          * @param applicationSettings
453          *            the applicationSettings to set
454          */
455         public void setApplicationSettings(
456                         final ApplicationSettings applicationSettings) {
457                 _applicationSettings = applicationSettings;
458         }
459 }