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");
137 /* else */_popup = getApplicationSettings().getPopupMenu(
139 _popup.setContext("study", new StudyRights(_cuser, _mystudy));
141 _urightstudy = new StudyRights(_cuser, _mystudy);
142 _urightstep = new StepRights(_cuser, _ustep);
144 // RKV menu = new StudyMenu(mystudy);
145 _menu = (getMenu()).init(_mystudy); // RKV
146 _menu.selects(_selection); // Initializes menu items to be displayed
147 setupContents(); // Initializes documents and knowledge at ustep
151 // ==============================================================================================================================
153 // ==============================================================================================================================
155 public String getAuthorName() {
156 return _mystudy.getAuthor().toString();
159 public Long getIndex() {
160 return _mystudy.getIndex();
163 public String getDate() {
168 public StudyMenu getMenu() {
169 return (StudyMenu) _menu;
172 public void setMenu(final StudyMenu aMenu) {
176 public ProgressState getProgressState() {
177 return _mystudy.getProgressState();
180 public String getLastModificationDate() {
184 public ToolBar getModuleBar() {
185 return getApplicationSettings().getModuleBar(getSelectedStep());
188 public String getReference() {
189 return _mystudy.getReference();
192 public Publication getSelectedDocument() {
196 public StepRights getSelectedStepRights() {
200 public StudyRights getStudyRights() {
204 public Study getStudyObject() {
208 public String getTitle() {
209 return _mystudy.getTitle();
212 public String getType() {
214 * RKV: return ResourceBundle.getBundle("labels", getApplicationSettings().getCurrentLocale()).getString( "label.study");
219 public String getVersion() {
223 public boolean isStepEnabled() {
224 return _urightstep.isEnabled();
227 // ==============================================================================================================================
229 // ==============================================================================================================================
231 public URL newTemplateBasedDocument(final String typename, final User author) {
232 String filename = typename + ".xml"; // Only XML templates are writeable
233 File template = new File(getRepositoryService().getTemplatePath()
235 if (!template.exists()) {
239 // Session connex = Database.getCurSession();
240 // Transaction transax = connex.beginTransaction();
242 File udir = getRepositoryService().getDownloadDirectory(author);
243 File credoc = new File(udir.getPath() + "/" + filename);
245 // Creation of the meta-document
246 Step step = getSelectedStep(); // Should we check if the given document type is compatible ?
247 DocumentType type = getDocumentTypeService().selectType(typename);
248 Document.Properties dprop = new Document.Properties();
249 Document medoc = getStepService().createDocument(step,
250 dprop.setType(type).setFormat("xml").setAuthor(author))
254 // Instantiation of the template into the user download directory
255 if (!udir.exists()) {
258 if (credoc.exists()) {
261 Do.copy(template, credoc);
263 // Transfer to the document of all known properties
264 ResourceBundle locale = ResourceBundle.getBundle("som",
265 getApplicationSettings().getCurrentLocale());
266 SimpleDateFormat get = new SimpleDateFormat(locale
267 .getString("date.format"));
268 Writer tool = Toolbox.getWriter(credoc);
269 List<Step> slist = getInvolvedSteps();
270 for (Iterator<Step> i = slist.iterator(); i.hasNext();) {
271 List<SimulationContext> clist = i.next()
272 .getAllSimulationContexts();
273 for (Iterator<SimulationContext> j = clist.iterator(); j
275 SimulationContext context = j.next();
276 tool.updateProperty(context.getType().getName(), context
280 tool.updateProperty("reference", medoc.getReference());
281 tool.updateProperty("study", _mystudy.getTitle());
282 tool.updateProperty("step", locale.getString(
283 "folder.step." + step.getNumber()).replaceAll("''", "'"));
284 tool.updateProperty("author", author.getUsername().toUpperCase());
285 tool.updateProperty("date", get.format(medoc.getCreationDate()));
286 tool.updateProperty("history", locale.getString("label.creation")
287 .replaceAll("''", "'"));
290 return new URL(getApplicationSettings().getDownloadURL(author)
292 } catch (Exception saverror) {
293 LOGGER.error("Reason:", saverror);
298 public void selectDocument(final String docurl) {
299 String prefix = getApplicationSettings().getRepositoryURL();
301 if (docurl.startsWith(prefix)) {
303 String path = docurl.substring(prefix.length());
304 Document value = getDocumentService().getDocumentByPath(path);
306 _selecdoc = _ustep.getDocument(value.getIndex());
307 } catch (Exception error) {
308 LOGGER.error("Reason:", error);
313 public void setSelection(final String step) {
314 if (!step.equals(_selection)) {
317 setupPreviousToSelectedSteps();
318 updateSimulationContexts(); // Initializes contexts according to the selected steps
320 _ustep = _involving.get(_involving.size() - 1);
321 _urightstep = new StepRights(_cuser, _ustep);
322 _ustep.setActor(_cuser);
323 _menu.selects(_selection); // Updates menu items to be displayed
324 setupContents(); // The contents may have changed even if the selection is the same
327 // ==============================================================================================================================
328 // Protected services
329 // ==============================================================================================================================
331 protected void add(final Publication doc) {
332 DocumentFacade facade = new DocumentFacade(this, doc,
333 getProjectSettings(), getPublicationService(),
334 getApplicationSettings());
335 boolean first = (_contents.size() == 0);
337 docpres.put(doc.getIndex(), facade);
338 _contents.add(0, facade); // Prepend the new publication
340 this.getMenu().refreshSelectedItem();
345 protected void add(final SimulationContext contex) {
346 SimulationContextFacade facade = new SimulationContextFacade(contex,
347 getProjectSettings().getAllSteps(), getApplicationSettings());
349 _context.add(facade);
352 protected void add(final KnowledgeElement kelm) {
353 KnowledgeElementFacade facade = new KnowledgeElementFacade(BeanHelper
354 .copyBean(kelm, KnowledgeElementDTO.class),
355 getApplicationSettings());
356 // RKV KnowledgeIterator known = knowledge.get(kelm.getType().getIndex() - 2);
357 // Knowledges are ordered by type index, from 0 to n-1, the first one being reserved (reason for -2)
358 // RKV:Begin: Find a knowledge iterator for appropriate knowledge type
359 KnowledgeIterator known = null;
360 for (KnowledgeIterator aKnowledgeSection : _knowledge) {
361 if (aKnowledgeSection.getIndex().equals(
362 String.valueOf(kelm.getType().getIndex()))) {
363 known = aKnowledgeSection;
367 if (known != null) { // RKV:End
368 knowpres.put(kelm.getIndex(), facade);
369 known._list.add(facade); // Insert the new knowledge at the end of the corresponding knowledge type
373 protected void remove(final Publication doctag) {
374 for (Iterator<DocumentFacade> i = _contents.iterator(); i.hasNext();) {
375 DocumentFacade facade = i.next();
376 if (facade.isFacadeOf(doctag)) {
381 if (_contents.size() == 0) {
382 this.getMenu().refreshSelectedItem();
386 protected void changeUser(final User user) {
389 if (getStudyService().isStaffedBy(_mystudy, _cuser)) {
390 _popup = getApplicationSettings().getPopupMenu("steditable");
391 _popup.setContext("study", new StudyRights(_cuser, _mystudy));
393 // ustep = getProjectElementService().getFirstStep(mystudy);
394 if (_ustep != null) {
395 _ustep.setActor(_cuser);
397 _urightstudy = new StudyRights(_cuser, _mystudy);
398 _urightstep = new StepRights(_cuser, _ustep);
401 protected void remove(final SimulationContext contex) {
402 for (Iterator<SimulationContextFacade> i = _context.iterator(); i
404 SimulationContextFacade facade = i.next();
405 if (!facade.isFacadeOf(contex)) {
413 protected void remove(final KnowledgeElement kelm) {
414 // RKV: KnowledgeIterator known = _knowledge.get((int) (kelm.getType()
415 // RKV: .getIndex() - 2));
416 // Knowledges are ordered by type index, from 0 to n-1, the first one being reserved (reason for -2)
417 // RKV:Begin: Find a knowledge iterator for appropriate knowledge type
418 KnowledgeIterator known = null;
419 for (KnowledgeIterator aKnowledgeSection : _knowledge) {
420 if (aKnowledgeSection.getIndex().equals(
421 String.valueOf(kelm.getType().getIndex()))) {
422 known = aKnowledgeSection;
426 if (known != null) { // RKV:End
427 knowpres.remove(kelm.getIndex());
428 for (Iterator<KnowledgeElementFacade> i = known._list.iterator(); i
430 KnowledgeElementFacade facade = i.next();
431 if (!facade.isFacadeOf(BeanHelper.copyBean(kelm,
432 KnowledgeElementDTO.class))) {
441 protected void update(final Publication doc) {
442 DocumentFacade facade = docpres.get(doc.getIndex());
443 if (facade != null) {
448 protected void update(final KnowledgeElementDTO kelm) {
449 KnowledgeElementFacade facade = knowpres.get(kelm.getIndex());
450 if (facade != null) {
451 facade.refresh(kelm);
455 protected void updateSimulationContexts() {
457 for (Iterator<Step> i = _involving.iterator(); i.hasNext();) {
458 for (Iterator<SimulationContext> j = i.next()
459 .getAllSimulationContexts().iterator(); j.hasNext();) {
460 _context.add(new SimulationContextFacade(j.next(),
461 getProjectSettings().getAllSteps(),
462 getApplicationSettings()));
467 // ==============================================================================================================================
469 // ==============================================================================================================================
471 private void setupPreviousToSelectedSteps() {
472 String[] item = _selection.split("\\x2E");
473 int major = Integer.valueOf(item[0]);
474 int minor = Integer.valueOf(item[1]);
480 Scenario[] branch = _mystudy.getScenarii();
481 Scenario scenar = branch[0];
482 for (int i = 0; i < branch.length; i++) {
484 if (scenar.getIndex() == major) {
485 break; // Supposed exist
488 step = getProjectElementService().getSteps(scenar);
489 base = step[0].getNumber() - 1;
490 for (int i = 0; i + base < minor; i++) {
491 _involving.add(step[i]);
494 step = getProjectElementService().getSteps(_mystudy);
495 for (int i = step.length - 1; i > -1; i--) {
496 Step firstep = step[i];
497 if (firstep.getNumber() > base) {
500 _involving.add(0, firstep);
505 * Get the stepService.
507 * @return the stepService
509 public StepService getStepService() {
514 * Set the stepService.
517 * the stepService to set
519 public void setStepService(final StepService stepService) {
520 _stepService = stepService;
524 * Get the repositoryService.
526 * @return the repositoryService
528 public RepositoryService getRepositoryService() {
529 return _repositoryService;
533 * Set the repositoryService.
535 * @param repositoryService
536 * the repositoryService to set
538 public void setRepositoryService(final RepositoryService repositoryService) {
539 _repositoryService = repositoryService;
543 * Get the documentTypeService.
545 * @return the documentTypeService
547 public DocumentTypeService getDocumentTypeService() {
548 return _documentTypeService;
552 * Set the documentTypeService.
554 * @param documentTypeService
555 * the documentTypeService to set
557 public void setDocumentTypeService(
558 final DocumentTypeService documentTypeService) {
559 _documentTypeService = documentTypeService;
563 * Get the studyService.
565 * @return the studyService
567 public StudyService getStudyService() {
568 return _studyService;
572 * Set the studyService.
574 * @param studyService
575 * the studyService to set
577 public void setStudyService(final StudyService studyService) {
578 _studyService = studyService;
582 * Get the documentService.
584 * @return the documentService
586 public DocumentService getDocumentService() {
587 return _documentService;
591 * Set the documentService.
593 * @param documentService
594 * the documentService to set
596 public void setDocumentService(final DocumentService documentService) {
597 _documentService = documentService;
600 public Study getMystudy() {
604 public void setMystudy(final Study mystudy) {
605 this._mystudy = mystudy;