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");
139 if (_mystudy.getProgressState() == ProgressState.TEMPLATE) {
140 if (_mystudy.isPublic()) {
141 _popup = getApplicationSettings().getPopupMenu(
142 "steditableunmarkprivate");
144 _popup = getApplicationSettings().getPopupMenu(
145 "steditableunmarkpublic");
149 if (_mystudy.isPublic()) {
150 _popup = getApplicationSettings().getPopupMenu(
151 "steditablemarkprivate");
153 _popup = getApplicationSettings().getPopupMenu(
154 "steditablemarkpublic");
158 _popup.setContext("study", new StudyRights(_cuser, _mystudy));
160 _urightstudy = new StudyRights(_cuser, _mystudy);
161 _urightstep = new StepRights(_cuser, _ustep);
163 // RKV menu = new StudyMenu(mystudy);
164 _menu = (getMenu()).init(_mystudy); // RKV
165 _menu.selects(_selection); // Initializes menu items to be displayed
166 setupContents(); // Initializes documents and knowledge at ustep
170 // ==============================================================================================================================
172 // ==============================================================================================================================
175 public String getAuthorName() {
176 return _mystudy.getAuthor().toString();
180 public Long getIndex() {
181 return _mystudy.getIndex();
184 public String getDate() {
189 public StudyMenu getMenu() {
190 return (StudyMenu) _menu;
193 public void setMenu(final StudyMenu aMenu) {
198 public ProgressState getProgressState() {
199 return _mystudy.getProgressState();
202 public String getLastModificationDate() {
206 public ToolBar getModuleBar() {
207 return getApplicationSettings().getModuleBar(getSelectedStep());
211 public String getReference() {
212 return _mystudy.getReference();
216 public Publication getSelectedDocument() {
220 public StepRights getSelectedStepRights() {
224 public StudyRights getStudyRights() {
228 public Study getStudyObject() {
233 public String getTitle() {
234 return _mystudy.getTitle();
238 public String getType() {
240 * RKV: return ResourceBundle.getBundle("labels", getApplicationSettings().getCurrentLocale()).getString( "label.study");
245 public String getVersion() {
249 public boolean isStepEnabled() {
250 return _urightstep.isEnabled();
253 // ==============================================================================================================================
255 // ==============================================================================================================================
258 public URL newTemplateBasedDocument(final String typename, final User author) {
259 String filename = typename + ".xml"; // Only XML templates are writeable
260 File template = new File(getRepositoryService().getTemplatePath()
262 if (!template.exists()) {
266 // Session connex = Database.getCurSession();
267 // Transaction transax = connex.beginTransaction();
269 File udir = getRepositoryService().getDownloadDirectory(author);
270 File credoc = new File(udir.getPath() + "/" + filename);
272 // Creation of the meta-document
273 Step step = getSelectedStep(); // Should we check if the given document type is compatible ?
274 DocumentType type = getDocumentTypeService().selectType(typename);
275 Document.Properties dprop = new Document.Properties();
276 Document medoc = getStepService().createDocument(step,
277 dprop.setType(type).setFormat("xml").setAuthor(author))
281 // Instantiation of the template into the user download directory
282 if (!udir.exists()) {
285 if (credoc.exists()) {
288 Do.copy(template, credoc);
290 // Transfer to the document of all known properties
291 ResourceBundle locale = ResourceBundle.getBundle("som",
292 getApplicationSettings().getCurrentLocale());
293 SimpleDateFormat get = new SimpleDateFormat(locale
294 .getString("date.format"));
295 Writer tool = Toolbox.getWriter(credoc);
296 List<Step> slist = getInvolvedSteps();
297 for (Iterator<Step> i = slist.iterator(); i.hasNext();) {
298 List<SimulationContext> clist = i.next()
299 .getAllSimulationContexts();
300 for (Iterator<SimulationContext> j = clist.iterator(); j
302 SimulationContext context = j.next();
303 tool.updateProperty(context.getType().getName(), context
307 tool.updateProperty("reference", medoc.getReference());
308 tool.updateProperty("study", _mystudy.getTitle());
309 tool.updateProperty("step", locale.getString(
310 "folder.step." + step.getNumber()).replaceAll("''", "'"));
311 tool.updateProperty("author", author.getUsername().toUpperCase());
312 tool.updateProperty("date", get.format(medoc.getCreationDate()));
313 tool.updateProperty("history", locale.getString("label.creation")
314 .replaceAll("''", "'"));
317 return new URL(getApplicationSettings().getDownloadURL(author)
319 } catch (Exception saverror) {
320 LOGGER.error("Reason:", saverror);
325 public void selectDocument(final String docurl) {
326 String prefix = getApplicationSettings().getRepositoryURL();
328 if (docurl.startsWith(prefix)) {
330 String path = docurl.substring(prefix.length());
331 Document value = getDocumentService().getDocumentByPath(path);
333 _selecdoc = _ustep.getDocument(value.getIndex());
334 } catch (Exception error) {
335 LOGGER.error("Reason:", error);
340 public void setSelection(final String step) {
341 if (!step.equals(_selection)) {
344 setupPreviousToSelectedSteps();
345 updateSimulationContexts(); // Initializes contexts according to the selected steps
347 _ustep = _involving.get(_involving.size() - 1);
348 _urightstep = new StepRights(_cuser, _ustep);
349 _ustep.setActor(_cuser);
350 _menu.selects(_selection); // Updates menu items to be displayed
351 setupContents(); // The contents may have changed even if the selection is the same
354 // ==============================================================================================================================
355 // Protected services
356 // ==============================================================================================================================
358 protected void add(final Publication doc) {
359 DocumentFacade facade = new DocumentFacade(this, doc,
360 getProjectSettings(), getPublicationService(),
361 getApplicationSettings());
362 boolean first = (_contents.size() == 0);
364 docpres.put(doc.getIndex(), facade);
365 _contents.add(0, facade); // Prepend the new publication
367 this.getMenu().refreshSelectedItem();
372 protected void add(final SimulationContext contex) {
373 SimulationContextFacade facade = new SimulationContextFacade(contex,
374 getProjectSettings().getAllSteps(), getApplicationSettings());
376 _context.add(facade);
379 protected void add(final KnowledgeElement kelm) {
380 KnowledgeElementFacade facade = new KnowledgeElementFacade(BeanHelper
381 .copyBean(kelm, KnowledgeElementDTO.class),
382 getApplicationSettings());
383 // RKV KnowledgeIterator known = knowledge.get(kelm.getType().getIndex() - 2);
384 // Knowledges are ordered by type index, from 0 to n-1, the first one being reserved (reason for -2)
385 // RKV:Begin: Find a knowledge iterator for appropriate knowledge type
386 KnowledgeIterator known = null;
387 for (KnowledgeIterator aKnowledgeSection : _knowledge) {
388 if (aKnowledgeSection.getIndex().equals(
389 String.valueOf(kelm.getType().getIndex()))) {
390 known = aKnowledgeSection;
394 if (known != null) { // RKV:End
395 knowpres.put(kelm.getIndex(), facade);
396 known._list.add(facade); // Insert the new knowledge at the end of the corresponding knowledge type
400 protected void remove(final Publication doctag) {
401 for (Iterator<DocumentFacade> i = _contents.iterator(); i.hasNext();) {
402 DocumentFacade facade = i.next();
403 if (facade.isFacadeOf(doctag)) {
408 if (_contents.size() == 0) {
409 this.getMenu().refreshSelectedItem();
413 protected void changeUser(final User user) {
416 if (getStudyService().isStaffedBy(_mystudy, _cuser)) {
417 _popup = getApplicationSettings().getPopupMenu("steditablemarkpublic");
418 _popup.setContext("study", new StudyRights(_cuser, _mystudy));
420 // ustep = getProjectElementService().getFirstStep(mystudy);
421 if (_ustep != null) {
422 _ustep.setActor(_cuser);
424 _urightstudy = new StudyRights(_cuser, _mystudy);
425 _urightstep = new StepRights(_cuser, _ustep);
428 protected void remove(final SimulationContext contex) {
429 for (Iterator<SimulationContextFacade> i = _context.iterator(); i
431 SimulationContextFacade facade = i.next();
432 if (!facade.isFacadeOf(contex)) {
440 protected void remove(final KnowledgeElement kelm) {
441 // RKV: KnowledgeIterator known = _knowledge.get((int) (kelm.getType()
442 // RKV: .getIndex() - 2));
443 // Knowledges are ordered by type index, from 0 to n-1, the first one being reserved (reason for -2)
444 // RKV:Begin: Find a knowledge iterator for appropriate knowledge type
445 KnowledgeIterator known = null;
446 for (KnowledgeIterator aKnowledgeSection : _knowledge) {
447 if (aKnowledgeSection.getIndex().equals(
448 String.valueOf(kelm.getType().getIndex()))) {
449 known = aKnowledgeSection;
453 if (known != null) { // RKV:End
454 knowpres.remove(kelm.getIndex());
455 for (Iterator<KnowledgeElementFacade> i = known._list.iterator(); i
457 KnowledgeElementFacade facade = i.next();
458 if (!facade.isFacadeOf(BeanHelper.copyBean(kelm,
459 KnowledgeElementDTO.class))) {
468 protected void update(final Publication doc) {
469 DocumentFacade facade = docpres.get(doc.getIndex());
470 if (facade != null) {
475 protected void update(final KnowledgeElementDTO kelm) {
476 KnowledgeElementFacade facade = knowpres.get(kelm.getIndex());
477 if (facade != null) {
478 facade.refresh(kelm);
482 protected void updateSimulationContexts() {
484 for (Iterator<Step> i = _involving.iterator(); i.hasNext();) {
485 for (Iterator<SimulationContext> j = i.next()
486 .getAllSimulationContexts().iterator(); j.hasNext();) {
487 _context.add(new SimulationContextFacade(j.next(),
488 getProjectSettings().getAllSteps(),
489 getApplicationSettings()));
494 // ==============================================================================================================================
496 // ==============================================================================================================================
498 private void setupPreviousToSelectedSteps() {
499 String[] item = _selection.split("\\x2E");
500 int major = Integer.valueOf(item[0]);
501 int minor = Integer.valueOf(item[1]);
507 Scenario[] branch = _mystudy.getScenarii();
508 Scenario scenar = branch[0];
509 for (int i = 0; i < branch.length; i++) {
511 if (scenar.getIndex() == major) {
512 break; // Supposed exist
515 step = getProjectElementService().getSteps(scenar);
516 base = step[0].getNumber() - 1;
517 for (int i = 0; i + base < minor; i++) {
518 _involving.add(step[i]);
521 step = getProjectElementService().getSteps(_mystudy);
522 for (int i = step.length - 1; i > -1; i--) {
523 Step firstep = step[i];
524 if (firstep.getNumber() > base) {
527 _involving.add(0, firstep);
532 * Get the stepService.
534 * @return the stepService
536 public StepService getStepService() {
541 * Set the stepService.
544 * the stepService to set
546 public void setStepService(final StepService stepService) {
547 _stepService = stepService;
551 * Get the repositoryService.
553 * @return the repositoryService
555 public RepositoryService getRepositoryService() {
556 return _repositoryService;
560 * Set the repositoryService.
562 * @param repositoryService
563 * the repositoryService to set
565 public void setRepositoryService(final RepositoryService repositoryService) {
566 _repositoryService = repositoryService;
570 * Get the documentTypeService.
572 * @return the documentTypeService
574 public DocumentTypeService getDocumentTypeService() {
575 return _documentTypeService;
579 * Set the documentTypeService.
581 * @param documentTypeService
582 * the documentTypeService to set
584 public void setDocumentTypeService(
585 final DocumentTypeService documentTypeService) {
586 _documentTypeService = documentTypeService;
590 * Get the studyService.
592 * @return the studyService
594 public StudyService getStudyService() {
595 return _studyService;
599 * Set the studyService.
601 * @param studyService
602 * the studyService to set
604 public void setStudyService(final StudyService studyService) {
605 _studyService = studyService;
609 * Get the documentService.
611 * @return the documentService
613 public DocumentService getDocumentService() {
614 return _documentService;
618 * Set the documentService.
620 * @param documentService
621 * the documentService to set
623 public void setDocumentService(final DocumentService documentService) {
624 _documentService = documentService;
627 public Study getMystudy() {
631 public void setMystudy(final Study mystudy) {
632 this._mystudy = mystudy;