Salome HOME
Modifications done to respect PMD rules. Versioning a document is fixed. Validation...
[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.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 public abstract class OpenObject implements Proxy {
29
30         protected static Map<Long, DocumentFacade> docpres = null;
31         protected static Map<Long, KnowledgeElementFacade> knowpres = null;
32
33         /**
34          * Connected user.
35          */
36         protected User cuser = null;
37         /**
38          * Menu selected by the user.
39          */
40         protected String selection = null;
41         /**
42          * Corresponding selected step.
43          */
44         protected Step ustep = null;
45         /**
46          * Object description (rich text).
47          */
48         protected String description = null;
49         protected List<Step> involving = new ArrayList<Step>();
50         /**
51          * Simulation Context display representations.
52          */
53         protected List<SimulationContextFacade> context = new ArrayList<SimulationContextFacade>(); // Simulation Context display representations
54         /**
55          * Document display representations.
56          */
57         protected List<DocumentFacade> contents = null;
58         /**
59          * Knowledge Element display representations structured by knowledge types.
60          */
61         protected List<KnowledgeIterator> knowledge = null;
62
63         /**
64          * Left pane menu of this object.
65          */
66         protected Menu menu = null;
67         /**
68          * Pop-up menu of this object, if the user has write access.
69          */
70         protected PopupMenu popup = null;
71         /**
72          * Injected project settings service.
73          */
74         private ProjectSettingsService _projectSettingsService;
75         /**
76          * Injected application settings bean.
77          */
78         private ApplicationSettings _applicationSettings;
79         /**
80          * Injected publication service.
81          */
82         private PublicationService _publicationService;
83         /**
84          * Injected knowledge element service.
85          */
86         private KnowledgeElementTypeService _knowledgeElementTypeService;
87         /**
88          * Injected project element service.
89          */
90         private ProjectElementService _projectElementService;
91
92         public class KnowledgeIterator {
93                 protected KnowledgeElementType type;
94                 protected List<KnowledgeElementFacade> list;
95
96                 public KnowledgeIterator(final KnowledgeElementType type,
97                                 final List<KnowledgeElementFacade> list) {
98                         this.type = type;
99                         this.list = list;
100                 }
101
102                 public String getIndex() {
103                         return String.valueOf(type.getIndex());
104                 }
105
106                 public String getType() {
107                         return type.getName();
108                 }
109
110                 public List<KnowledgeElementFacade> getKnowledgeElements() {
111                         return list;
112                 }
113         }
114
115         // ==============================================================================================================================
116         // Constructor
117         // ==============================================================================================================================
118
119         protected OpenObject() {
120                 // -----------------------
121                 // All member fields are supposed initialized by subclasses
122                 if (docpres == null) {
123                         docpres = new HashMap<Long, DocumentFacade>();
124                 }
125                 if (knowpres == null) {
126                         knowpres = new HashMap<Long, KnowledgeElementFacade>();
127                 }
128         }
129
130         // ==============================================================================================================================
131         // Public member functions
132         // ==============================================================================================================================
133
134         public void developDocument(final String index) {
135                 // ------------------------------------------
136                 for (Iterator<DocumentFacade> i = contents.iterator(); i.hasNext();) {
137                         DocumentFacade doc = i.next();
138                         if (!doc.getIndex().equals(index)) {
139                                 continue;
140                         }
141                         doc.develop();
142                         return;
143                 }
144         }
145
146         public void developKnowledge(final String index) {
147                 // -------------------------------------------
148                 for (Iterator<KnowledgeIterator> i = knowledge.iterator(); i.hasNext();) {
149                         List<KnowledgeElementFacade> knowelms = i.next()
150                                         .getKnowledgeElements();
151                         for (Iterator<KnowledgeElementFacade> j = knowelms.iterator(); j
152                                         .hasNext();) {
153                                 KnowledgeElementFacade kelm = j.next();
154                                 if (!kelm.getIndex().equals(index)) {
155                                         continue;
156                                 }
157                                 kelm.develop();
158                                 return;
159                         }
160                 }
161         }
162
163         public void clearFacades() {
164                 // ---------------------------
165                 docpres.clear(); // For eventually reopening the knowledge from a fresh context
166                 knowpres.clear(); // For eventually reopening the knowledge from a fresh context
167         }
168
169         public List<Document> collectInvolvedDocuments(final DocumentType type) {
170                 // ------------------------------------------------------------------
171                 List<Document> found = new ArrayList<Document>();
172                 for (Iterator<Step> i = involving.iterator(); i.hasNext();) {
173                         Step step = i.next();
174                         List<Publication> exist = step.getAllDocuments();
175                         for (Iterator<Publication> j = exist.iterator(); j.hasNext();) {
176                                 Document doc = j.next().value();
177                                 if (doc.getType().equals(type)) {
178                                         found.add(doc);
179                                 }
180                         }
181                 }
182                 return found;
183         }
184
185         public String getDisplayedDescription() {
186                 // ----------------------------------------
187                 return description;
188         }
189
190         public List<DocumentFacade> getDisplayedDocuments() {
191                 // -----------------------------------------------------
192                 return contents;
193         }
194
195         public List<SimulationContextFacade> getDisplayedSimulationContexts() {
196                 // ----------------------------------------------------------------------
197                 return context;
198         }
199
200         public List<KnowledgeIterator> getDisplayedKnowledges() {
201                 // --------------------------------------------------------
202                 return knowledge;
203         }
204
205         public List<Step> getInvolvedSteps() {
206                 // -------------------------------------
207                 return involving;
208         }
209
210         public Menu getMenu() {
211                 // ----------------------
212                 return menu;
213         }
214
215         public PopupMenu getPopup() {
216                 // ----------------------------
217                 return popup;
218         }
219
220         public Step getSelectedStep() {
221                 // ------------------------------
222                 return ustep;
223         }
224
225         public String getSelection() {
226                 // -----------------------------
227                 return selection;
228         }
229
230         public User getUser() {
231                 // ----------------------
232                 return cuser;
233         }
234
235         public boolean isOpenForWriting() {
236                 // ----------------------------------
237                 return (popup != null); // The pop-up is supposed existed when the user is staffed on the study
238         }
239
240         public void reduceDocument(final String index) {
241                 // -----------------------------------------
242                 for (Iterator<DocumentFacade> i = contents.iterator(); i.hasNext();) {
243                         DocumentFacade doc = i.next();
244                         if (!doc.getIndex().equals(index)) {
245                                 continue;
246                         }
247                         doc.reduceAll();
248                         return;
249                 }
250         }
251
252         public void reduceHistory(final String index) {
253                 // ----------------------------------------
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                 // ------------------------------------------
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                 // -------------------------------
287                 // Description
288                 // Initialized in subclasses
289
290                 // Knowledge elements supposed ordered by type
291                 if (ustep.mayContain(KnowledgeElement.class)) {
292                         Scenario scene = (Scenario) ustep.getOwner();
293                         List<KnowledgeElementType> types = getKnowledgeElementTypeService()
294                                         .selectTypesWhere(ProgressState.APPROVED);
295                         List<KnowledgeElement> kelms = scene.getAllKnowledgeElements();
296                         Iterator<KnowledgeElement> more = kelms.iterator();
297                         KnowledgeElement current = null;
298                         if (more.hasNext()) {
299                                 current = more.next();
300                         }
301
302                         knowledge = new ArrayList<KnowledgeIterator>(types.size());
303                         for (Iterator<KnowledgeElementType> i = types.iterator(); i
304                                         .hasNext();) {
305                                 KnowledgeElementType type = i.next();
306                                 List<KnowledgeElementFacade> display = new ArrayList<KnowledgeElementFacade>(
307                                                 kelms.size());
308                                 while (current != null && current.getType().equals(type)) {
309                                         KnowledgeElementFacade facade = knowpres.get(current
310                                                         .getIndex());
311                                         if (facade == null) {
312                                                 facade = new KnowledgeElementFacade(BeanHelper.copyBean(current, KnowledgeElementDTO.class));
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                                         docpres.put(index, facade);
340                                 }
341                                 contents.add(facade);
342                         }
343                 } else {
344                         contents = null;
345                 }
346         }
347
348         /**
349          * Get project settings.
350          * 
351          * @return Project settings service
352          */
353         public ProjectSettingsService getProjectSettings() {
354                 return _projectSettingsService;
355         }
356
357         /**
358          * Get the publicationService.
359          * 
360          * @return the publicationService
361          */
362         public PublicationService getPublicationService() {
363                 return _publicationService;
364         }
365
366         /**
367          * Set project settings service.
368          * 
369          * @param projectSettingsService
370          *            project settings service
371          */
372         public void setProjectSettings(final ProjectSettingsService projectSettingsService) {
373                 _projectSettingsService = projectSettingsService;
374         }
375
376         /**
377          * Set the publicationService.
378          * 
379          * @param publicationService
380          *            the publicationService to set
381          */
382         public void setPublicationService(final PublicationService publicationService) {
383                 _publicationService = publicationService;
384         }
385
386         /**
387          * Set the menu.
388          * 
389          * @param menu
390          *            the menu to set
391          */
392         public void setMenu(final Menu menu) {
393                 this.menu = menu;
394         }
395
396         /**
397          * Get the projectElementService.
398          * 
399          * @return the projectElementService
400          */
401         public ProjectElementService getProjectElementService() {
402                 return _projectElementService;
403         }
404
405         /**
406          * Set the projectElementService.
407          * 
408          * @param projectElementService
409          *            the projectElementService to set
410          */
411         public void setProjectElementService(
412                         final ProjectElementService projectElementService) {
413                 _projectElementService = projectElementService;
414         }
415
416         /**
417          * Get the knowledgeElementTypeService.
418          * @return the knowledgeElementTypeService
419          */
420         public KnowledgeElementTypeService getKnowledgeElementTypeService() {
421                 return _knowledgeElementTypeService;
422         }
423
424         /**
425          * Set the knowledgeElementTypeService.
426          * @param knowledgeElementTypeService the knowledgeElementTypeService to set
427          */
428         public void setKnowledgeElementTypeService(
429                         final KnowledgeElementTypeService knowledgeElementTypeService) {
430                 _knowledgeElementTypeService = knowledgeElementTypeService;
431         }
432
433         /**
434          * Get the applicationSettings.
435          * @return the applicationSettings
436          */
437         public ApplicationSettings getApplicationSettings() {
438                 return _applicationSettings;
439         }
440
441         /**
442          * Set the applicationSettings.
443          * @param applicationSettings the applicationSettings to set
444          */
445         public void setApplicationSettings(final ApplicationSettings applicationSettings) {
446                 _applicationSettings = applicationSettings;
447         }
448 }