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.dal.bo.som.UsesRelation;
27 import org.splat.kernel.Do;
28 import org.splat.log.AppLogger;
29 import org.splat.manox.Toolbox;
30 import org.splat.manox.Writer;
31 import org.splat.service.DocumentService;
32 import org.splat.service.DocumentTypeService;
33 import org.splat.service.StepService;
34 import org.splat.service.StudyService;
35 import org.splat.service.dto.KnowledgeElementDTO;
36 import org.splat.service.technical.RepositoryService;
37 import org.splat.som.Revision;
38 import org.splat.som.Step;
39 import org.splat.som.StepRights;
40 import org.splat.som.StudyRights;
41 import org.splat.util.BeanHelper;
42 import org.splat.wapp.Constants;
43 import org.splat.wapp.ToolBar;
46 * Presentation of the currently open study.
48 public class OpenStudy extends AbstractOpenObject implements OpenStudyServices {
53 protected final static AppLogger LOGGER = org.splat.simer.Action.LOG;
56 * The currently open study.
58 private transient Study _mystudy;
60 * User rights on the open study.
62 private transient StudyRights _urightstudy;
64 * User rights on the selected step.
66 private transient StepRights _urightstep;
70 private transient String _version;
72 * The study creation date.
74 private transient String _credate;
76 * The study modification date.
78 private transient String _lasdate;
80 * The selected document publication.
82 private transient Publication _selecdoc;
84 * Injected step service.
86 private StepService _stepService;
88 * Injected repository service.
90 private RepositoryService _repositoryService;
92 * Injected document type service.
94 private DocumentTypeService _documentTypeService;
96 * Injected document service.
98 private DocumentService _documentService;
100 * The injected Study service.
102 private StudyService _studyService;
104 // =========================================================================
106 // =========================================================================
109 * Open the given study in the current http session.
115 * @return this open study object
117 public OpenStudy open(final User user, final Study study) {
118 ResourceBundle custom = ResourceBundle.getBundle("som",
119 getApplicationSettings().getCurrentLocale());
120 SimpleDateFormat datstring = new SimpleDateFormat(custom
121 .getString("date.format"), getApplicationSettings()
122 .getCurrentLocale());
123 Revision.Format verstring = new Revision.Format(getProjectSettings()
124 .getRevisionPattern());
126 _cuser = user; // May be null if nobody connected
127 if(!study.equals(_mystudy)) {
128 _selection = "0.1"; // Default selection
133 // Preparation of the display
134 _version = verstring.format(_mystudy.getVersion());
135 _credate = datstring.format(_mystudy.getDate());
136 _lasdate = datstring.format(_mystudy.getLastModificationDate());
137 _description = _mystudy.getDescription();
138 //_involving = new ArrayList<Step>(1);
139 _context = new ArrayList<SimulationContextFacade>();
140 if (_selection.equals("0.1")) {
141 _ustep = getProjectElementService().getFirstStep(_mystudy);
143 _ustep.setActor(_cuser);
144 if ((_involving.isEmpty()) || (_involving.get(0).getOwnerStudy().getIndex()
145 != _ustep.getOwnerStudy().getIndex())) {
147 _involving.add(_ustep);
149 for (Iterator<SimulationContext> i = _ustep.getAllSimulationContexts()
150 .iterator(); i.hasNext();) {
151 _context.add(new SimulationContextFacade(i.next(),
152 getProjectSettings().getAllSteps(),
153 getApplicationSettings()));
155 if (getStudyService().isStaffedBy(_mystudy, _cuser)
156 || getStudyService().hasActor(_mystudy, _cuser)) {
157 // ProgressState state = mystudy.getProgressState();
158 // if (state == ProgressState.inCHECK) popup = getApplicationSettings().getPopupMenu("stapprovable");
159 // else if (state == ProgressState.APPROVED) popup = getApplicationSettings().getPopupMenu("stapproved");
162 if (_mystudy.getProgressState() == ProgressState.TEMPLATE) {
163 if (_mystudy.isPublic()) {
164 _popup = getApplicationSettings().getPopupMenu(
165 "steditableunmarkprivate");
167 _popup = getApplicationSettings().getPopupMenu(
168 "steditableunmarkpublic");
172 if (_mystudy.isPublic()) {
173 _popup = getApplicationSettings().getPopupMenu(
174 "steditablemarkprivate");
177 switch(_mystudy.getProgressState()) {
182 key = "streviewable";
185 key = "stapprovable";
188 key = "steditablemarkpublic";
191 if(Long.valueOf(getSelectedScenarioId()) != 0) {
192 key += "ScenarioSelected";
194 _popup = getApplicationSettings().getPopupMenu(key);
198 _popup.setContext(Constants.STUDY_MENU, new StudyRights(_cuser,
201 _urightstudy = new StudyRights(_cuser, _mystudy);
202 _urightstep = new StepRights(_cuser, _ustep);
204 // RKV menu = new StudyMenu(mystudy);
205 _menu = (getMenu()).init(_mystudy); // RKV
206 _menu.selects(_selection); // Initializes menu items to be displayed
207 setupContents(); // Initializes documents and knowledge at ustep
211 // =========================================================================
213 // =========================================================================
218 * @see org.splat.service.dto.Proxy#getAuthorName()
221 public String getAuthorName() {
222 return _mystudy.getAuthor().toString();
228 * @see org.splat.service.dto.Proxy#getIndex()
231 public Long getIndex() {
232 return _mystudy.getIndex();
240 public String getDate() {
247 * @see org.splat.simer.AbstractOpenObject#getMenu()
250 public StudyMenu getMenu() {
251 return (StudyMenu) _menu;
260 public void setMenu(final StudyMenu aMenu) {
267 * @see org.splat.service.dto.Proxy#getProgressState()
270 public ProgressState getProgressState() {
271 return _mystudy.getProgressState();
275 * Get last modification date.
279 public String getLastModificationDate() {
284 * Get the toolbar with buttons of available modules.
286 * @return the toolbar
288 public ToolBar getModuleBar() {
289 return getApplicationSettings().getModuleBar(getSelectedStep());
295 * @see org.splat.service.dto.Proxy#getReference()
298 public String getReference() {
299 return _mystudy.getReference();
305 * @see org.splat.simer.OpenStudyServices#getSelectedDocument()
308 public Publication getSelectedDocument() {
313 * Get user rights for the selected step.
315 * @return user step rights
317 public StepRights getSelectedStepRights() {
322 * Get user rights for the study.
324 * @return user study rights
326 public StudyRights getStudyRights() {
331 * Get the detached study object.
333 * @return the detached study object
335 public Study getStudyObject() {
342 * @see org.splat.service.dto.Proxy#getTitle()
345 public String getTitle() {
346 return _mystudy.getTitle();
352 * @see org.splat.service.dto.Proxy#getType()
355 public String getType() {
356 return Constants.STUDY_MENU;
361 * @see org.splat.simer.AbstractOpenObject#getTypeName()
364 public String getTypeName() {
365 return ResourceBundle.getBundle("labels", getApplicationSettings().getCurrentLocale()).getString( "label.study");
369 * Get the study version.
371 * @return the version string
373 public String getVersion() {
378 * Check if the selected step is enabled for writing.
380 * @return true if the selected step is enabled for writing
382 public boolean isStepEnabled() {
383 return _urightstep.isEnabled();
386 // =========================================================================
388 // =========================================================================
391 public URL newTemplateBasedDocument(final String typename, final User author) {
392 String filename = typename + ".xml"; // Only XML templates are writeable
393 File template = new File(getRepositoryService().getTemplatePath()
395 if (!template.exists()) {
399 // Session connex = Database.getCurSession();
400 // Transaction transax = connex.beginTransaction();
402 File udir = getRepositoryService().getDownloadDirectory(author);
403 File credoc = new File(udir.getPath() + "/" + filename);
405 // Creation of the meta-document
406 Step step = getSelectedStep(); // Should we check if the given document type is compatible ?
407 DocumentType type = getDocumentTypeService().selectType(typename);
408 Document.Properties dprop = new Document.Properties();
409 Document medoc = getStepService().createDocument(step,
410 dprop.setType(type).setFormat("xml").setAuthor(author))
414 // Instantiation of the template into the user download directory
415 if (!udir.exists()) {
418 if (credoc.exists()) {
421 Do.copy(template, credoc);
423 // Transfer to the document of all known properties
424 ResourceBundle locale = ResourceBundle.getBundle("som",
425 getApplicationSettings().getCurrentLocale());
426 SimpleDateFormat get = new SimpleDateFormat(locale
427 .getString("date.format"), getApplicationSettings()
428 .getCurrentLocale());
429 Writer tool = Toolbox.getWriter(credoc);
430 List<Step> slist = getInvolvedSteps();
431 for (Iterator<Step> i = slist.iterator(); i.hasNext();) {
432 List<SimulationContext> clist = i.next()
433 .getAllSimulationContexts();
434 for (Iterator<SimulationContext> j = clist.iterator(); j
436 SimulationContext context = j.next();
437 tool.updateProperty(context.getType().getName(), context
441 tool.updateProperty("reference", medoc.getReference());
442 tool.updateProperty(Constants.STUDY_MENU, _mystudy.getTitle());
443 tool.updateProperty("step", locale.getString(
444 "folder.step." + step.getNumber()).replaceAll("''", "'"));
445 tool.updateProperty("author", author.getUsername().toUpperCase());
446 tool.updateProperty("date", get.format(medoc.getCreationDate()));
447 tool.updateProperty("history", locale.getString("label.creation")
448 .replaceAll("''", "'"));
451 return new URL(getApplicationSettings().getDownloadURL(author)
453 } catch (Exception saverror) {
454 LOGGER.error("Reason:", saverror);
459 public void selectDocument(final String docurl) {
460 String prefix = getApplicationSettings().getRepositoryURL();
462 if (docurl.startsWith(prefix)) {
464 String path = docurl.substring(prefix.length());
465 Document value = getDocumentService().getDocumentByPath(path);
467 _selecdoc = _ustep.getDocument(value.getIndex());
468 } catch (Exception error) {
469 LOGGER.error("Reason:", error);
475 * Select an activity (step).
478 * the key of the step to select
480 public void setSelection(final String step) {
481 if (!step.equals(_selection)) {
484 setupPreviousToSelectedSteps();
485 updateSimulationContexts(); // Initializes contexts according to the selected steps
487 _ustep = _involving.get(_involving.size() - 1);
488 _urightstep = new StepRights(_cuser, _ustep);
489 _ustep.setActor(_cuser);
490 _menu.selects(_selection); // Updates menu items to be displayed
491 setupContents(); // The contents may have changed even if the selection is the same
494 // =========================================================================
495 // Protected services
496 // =========================================================================
499 * Add a new document presentation facade.
502 * the document publication
504 protected void add(final Publication doc) {
505 DocumentFacade facade = new DocumentFacade(this, doc,
506 getProjectSettings(), getPublicationService(),
507 getApplicationSettings());
508 boolean first = (_contents.size() == 0);
510 _docpres.put(doc.getIndex(), facade);
511 _contents.add(0, facade); // Prepend the new publication
513 this.getMenu().refreshSelectedItem();
515 // Refresh dependencies. They can not be removed until removing this document.
516 for (Publication pub : doc.getRelations(UsesRelation.class)) {
522 * Add a simulation context presentation facade.
525 * the simulation context to add
527 protected void add(final SimulationContext contex) {
528 SimulationContextFacade facade = new SimulationContextFacade(contex,
529 getProjectSettings().getAllSteps(), getApplicationSettings());
531 _context.add(facade);
535 * Add a knowledge element presentation facade.
538 * the knowledge element to add
540 protected void add(final KnowledgeElement kelm) {
541 KnowledgeElementFacade facade = new KnowledgeElementFacade(BeanHelper
542 .copyBean(kelm, KnowledgeElementDTO.class),
543 getApplicationSettings());
544 // RKV KnowledgeIterator known = knowledge.get(kelm.getType().getIndex() - 2);
545 // Knowledges are ordered by type index, from 0 to n-1, the first one being reserved (reason for -2)
546 // RKV:Begin: Find a knowledge iterator for appropriate knowledge type
547 KnowledgeIterator known = null;
548 for (KnowledgeIterator aKnowledgeSection : _knowledge) {
549 if (aKnowledgeSection.getIndex().equals(
550 String.valueOf(kelm.getType().getIndex()))) {
551 known = aKnowledgeSection;
555 if (known != null) { // RKV:End
556 _knowpres.put(kelm.getIndex(), facade);
557 known._list.add(facade); // Insert the new knowledge at the end of the corresponding knowledge type
562 * Remove the document presentation facade.
565 * the document publication to remove
567 protected void remove(final Publication doctag) {
568 for (Iterator<DocumentFacade> i = _contents.iterator(); i.hasNext();) {
569 DocumentFacade facade = i.next();
570 if (facade.isFacadeOf(doctag)) {
575 if (_contents.size() == 0) {
576 this.getMenu().refreshSelectedItem();
581 * Change the currently connected user and refresh user's rights.
584 * the new connected user
586 protected void changeUser(final User user) {
589 if (getStudyService().isStaffedBy(_mystudy, _cuser)) {
590 _popup = getApplicationSettings().getPopupMenu(
591 "steditablemarkpublic");
592 _popup.setContext(Constants.STUDY_MENU, new StudyRights(_cuser,
595 // ustep = getProjectElementService().getFirstStep(mystudy);
596 if (_ustep != null) {
597 _ustep.setActor(_cuser);
599 _urightstudy = new StudyRights(_cuser, _mystudy);
600 _urightstep = new StepRights(_cuser, _ustep);
604 * Remove the simulation context presentation facade.
607 * the simulation context to remove
609 protected void remove(final SimulationContext contex) {
610 for (Iterator<SimulationContextFacade> i = _context.iterator(); i
612 SimulationContextFacade facade = i.next();
613 if (facade.isFacadeOf(contex)) {
621 * Remove the knowledge element presentation facade.
624 * the knowledge element to remove
626 protected void remove(final KnowledgeElement kelm) {
627 // RKV: KnowledgeIterator known = _knowledge.get((int) (kelm.getType()
628 // RKV: .getIndex() - 2));
629 // Knowledges are ordered by type index, from 0 to n-1, the first one being reserved (reason for -2)
630 // RKV:Begin: Find a knowledge iterator for appropriate knowledge type
631 KnowledgeIterator known = null;
632 for (KnowledgeIterator aKnowledgeSection : _knowledge) {
633 if (aKnowledgeSection.getIndex().equals(
634 String.valueOf(kelm.getType().getIndex()))) {
635 known = aKnowledgeSection;
639 if (known != null) { // RKV:End
640 _knowpres.remove(kelm.getIndex());
641 for (Iterator<KnowledgeElementFacade> i = known._list.iterator(); i
643 KnowledgeElementFacade facade = i.next();
644 if (!facade.isFacadeOf(BeanHelper.copyBean(kelm,
645 KnowledgeElementDTO.class))) {
655 * Refresh the document presentation facade.
658 * the document publication
660 protected void update(final Publication doc) {
661 DocumentFacade facade = _docpres.get(doc.getIndex());
662 if (facade != null) {
663 _contents.remove(facade);
664 facade = new DocumentFacade(this, doc,
665 getProjectSettings(), getPublicationService(),
666 getApplicationSettings());
667 _docpres.put(doc.getIndex(), facade);
668 _contents.add(facade);
673 * Refresh the knowledge element presentation facade.
676 * the knowledge element DTO
678 protected void update(final KnowledgeElementDTO kelm) {
679 KnowledgeElementFacade facade = _knowpres.get(kelm.getIndex());
680 if (facade != null) {
681 facade.refresh(kelm);
686 * Refresh simulation contexts presentation facades.
688 protected void updateSimulationContexts() {
690 for (Iterator<Step> i = _involving.iterator(); i.hasNext();) {
691 for (Iterator<SimulationContext> j = i.next()
692 .getAllSimulationContexts().iterator(); j.hasNext();) {
693 _context.add(new SimulationContextFacade(j.next(),
694 getProjectSettings().getAllSteps(),
695 getApplicationSettings()));
700 // =========================================================================
702 // =========================================================================
704 private void setupPreviousToSelectedSteps() {
705 String[] item = _selection.split("\\x2E");
706 int major = Integer.valueOf(item[0]);
707 int minor = Integer.valueOf(item[1]);
713 Scenario[] branch = _mystudy.getScenarii();
714 Scenario scenar = branch[0];
715 for (int i = 0; i < branch.length; i++) {
717 if (scenar.getIndex() == major) {
718 break; // Supposed exist
721 step = getProjectElementService().getSteps(scenar);
722 base = step[0].getNumber() - 1;
723 for (int i = 0; i + base < minor; i++) {
724 _involving.add(step[i]);
727 step = getProjectElementService().getSteps(_mystudy);
728 for (int i = step.length - 1; i > -1; i--) {
729 Step firstep = step[i];
730 if (firstep.getNumber() <= base) {
731 _involving.add(0, firstep);
737 * Get the stepService.
739 * @return the stepService
741 public StepService getStepService() {
746 * Set the stepService.
749 * the stepService to set
751 public void setStepService(final StepService stepService) {
752 _stepService = stepService;
756 * Get the repositoryService.
758 * @return the repositoryService
760 public RepositoryService getRepositoryService() {
761 return _repositoryService;
765 * Set the repositoryService.
767 * @param repositoryService
768 * the repositoryService to set
770 public void setRepositoryService(final RepositoryService repositoryService) {
771 _repositoryService = repositoryService;
775 * Get the documentTypeService.
777 * @return the documentTypeService
779 public DocumentTypeService getDocumentTypeService() {
780 return _documentTypeService;
784 * Set the documentTypeService.
786 * @param documentTypeService
787 * the documentTypeService to set
789 public void setDocumentTypeService(
790 final DocumentTypeService documentTypeService) {
791 _documentTypeService = documentTypeService;
795 * Get the studyService.
797 * @return the studyService
799 public StudyService getStudyService() {
800 return _studyService;
804 * Set the studyService.
806 * @param studyService
807 * the studyService to set
809 public void setStudyService(final StudyService studyService) {
810 _studyService = studyService;
814 * Get the documentService.
816 * @return the documentService
818 public DocumentService getDocumentService() {
819 return _documentService;
823 * Set the documentService.
825 * @param documentService
826 * the documentService to set
828 public void setDocumentService(final DocumentService documentService) {
829 _documentService = documentService;
833 * Get the detached study object.
835 * @return the detached study object
837 public Study getMystudy() {
842 * Set the study to present.
845 * the selected study detached object
847 public void setMystudy(final Study mystudy) {
848 this._mystudy = mystudy;