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 {
34 * Current open study step cache for presented documents.
36 protected transient Map<Long, DocumentFacade> _docpres = null;
38 * Current open study step cache for presented knowledges.
40 protected transient Map<Long, KnowledgeElementFacade> _knowpres = null;
45 protected transient User _cuser = null;
47 * Menu selected by the user.
49 protected transient String _selection = null;
51 * Corresponding selected step.
53 protected transient Step _ustep = null;
55 * Object description (rich text).
57 protected transient String _description = null;
58 protected transient List<Step> _involving = new ArrayList<Step>();
60 * Simulation Context display representations.
62 protected transient List<SimulationContextFacade> _context = new ArrayList<SimulationContextFacade>();
64 * Document display representations.
66 protected transient List<DocumentFacade> _contents = null;
68 * Knowledge Element display representations structured by knowledge types.
70 protected transient List<KnowledgeIterator> _knowledge = null;
73 * Left pane menu of this object.
75 protected Menu _menu = null;
77 * Pop-up menu of this object, if the user has write access.
79 protected transient PopupMenu _popup = null;
81 * Injected project settings service.
83 private ProjectSettingsService _projectSettings;
85 * Injected application settings bean.
87 private ApplicationSettings _applicationSettings;
89 * Injected publication service.
91 private PublicationService _publicationService;
93 * Injected knowledge element service.
95 private KnowledgeElementTypeService _knowledgeElementTypeService;
97 * Injected project element service.
99 private ProjectElementService _projectElementService;
101 public class KnowledgeIterator {
102 protected transient KnowledgeElementType _type;
103 protected transient List<KnowledgeElementFacade> _list;
105 public KnowledgeIterator(final KnowledgeElementType type,
106 final List<KnowledgeElementFacade> list) {
111 public String getIndex() {
112 return String.valueOf(_type.getIndex());
115 public String getType() {
116 return _type.getName();
119 public List<KnowledgeElementFacade> getKnowledgeElements() {
124 // ==============================================================================================================================
126 // ==============================================================================================================================
128 protected AbstractOpenObject() {
129 // All member fields are supposed initialized by subclasses
130 if (_docpres == null) {
131 _docpres = new HashMap<Long, DocumentFacade>();
133 if (_knowpres == null) {
134 _knowpres = new HashMap<Long, KnowledgeElementFacade>();
138 // ==============================================================================================================================
139 // Public member functions
140 // ==============================================================================================================================
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)) {
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
158 KnowledgeElementFacade kelm = j.next();
159 if (!kelm.getIndex().equals(index)) {
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
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)) {
188 public String getDisplayedDescription() {
192 public List<DocumentFacade> getDisplayedDocuments() {
196 public List<SimulationContextFacade> getDisplayedSimulationContexts() {
200 public List<KnowledgeIterator> getDisplayedKnowledges() {
204 public List<Step> getInvolvedSteps() {
208 public Menu getMenu() {
212 public PopupMenu getPopup() {
216 public Step getSelectedStep() {
220 public String getSelection() {
224 public String getSelectedScenarioId() {
226 if (_selection == null) {
229 res = _selection.substring(0, _selection.indexOf('.'));
234 public User getUser() {
238 public boolean isOpenForWriting() {
239 return (_popup != null); // The pop-up is supposed existed when the user is staffed on the study
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)) {
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)) {
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
270 KnowledgeElementFacade kelm = j.next();
271 if (!kelm.getIndex().equals(index)) {
280 // ==============================================================================================================================
281 // Protected services
282 // ==============================================================================================================================
284 protected void setupContents() {
286 // Initialized in subclasses
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();
300 _knowledge = new ArrayList<KnowledgeIterator>(types.size());
301 for (Iterator<KnowledgeElementType> i = types.iterator(); i
303 KnowledgeElementType type = i.next();
304 List<KnowledgeElementFacade> display = new ArrayList<KnowledgeElementFacade>(
306 while (current != null && current.getType().equals(type)) {
307 KnowledgeElementFacade facade = _knowpres.get(current
309 if (facade == null) {
310 facade = new KnowledgeElementFacade(BeanHelper
311 .copyBean(current, KnowledgeElementDTO.class),
312 getApplicationSettings());
313 _knowpres.put(current.getIndex(), facade);
316 if (more.hasNext()) {
317 current = more.next();
322 _knowledge.add(new KnowledgeIterator(type, display));
328 if (_ustep.mayContain(Document.class)) {
329 List<Publication> list = _ustep.getAllDocuments();
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);
342 _contents.add(facade);
350 * Get project settings.
352 * @return Project settings service
354 public ProjectSettingsService getProjectSettings() {
355 return _projectSettings;
359 * Get the publicationService.
361 * @return the publicationService
363 public PublicationService getPublicationService() {
364 return _publicationService;
368 * Set project settings service.
370 * @param projectSettingsService
371 * project settings service
373 public void setProjectSettings(
374 final ProjectSettingsService projectSettingsService) {
375 _projectSettings = projectSettingsService;
379 * Set the publicationService.
381 * @param publicationService
382 * the publicationService to set
384 public void setPublicationService(
385 final PublicationService publicationService) {
386 _publicationService = publicationService;
395 public void setMenu(final Menu menu) {
400 * Get the projectElementService.
402 * @return the projectElementService
404 public ProjectElementService getProjectElementService() {
405 return _projectElementService;
409 * Set the projectElementService.
411 * @param projectElementService
412 * the projectElementService to set
414 public void setProjectElementService(
415 final ProjectElementService projectElementService) {
416 _projectElementService = projectElementService;
420 * Get the knowledgeElementTypeService.
422 * @return the knowledgeElementTypeService
424 public KnowledgeElementTypeService getKnowledgeElementTypeService() {
425 return _knowledgeElementTypeService;
429 * Set the knowledgeElementTypeService.
431 * @param knowledgeElementTypeService
432 * the knowledgeElementTypeService to set
434 public void setKnowledgeElementTypeService(
435 final KnowledgeElementTypeService knowledgeElementTypeService) {
436 _knowledgeElementTypeService = knowledgeElementTypeService;
440 * Get the applicationSettings.
442 * @return the applicationSettings
444 public ApplicationSettings getApplicationSettings() {
445 return _applicationSettings;
449 * Set the applicationSettings.
451 * @param applicationSettings
452 * the applicationSettings to set
454 public void setApplicationSettings(
455 final ApplicationSettings applicationSettings) {
456 _applicationSettings = applicationSettings;