1 package org.splat.simer;
3 import java.util.ArrayList;
4 import java.util.HashMap;
5 import java.util.Iterator;
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;
29 * Open study or knowledge.
31 public abstract class AbstractOpenObject implements Proxy {
33 protected static Map<Long, DocumentFacade> docpres = null;
34 protected static Map<Long, KnowledgeElementFacade> knowpres = null;
39 protected transient User _cuser = null;
41 * Menu selected by the user.
43 protected transient String _selection = null;
45 * Corresponding selected step.
47 protected transient Step _ustep = null;
49 * Object description (rich text).
51 protected transient String _description = null;
52 protected transient List<Step> _involving = new ArrayList<Step>();
54 * Simulation Context display representations.
56 protected transient List<SimulationContextFacade> _context = new ArrayList<SimulationContextFacade>();
58 * Document display representations.
60 protected transient List<DocumentFacade> _contents = null;
62 * Knowledge Element display representations structured by knowledge types.
64 protected transient List<KnowledgeIterator> _knowledge = null;
67 * Left pane menu of this object.
69 protected Menu _menu = null;
71 * Pop-up menu of this object, if the user has write access.
73 protected transient PopupMenu _popup = null;
75 * Injected project settings service.
77 private ProjectSettingsService _projectSettings;
79 * Injected application settings bean.
81 private ApplicationSettings _applicationSettings;
83 * Injected publication service.
85 private PublicationService _publicationService;
87 * Injected knowledge element service.
89 private KnowledgeElementTypeService _knowledgeElementTypeService;
91 * Injected project element service.
93 private ProjectElementService _projectElementService;
95 public class KnowledgeIterator {
96 protected transient KnowledgeElementType _type;
97 protected transient List<KnowledgeElementFacade> _list;
99 public KnowledgeIterator(final KnowledgeElementType type,
100 final List<KnowledgeElementFacade> list) {
105 public String getIndex() {
106 return String.valueOf(_type.getIndex());
109 public String getType() {
110 return _type.getName();
113 public List<KnowledgeElementFacade> getKnowledgeElements() {
118 // ==============================================================================================================================
120 // ==============================================================================================================================
122 protected AbstractOpenObject() {
123 // All member fields are supposed initialized by subclasses
124 if (docpres == null) {
125 docpres = new HashMap<Long, DocumentFacade>();
127 if (knowpres == null) {
128 knowpres = new HashMap<Long, KnowledgeElementFacade>();
132 // ==============================================================================================================================
133 // Public member functions
134 // ==============================================================================================================================
136 public void developDocument(final String index) {
137 for (Iterator<DocumentFacade> i = _contents.iterator(); i.hasNext();) {
138 DocumentFacade doc = i.next();
139 if (!doc.getIndex().equals(index)) {
147 public void developKnowledge(final String index) {
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
153 KnowledgeElementFacade kelm = j.next();
154 if (!kelm.getIndex().equals(index)) {
163 public void clearFacades() {
164 docpres.clear(); // For eventually reopening the knowledge from a fresh context
165 knowpres.clear(); // For eventually reopening the knowledge from a fresh context
168 public List<Document> collectInvolvedDocuments(final DocumentType type) {
169 List<Document> found = new ArrayList<Document>();
170 for (Iterator<Step> i = _involving.iterator(); i.hasNext();) {
171 Step step = i.next();
172 List<Publication> exist = step.getAllDocuments();
173 for (Iterator<Publication> j = exist.iterator(); j.hasNext();) {
174 Document doc = j.next().value();
175 if (doc.getType().equals(type)) {
183 public String getDisplayedDescription() {
187 public List<DocumentFacade> getDisplayedDocuments() {
191 public List<SimulationContextFacade> getDisplayedSimulationContexts() {
195 public List<KnowledgeIterator> getDisplayedKnowledges() {
199 public List<Step> getInvolvedSteps() {
203 public Menu getMenu() {
207 public PopupMenu getPopup() {
211 public Step getSelectedStep() {
215 public String getSelection() {
219 public String getSelectedScenarioId() {
221 if (_selection == null) {
224 res = _selection.substring(0, _selection.indexOf('.'));
229 public User getUser() {
233 public boolean isOpenForWriting() {
234 return (_popup != null); // The pop-up is supposed existed when the user is staffed on the study
237 public void reduceDocument(final String index) {
238 for (Iterator<DocumentFacade> i = _contents.iterator(); i.hasNext();) {
239 DocumentFacade doc = i.next();
240 if (!doc.getIndex().equals(index)) {
248 public void reduceHistory(final String index) {
249 for (Iterator<DocumentFacade> i = _contents.iterator(); i.hasNext();) {
250 DocumentFacade doc = i.next();
251 if (!doc.getIndex().equals(index)) {
259 public void reduceKnowledge(final String index) {
260 for (Iterator<KnowledgeIterator> i = _knowledge.iterator(); i.hasNext();) {
261 List<KnowledgeElementFacade> knowelms = i.next()
262 .getKnowledgeElements();
263 for (Iterator<KnowledgeElementFacade> j = knowelms.iterator(); j
265 KnowledgeElementFacade kelm = j.next();
266 if (!kelm.getIndex().equals(index)) {
275 // ==============================================================================================================================
276 // Protected services
277 // ==============================================================================================================================
279 protected void setupContents() {
281 // Initialized in subclasses
283 // Knowledge elements supposed ordered by type
284 if (_ustep.mayContain(KnowledgeElement.class)) {
285 Scenario scene = (Scenario) _ustep.getOwner();
286 List<KnowledgeElementType> types = getKnowledgeElementTypeService()
287 .selectTypesWhere(ProgressState.APPROVED);
288 List<KnowledgeElement> kelms = scene.getAllKnowledgeElements();
289 Iterator<KnowledgeElement> more = kelms.iterator();
290 KnowledgeElement current = null;
291 if (more.hasNext()) {
292 current = more.next();
295 _knowledge = new ArrayList<KnowledgeIterator>(types.size());
296 for (Iterator<KnowledgeElementType> i = types.iterator(); i
298 KnowledgeElementType type = i.next();
299 List<KnowledgeElementFacade> display = new ArrayList<KnowledgeElementFacade>(
301 while (current != null && current.getType().equals(type)) {
302 KnowledgeElementFacade facade = knowpres.get(current
304 if (facade == null) {
305 facade = new KnowledgeElementFacade(BeanHelper
306 .copyBean(current, KnowledgeElementDTO.class));
307 knowpres.put(current.getIndex(), facade);
310 if (more.hasNext()) {
311 current = more.next();
316 _knowledge.add(new KnowledgeIterator(type, display));
322 if (_ustep.mayContain(Document.class)) {
323 List<Publication> list = _ustep.getAllDocuments();
325 _contents = new ArrayList<DocumentFacade>(list.size());
326 for (Iterator<Publication> i = list.iterator(); i.hasNext();) {
327 Publication present = i.next();
328 Long index = present.getIndex();
329 DocumentFacade facade = docpres.get(index);
330 if (facade == null) {
331 facade = new DocumentFacade(this, present,
332 getProjectSettings(), getPublicationService());
333 docpres.put(index, facade);
335 _contents.add(facade);
343 * Get project settings.
345 * @return Project settings service
347 public ProjectSettingsService getProjectSettings() {
348 return _projectSettings;
352 * Get the publicationService.
354 * @return the publicationService
356 public PublicationService getPublicationService() {
357 return _publicationService;
361 * Set project settings service.
363 * @param projectSettingsService
364 * project settings service
366 public void setProjectSettings(
367 final ProjectSettingsService projectSettingsService) {
368 _projectSettings = projectSettingsService;
372 * Set the publicationService.
374 * @param publicationService
375 * the publicationService to set
377 public void setPublicationService(
378 final PublicationService publicationService) {
379 _publicationService = publicationService;
388 public void setMenu(final Menu menu) {
393 * Get the projectElementService.
395 * @return the projectElementService
397 public ProjectElementService getProjectElementService() {
398 return _projectElementService;
402 * Set the projectElementService.
404 * @param projectElementService
405 * the projectElementService to set
407 public void setProjectElementService(
408 final ProjectElementService projectElementService) {
409 _projectElementService = projectElementService;
413 * Get the knowledgeElementTypeService.
415 * @return the knowledgeElementTypeService
417 public KnowledgeElementTypeService getKnowledgeElementTypeService() {
418 return _knowledgeElementTypeService;
422 * Set the knowledgeElementTypeService.
424 * @param knowledgeElementTypeService
425 * the knowledgeElementTypeService to set
427 public void setKnowledgeElementTypeService(
428 final KnowledgeElementTypeService knowledgeElementTypeService) {
429 _knowledgeElementTypeService = knowledgeElementTypeService;
433 * Get the applicationSettings.
435 * @return the applicationSettings
437 public ApplicationSettings getApplicationSettings() {
438 return _applicationSettings;
442 * Set the applicationSettings.
444 * @param applicationSettings
445 * the applicationSettings to set
447 public void setApplicationSettings(
448 final ApplicationSettings applicationSettings) {
449 _applicationSettings = applicationSettings;