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 _popup = getApplicationSettings().getPopupMenu(
156 "steditablemarkpublic");
160 _popup.setContext(Constants.STUDY_MENU, new StudyRights(_cuser,
163 _urightstudy = new StudyRights(_cuser, _mystudy);
164 _urightstep = new StepRights(_cuser, _ustep);
166 // RKV menu = new StudyMenu(mystudy);
167 _menu = (getMenu()).init(_mystudy); // RKV
168 _menu.selects(_selection); // Initializes menu items to be displayed
169 setupContents(); // Initializes documents and knowledge at ustep
173 // ==============================================================================================================================
175 // ==============================================================================================================================
178 public String getAuthorName() {
179 return _mystudy.getAuthor().toString();
183 public Long getIndex() {
184 return _mystudy.getIndex();
187 public String getDate() {
192 public StudyMenu getMenu() {
193 return (StudyMenu) _menu;
196 public void setMenu(final StudyMenu aMenu) {
201 public ProgressState getProgressState() {
202 return _mystudy.getProgressState();
205 public String getLastModificationDate() {
209 public ToolBar getModuleBar() {
210 return getApplicationSettings().getModuleBar(getSelectedStep());
214 public String getReference() {
215 return _mystudy.getReference();
219 public Publication getSelectedDocument() {
223 public StepRights getSelectedStepRights() {
227 public StudyRights getStudyRights() {
231 public Study getStudyObject() {
236 public String getTitle() {
237 return _mystudy.getTitle();
241 public String getType() {
243 * RKV: return ResourceBundle.getBundle("labels", getApplicationSettings().getCurrentLocale()).getString( "label.study");
245 return Constants.STUDY_MENU;
248 public String getVersion() {
252 public boolean isStepEnabled() {
253 return _urightstep.isEnabled();
256 // ==============================================================================================================================
258 // ==============================================================================================================================
261 public URL newTemplateBasedDocument(final String typename, final User author) {
262 String filename = typename + ".xml"; // Only XML templates are writeable
263 File template = new File(getRepositoryService().getTemplatePath()
265 if (!template.exists()) {
269 // Session connex = Database.getCurSession();
270 // Transaction transax = connex.beginTransaction();
272 File udir = getRepositoryService().getDownloadDirectory(author);
273 File credoc = new File(udir.getPath() + "/" + filename);
275 // Creation of the meta-document
276 Step step = getSelectedStep(); // Should we check if the given document type is compatible ?
277 DocumentType type = getDocumentTypeService().selectType(typename);
278 Document.Properties dprop = new Document.Properties();
279 Document medoc = getStepService().createDocument(step,
280 dprop.setType(type).setFormat("xml").setAuthor(author))
284 // Instantiation of the template into the user download directory
285 if (!udir.exists()) {
288 if (credoc.exists()) {
291 Do.copy(template, credoc);
293 // Transfer to the document of all known properties
294 ResourceBundle locale = ResourceBundle.getBundle("som",
295 getApplicationSettings().getCurrentLocale());
296 SimpleDateFormat get = new SimpleDateFormat(locale
297 .getString("date.format"), getApplicationSettings()
298 .getCurrentLocale());
299 Writer tool = Toolbox.getWriter(credoc);
300 List<Step> slist = getInvolvedSteps();
301 for (Iterator<Step> i = slist.iterator(); i.hasNext();) {
302 List<SimulationContext> clist = i.next()
303 .getAllSimulationContexts();
304 for (Iterator<SimulationContext> j = clist.iterator(); j
306 SimulationContext context = j.next();
307 tool.updateProperty(context.getType().getName(), context
311 tool.updateProperty("reference", medoc.getReference());
312 tool.updateProperty(Constants.STUDY_MENU, _mystudy.getTitle());
313 tool.updateProperty("step", locale.getString(
314 "folder.step." + step.getNumber()).replaceAll("''", "'"));
315 tool.updateProperty("author", author.getUsername().toUpperCase());
316 tool.updateProperty("date", get.format(medoc.getCreationDate()));
317 tool.updateProperty("history", locale.getString("label.creation")
318 .replaceAll("''", "'"));
321 return new URL(getApplicationSettings().getDownloadURL(author)
323 } catch (Exception saverror) {
324 LOGGER.error("Reason:", saverror);
329 public void selectDocument(final String docurl) {
330 String prefix = getApplicationSettings().getRepositoryURL();
332 if (docurl.startsWith(prefix)) {
334 String path = docurl.substring(prefix.length());
335 Document value = getDocumentService().getDocumentByPath(path);
337 _selecdoc = _ustep.getDocument(value.getIndex());
338 } catch (Exception error) {
339 LOGGER.error("Reason:", error);
344 public void setSelection(final String step) {
345 if (!step.equals(_selection)) {
348 setupPreviousToSelectedSteps();
349 updateSimulationContexts(); // Initializes contexts according to the selected steps
351 _ustep = _involving.get(_involving.size() - 1);
352 _urightstep = new StepRights(_cuser, _ustep);
353 _ustep.setActor(_cuser);
354 _menu.selects(_selection); // Updates menu items to be displayed
355 setupContents(); // The contents may have changed even if the selection is the same
358 // ==============================================================================================================================
359 // Protected services
360 // ==============================================================================================================================
362 protected void add(final Publication doc) {
363 DocumentFacade facade = new DocumentFacade(this, doc,
364 getProjectSettings(), getPublicationService(),
365 getApplicationSettings());
366 boolean first = (_contents.size() == 0);
368 _docpres.put(doc.getIndex(), facade);
369 _contents.add(0, facade); // Prepend the new publication
371 this.getMenu().refreshSelectedItem();
376 protected void add(final SimulationContext contex) {
377 SimulationContextFacade facade = new SimulationContextFacade(contex,
378 getProjectSettings().getAllSteps(), getApplicationSettings());
380 _context.add(facade);
383 protected void add(final KnowledgeElement kelm) {
384 KnowledgeElementFacade facade = new KnowledgeElementFacade(BeanHelper
385 .copyBean(kelm, KnowledgeElementDTO.class),
386 getApplicationSettings());
387 // RKV KnowledgeIterator known = knowledge.get(kelm.getType().getIndex() - 2);
388 // Knowledges are ordered by type index, from 0 to n-1, the first one being reserved (reason for -2)
389 // RKV:Begin: Find a knowledge iterator for appropriate knowledge type
390 KnowledgeIterator known = null;
391 for (KnowledgeIterator aKnowledgeSection : _knowledge) {
392 if (aKnowledgeSection.getIndex().equals(
393 String.valueOf(kelm.getType().getIndex()))) {
394 known = aKnowledgeSection;
398 if (known != null) { // RKV:End
399 _knowpres.put(kelm.getIndex(), facade);
400 known._list.add(facade); // Insert the new knowledge at the end of the corresponding knowledge type
404 protected void remove(final Publication doctag) {
405 for (Iterator<DocumentFacade> i = _contents.iterator(); i.hasNext();) {
406 DocumentFacade facade = i.next();
407 if (facade.isFacadeOf(doctag)) {
412 if (_contents.size() == 0) {
413 this.getMenu().refreshSelectedItem();
417 protected void changeUser(final User user) {
420 if (getStudyService().isStaffedBy(_mystudy, _cuser)) {
421 _popup = getApplicationSettings().getPopupMenu(
422 "steditablemarkpublic");
423 _popup.setContext(Constants.STUDY_MENU, new StudyRights(_cuser,
426 // ustep = getProjectElementService().getFirstStep(mystudy);
427 if (_ustep != null) {
428 _ustep.setActor(_cuser);
430 _urightstudy = new StudyRights(_cuser, _mystudy);
431 _urightstep = new StepRights(_cuser, _ustep);
434 protected void remove(final SimulationContext contex) {
435 for (Iterator<SimulationContextFacade> i = _context.iterator(); i
437 SimulationContextFacade facade = i.next();
438 if (!facade.isFacadeOf(contex)) {
446 protected void remove(final KnowledgeElement kelm) {
447 // RKV: KnowledgeIterator known = _knowledge.get((int) (kelm.getType()
448 // RKV: .getIndex() - 2));
449 // Knowledges are ordered by type index, from 0 to n-1, the first one being reserved (reason for -2)
450 // RKV:Begin: Find a knowledge iterator for appropriate knowledge type
451 KnowledgeIterator known = null;
452 for (KnowledgeIterator aKnowledgeSection : _knowledge) {
453 if (aKnowledgeSection.getIndex().equals(
454 String.valueOf(kelm.getType().getIndex()))) {
455 known = aKnowledgeSection;
459 if (known != null) { // RKV:End
460 _knowpres.remove(kelm.getIndex());
461 for (Iterator<KnowledgeElementFacade> i = known._list.iterator(); i
463 KnowledgeElementFacade facade = i.next();
464 if (!facade.isFacadeOf(BeanHelper.copyBean(kelm,
465 KnowledgeElementDTO.class))) {
474 protected void update(final Publication doc) {
475 DocumentFacade facade = _docpres.get(doc.getIndex());
476 if (facade != null) {
481 protected void update(final KnowledgeElementDTO kelm) {
482 KnowledgeElementFacade facade = _knowpres.get(kelm.getIndex());
483 if (facade != null) {
484 facade.refresh(kelm);
488 protected void updateSimulationContexts() {
490 for (Iterator<Step> i = _involving.iterator(); i.hasNext();) {
491 for (Iterator<SimulationContext> j = i.next()
492 .getAllSimulationContexts().iterator(); j.hasNext();) {
493 _context.add(new SimulationContextFacade(j.next(),
494 getProjectSettings().getAllSteps(),
495 getApplicationSettings()));
500 // ==============================================================================================================================
502 // ==============================================================================================================================
504 private void setupPreviousToSelectedSteps() {
505 String[] item = _selection.split("\\x2E");
506 int major = Integer.valueOf(item[0]);
507 int minor = Integer.valueOf(item[1]);
513 Scenario[] branch = _mystudy.getScenarii();
514 Scenario scenar = branch[0];
515 for (int i = 0; i < branch.length; i++) {
517 if (scenar.getIndex() == major) {
518 break; // Supposed exist
521 step = getProjectElementService().getSteps(scenar);
522 base = step[0].getNumber() - 1;
523 for (int i = 0; i + base < minor; i++) {
524 _involving.add(step[i]);
527 step = getProjectElementService().getSteps(_mystudy);
528 for (int i = step.length - 1; i > -1; i--) {
529 Step firstep = step[i];
530 if (firstep.getNumber() <= base) {
531 _involving.add(0, firstep);
537 * Get the stepService.
539 * @return the stepService
541 public StepService getStepService() {
546 * Set the stepService.
549 * the stepService to set
551 public void setStepService(final StepService stepService) {
552 _stepService = stepService;
556 * Get the repositoryService.
558 * @return the repositoryService
560 public RepositoryService getRepositoryService() {
561 return _repositoryService;
565 * Set the repositoryService.
567 * @param repositoryService
568 * the repositoryService to set
570 public void setRepositoryService(final RepositoryService repositoryService) {
571 _repositoryService = repositoryService;
575 * Get the documentTypeService.
577 * @return the documentTypeService
579 public DocumentTypeService getDocumentTypeService() {
580 return _documentTypeService;
584 * Set the documentTypeService.
586 * @param documentTypeService
587 * the documentTypeService to set
589 public void setDocumentTypeService(
590 final DocumentTypeService documentTypeService) {
591 _documentTypeService = documentTypeService;
595 * Get the studyService.
597 * @return the studyService
599 public StudyService getStudyService() {
600 return _studyService;
604 * Set the studyService.
606 * @param studyService
607 * the studyService to set
609 public void setStudyService(final StudyService studyService) {
610 _studyService = studyService;
614 * Get the documentService.
616 * @return the documentService
618 public DocumentService getDocumentService() {
619 return _documentService;
623 * Set the documentService.
625 * @param documentService
626 * the documentService to set
628 public void setDocumentService(final DocumentService documentService) {
629 _documentService = documentService;
632 public Study getMystudy() {
636 public void setMystudy(final Study mystudy) {
637 this._mystudy = mystudy;