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 _involving.add(_ustep);
145 for (Iterator<SimulationContext> i = _ustep.getAllSimulationContexts()
146 .iterator(); i.hasNext();) {
147 _context.add(new SimulationContextFacade(i.next(),
148 getProjectSettings().getAllSteps(),
149 getApplicationSettings()));
151 if (getStudyService().isStaffedBy(_mystudy, _cuser)
152 || getStudyService().hasActor(_mystudy, _cuser)) {
153 // ProgressState state = mystudy.getProgressState();
154 // if (state == ProgressState.inCHECK) popup = getApplicationSettings().getPopupMenu("stapprovable");
155 // else if (state == ProgressState.APPROVED) popup = getApplicationSettings().getPopupMenu("stapproved");
158 if (_mystudy.getProgressState() == ProgressState.TEMPLATE) {
159 if (_mystudy.isPublic()) {
160 _popup = getApplicationSettings().getPopupMenu(
161 "steditableunmarkprivate");
163 _popup = getApplicationSettings().getPopupMenu(
164 "steditableunmarkpublic");
168 if (_mystudy.isPublic()) {
169 _popup = getApplicationSettings().getPopupMenu(
170 "steditablemarkprivate");
173 switch(_mystudy.getProgressState()) {
178 key = "streviewable";
181 key = "stapprovable";
184 key = "steditablemarkpublic";
187 if(Long.valueOf(getSelectedScenarioId()) != 0) {
188 key += "ScenarioSelected";
190 _popup = getApplicationSettings().getPopupMenu(key);
194 _popup.setContext(Constants.STUDY_MENU, new StudyRights(_cuser,
197 _urightstudy = new StudyRights(_cuser, _mystudy);
198 _urightstep = new StepRights(_cuser, _ustep);
200 // RKV menu = new StudyMenu(mystudy);
201 _menu = (getMenu()).init(_mystudy); // RKV
202 _menu.selects(_selection); // Initializes menu items to be displayed
203 setupContents(); // Initializes documents and knowledge at ustep
207 // =========================================================================
209 // =========================================================================
214 * @see org.splat.service.dto.Proxy#getAuthorName()
217 public String getAuthorName() {
218 return _mystudy.getAuthor().toString();
224 * @see org.splat.service.dto.Proxy#getIndex()
227 public Long getIndex() {
228 return _mystudy.getIndex();
236 public String getDate() {
243 * @see org.splat.simer.AbstractOpenObject#getMenu()
246 public StudyMenu getMenu() {
247 return (StudyMenu) _menu;
256 public void setMenu(final StudyMenu aMenu) {
263 * @see org.splat.service.dto.Proxy#getProgressState()
266 public ProgressState getProgressState() {
267 return _mystudy.getProgressState();
271 * Get last modification date.
275 public String getLastModificationDate() {
280 * Get the toolbar with buttons of available modules.
282 * @return the toolbar
284 public ToolBar getModuleBar() {
285 return getApplicationSettings().getModuleBar(getSelectedStep());
291 * @see org.splat.service.dto.Proxy#getReference()
294 public String getReference() {
295 return _mystudy.getReference();
301 * @see org.splat.simer.OpenStudyServices#getSelectedDocument()
304 public Publication getSelectedDocument() {
309 * Get user rights for the selected step.
311 * @return user step rights
313 public StepRights getSelectedStepRights() {
318 * Get user rights for the study.
320 * @return user study rights
322 public StudyRights getStudyRights() {
327 * Get the detached study object.
329 * @return the detached study object
331 public Study getStudyObject() {
338 * @see org.splat.service.dto.Proxy#getTitle()
341 public String getTitle() {
342 return _mystudy.getTitle();
348 * @see org.splat.service.dto.Proxy#getType()
351 public String getType() {
352 return Constants.STUDY_MENU;
357 * @see org.splat.simer.AbstractOpenObject#getTypeName()
360 public String getTypeName() {
361 return ResourceBundle.getBundle("labels", getApplicationSettings().getCurrentLocale()).getString( "label.study");
365 * Get the study version.
367 * @return the version string
369 public String getVersion() {
374 * Check if the selected step is enabled for writing.
376 * @return true if the selected step is enabled for writing
378 public boolean isStepEnabled() {
379 return _urightstep.isEnabled();
382 // =========================================================================
384 // =========================================================================
387 public URL newTemplateBasedDocument(final String typename, final User author) {
388 String filename = typename + ".xml"; // Only XML templates are writeable
389 File template = new File(getRepositoryService().getTemplatePath()
391 if (!template.exists()) {
395 // Session connex = Database.getCurSession();
396 // Transaction transax = connex.beginTransaction();
398 File udir = getRepositoryService().getDownloadDirectory(author);
399 File credoc = new File(udir.getPath() + "/" + filename);
401 // Creation of the meta-document
402 Step step = getSelectedStep(); // Should we check if the given document type is compatible ?
403 DocumentType type = getDocumentTypeService().selectType(typename);
404 Document.Properties dprop = new Document.Properties();
405 Document medoc = getStepService().createDocument(step,
406 dprop.setType(type).setFormat("xml").setAuthor(author))
410 // Instantiation of the template into the user download directory
411 if (!udir.exists()) {
414 if (credoc.exists()) {
417 Do.copy(template, credoc);
419 // Transfer to the document of all known properties
420 ResourceBundle locale = ResourceBundle.getBundle("som",
421 getApplicationSettings().getCurrentLocale());
422 SimpleDateFormat get = new SimpleDateFormat(locale
423 .getString("date.format"), getApplicationSettings()
424 .getCurrentLocale());
425 Writer tool = Toolbox.getWriter(credoc);
426 List<Step> slist = getInvolvedSteps();
427 for (Iterator<Step> i = slist.iterator(); i.hasNext();) {
428 List<SimulationContext> clist = i.next()
429 .getAllSimulationContexts();
430 for (Iterator<SimulationContext> j = clist.iterator(); j
432 SimulationContext context = j.next();
433 tool.updateProperty(context.getType().getName(), context
437 tool.updateProperty("reference", medoc.getReference());
438 tool.updateProperty(Constants.STUDY_MENU, _mystudy.getTitle());
439 tool.updateProperty("step", locale.getString(
440 "folder.step." + step.getNumber()).replaceAll("''", "'"));
441 tool.updateProperty("author", author.getUsername().toUpperCase());
442 tool.updateProperty("date", get.format(medoc.getCreationDate()));
443 tool.updateProperty("history", locale.getString("label.creation")
444 .replaceAll("''", "'"));
447 return new URL(getApplicationSettings().getDownloadURL(author)
449 } catch (Exception saverror) {
450 LOGGER.error("Reason:", saverror);
455 public void selectDocument(final String docurl) {
456 String prefix = getApplicationSettings().getRepositoryURL();
458 if (docurl.startsWith(prefix)) {
460 String path = docurl.substring(prefix.length());
461 Document value = getDocumentService().getDocumentByPath(path);
463 _selecdoc = _ustep.getDocument(value.getIndex());
464 } catch (Exception error) {
465 LOGGER.error("Reason:", error);
471 * Select an activity (step).
474 * the key of the step to select
476 public void setSelection(final String step) {
477 if (!step.equals(_selection)) {
480 setupPreviousToSelectedSteps();
481 updateSimulationContexts(); // Initializes contexts according to the selected steps
483 _ustep = _involving.get(_involving.size() - 1);
484 _urightstep = new StepRights(_cuser, _ustep);
485 _ustep.setActor(_cuser);
486 _menu.selects(_selection); // Updates menu items to be displayed
487 setupContents(); // The contents may have changed even if the selection is the same
490 // =========================================================================
491 // Protected services
492 // =========================================================================
495 * Add a new document presentation facade.
498 * the document publication
500 protected void add(final Publication doc) {
501 DocumentFacade facade = new DocumentFacade(this, doc,
502 getProjectSettings(), getPublicationService(),
503 getApplicationSettings());
504 boolean first = (_contents.size() == 0);
506 _docpres.put(doc.getIndex(), facade);
507 _contents.add(0, facade); // Prepend the new publication
509 this.getMenu().refreshSelectedItem();
511 // Refresh dependencies. They can not be removed until removing this document.
512 for (Publication pub : doc.getRelations(UsesRelation.class)) {
518 * Add a simulation context presentation facade.
521 * the simulation context to add
523 protected void add(final SimulationContext contex) {
524 SimulationContextFacade facade = new SimulationContextFacade(contex,
525 getProjectSettings().getAllSteps(), getApplicationSettings());
527 _context.add(facade);
531 * Add a knowledge element presentation facade.
534 * the knowledge element to add
536 protected void add(final KnowledgeElement kelm) {
537 KnowledgeElementFacade facade = new KnowledgeElementFacade(BeanHelper
538 .copyBean(kelm, KnowledgeElementDTO.class),
539 getApplicationSettings());
540 // RKV KnowledgeIterator known = knowledge.get(kelm.getType().getIndex() - 2);
541 // Knowledges are ordered by type index, from 0 to n-1, the first one being reserved (reason for -2)
542 // RKV:Begin: Find a knowledge iterator for appropriate knowledge type
543 KnowledgeIterator known = null;
544 for (KnowledgeIterator aKnowledgeSection : _knowledge) {
545 if (aKnowledgeSection.getIndex().equals(
546 String.valueOf(kelm.getType().getIndex()))) {
547 known = aKnowledgeSection;
551 if (known != null) { // RKV:End
552 _knowpres.put(kelm.getIndex(), facade);
553 known._list.add(facade); // Insert the new knowledge at the end of the corresponding knowledge type
558 * Remove the document presentation facade.
561 * the document publication to remove
563 protected void remove(final Publication doctag) {
564 for (Iterator<DocumentFacade> i = _contents.iterator(); i.hasNext();) {
565 DocumentFacade facade = i.next();
566 if (facade.isFacadeOf(doctag)) {
571 if (_contents.size() == 0) {
572 this.getMenu().refreshSelectedItem();
577 * Change the currently connected user and refresh user's rights.
580 * the new connected user
582 protected void changeUser(final User user) {
585 if (getStudyService().isStaffedBy(_mystudy, _cuser)) {
586 _popup = getApplicationSettings().getPopupMenu(
587 "steditablemarkpublic");
588 _popup.setContext(Constants.STUDY_MENU, new StudyRights(_cuser,
591 // ustep = getProjectElementService().getFirstStep(mystudy);
592 if (_ustep != null) {
593 _ustep.setActor(_cuser);
595 _urightstudy = new StudyRights(_cuser, _mystudy);
596 _urightstep = new StepRights(_cuser, _ustep);
600 * Remove the simulation context presentation facade.
603 * the simulation context to remove
605 protected void remove(final SimulationContext contex) {
606 for (Iterator<SimulationContextFacade> i = _context.iterator(); i
608 SimulationContextFacade facade = i.next();
609 if (facade.isFacadeOf(contex)) {
617 * Remove the knowledge element presentation facade.
620 * the knowledge element to remove
622 protected void remove(final KnowledgeElement kelm) {
623 // RKV: KnowledgeIterator known = _knowledge.get((int) (kelm.getType()
624 // RKV: .getIndex() - 2));
625 // Knowledges are ordered by type index, from 0 to n-1, the first one being reserved (reason for -2)
626 // RKV:Begin: Find a knowledge iterator for appropriate knowledge type
627 KnowledgeIterator known = null;
628 for (KnowledgeIterator aKnowledgeSection : _knowledge) {
629 if (aKnowledgeSection.getIndex().equals(
630 String.valueOf(kelm.getType().getIndex()))) {
631 known = aKnowledgeSection;
635 if (known != null) { // RKV:End
636 _knowpres.remove(kelm.getIndex());
637 for (Iterator<KnowledgeElementFacade> i = known._list.iterator(); i
639 KnowledgeElementFacade facade = i.next();
640 if (!facade.isFacadeOf(BeanHelper.copyBean(kelm,
641 KnowledgeElementDTO.class))) {
651 * Refresh the document presentation facade.
654 * the document publication
656 protected void update(final Publication doc) {
657 DocumentFacade facade = _docpres.get(doc.getIndex());
658 if (facade != null) {
659 _contents.remove(facade);
660 facade = new DocumentFacade(this, doc,
661 getProjectSettings(), getPublicationService(),
662 getApplicationSettings());
663 _docpres.put(doc.getIndex(), facade);
664 _contents.add(facade);
669 * Refresh the knowledge element presentation facade.
672 * the knowledge element DTO
674 protected void update(final KnowledgeElementDTO kelm) {
675 KnowledgeElementFacade facade = _knowpres.get(kelm.getIndex());
676 if (facade != null) {
677 facade.refresh(kelm);
682 * Refresh simulation contexts presentation facades.
684 protected void updateSimulationContexts() {
686 for (Iterator<Step> i = _involving.iterator(); i.hasNext();) {
687 for (Iterator<SimulationContext> j = i.next()
688 .getAllSimulationContexts().iterator(); j.hasNext();) {
689 _context.add(new SimulationContextFacade(j.next(),
690 getProjectSettings().getAllSteps(),
691 getApplicationSettings()));
696 // =========================================================================
698 // =========================================================================
700 private void setupPreviousToSelectedSteps() {
701 String[] item = _selection.split("\\x2E");
702 int major = Integer.valueOf(item[0]);
703 int minor = Integer.valueOf(item[1]);
709 Scenario[] branch = _mystudy.getScenarii();
710 Scenario scenar = branch[0];
711 for (int i = 0; i < branch.length; i++) {
713 if (scenar.getIndex() == major) {
714 break; // Supposed exist
717 step = getProjectElementService().getSteps(scenar);
718 base = step[0].getNumber() - 1;
719 for (int i = 0; i + base < minor; i++) {
720 _involving.add(step[i]);
723 step = getProjectElementService().getSteps(_mystudy);
724 for (int i = step.length - 1; i > -1; i--) {
725 Step firstep = step[i];
726 if (firstep.getNumber() <= base) {
727 _involving.add(0, firstep);
733 * Get the stepService.
735 * @return the stepService
737 public StepService getStepService() {
742 * Set the stepService.
745 * the stepService to set
747 public void setStepService(final StepService stepService) {
748 _stepService = stepService;
752 * Get the repositoryService.
754 * @return the repositoryService
756 public RepositoryService getRepositoryService() {
757 return _repositoryService;
761 * Set the repositoryService.
763 * @param repositoryService
764 * the repositoryService to set
766 public void setRepositoryService(final RepositoryService repositoryService) {
767 _repositoryService = repositoryService;
771 * Get the documentTypeService.
773 * @return the documentTypeService
775 public DocumentTypeService getDocumentTypeService() {
776 return _documentTypeService;
780 * Set the documentTypeService.
782 * @param documentTypeService
783 * the documentTypeService to set
785 public void setDocumentTypeService(
786 final DocumentTypeService documentTypeService) {
787 _documentTypeService = documentTypeService;
791 * Get the studyService.
793 * @return the studyService
795 public StudyService getStudyService() {
796 return _studyService;
800 * Set the studyService.
802 * @param studyService
803 * the studyService to set
805 public void setStudyService(final StudyService studyService) {
806 _studyService = studyService;
810 * Get the documentService.
812 * @return the documentService
814 public DocumentService getDocumentService() {
815 return _documentService;
819 * Set the documentService.
821 * @param documentService
822 * the documentService to set
824 public void setDocumentService(final DocumentService documentService) {
825 _documentService = documentService;
829 * Get the detached study object.
831 * @return the detached study object
833 public Study getMystudy() {
838 * Set the study to present.
841 * the selected study detached object
843 public void setMystudy(final Study mystudy) {
844 this._mystudy = mystudy;