Salome HOME
a7888b3b4e916d97d151d7623e84d936ee83efa6
[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 transient Map<Long, DocumentFacade> _docpres = null;
37         /**
38          * Current open study step cache for presented knowledges.
39          */
40         protected transient 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                                 doc.develop();
147                                 break;
148                         }
149                 }
150         }
151
152         public void developKnowledge(final String index) {
153                 for (Iterator<KnowledgeIterator> i = _knowledge.iterator(); i.hasNext();) {
154                         List<KnowledgeElementFacade> knowelms = i.next()
155                                         .getKnowledgeElements();
156                         for (Iterator<KnowledgeElementFacade> j = knowelms.iterator(); j
157                                         .hasNext();) {
158                                 KnowledgeElementFacade kelm = j.next();
159                                 if (!kelm.getIndex().equals(index)) {
160                                         continue;
161                                 }
162                                 kelm.develop();
163                                 return;
164                         }
165                 }
166         }
167
168         public void clearFacades() {
169                 _docpres.clear(); // For eventually reopening the knowledge from a fresh context
170                 _knowpres.clear(); // For eventually reopening the knowledge from a fresh context
171         }
172
173         public List<Document> collectInvolvedDocuments(final DocumentType type) {
174                 List<Document> found = new ArrayList<Document>();
175                 for (Iterator<Step> i = _involving.iterator(); i.hasNext();) {
176                         Step step = i.next();
177                         List<Publication> exist = step.getAllDocuments();
178                         for (Iterator<Publication> j = exist.iterator(); j.hasNext();) {
179                                 Document doc = j.next().value();
180                                 if (doc.getType().equals(type)) {
181                                         found.add(doc);
182                                 }
183                         }
184                 }
185                 return found;
186         }
187
188         public String getDisplayedDescription() {
189                 return _description;
190         }
191
192         public List<DocumentFacade> getDisplayedDocuments() {
193                 return _contents;
194         }
195
196         public List<SimulationContextFacade> getDisplayedSimulationContexts() {
197                 return _context;
198         }
199
200         public List<KnowledgeIterator> getDisplayedKnowledges() {
201                 return _knowledge;
202         }
203
204         public List<Step> getInvolvedSteps() {
205                 return _involving;
206         }
207
208         public Menu getMenu() {
209                 return _menu;
210         }
211
212         public PopupMenu getPopup() {
213                 return _popup;
214         }
215
216         public Step getSelectedStep() {
217                 return _ustep;
218         }
219
220         public String getSelection() {
221                 return _selection;
222         }
223
224         public String getSelectedScenarioId() {
225                 String res;
226                 if (_selection == null) {
227                         res = "0";
228                 } else {
229                         res = _selection.substring(0, _selection.indexOf('.'));
230                 }
231                 return res;
232         }
233
234         public User getUser() {
235                 return _cuser;
236         }
237
238         public boolean isOpenForWriting() {
239                 return (_popup != null); // The pop-up is supposed existed when the user is staffed on the study
240         }
241
242         public void reduceDocument(final String index) {
243                 for (Iterator<DocumentFacade> i = _contents.iterator(); i.hasNext();) {
244                         DocumentFacade doc = i.next();
245                         if (!doc.getIndex().equals(index)) {
246                                 continue;
247                         }
248                         doc.reduceAll();
249                         return;
250                 }
251         }
252
253         public void reduceHistory(final String index) {
254                 for (Iterator<DocumentFacade> i = _contents.iterator(); i.hasNext();) {
255                         DocumentFacade doc = i.next();
256                         if (!doc.getIndex().equals(index)) {
257                                 continue;
258                         }
259                         doc.reduce();
260                         return;
261                 }
262         }
263
264         public void reduceKnowledge(final String index) {
265                 for (Iterator<KnowledgeIterator> i = _knowledge.iterator(); i.hasNext();) {
266                         List<KnowledgeElementFacade> knowelms = i.next()
267                                         .getKnowledgeElements();
268                         for (Iterator<KnowledgeElementFacade> j = knowelms.iterator(); j
269                                         .hasNext();) {
270                                 KnowledgeElementFacade kelm = j.next();
271                                 if (!kelm.getIndex().equals(index)) {
272                                         continue;
273                                 }
274                                 kelm.reduce();
275                                 return;
276                         }
277                 }
278         }
279
280         // ==============================================================================================================================
281         // Protected services
282         // ==============================================================================================================================
283
284         protected void setupContents() {
285                 // Description
286                 // Initialized in subclasses
287
288                 // Knowledge elements supposed ordered by type
289                 if (_ustep.mayContain(KnowledgeElement.class)) {
290                         Scenario scene = (Scenario) _ustep.getOwner();
291                         List<KnowledgeElementType> types = getKnowledgeElementTypeService()
292                                         .selectTypesWhere(ProgressState.APPROVED);
293                         List<KnowledgeElement> kelms = scene.getAllKnowledgeElements();
294                         Iterator<KnowledgeElement> more = kelms.iterator();
295                         KnowledgeElement current = null;
296                         if (more.hasNext()) {
297                                 current = more.next();
298                         }
299
300                         _knowledge = new ArrayList<KnowledgeIterator>(types.size());
301                         for (Iterator<KnowledgeElementType> i = types.iterator(); i
302                                         .hasNext();) {
303                                 KnowledgeElementType type = i.next();
304                                 List<KnowledgeElementFacade> display = new ArrayList<KnowledgeElementFacade>(
305                                                 kelms.size());
306                                 while (current != null && current.getType().equals(type)) {
307                                         KnowledgeElementFacade facade = _knowpres.get(current
308                                                         .getIndex());
309                                         if (facade == null) {
310                                                 facade = new KnowledgeElementFacade(BeanHelper
311                                                                 .copyBean(current, KnowledgeElementDTO.class),
312                                                                 getApplicationSettings());
313                                                 _knowpres.put(current.getIndex(), facade);
314                                         }
315                                         display.add(facade);
316                                         if (more.hasNext()) {
317                                                 current = more.next();
318                                         } else {
319                                                 current = null;
320                                         }
321                                 }
322                                 _knowledge.add(new KnowledgeIterator(type, display));
323                         }
324                 } else {
325                         _knowledge = null;
326                 }
327                 // Documents
328                 if (_ustep.mayContain(Document.class)) {
329                         List<Publication> list = _ustep.getAllDocuments();
330
331                         _contents = new ArrayList<DocumentFacade>(list.size());
332                         for (Iterator<Publication> i = list.iterator(); i.hasNext();) {
333                                 Publication present = i.next();
334                                 Long index = present.getIndex();
335                                 DocumentFacade facade = _docpres.get(index);
336                                 if (facade == null) {
337                                         facade = new DocumentFacade(this, present,
338                                                         getProjectSettings(), getPublicationService(),
339                                                         getApplicationSettings());
340                                         _docpres.put(index, facade);
341                                 }
342                                 _contents.add(facade);
343                         }
344                 } else {
345                         _contents = null;
346                 }
347         }
348
349         /**
350          * Get project settings.
351          * 
352          * @return Project settings service
353          */
354         public ProjectSettingsService getProjectSettings() {
355                 return _projectSettings;
356         }
357
358         /**
359          * Get the publicationService.
360          * 
361          * @return the publicationService
362          */
363         public PublicationService getPublicationService() {
364                 return _publicationService;
365         }
366
367         /**
368          * Set project settings service.
369          * 
370          * @param projectSettingsService
371          *            project settings service
372          */
373         public void setProjectSettings(
374                         final ProjectSettingsService projectSettingsService) {
375                 _projectSettings = projectSettingsService;
376         }
377
378         /**
379          * Set the publicationService.
380          * 
381          * @param publicationService
382          *            the publicationService to set
383          */
384         public void setPublicationService(
385                         final PublicationService publicationService) {
386                 _publicationService = publicationService;
387         }
388
389         /**
390          * Set the menu.
391          * 
392          * @param menu
393          *            the menu to set
394          */
395         public void setMenu(final Menu menu) {
396                 this._menu = menu;
397         }
398
399         /**
400          * Get the projectElementService.
401          * 
402          * @return the projectElementService
403          */
404         public ProjectElementService getProjectElementService() {
405                 return _projectElementService;
406         }
407
408         /**
409          * Set the projectElementService.
410          * 
411          * @param projectElementService
412          *            the projectElementService to set
413          */
414         public void setProjectElementService(
415                         final ProjectElementService projectElementService) {
416                 _projectElementService = projectElementService;
417         }
418
419         /**
420          * Get the knowledgeElementTypeService.
421          * 
422          * @return the knowledgeElementTypeService
423          */
424         public KnowledgeElementTypeService getKnowledgeElementTypeService() {
425                 return _knowledgeElementTypeService;
426         }
427
428         /**
429          * Set the knowledgeElementTypeService.
430          * 
431          * @param knowledgeElementTypeService
432          *            the knowledgeElementTypeService to set
433          */
434         public void setKnowledgeElementTypeService(
435                         final KnowledgeElementTypeService knowledgeElementTypeService) {
436                 _knowledgeElementTypeService = knowledgeElementTypeService;
437         }
438
439         /**
440          * Get the applicationSettings.
441          * 
442          * @return the applicationSettings
443          */
444         public ApplicationSettings getApplicationSettings() {
445                 return _applicationSettings;
446         }
447
448         /**
449          * Set the applicationSettings.
450          * 
451          * @param applicationSettings
452          *            the applicationSettings to set
453          */
454         public void setApplicationSettings(
455                         final ApplicationSettings applicationSettings) {
456                 _applicationSettings = applicationSettings;
457         }
458 }