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.apache.log4j.Logger;
18 import org.splat.dal.bo.kernel.User;
19 import org.splat.dal.bo.som.Document;
20 import org.splat.dal.bo.som.DocumentType;
21 import org.splat.dal.bo.som.KnowledgeElement;
22 import org.splat.dal.bo.som.ProgressState;
23 import org.splat.dal.bo.som.Publication;
24 import org.splat.dal.bo.som.Scenario;
25 import org.splat.dal.bo.som.SimulationContext;
26 import org.splat.dal.bo.som.Study;
27 import org.splat.kernel.Do;
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.ToolBar;
44 * Presentation of the currently open study.
46 public class OpenStudy extends AbstractOpenObject implements OpenStudyServices {
51 protected final static Logger LOGGER = org.splat.simer.Action.LOG;
54 * The currently open study.
56 private transient Study _mystudy;
58 * User rights on the open study.
60 private transient StudyRights _urightstudy;
62 * User rights on the selected step.
64 private transient StepRights _urightstep;
65 private transient String _version;
66 private transient String _credate;
67 private transient String _lasdate;
68 private transient Publication _selecdoc;
70 * Injected step service.
72 private StepService _stepService;
74 * Injected repository service.
76 private RepositoryService _repositoryService;
78 * Injected document type service.
80 private DocumentTypeService _documentTypeService;
82 * Injected document service.
84 private DocumentService _documentService;
86 * The injected Study service.
88 private StudyService _studyService;
90 // ==============================================================================================================================
92 // ==============================================================================================================================
95 * Open the given study in the current http session.
101 * @return this open study object
103 public OpenStudy open(final User user, final Study study) {
104 ResourceBundle custom = ResourceBundle.getBundle("som",
105 getApplicationSettings().getCurrentLocale());
106 SimpleDateFormat datstring = new SimpleDateFormat(custom
107 .getString("date.format"));
108 Revision.Format verstring = new Revision.Format(getProjectSettings()
109 .getRevisionPattern());
111 _cuser = user; // May be null if nobody connected
113 _selection = "0.1"; // Default selection
116 // Preparation of the display
117 _version = verstring.format(_mystudy.getVersion());
118 _credate = datstring.format(_mystudy.getDate());
119 _lasdate = ""; // Not yet supported
120 _description = _mystudy.getDescription();
121 _involving = new ArrayList<Step>(1);
122 _context = new ArrayList<SimulationContextFacade>();
123 _ustep = getProjectElementService().getFirstStep(_mystudy);
124 _ustep.setActor(_cuser);
125 _involving.add(_ustep);
126 for (Iterator<SimulationContext> i = _ustep.getAllSimulationContexts()
127 .iterator(); i.hasNext();) {
128 _context.add(new SimulationContextFacade(i.next(),
129 getProjectSettings().getAllSteps(),
130 getApplicationSettings()));
132 if (getStudyService().isStaffedBy(_mystudy, _cuser)
133 || getStudyService().hasActor(_mystudy, _cuser)) {
134 // ProgressState state = mystudy.getProgressState();
135 // if (state == ProgressState.inCHECK) popup = getApplicationSettings().getPopupMenu("stapprovable");
136 // else if (state == ProgressState.APPROVED) popup = getApplicationSettings().getPopupMenu("stapproved");
138 if (_mystudy.getProgressState() == ProgressState.TEMPLATE) {
139 _popup = getApplicationSettings().getPopupMenu(
142 _popup = getApplicationSettings().getPopupMenu(
145 _popup.setContext("study", new StudyRights(_cuser, _mystudy));
147 _urightstudy = new StudyRights(_cuser, _mystudy);
148 _urightstep = new StepRights(_cuser, _ustep);
150 // RKV menu = new StudyMenu(mystudy);
151 _menu = (getMenu()).init(_mystudy); // RKV
152 _menu.selects(_selection); // Initializes menu items to be displayed
153 setupContents(); // Initializes documents and knowledge at ustep
157 // ==============================================================================================================================
159 // ==============================================================================================================================
161 public String getAuthorName() {
162 return _mystudy.getAuthor().toString();
165 public Long getIndex() {
166 return _mystudy.getIndex();
169 public String getDate() {
174 public StudyMenu getMenu() {
175 return (StudyMenu) _menu;
178 public void setMenu(final StudyMenu aMenu) {
182 public ProgressState getProgressState() {
183 return _mystudy.getProgressState();
186 public String getLastModificationDate() {
190 public ToolBar getModuleBar() {
191 return getApplicationSettings().getModuleBar(getSelectedStep());
194 public String getReference() {
195 return _mystudy.getReference();
198 public Publication getSelectedDocument() {
202 public StepRights getSelectedStepRights() {
206 public StudyRights getStudyRights() {
210 public Study getStudyObject() {
214 public String getTitle() {
215 return _mystudy.getTitle();
218 public String getType() {
220 * RKV: return ResourceBundle.getBundle("labels", getApplicationSettings().getCurrentLocale()).getString( "label.study");
225 public String getVersion() {
229 public boolean isStepEnabled() {
230 return _urightstep.isEnabled();
233 // ==============================================================================================================================
235 // ==============================================================================================================================
237 public URL newTemplateBasedDocument(final String typename, final User author) {
238 String filename = typename + ".xml"; // Only XML templates are writeable
239 File template = new File(getRepositoryService().getTemplatePath()
241 if (!template.exists()) {
245 // Session connex = Database.getCurSession();
246 // Transaction transax = connex.beginTransaction();
248 File udir = getRepositoryService().getDownloadDirectory(author);
249 File credoc = new File(udir.getPath() + "/" + filename);
251 // Creation of the meta-document
252 Step step = getSelectedStep(); // Should we check if the given document type is compatible ?
253 DocumentType type = getDocumentTypeService().selectType(typename);
254 Document.Properties dprop = new Document.Properties();
255 Document medoc = getStepService().createDocument(step,
256 dprop.setType(type).setFormat("xml").setAuthor(author))
260 // Instantiation of the template into the user download directory
261 if (!udir.exists()) {
264 if (credoc.exists()) {
267 Do.copy(template, credoc);
269 // Transfer to the document of all known properties
270 ResourceBundle locale = ResourceBundle.getBundle("som",
271 getApplicationSettings().getCurrentLocale());
272 SimpleDateFormat get = new SimpleDateFormat(locale
273 .getString("date.format"));
274 Writer tool = Toolbox.getWriter(credoc);
275 List<Step> slist = getInvolvedSteps();
276 for (Iterator<Step> i = slist.iterator(); i.hasNext();) {
277 List<SimulationContext> clist = i.next()
278 .getAllSimulationContexts();
279 for (Iterator<SimulationContext> j = clist.iterator(); j
281 SimulationContext context = j.next();
282 tool.updateProperty(context.getType().getName(), context
286 tool.updateProperty("reference", medoc.getReference());
287 tool.updateProperty("study", _mystudy.getTitle());
288 tool.updateProperty("step", locale.getString(
289 "folder.step." + step.getNumber()).replaceAll("''", "'"));
290 tool.updateProperty("author", author.getUsername().toUpperCase());
291 tool.updateProperty("date", get.format(medoc.getCreationDate()));
292 tool.updateProperty("history", locale.getString("label.creation")
293 .replaceAll("''", "'"));
296 return new URL(getApplicationSettings().getDownloadURL(author)
298 } catch (Exception saverror) {
299 LOGGER.error("Reason:", saverror);
304 public void selectDocument(final String docurl) {
305 String prefix = getApplicationSettings().getRepositoryURL();
307 if (docurl.startsWith(prefix)) {
309 String path = docurl.substring(prefix.length());
310 Document value = getDocumentService().getDocumentByPath(path);
312 _selecdoc = _ustep.getDocument(value.getIndex());
313 } catch (Exception error) {
314 LOGGER.error("Reason:", error);
319 public void setSelection(final String step) {
320 if (!step.equals(_selection)) {
323 setupPreviousToSelectedSteps();
324 updateSimulationContexts(); // Initializes contexts according to the selected steps
326 _ustep = _involving.get(_involving.size() - 1);
327 _urightstep = new StepRights(_cuser, _ustep);
328 _ustep.setActor(_cuser);
329 _menu.selects(_selection); // Updates menu items to be displayed
330 setupContents(); // The contents may have changed even if the selection is the same
333 // ==============================================================================================================================
334 // Protected services
335 // ==============================================================================================================================
337 protected void add(final Publication doc) {
338 DocumentFacade facade = new DocumentFacade(this, doc,
339 getProjectSettings(), getPublicationService(),
340 getApplicationSettings());
341 boolean first = (_contents.size() == 0);
343 docpres.put(doc.getIndex(), facade);
344 _contents.add(0, facade); // Prepend the new publication
346 this.getMenu().refreshSelectedItem();
351 protected void add(final SimulationContext contex) {
352 SimulationContextFacade facade = new SimulationContextFacade(contex,
353 getProjectSettings().getAllSteps(), getApplicationSettings());
355 _context.add(facade);
358 protected void add(final KnowledgeElement kelm) {
359 KnowledgeElementFacade facade = new KnowledgeElementFacade(BeanHelper
360 .copyBean(kelm, KnowledgeElementDTO.class),
361 getApplicationSettings());
362 // RKV KnowledgeIterator known = knowledge.get(kelm.getType().getIndex() - 2);
363 // Knowledges are ordered by type index, from 0 to n-1, the first one being reserved (reason for -2)
364 // RKV:Begin: Find a knowledge iterator for appropriate knowledge type
365 KnowledgeIterator known = null;
366 for (KnowledgeIterator aKnowledgeSection : _knowledge) {
367 if (aKnowledgeSection.getIndex().equals(
368 String.valueOf(kelm.getType().getIndex()))) {
369 known = aKnowledgeSection;
373 if (known != null) { // RKV:End
374 knowpres.put(kelm.getIndex(), facade);
375 known._list.add(facade); // Insert the new knowledge at the end of the corresponding knowledge type
379 protected void remove(final Publication doctag) {
380 for (Iterator<DocumentFacade> i = _contents.iterator(); i.hasNext();) {
381 DocumentFacade facade = i.next();
382 if (facade.isFacadeOf(doctag)) {
387 if (_contents.size() == 0) {
388 this.getMenu().refreshSelectedItem();
392 protected void changeUser(final User user) {
395 if (getStudyService().isStaffedBy(_mystudy, _cuser)) {
396 _popup = getApplicationSettings().getPopupMenu("steditablemark");
397 _popup.setContext("study", new StudyRights(_cuser, _mystudy));
399 // ustep = getProjectElementService().getFirstStep(mystudy);
400 if (_ustep != null) {
401 _ustep.setActor(_cuser);
403 _urightstudy = new StudyRights(_cuser, _mystudy);
404 _urightstep = new StepRights(_cuser, _ustep);
407 protected void remove(final SimulationContext contex) {
408 for (Iterator<SimulationContextFacade> i = _context.iterator(); i
410 SimulationContextFacade facade = i.next();
411 if (!facade.isFacadeOf(contex)) {
419 protected void remove(final KnowledgeElement kelm) {
420 // RKV: KnowledgeIterator known = _knowledge.get((int) (kelm.getType()
421 // RKV: .getIndex() - 2));
422 // Knowledges are ordered by type index, from 0 to n-1, the first one being reserved (reason for -2)
423 // RKV:Begin: Find a knowledge iterator for appropriate knowledge type
424 KnowledgeIterator known = null;
425 for (KnowledgeIterator aKnowledgeSection : _knowledge) {
426 if (aKnowledgeSection.getIndex().equals(
427 String.valueOf(kelm.getType().getIndex()))) {
428 known = aKnowledgeSection;
432 if (known != null) { // RKV:End
433 knowpres.remove(kelm.getIndex());
434 for (Iterator<KnowledgeElementFacade> i = known._list.iterator(); i
436 KnowledgeElementFacade facade = i.next();
437 if (!facade.isFacadeOf(BeanHelper.copyBean(kelm,
438 KnowledgeElementDTO.class))) {
447 protected void update(final Publication doc) {
448 DocumentFacade facade = docpres.get(doc.getIndex());
449 if (facade != null) {
454 protected void update(final KnowledgeElementDTO kelm) {
455 KnowledgeElementFacade facade = knowpres.get(kelm.getIndex());
456 if (facade != null) {
457 facade.refresh(kelm);
461 protected void updateSimulationContexts() {
463 for (Iterator<Step> i = _involving.iterator(); i.hasNext();) {
464 for (Iterator<SimulationContext> j = i.next()
465 .getAllSimulationContexts().iterator(); j.hasNext();) {
466 _context.add(new SimulationContextFacade(j.next(),
467 getProjectSettings().getAllSteps(),
468 getApplicationSettings()));
473 // ==============================================================================================================================
475 // ==============================================================================================================================
477 private void setupPreviousToSelectedSteps() {
478 String[] item = _selection.split("\\x2E");
479 int major = Integer.valueOf(item[0]);
480 int minor = Integer.valueOf(item[1]);
486 Scenario[] branch = _mystudy.getScenarii();
487 Scenario scenar = branch[0];
488 for (int i = 0; i < branch.length; i++) {
490 if (scenar.getIndex() == major) {
491 break; // Supposed exist
494 step = getProjectElementService().getSteps(scenar);
495 base = step[0].getNumber() - 1;
496 for (int i = 0; i + base < minor; i++) {
497 _involving.add(step[i]);
500 step = getProjectElementService().getSteps(_mystudy);
501 for (int i = step.length - 1; i > -1; i--) {
502 Step firstep = step[i];
503 if (firstep.getNumber() > base) {
506 _involving.add(0, firstep);
511 * Get the stepService.
513 * @return the stepService
515 public StepService getStepService() {
520 * Set the stepService.
523 * the stepService to set
525 public void setStepService(final StepService stepService) {
526 _stepService = stepService;
530 * Get the repositoryService.
532 * @return the repositoryService
534 public RepositoryService getRepositoryService() {
535 return _repositoryService;
539 * Set the repositoryService.
541 * @param repositoryService
542 * the repositoryService to set
544 public void setRepositoryService(final RepositoryService repositoryService) {
545 _repositoryService = repositoryService;
549 * Get the documentTypeService.
551 * @return the documentTypeService
553 public DocumentTypeService getDocumentTypeService() {
554 return _documentTypeService;
558 * Set the documentTypeService.
560 * @param documentTypeService
561 * the documentTypeService to set
563 public void setDocumentTypeService(
564 final DocumentTypeService documentTypeService) {
565 _documentTypeService = documentTypeService;
569 * Get the studyService.
571 * @return the studyService
573 public StudyService getStudyService() {
574 return _studyService;
578 * Set the studyService.
580 * @param studyService
581 * the studyService to set
583 public void setStudyService(final StudyService studyService) {
584 _studyService = studyService;
588 * Get the documentService.
590 * @return the documentService
592 public DocumentService getDocumentService() {
593 return _documentService;
597 * Set the documentService.
599 * @param documentService
600 * the documentService to set
602 public void setDocumentService(final DocumentService documentService) {
603 _documentService = documentService;
606 public Study getMystudy() {
610 public void setMystudy(final Study mystudy) {
611 this._mystudy = mystudy;