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