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
128 _selection = "0.1"; // Default selection
131 // Preparation of the display
132 _version = verstring.format(_mystudy.getVersion());
133 _credate = datstring.format(_mystudy.getDate());
134 _lasdate = datstring.format(_mystudy.getLastModificationDate());
135 _description = _mystudy.getDescription();
136 _involving = new ArrayList<Step>(1);
137 _context = new ArrayList<SimulationContextFacade>();
138 _ustep = getProjectElementService().getFirstStep(_mystudy);
139 _ustep.setActor(_cuser);
140 _involving.add(_ustep);
141 for (Iterator<SimulationContext> i = _ustep.getAllSimulationContexts()
142 .iterator(); i.hasNext();) {
143 _context.add(new SimulationContextFacade(i.next(),
144 getProjectSettings().getAllSteps(),
145 getApplicationSettings()));
147 if (getStudyService().isStaffedBy(_mystudy, _cuser)
148 || getStudyService().hasActor(_mystudy, _cuser)) {
149 // ProgressState state = mystudy.getProgressState();
150 // if (state == ProgressState.inCHECK) popup = getApplicationSettings().getPopupMenu("stapprovable");
151 // else if (state == ProgressState.APPROVED) popup = getApplicationSettings().getPopupMenu("stapproved");
154 if (_mystudy.getProgressState() == ProgressState.TEMPLATE) {
155 if (_mystudy.isPublic()) {
156 _popup = getApplicationSettings().getPopupMenu(
157 "steditableunmarkprivate");
159 _popup = getApplicationSettings().getPopupMenu(
160 "steditableunmarkpublic");
164 if (_mystudy.isPublic()) {
165 _popup = getApplicationSettings().getPopupMenu(
166 "steditablemarkprivate");
168 if (_mystudy.getProgressState() == ProgressState.inWORK) {
169 _popup = getApplicationSettings().getPopupMenu(
171 } else if (_mystudy.getProgressState() == ProgressState.inDRAFT) {
172 _popup = getApplicationSettings().getPopupMenu(
174 } else if (_mystudy.getProgressState() == ProgressState.inCHECK) {
175 _popup = getApplicationSettings().getPopupMenu(
178 _popup = getApplicationSettings().getPopupMenu(
179 "steditablemarkpublic");
184 _popup.setContext(Constants.STUDY_MENU, new StudyRights(_cuser,
187 _urightstudy = new StudyRights(_cuser, _mystudy);
188 _urightstep = new StepRights(_cuser, _ustep);
190 // RKV menu = new StudyMenu(mystudy);
191 _menu = (getMenu()).init(_mystudy); // RKV
192 _menu.selects(_selection); // Initializes menu items to be displayed
193 setupContents(); // Initializes documents and knowledge at ustep
197 // =========================================================================
199 // =========================================================================
204 * @see org.splat.service.dto.Proxy#getAuthorName()
207 public String getAuthorName() {
208 return _mystudy.getAuthor().toString();
214 * @see org.splat.service.dto.Proxy#getIndex()
217 public Long getIndex() {
218 return _mystudy.getIndex();
226 public String getDate() {
233 * @see org.splat.simer.AbstractOpenObject#getMenu()
236 public StudyMenu getMenu() {
237 return (StudyMenu) _menu;
246 public void setMenu(final StudyMenu aMenu) {
253 * @see org.splat.service.dto.Proxy#getProgressState()
256 public ProgressState getProgressState() {
257 return _mystudy.getProgressState();
261 * Get last modification date.
265 public String getLastModificationDate() {
270 * Get the toolbar with buttons of available modules.
272 * @return the toolbar
274 public ToolBar getModuleBar() {
275 return getApplicationSettings().getModuleBar(getSelectedStep());
281 * @see org.splat.service.dto.Proxy#getReference()
284 public String getReference() {
285 return _mystudy.getReference();
291 * @see org.splat.simer.OpenStudyServices#getSelectedDocument()
294 public Publication getSelectedDocument() {
299 * Get user rights for the selected step.
301 * @return user step rights
303 public StepRights getSelectedStepRights() {
308 * Get user rights for the study.
310 * @return user study rights
312 public StudyRights getStudyRights() {
317 * Get the detached study object.
319 * @return the detached study object
321 public Study getStudyObject() {
328 * @see org.splat.service.dto.Proxy#getTitle()
331 public String getTitle() {
332 return _mystudy.getTitle();
338 * @see org.splat.service.dto.Proxy#getType()
341 public String getType() {
342 return Constants.STUDY_MENU;
345 public String getTypeName() {
346 return ResourceBundle.getBundle("labels", getApplicationSettings().getCurrentLocale()).getString( "label.study");
350 * Get the study version.
352 * @return the version string
354 public String getVersion() {
359 * Check if the selected step is enabled for writing.
361 * @return true if the selected step is enabled for writing
363 public boolean isStepEnabled() {
364 return _urightstep.isEnabled();
367 // =========================================================================
369 // =========================================================================
372 public URL newTemplateBasedDocument(final String typename, final User author) {
373 String filename = typename + ".xml"; // Only XML templates are writeable
374 File template = new File(getRepositoryService().getTemplatePath()
376 if (!template.exists()) {
380 // Session connex = Database.getCurSession();
381 // Transaction transax = connex.beginTransaction();
383 File udir = getRepositoryService().getDownloadDirectory(author);
384 File credoc = new File(udir.getPath() + "/" + filename);
386 // Creation of the meta-document
387 Step step = getSelectedStep(); // Should we check if the given document type is compatible ?
388 DocumentType type = getDocumentTypeService().selectType(typename);
389 Document.Properties dprop = new Document.Properties();
390 Document medoc = getStepService().createDocument(step,
391 dprop.setType(type).setFormat("xml").setAuthor(author))
395 // Instantiation of the template into the user download directory
396 if (!udir.exists()) {
399 if (credoc.exists()) {
402 Do.copy(template, credoc);
404 // Transfer to the document of all known properties
405 ResourceBundle locale = ResourceBundle.getBundle("som",
406 getApplicationSettings().getCurrentLocale());
407 SimpleDateFormat get = new SimpleDateFormat(locale
408 .getString("date.format"), getApplicationSettings()
409 .getCurrentLocale());
410 Writer tool = Toolbox.getWriter(credoc);
411 List<Step> slist = getInvolvedSteps();
412 for (Iterator<Step> i = slist.iterator(); i.hasNext();) {
413 List<SimulationContext> clist = i.next()
414 .getAllSimulationContexts();
415 for (Iterator<SimulationContext> j = clist.iterator(); j
417 SimulationContext context = j.next();
418 tool.updateProperty(context.getType().getName(), context
422 tool.updateProperty("reference", medoc.getReference());
423 tool.updateProperty(Constants.STUDY_MENU, _mystudy.getTitle());
424 tool.updateProperty("step", locale.getString(
425 "folder.step." + step.getNumber()).replaceAll("''", "'"));
426 tool.updateProperty("author", author.getUsername().toUpperCase());
427 tool.updateProperty("date", get.format(medoc.getCreationDate()));
428 tool.updateProperty("history", locale.getString("label.creation")
429 .replaceAll("''", "'"));
432 return new URL(getApplicationSettings().getDownloadURL(author)
434 } catch (Exception saverror) {
435 LOGGER.error("Reason:", saverror);
440 public void selectDocument(final String docurl) {
441 String prefix = getApplicationSettings().getRepositoryURL();
443 if (docurl.startsWith(prefix)) {
445 String path = docurl.substring(prefix.length());
446 Document value = getDocumentService().getDocumentByPath(path);
448 _selecdoc = _ustep.getDocument(value.getIndex());
449 } catch (Exception error) {
450 LOGGER.error("Reason:", error);
456 * Select an activity (step).
459 * the key of the step to select
461 public void setSelection(final String step) {
462 if (!step.equals(_selection)) {
465 setupPreviousToSelectedSteps();
466 updateSimulationContexts(); // Initializes contexts according to the selected steps
468 _ustep = _involving.get(_involving.size() - 1);
469 _urightstep = new StepRights(_cuser, _ustep);
470 _ustep.setActor(_cuser);
471 _menu.selects(_selection); // Updates menu items to be displayed
472 setupContents(); // The contents may have changed even if the selection is the same
475 // =========================================================================
476 // Protected services
477 // =========================================================================
480 * Add a new document presentation facade.
483 * the document publication
485 protected void add(final Publication doc) {
486 DocumentFacade facade = new DocumentFacade(this, doc,
487 getProjectSettings(), getPublicationService(),
488 getApplicationSettings());
489 boolean first = (_contents.size() == 0);
491 _docpres.put(doc.getIndex(), facade);
492 _contents.add(0, facade); // Prepend the new publication
494 this.getMenu().refreshSelectedItem();
496 // Refresh dependencies. They can not be removed until removing this document.
497 for (Publication pub : doc.getRelations(UsesRelation.class)) {
503 * Add a simulation context presentation facade.
506 * the simulation context to add
508 protected void add(final SimulationContext contex) {
509 SimulationContextFacade facade = new SimulationContextFacade(contex,
510 getProjectSettings().getAllSteps(), getApplicationSettings());
512 _context.add(facade);
516 * Add a knowledge element presentation facade.
519 * the knowledge element to add
521 protected void add(final KnowledgeElement kelm) {
522 KnowledgeElementFacade facade = new KnowledgeElementFacade(BeanHelper
523 .copyBean(kelm, KnowledgeElementDTO.class),
524 getApplicationSettings());
525 // RKV KnowledgeIterator known = knowledge.get(kelm.getType().getIndex() - 2);
526 // Knowledges are ordered by type index, from 0 to n-1, the first one being reserved (reason for -2)
527 // RKV:Begin: Find a knowledge iterator for appropriate knowledge type
528 KnowledgeIterator known = null;
529 for (KnowledgeIterator aKnowledgeSection : _knowledge) {
530 if (aKnowledgeSection.getIndex().equals(
531 String.valueOf(kelm.getType().getIndex()))) {
532 known = aKnowledgeSection;
536 if (known != null) { // RKV:End
537 _knowpres.put(kelm.getIndex(), facade);
538 known._list.add(facade); // Insert the new knowledge at the end of the corresponding knowledge type
543 * Remove the document presentation facade.
546 * the document publication to remove
548 protected void remove(final Publication doctag) {
549 for (Iterator<DocumentFacade> i = _contents.iterator(); i.hasNext();) {
550 DocumentFacade facade = i.next();
551 if (facade.isFacadeOf(doctag)) {
556 if (_contents.size() == 0) {
557 this.getMenu().refreshSelectedItem();
562 * Change the currently connected user and refresh user's rights.
565 * the new connected user
567 protected void changeUser(final User user) {
570 if (getStudyService().isStaffedBy(_mystudy, _cuser)) {
571 _popup = getApplicationSettings().getPopupMenu(
572 "steditablemarkpublic");
573 _popup.setContext(Constants.STUDY_MENU, new StudyRights(_cuser,
576 // ustep = getProjectElementService().getFirstStep(mystudy);
577 if (_ustep != null) {
578 _ustep.setActor(_cuser);
580 _urightstudy = new StudyRights(_cuser, _mystudy);
581 _urightstep = new StepRights(_cuser, _ustep);
585 * Remove the simulation context presentation facade.
588 * the simulation context to remove
590 protected void remove(final SimulationContext contex) {
591 for (Iterator<SimulationContextFacade> i = _context.iterator(); i
593 SimulationContextFacade facade = i.next();
594 if (facade.isFacadeOf(contex)) {
602 * Remove the knowledge element presentation facade.
605 * the knowledge element to remove
607 protected void remove(final KnowledgeElement kelm) {
608 // RKV: KnowledgeIterator known = _knowledge.get((int) (kelm.getType()
609 // RKV: .getIndex() - 2));
610 // Knowledges are ordered by type index, from 0 to n-1, the first one being reserved (reason for -2)
611 // RKV:Begin: Find a knowledge iterator for appropriate knowledge type
612 KnowledgeIterator known = null;
613 for (KnowledgeIterator aKnowledgeSection : _knowledge) {
614 if (aKnowledgeSection.getIndex().equals(
615 String.valueOf(kelm.getType().getIndex()))) {
616 known = aKnowledgeSection;
620 if (known != null) { // RKV:End
621 _knowpres.remove(kelm.getIndex());
622 for (Iterator<KnowledgeElementFacade> i = known._list.iterator(); i
624 KnowledgeElementFacade facade = i.next();
625 if (!facade.isFacadeOf(BeanHelper.copyBean(kelm,
626 KnowledgeElementDTO.class))) {
636 * Refresh the document presentation facade.
639 * the document publication
641 protected void update(final Publication doc) {
642 DocumentFacade facade = _docpres.get(doc.getIndex());
643 if (facade != null) {
649 * Refresh the knowledge element presentation facade.
652 * the knowledge element DTO
654 protected void update(final KnowledgeElementDTO kelm) {
655 KnowledgeElementFacade facade = _knowpres.get(kelm.getIndex());
656 if (facade != null) {
657 facade.refresh(kelm);
662 * Refresh simulation contexts presentation facades.
664 protected void updateSimulationContexts() {
666 for (Iterator<Step> i = _involving.iterator(); i.hasNext();) {
667 for (Iterator<SimulationContext> j = i.next()
668 .getAllSimulationContexts().iterator(); j.hasNext();) {
669 _context.add(new SimulationContextFacade(j.next(),
670 getProjectSettings().getAllSteps(),
671 getApplicationSettings()));
676 // =========================================================================
678 // =========================================================================
680 private void setupPreviousToSelectedSteps() {
681 String[] item = _selection.split("\\x2E");
682 int major = Integer.valueOf(item[0]);
683 int minor = Integer.valueOf(item[1]);
689 Scenario[] branch = _mystudy.getScenarii();
690 Scenario scenar = branch[0];
691 for (int i = 0; i < branch.length; i++) {
693 if (scenar.getIndex() == major) {
694 break; // Supposed exist
697 step = getProjectElementService().getSteps(scenar);
698 base = step[0].getNumber() - 1;
699 for (int i = 0; i + base < minor; i++) {
700 _involving.add(step[i]);
703 step = getProjectElementService().getSteps(_mystudy);
704 for (int i = step.length - 1; i > -1; i--) {
705 Step firstep = step[i];
706 if (firstep.getNumber() <= base) {
707 _involving.add(0, firstep);
713 * Get the stepService.
715 * @return the stepService
717 public StepService getStepService() {
722 * Set the stepService.
725 * the stepService to set
727 public void setStepService(final StepService stepService) {
728 _stepService = stepService;
732 * Get the repositoryService.
734 * @return the repositoryService
736 public RepositoryService getRepositoryService() {
737 return _repositoryService;
741 * Set the repositoryService.
743 * @param repositoryService
744 * the repositoryService to set
746 public void setRepositoryService(final RepositoryService repositoryService) {
747 _repositoryService = repositoryService;
751 * Get the documentTypeService.
753 * @return the documentTypeService
755 public DocumentTypeService getDocumentTypeService() {
756 return _documentTypeService;
760 * Set the documentTypeService.
762 * @param documentTypeService
763 * the documentTypeService to set
765 public void setDocumentTypeService(
766 final DocumentTypeService documentTypeService) {
767 _documentTypeService = documentTypeService;
771 * Get the studyService.
773 * @return the studyService
775 public StudyService getStudyService() {
776 return _studyService;
780 * Set the studyService.
782 * @param studyService
783 * the studyService to set
785 public void setStudyService(final StudyService studyService) {
786 _studyService = studyService;
790 * Get the documentService.
792 * @return the documentService
794 public DocumentService getDocumentService() {
795 return _documentService;
799 * Set the documentService.
801 * @param documentService
802 * the documentService to set
804 public void setDocumentService(final DocumentService documentService) {
805 _documentService = documentService;
809 * Get the detached study object.
811 * @return the detached study object
813 public Study getMystudy() {
818 * Set the study to present.
821 * the selected study detached object
823 public void setMystudy(final Study mystudy) {
824 this._mystudy = mystudy;