Salome HOME
b00ad00f71a7e597f2d340ada6d3482521e1bf5b
[tools/siman.git] / Workspace / Siman / src / org / splat / simer / OpenObject.java
1 package org.splat.simer;
2
3 import java.util.HashMap;
4 import java.util.Iterator;
5 import java.util.List;
6 import java.util.Vector;
7
8 import org.splat.dal.bo.kernel.User;
9 import org.splat.dal.bo.som.Document;
10 import org.splat.dal.bo.som.DocumentType;
11 import org.splat.dal.bo.som.KnowledgeElement;
12 import org.splat.dal.bo.som.KnowledgeElementType;
13 import org.splat.dal.bo.som.ProgressState;
14 import org.splat.service.PublicationService;
15 import org.splat.service.dto.Proxy;
16 import org.splat.service.technical.ProjectSettingsService;
17 import org.splat.dal.bo.som.Publication;
18 import org.splat.dal.bo.som.Scenario;
19 import org.splat.som.Step;
20 import org.splat.wapp.Menu;
21 import org.splat.wapp.PopupMenu;
22
23
24 public abstract class OpenObject implements Proxy {
25
26         protected  User                          cuser       = null;   // Connected user
27         protected  String                        selection   = null;   // Menu selected by the user
28         protected  Step                          ustep       = null;   // Corresponding selected step
29         protected  String                        description = null;   // Object description (rich text)
30         protected  List<Step>                    involving   = null;
31         protected  List<SimulationContextFacade> context     = null;   // Simulation Context display representations
32         protected  List<DocumentFacade>          contents    = null;   // Document display representations
33         protected  List<KnowledgeIterator>       knowledge   = null;   // Knowledge Element display representations structured by knowledge types
34         
35         protected  Menu                          menu        = null;   // Left pane menu of this object
36         protected  PopupMenu                     popup       = null;   // Pop-up menu of this object, if the user has write access
37         private ProjectSettingsService _projectSettingsService;
38         private PublicationService _publicationService;
39
40     protected static HashMap<Integer, DocumentFacade>         docpres  = null;
41     protected static HashMap<Integer, KnowledgeElementFacade> knowpres = null;
42
43         public class KnowledgeIterator {
44 //  ------------------------------
45       protected KnowledgeElementType         type;
46       protected List<KnowledgeElementFacade> list;
47
48       public KnowledgeIterator (KnowledgeElementType type, List<KnowledgeElementFacade> list)
49       {
50         this.type = type;
51         this.list = list;
52       }
53       public String getIndex ()
54       {
55         return String.valueOf(type.getIndex());
56       }
57       public String getType ()
58       {
59         return type.getName();
60       }
61       public List<KnowledgeElementFacade> getKnowledgeElements ()
62       {
63         return list;
64       }
65         }
66     
67 //  ==============================================================================================================================
68 //  Constructor
69 //  ==============================================================================================================================
70
71     protected OpenObject () {
72 //  -----------------------
73 //    All member fields are supposed initialized by subclasses 
74       if (docpres == null)  docpres  = new HashMap<Integer, DocumentFacade>();
75       if (knowpres == null) knowpres = new HashMap<Integer, KnowledgeElementFacade>();
76     }
77
78 //  ==============================================================================================================================
79 //  Public member functions
80 //  ==============================================================================================================================
81
82     public void developDocument (String index) {
83 //  ------------------------------------------
84       for (Iterator<DocumentFacade> i=contents.iterator(); i.hasNext();) {
85         DocumentFacade doc = i.next();
86         if (!doc.getIndex().equals(index)) continue;
87         doc.develop();
88         return;
89       }
90     }
91
92     public void developKnowledge (String index) {
93 //  -------------------------------------------
94       for (Iterator<KnowledgeIterator> i=knowledge.iterator(); i.hasNext();) {
95         List<KnowledgeElementFacade> knowelms = i.next().getKnowledgeElements();
96         for (Iterator<KnowledgeElementFacade> j = knowelms.iterator(); j.hasNext(); ) {
97           KnowledgeElementFacade kelm = j.next();
98           if (!kelm.getIndex().equals(index)) continue;
99           kelm.develop();
100           return;
101         }
102       }
103     }
104
105     public void clearFacades () {
106 //  ---------------------------
107       docpres.clear();      // For eventually reopening the knowledge from a fresh context
108       knowpres.clear();     // For eventually reopening the knowledge from a fresh context
109     }
110
111     public List<Document> collectInvolvedDocuments (DocumentType type) {
112 //  ------------------------------------------------------------------
113       List<Document> found = new Vector<Document>();
114       for (Iterator<Step> i=involving.iterator(); i.hasNext();) {
115         Step              step  = i.next();
116         List<Publication> exist = step.getAllDocuments();
117         for (Iterator<Publication> j=exist.iterator(); j.hasNext();) {
118           Document doc = j.next().value();
119           if (doc.getType().equals(type)) found.add(doc);
120         }
121       }
122       return found;
123     }
124
125     public String getDisplayedDescription () {
126 //  ----------------------------------------
127       return description;
128     }
129
130     public List<DocumentFacade> getDisplayedDocuments () {
131 //  -----------------------------------------------------
132       return contents;
133     }
134
135     public List<SimulationContextFacade> getDisplayedSimulationContexts () {
136 //  ----------------------------------------------------------------------
137       return context;
138     }
139
140     public List<KnowledgeIterator> getDisplayedKnowledges () {
141 //  --------------------------------------------------------
142       return knowledge;
143     }
144
145     public List<Step> getInvolvedSteps () {
146 //  -------------------------------------
147       return involving;
148     }
149
150     public Menu getMenu () {
151 //  ----------------------
152       return menu;
153     }
154
155     public PopupMenu getPopup () {
156 //  ----------------------------
157       return popup;
158     }
159
160     public Step getSelectedStep () {
161 //  ------------------------------
162       return ustep;
163     }
164
165     public String getSelection () {
166 //  -----------------------------
167       return selection;
168     }
169
170     public User getUser () {
171 //  ----------------------
172       return cuser;
173     }
174
175     public boolean isOpenForWriting () {
176 //  ----------------------------------
177       return (popup != null);     // The pop-up is supposed existed when the user is staffed on the study
178     }
179
180     public void reduceDocument (String index) {
181 //  -----------------------------------------
182       for (Iterator<DocumentFacade> i=contents.iterator(); i.hasNext();) {
183         DocumentFacade doc = i.next();
184         if (!doc.getIndex().equals(index)) continue;
185         doc.reduceAll();
186         return;
187       }
188     }
189
190     public void reduceHistory (String index) {
191 //  ----------------------------------------
192       for (Iterator<DocumentFacade> i=contents.iterator(); i.hasNext();) {
193         DocumentFacade doc = i.next();
194         if (!doc.getIndex().equals(index)) continue;
195         doc.reduce();
196         return;
197       }
198     }
199
200     public void reduceKnowledge (String index) {
201 //  ------------------------------------------
202       for (Iterator<KnowledgeIterator> i=knowledge.iterator(); i.hasNext();) {
203         List<KnowledgeElementFacade> knowelms = i.next().getKnowledgeElements();
204         for (Iterator<KnowledgeElementFacade> j = knowelms.iterator(); j.hasNext(); ) {
205           KnowledgeElementFacade kelm = j.next();
206           if (!kelm.getIndex().equals(index)) continue;
207           kelm.reduce();
208           return;
209         }
210       }
211     }
212
213 //  ==============================================================================================================================
214 //  Protected services
215 //  ==============================================================================================================================
216
217     protected void setupContents () {
218 //  -------------------------------
219 //  Description
220 //    Initialized in subclasses
221
222 //  Knowledge elements supposed ordered by type
223       if (ustep.mayContain(KnowledgeElement.class)) {
224         Scenario                   scene   = (Scenario)ustep.getOwner();
225             List<KnowledgeElementType> types   = KnowledgeElement.selectTypesWhere(ProgressState.APPROVED);
226         List<KnowledgeElement>     kelms   = scene.getAllKnowledgeElements();
227         Iterator<KnowledgeElement> more    = kelms.iterator();
228         KnowledgeElement           current = null;
229         if (more.hasNext())        current = more.next();
230
231         knowledge = new Vector<KnowledgeIterator>(types.size());
232             for (Iterator<KnowledgeElementType> i=types.iterator(); i.hasNext();) {
233           KnowledgeElementType         type    = i.next();
234           List<KnowledgeElementFacade> display = new Vector<KnowledgeElementFacade>(kelms.size());
235           while (current != null && current.getType().equals(type)) {
236                 KnowledgeElementFacade facade = knowpres.get(current.getIndex());
237                 if (facade == null) {
238               facade = new KnowledgeElementFacade(current);
239               knowpres.put(current.getIndex(), facade);
240                 }
241                 display.add(facade);
242                 if (more.hasNext()) current = more.next();
243                 else                current = null;
244           }
245           knowledge.add( new KnowledgeIterator(type, display) );
246         }
247       } else {
248         knowledge = null;
249       }
250 //  Documents
251           if (ustep.mayContain(Document.class)) {
252         List<Publication> list = ustep.getAllDocuments();
253           
254         contents = new Vector<DocumentFacade>(list.size());
255         for (Iterator<Publication> i=list.iterator(); i.hasNext();) {
256           Publication     present = i.next();
257           Integer         index   = present.getIndex();
258           DocumentFacade  facade  = docpres.get(index);
259           if (facade == null) {
260             facade = new DocumentFacade(this, present, getProjectSettings(), getPublicationService());
261             docpres.put(index, facade);
262           }
263           contents.add(facade);
264         }
265           } else {
266         contents = null;
267           }
268     }
269
270         /**
271          * Get project settings.
272          * @return Project settings service
273          */
274     public ProjectSettingsService getProjectSettings() {
275                 return _projectSettingsService;
276         }
277
278         /**
279          * Get the publicationService.
280          * 
281          * @return the publicationService
282          */
283         public PublicationService getPublicationService() {
284                 return _publicationService;
285         }
286
287         /**
288          * Set project settings service.
289          * @param projectSettingsService project settings service
290          */
291         public void setProjectSettings(
292                         ProjectSettingsService projectSettingsService) {
293                 _projectSettingsService = projectSettingsService;
294         }
295
296         /**
297          * Set the publicationService.
298          * 
299          * @param publicationService
300          *            the publicationService to set
301          */
302         public void setPublicationService(PublicationService publicationService) {
303                 _publicationService = publicationService;
304         }
305 }