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)) {
153 public void developKnowledge(final String index) {
154 for (Iterator<KnowledgeIterator> i = _knowledge.iterator(); i.hasNext();) {
155 List<KnowledgeElementFacade> knowelms = i.next()
156 .getKnowledgeElements();
157 for (Iterator<KnowledgeElementFacade> j = knowelms.iterator(); j
159 KnowledgeElementFacade kelm = j.next();
160 if (!kelm.getIndex().equals(index)) {
169 public void clearFacades() {
170 _docpres.clear(); // For eventually reopening the knowledge from a fresh context
171 _knowpres.clear(); // For eventually reopening the knowledge from a fresh context
174 public List<Document> collectInvolvedDocuments(final DocumentType type) {
175 List<Document> found = new ArrayList<Document>();
176 for (Iterator<Step> i = _involving.iterator(); i.hasNext();) {
177 Step step = i.next();
178 List<Publication> exist = step.getAllDocuments();
179 for (Iterator<Publication> j = exist.iterator(); j.hasNext();) {
180 Document doc = j.next().value();
181 if (doc.getType().equals(type)) {
189 public String getDisplayedDescription() {
193 public List<DocumentFacade> getDisplayedDocuments() {
197 public List<SimulationContextFacade> getDisplayedSimulationContexts() {
201 public List<KnowledgeIterator> getDisplayedKnowledges() {
205 public List<Step> getInvolvedSteps() {
209 public Menu getMenu() {
213 public PopupMenu getPopup() {
217 public Step getSelectedStep() {
221 public String getSelection() {
225 public String getSelectedScenarioId() {
227 if (_selection == null) {
230 res = _selection.substring(0, _selection.indexOf('.'));
235 public User getUser() {
239 public boolean isOpenForWriting() {
240 return (_popup != null); // The pop-up is supposed existed when the user is staffed on the study
243 public void reduceDocument(final String index) {
244 for (Iterator<DocumentFacade> i = _contents.iterator(); i.hasNext();) {
245 DocumentFacade doc = i.next();
246 if (!doc.getIndex().equals(index)) {
254 public void reduceHistory(final String index) {
255 for (Iterator<DocumentFacade> i = _contents.iterator(); i.hasNext();) {
256 DocumentFacade doc = i.next();
257 if (!doc.getIndex().equals(index)) {
265 public void reduceKnowledge(final String index) {
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
271 KnowledgeElementFacade kelm = j.next();
272 if (!kelm.getIndex().equals(index)) {
281 // ==============================================================================================================================
282 // Protected services
283 // ==============================================================================================================================
285 protected void setupContents() {
287 // Initialized in subclasses
289 // Knowledge elements supposed ordered by type
290 if (_ustep.mayContain(KnowledgeElement.class)) {
291 Scenario scene = (Scenario) _ustep.getOwner();
292 List<KnowledgeElementType> types = getKnowledgeElementTypeService()
293 .selectTypesWhere(ProgressState.APPROVED);
294 List<KnowledgeElement> kelms = scene.getAllKnowledgeElements();
295 Iterator<KnowledgeElement> more = kelms.iterator();
296 KnowledgeElement current = null;
297 if (more.hasNext()) {
298 current = more.next();
301 _knowledge = new ArrayList<KnowledgeIterator>(types.size());
302 for (Iterator<KnowledgeElementType> i = types.iterator(); i
304 KnowledgeElementType type = i.next();
305 List<KnowledgeElementFacade> display = new ArrayList<KnowledgeElementFacade>(
307 while (current != null && current.getType().equals(type)) {
308 KnowledgeElementFacade facade = _knowpres.get(current
310 if (facade == null) {
311 facade = new KnowledgeElementFacade(BeanHelper
312 .copyBean(current, KnowledgeElementDTO.class),
313 getApplicationSettings());
314 _knowpres.put(current.getIndex(), facade);
317 if (more.hasNext()) {
318 current = more.next();
323 _knowledge.add(new KnowledgeIterator(type, display));
329 if (_ustep.mayContain(Document.class)) {
330 List<Publication> list = _ustep.getAllDocuments();
332 _contents = new ArrayList<DocumentFacade>(list.size());
333 for (Iterator<Publication> i = list.iterator(); i.hasNext();) {
334 Publication present = i.next();
335 Long index = present.getIndex();
336 DocumentFacade facade = _docpres.get(index);
337 if (facade == null) {
338 facade = new DocumentFacade(this, present,
339 getProjectSettings(), getPublicationService(),
340 getApplicationSettings());
341 _docpres.put(index, facade);
343 _contents.add(facade);
351 * Get project settings.
353 * @return Project settings service
355 public ProjectSettingsService getProjectSettings() {
356 return _projectSettings;
360 * Get the publicationService.
362 * @return the publicationService
364 public PublicationService getPublicationService() {
365 return _publicationService;
369 * Set project settings service.
371 * @param projectSettingsService
372 * project settings service
374 public void setProjectSettings(
375 final ProjectSettingsService projectSettingsService) {
376 _projectSettings = projectSettingsService;
380 * Set the publicationService.
382 * @param publicationService
383 * the publicationService to set
385 public void setPublicationService(
386 final PublicationService publicationService) {
387 _publicationService = publicationService;
396 public void setMenu(final Menu menu) {
401 * Get the projectElementService.
403 * @return the projectElementService
405 public ProjectElementService getProjectElementService() {
406 return _projectElementService;
410 * Set the projectElementService.
412 * @param projectElementService
413 * the projectElementService to set
415 public void setProjectElementService(
416 final ProjectElementService projectElementService) {
417 _projectElementService = projectElementService;
421 * Get the knowledgeElementTypeService.
423 * @return the knowledgeElementTypeService
425 public KnowledgeElementTypeService getKnowledgeElementTypeService() {
426 return _knowledgeElementTypeService;
430 * Set the knowledgeElementTypeService.
432 * @param knowledgeElementTypeService
433 * the knowledgeElementTypeService to set
435 public void setKnowledgeElementTypeService(
436 final KnowledgeElementTypeService knowledgeElementTypeService) {
437 _knowledgeElementTypeService = knowledgeElementTypeService;
441 * Get the applicationSettings.
443 * @return the applicationSettings
445 public ApplicationSettings getApplicationSettings() {
446 return _applicationSettings;
450 * Set the applicationSettings.
452 * @param applicationSettings
453 * the applicationSettings to set
455 public void setApplicationSettings(
456 final ApplicationSettings applicationSettings) {
457 _applicationSettings = applicationSettings;