1 package org.splat.simer;
5 * @author Daniel Brunier-Coulin
6 * @copyright OPEN CASCADE 2012
11 import java.text.SimpleDateFormat;
12 import java.util.ArrayList;
13 import java.util.Iterator;
14 import java.util.List;
15 import java.util.ResourceBundle;
17 import org.splat.dal.bo.kernel.User;
18 import org.splat.dal.bo.som.Document;
19 import org.splat.dal.bo.som.DocumentType;
20 import org.splat.dal.bo.som.KnowledgeElement;
21 import org.splat.dal.bo.som.ProgressState;
22 import org.splat.dal.bo.som.Publication;
23 import org.splat.dal.bo.som.Scenario;
24 import org.splat.dal.bo.som.SimulationContext;
25 import org.splat.dal.bo.som.Study;
26 import org.splat.kernel.Do;
27 import org.splat.log.AppLogger;
28 import org.splat.manox.Toolbox;
29 import org.splat.manox.Writer;
30 import org.splat.service.DocumentService;
31 import org.splat.service.DocumentTypeService;
32 import org.splat.service.StepService;
33 import org.splat.service.StudyService;
34 import org.splat.service.dto.KnowledgeElementDTO;
35 import org.splat.service.technical.RepositoryService;
36 import org.splat.som.Revision;
37 import org.splat.som.Step;
38 import org.splat.som.StepRights;
39 import org.splat.som.StudyRights;
40 import org.splat.util.BeanHelper;
41 import org.splat.wapp.Constants;
42 import org.splat.wapp.ToolBar;
45 * Presentation of the currently open study.
47 public class OpenStudy extends AbstractOpenObject implements OpenStudyServices {
52 protected final static AppLogger LOGGER = org.splat.simer.Action.LOG;
55 * The currently open study.
57 private transient Study _mystudy;
59 * User rights on the open study.
61 private transient StudyRights _urightstudy;
63 * User rights on the selected step.
65 private transient StepRights _urightstep;
66 private transient String _version;
67 private transient String _credate;
68 private transient String _lasdate;
69 private transient Publication _selecdoc;
71 * Injected step service.
73 private StepService _stepService;
75 * Injected repository service.
77 private RepositoryService _repositoryService;
79 * Injected document type service.
81 private DocumentTypeService _documentTypeService;
83 * Injected document service.
85 private DocumentService _documentService;
87 * The injected Study service.
89 private StudyService _studyService;
91 // ==============================================================================================================================
93 // ==============================================================================================================================
96 * Open the given study in the current http session.
102 * @return this open study object
104 public OpenStudy open(final User user, final Study study) {
105 ResourceBundle custom = ResourceBundle.getBundle("som",
106 getApplicationSettings().getCurrentLocale());
107 SimpleDateFormat datstring = new SimpleDateFormat(custom
108 .getString("date.format"), getApplicationSettings()
109 .getCurrentLocale());
110 Revision.Format verstring = new Revision.Format(getProjectSettings()
111 .getRevisionPattern());
113 _cuser = user; // May be null if nobody connected
115 _selection = "0.1"; // Default selection
118 // Preparation of the display
119 _version = verstring.format(_mystudy.getVersion());
120 _credate = datstring.format(_mystudy.getDate());
121 _lasdate = ""; // Not yet supported
122 _description = _mystudy.getDescription();
123 _involving = new ArrayList<Step>(1);
124 _context = new ArrayList<SimulationContextFacade>();
125 _ustep = getProjectElementService().getFirstStep(_mystudy);
126 _ustep.setActor(_cuser);
127 _involving.add(_ustep);
128 for (Iterator<SimulationContext> i = _ustep.getAllSimulationContexts()
129 .iterator(); i.hasNext();) {
130 _context.add(new SimulationContextFacade(i.next(),
131 getProjectSettings().getAllSteps(),
132 getApplicationSettings()));
134 if (getStudyService().isStaffedBy(_mystudy, _cuser)
135 || getStudyService().hasActor(_mystudy, _cuser)) {
136 // ProgressState state = mystudy.getProgressState();
137 // if (state == ProgressState.inCHECK) popup = getApplicationSettings().getPopupMenu("stapprovable");
138 // else if (state == ProgressState.APPROVED) popup = getApplicationSettings().getPopupMenu("stapproved");
141 if (_mystudy.getProgressState() == ProgressState.TEMPLATE) {
142 if (_mystudy.isPublic()) {
143 _popup = getApplicationSettings().getPopupMenu(
144 "steditableunmarkprivate");
146 _popup = getApplicationSettings().getPopupMenu(
147 "steditableunmarkpublic");
151 if (_mystudy.isPublic()) {
152 _popup = getApplicationSettings().getPopupMenu(
153 "steditablemarkprivate");
155 if (_mystudy.getProgressState() == ProgressState.inWORK) {
156 _popup = getApplicationSettings().getPopupMenu(
158 } else if (_mystudy.getProgressState() == ProgressState.inDRAFT) {
159 _popup = getApplicationSettings().getPopupMenu(
161 } else if (_mystudy.getProgressState() == ProgressState.inCHECK) {
162 _popup = getApplicationSettings().getPopupMenu(
165 _popup = getApplicationSettings().getPopupMenu(
166 "steditablemarkpublic");
171 _popup.setContext(Constants.STUDY_MENU, new StudyRights(_cuser,
174 _urightstudy = new StudyRights(_cuser, _mystudy);
175 _urightstep = new StepRights(_cuser, _ustep);
177 // RKV menu = new StudyMenu(mystudy);
178 _menu = (getMenu()).init(_mystudy); // RKV
179 _menu.selects(_selection); // Initializes menu items to be displayed
180 setupContents(); // Initializes documents and knowledge at ustep
184 // ==============================================================================================================================
186 // ==============================================================================================================================
189 public String getAuthorName() {
190 return _mystudy.getAuthor().toString();
194 public Long getIndex() {
195 return _mystudy.getIndex();
198 public String getDate() {
203 public StudyMenu getMenu() {
204 return (StudyMenu) _menu;
207 public void setMenu(final StudyMenu aMenu) {
212 public ProgressState getProgressState() {
213 return _mystudy.getProgressState();
216 public String getLastModificationDate() {
220 public ToolBar getModuleBar() {
221 return getApplicationSettings().getModuleBar(getSelectedStep());
225 public String getReference() {
226 return _mystudy.getReference();
230 public Publication getSelectedDocument() {
234 public StepRights getSelectedStepRights() {
238 public StudyRights getStudyRights() {
242 public Study getStudyObject() {
247 public String getTitle() {
248 return _mystudy.getTitle();
252 public String getType() {
254 * RKV: return ResourceBundle.getBundle("labels", getApplicationSettings().getCurrentLocale()).getString( "label.study");
256 return Constants.STUDY_MENU;
259 public String getVersion() {
263 public boolean isStepEnabled() {
264 return _urightstep.isEnabled();
267 // ==============================================================================================================================
269 // ==============================================================================================================================
272 public URL newTemplateBasedDocument(final String typename, final User author) {
273 String filename = typename + ".xml"; // Only XML templates are writeable
274 File template = new File(getRepositoryService().getTemplatePath()
276 if (!template.exists()) {
280 // Session connex = Database.getCurSession();
281 // Transaction transax = connex.beginTransaction();
283 File udir = getRepositoryService().getDownloadDirectory(author);
284 File credoc = new File(udir.getPath() + "/" + filename);
286 // Creation of the meta-document
287 Step step = getSelectedStep(); // Should we check if the given document type is compatible ?
288 DocumentType type = getDocumentTypeService().selectType(typename);
289 Document.Properties dprop = new Document.Properties();
290 Document medoc = getStepService().createDocument(step,
291 dprop.setType(type).setFormat("xml").setAuthor(author))
295 // Instantiation of the template into the user download directory
296 if (!udir.exists()) {
299 if (credoc.exists()) {
302 Do.copy(template, credoc);
304 // Transfer to the document of all known properties
305 ResourceBundle locale = ResourceBundle.getBundle("som",
306 getApplicationSettings().getCurrentLocale());
307 SimpleDateFormat get = new SimpleDateFormat(locale
308 .getString("date.format"), getApplicationSettings()
309 .getCurrentLocale());
310 Writer tool = Toolbox.getWriter(credoc);
311 List<Step> slist = getInvolvedSteps();
312 for (Iterator<Step> i = slist.iterator(); i.hasNext();) {
313 List<SimulationContext> clist = i.next()
314 .getAllSimulationContexts();
315 for (Iterator<SimulationContext> j = clist.iterator(); j
317 SimulationContext context = j.next();
318 tool.updateProperty(context.getType().getName(), context
322 tool.updateProperty("reference", medoc.getReference());
323 tool.updateProperty(Constants.STUDY_MENU, _mystudy.getTitle());
324 tool.updateProperty("step", locale.getString(
325 "folder.step." + step.getNumber()).replaceAll("''", "'"));
326 tool.updateProperty("author", author.getUsername().toUpperCase());
327 tool.updateProperty("date", get.format(medoc.getCreationDate()));
328 tool.updateProperty("history", locale.getString("label.creation")
329 .replaceAll("''", "'"));
332 return new URL(getApplicationSettings().getDownloadURL(author)
334 } catch (Exception saverror) {
335 LOGGER.error("Reason:", saverror);
340 public void selectDocument(final String docurl) {
341 String prefix = getApplicationSettings().getRepositoryURL();
343 if (docurl.startsWith(prefix)) {
345 String path = docurl.substring(prefix.length());
346 Document value = getDocumentService().getDocumentByPath(path);
348 _selecdoc = _ustep.getDocument(value.getIndex());
349 } catch (Exception error) {
350 LOGGER.error("Reason:", error);
355 public void setSelection(final String step) {
356 if (!step.equals(_selection)) {
359 setupPreviousToSelectedSteps();
360 updateSimulationContexts(); // Initializes contexts according to the selected steps
362 _ustep = _involving.get(_involving.size() - 1);
363 _urightstep = new StepRights(_cuser, _ustep);
364 _ustep.setActor(_cuser);
365 _menu.selects(_selection); // Updates menu items to be displayed
366 setupContents(); // The contents may have changed even if the selection is the same
369 // ==============================================================================================================================
370 // Protected services
371 // ==============================================================================================================================
373 protected void add(final Publication doc) {
374 DocumentFacade facade = new DocumentFacade(this, doc,
375 getProjectSettings(), getPublicationService(),
376 getApplicationSettings());
377 boolean first = (_contents.size() == 0);
379 _docpres.put(doc.getIndex(), facade);
380 _contents.add(0, facade); // Prepend the new publication
382 this.getMenu().refreshSelectedItem();
387 protected void add(final SimulationContext contex) {
388 SimulationContextFacade facade = new SimulationContextFacade(contex,
389 getProjectSettings().getAllSteps(), getApplicationSettings());
391 _context.add(facade);
394 protected void add(final KnowledgeElement kelm) {
395 KnowledgeElementFacade facade = new KnowledgeElementFacade(BeanHelper
396 .copyBean(kelm, KnowledgeElementDTO.class),
397 getApplicationSettings());
398 // RKV KnowledgeIterator known = knowledge.get(kelm.getType().getIndex() - 2);
399 // Knowledges are ordered by type index, from 0 to n-1, the first one being reserved (reason for -2)
400 // RKV:Begin: Find a knowledge iterator for appropriate knowledge type
401 KnowledgeIterator known = null;
402 for (KnowledgeIterator aKnowledgeSection : _knowledge) {
403 if (aKnowledgeSection.getIndex().equals(
404 String.valueOf(kelm.getType().getIndex()))) {
405 known = aKnowledgeSection;
409 if (known != null) { // RKV:End
410 _knowpres.put(kelm.getIndex(), facade);
411 known._list.add(facade); // Insert the new knowledge at the end of the corresponding knowledge type
415 protected void remove(final Publication doctag) {
416 for (Iterator<DocumentFacade> i = _contents.iterator(); i.hasNext();) {
417 DocumentFacade facade = i.next();
418 if (facade.isFacadeOf(doctag)) {
423 if (_contents.size() == 0) {
424 this.getMenu().refreshSelectedItem();
428 protected void changeUser(final User user) {
431 if (getStudyService().isStaffedBy(_mystudy, _cuser)) {
432 _popup = getApplicationSettings().getPopupMenu(
433 "steditablemarkpublic");
434 _popup.setContext(Constants.STUDY_MENU, new StudyRights(_cuser,
437 // ustep = getProjectElementService().getFirstStep(mystudy);
438 if (_ustep != null) {
439 _ustep.setActor(_cuser);
441 _urightstudy = new StudyRights(_cuser, _mystudy);
442 _urightstep = new StepRights(_cuser, _ustep);
445 protected void remove(final SimulationContext contex) {
446 for (Iterator<SimulationContextFacade> i = _context.iterator(); i
448 SimulationContextFacade facade = i.next();
449 if (!facade.isFacadeOf(contex)) {
457 protected void remove(final KnowledgeElement kelm) {
458 // RKV: KnowledgeIterator known = _knowledge.get((int) (kelm.getType()
459 // RKV: .getIndex() - 2));
460 // Knowledges are ordered by type index, from 0 to n-1, the first one being reserved (reason for -2)
461 // RKV:Begin: Find a knowledge iterator for appropriate knowledge type
462 KnowledgeIterator known = null;
463 for (KnowledgeIterator aKnowledgeSection : _knowledge) {
464 if (aKnowledgeSection.getIndex().equals(
465 String.valueOf(kelm.getType().getIndex()))) {
466 known = aKnowledgeSection;
470 if (known != null) { // RKV:End
471 _knowpres.remove(kelm.getIndex());
472 for (Iterator<KnowledgeElementFacade> i = known._list.iterator(); i
474 KnowledgeElementFacade facade = i.next();
475 if (!facade.isFacadeOf(BeanHelper.copyBean(kelm,
476 KnowledgeElementDTO.class))) {
485 protected void update(final Publication doc) {
486 DocumentFacade facade = _docpres.get(doc.getIndex());
487 if (facade != null) {
492 protected void update(final KnowledgeElementDTO kelm) {
493 KnowledgeElementFacade facade = _knowpres.get(kelm.getIndex());
494 if (facade != null) {
495 facade.refresh(kelm);
499 protected void updateSimulationContexts() {
501 for (Iterator<Step> i = _involving.iterator(); i.hasNext();) {
502 for (Iterator<SimulationContext> j = i.next()
503 .getAllSimulationContexts().iterator(); j.hasNext();) {
504 _context.add(new SimulationContextFacade(j.next(),
505 getProjectSettings().getAllSteps(),
506 getApplicationSettings()));
511 // ==============================================================================================================================
513 // ==============================================================================================================================
515 private void setupPreviousToSelectedSteps() {
516 String[] item = _selection.split("\\x2E");
517 int major = Integer.valueOf(item[0]);
518 int minor = Integer.valueOf(item[1]);
524 Scenario[] branch = _mystudy.getScenarii();
525 Scenario scenar = branch[0];
526 for (int i = 0; i < branch.length; i++) {
528 if (scenar.getIndex() == major) {
529 break; // Supposed exist
532 step = getProjectElementService().getSteps(scenar);
533 base = step[0].getNumber() - 1;
534 for (int i = 0; i + base < minor; i++) {
535 _involving.add(step[i]);
538 step = getProjectElementService().getSteps(_mystudy);
539 for (int i = step.length - 1; i > -1; i--) {
540 Step firstep = step[i];
541 if (firstep.getNumber() <= base) {
542 _involving.add(0, firstep);
548 * Get the stepService.
550 * @return the stepService
552 public StepService getStepService() {
557 * Set the stepService.
560 * the stepService to set
562 public void setStepService(final StepService stepService) {
563 _stepService = stepService;
567 * Get the repositoryService.
569 * @return the repositoryService
571 public RepositoryService getRepositoryService() {
572 return _repositoryService;
576 * Set the repositoryService.
578 * @param repositoryService
579 * the repositoryService to set
581 public void setRepositoryService(final RepositoryService repositoryService) {
582 _repositoryService = repositoryService;
586 * Get the documentTypeService.
588 * @return the documentTypeService
590 public DocumentTypeService getDocumentTypeService() {
591 return _documentTypeService;
595 * Set the documentTypeService.
597 * @param documentTypeService
598 * the documentTypeService to set
600 public void setDocumentTypeService(
601 final DocumentTypeService documentTypeService) {
602 _documentTypeService = documentTypeService;
606 * Get the studyService.
608 * @return the studyService
610 public StudyService getStudyService() {
611 return _studyService;
615 * Set the studyService.
617 * @param studyService
618 * the studyService to set
620 public void setStudyService(final StudyService studyService) {
621 _studyService = studyService;
625 * Get the documentService.
627 * @return the documentService
629 public DocumentService getDocumentService() {
630 return _documentService;
634 * Set the documentService.
636 * @param documentService
637 * the documentService to set
639 public void setDocumentService(final DocumentService documentService) {
640 _documentService = documentService;
643 public Study getMystudy() {
647 public void setMystudy(final Study mystudy) {
648 this._mystudy = mystudy;