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.kernel.Do;
19 import org.splat.dal.bo.kernel.User;
20 import org.splat.manox.Toolbox;
21 import org.splat.manox.Writer;
22 import org.splat.dal.bo.som.Document;
23 import org.splat.dal.bo.som.DocumentType;
24 import org.splat.dal.bo.som.KnowledgeElement;
25 import org.splat.dal.bo.som.ProgressState;
26 import org.splat.service.DocumentService;
27 import org.splat.service.DocumentTypeService;
28 import org.splat.service.StepService;
29 import org.splat.service.StudyService;
30 import org.splat.service.technical.RepositoryService;
31 import org.splat.dal.bo.som.Publication;
32 import org.splat.som.Revision;
33 import org.splat.dal.bo.som.Scenario;
34 import org.splat.dal.bo.som.SimulationContext;
35 import org.splat.som.Step;
36 import org.splat.som.StepRights;
37 import org.splat.dal.bo.som.Study;
38 import org.splat.som.StudyRights;
39 import org.splat.wapp.ToolBar;
41 public class OpenStudy extends OpenObject implements OpenStudyServices {
46 protected final static Logger logger = org.splat.simer.Action.logger;
48 private Study mystudy;
49 private StudyRights urightstudy; // User rights on the open study
50 private StepRights urightstep; // User rights on the selected step
51 private String version;
52 private String credate;
53 private String lasdate;
54 private Publication selecdoc;
56 * Injected step service.
58 private StepService _stepService;
60 * Injected repository service.
62 private RepositoryService _repositoryService;
64 * Injected document type service.
66 private DocumentTypeService _documentTypeService;
68 * Injected document service.
70 private DocumentService _documentService;
72 * The injected Study service.
74 private StudyService _studyService;
76 // ==============================================================================================================================
78 // ==============================================================================================================================
81 * Open the given study in the current http session.
87 * @return this open study object
89 public OpenStudy open(User user, Study study) {
90 // -----------------------------------------
91 ResourceBundle custom = ResourceBundle.getBundle("som",
92 ApplicationSettings.getCurrentLocale());
93 SimpleDateFormat datstring = new SimpleDateFormat(custom
94 .getString("date.format"));
95 Revision.Format verstring = new Revision.Format(getProjectSettings()
96 .getRevisionPattern());
98 cuser = user; // May be null if nobody connected
100 selection = "0.1"; // Default selection
103 // Preparation of the display
104 version = verstring.format(mystudy.getVersion());
105 credate = datstring.format(mystudy.getDate());
106 lasdate = ""; // Not yet supported
107 description = mystudy.getDescription();
108 involving = new ArrayList<Step>(1);
109 context = new ArrayList<SimulationContextFacade>();
110 ustep = getProjectElementService().getFirstStep(mystudy);
111 ustep.setActor(cuser);
112 involving.add(ustep);
113 for (Iterator<SimulationContext> i = ustep.getAllSimulationContexts()
114 .iterator(); i.hasNext();) {
115 context.add(new SimulationContextFacade(i.next(),
116 getProjectSettings().getAllSteps()));
118 if (getStudyService().isStaffedBy(mystudy, cuser) || getStudyService().hasActor(mystudy, cuser)) {
119 // ProgressState state = mystudy.getProgressState();
120 // if (state == ProgressState.inCHECK) popup = ApplicationSettings.getPopupMenu("stapprovable");
121 // else if (state == ProgressState.APPROVED) popup = ApplicationSettings.getPopupMenu("stapproved");
122 /* else */popup = ApplicationSettings
123 .getPopupMenu("steditable");
124 popup.setContext("study", new StudyRights(cuser, mystudy));
126 urightstudy = new StudyRights(cuser, mystudy);
127 urightstep = new StepRights(cuser, ustep);
129 // RKV menu = new StudyMenu(mystudy);
130 menu = ((StudyMenu) getMenu()).init(mystudy); // RKV
131 menu.selects(selection); // Initializes menu items to be displayed
132 setupContents(); // Initializes documents and knowledge at ustep
136 // ==============================================================================================================================
138 // ==============================================================================================================================
140 public String getAuthorName() {
141 // -----------------------------
142 return mystudy.getAuthor().toString();
145 public Long getIndex() {
146 // -------------------------
147 return mystudy.getIndex();
150 public String getDate() {
151 // ------------------------
155 public StudyMenu getMenu() {
156 // ---------------------------
157 return (StudyMenu) menu;
160 public void setMenu(StudyMenu aMenu) {
164 public ProgressState getProgressState() {
165 // ----------------------------------------
166 return mystudy.getProgressState();
169 public String getLastModificationDate() {
170 // ----------------------------------------
174 public ToolBar getModuleBar() {
175 // ------------------------------
176 return ApplicationSettings.getMe().getModuleBar(getSelectedStep());
179 public String getReference() {
180 // ----------------------------
181 return mystudy.getReference();
184 public Publication getSelectedDocument() {
185 // -----------------------------------------
189 public StepRights getSelectedStepRights() {
190 // ------------------------------------------
194 public StudyRights getStudyRights() {
195 // ------------------------------------
199 public Study getStudyObject() {
200 // ------------------------------
204 public String getTitle() {
205 // ------------------------
206 return mystudy.getTitle();
209 public String getType() {
210 // ------------------------
211 return ResourceBundle.getBundle("labels",
212 ApplicationSettings.getCurrentLocale())
213 .getString("label.study");
216 public String getVersion() {
217 // ---------------------------
221 public boolean isStepEnabled() {
222 // -------------------------------
223 return urightstep.isEnabled();
226 // ==============================================================================================================================
228 // ==============================================================================================================================
230 public URL newTemplateBasedDocument(String typename, User author) {
231 // ------------------------------------------------------------------
232 String filename = typename + ".xml"; // Only XML templates are writeable
233 File template = new File(getRepositoryService().getTemplatePath()
235 if (!template.exists())
238 // Session connex = Database.getCurSession();
239 // Transaction transax = connex.beginTransaction();
241 File udir = getRepositoryService().getDownloadDirectory(author);
242 File credoc = new File(udir.getPath() + "/" + filename);
244 // Creation of the meta-document
245 Step step = getSelectedStep(); // Should we check if the given document type is compatible ?
246 DocumentType type = getDocumentTypeService().selectType(typename);
247 Document.Properties dprop = new Document.Properties();
248 Document medoc = getStepService().createDocument(step,
249 dprop.setType(type).setFormat("xml").setAuthor(author))
253 // Instantiation of the template into the user download directory
258 Do.copy(template, credoc);
260 // Transfer to the document of all known properties
261 ResourceBundle locale = ResourceBundle.getBundle("som",
262 ApplicationSettings.getCurrentLocale());
263 SimpleDateFormat get = new SimpleDateFormat(locale
264 .getString("date.format"));
265 Writer tool = Toolbox.getWriter(credoc);
266 List<Step> slist = getInvolvedSteps();
267 for (Iterator<Step> i = slist.iterator(); i.hasNext();) {
268 List<SimulationContext> clist = i.next()
269 .getAllSimulationContexts();
270 for (Iterator<SimulationContext> j = clist.iterator(); j
272 SimulationContext context = j.next();
273 tool.updateProperty(context.getType().getName(), context
277 tool.updateProperty("reference", medoc.getReference());
278 tool.updateProperty("study", mystudy.getTitle());
279 tool.updateProperty("step", locale.getString(
280 "folder.step." + step.getNumber()).replaceAll("''", "'"));
281 tool.updateProperty("author", author.getUsername().toUpperCase());
282 tool.updateProperty("date", get.format(medoc.getCreationDate()));
283 tool.updateProperty("history", locale.getString("label.creation")
284 .replaceAll("''", "'"));
287 return new URL(ApplicationSettings.getDownloadURL(author)
289 } catch (Exception saverror) {
290 logger.error("Reason:", saverror);
295 public void selectDocument(String docurl) {
296 // ------------------------------------------
297 String prefix = ApplicationSettings.getRepositoryURL();
299 if (docurl.startsWith(prefix))
301 String path = docurl.substring(prefix.length());
302 Document value = getDocumentService().getDocumentByPath(path);
304 selecdoc = ustep.getDocument(value.getIndex());
305 } catch (Exception error) {
306 logger.error("Reason:", error);
310 public void setSelection(String step) {
311 // --------------------------------------
312 if (!step.equals(selection)) {
315 setupPreviousToSelectedSteps();
316 updateSimulationContexts(); // Initializes contexts according to the selected steps
318 ustep = involving.get(involving.size() - 1);
319 urightstep = new StepRights(cuser, ustep);
320 ustep.setActor(cuser);
321 menu.selects(selection); // Updates menu items to be displayed
322 setupContents(); // The contents may have changed even if the selection is the same
325 // ==============================================================================================================================
326 // Protected services
327 // ==============================================================================================================================
329 protected void add(Publication doc) {
330 // ------------------------------------
331 DocumentFacade facade = new DocumentFacade(this, doc,
332 getProjectSettings(), getPublicationService());
333 boolean first = (contents.size() == 0);
335 docpres.put(doc.getIndex(), facade);
336 contents.add(0, facade); // Prepend the new publication
338 this.getMenu().refreshSelectedItem();
342 protected void add(SimulationContext contex) {
343 // ---------------------------------------------
344 SimulationContextFacade facade = new SimulationContextFacade(contex,
345 getProjectSettings().getAllSteps());
350 protected void add(KnowledgeElement kelm) {
351 // ------------------------------------------
352 KnowledgeElementFacade facade = new KnowledgeElementFacade(kelm);
353 // RKV KnowledgeIterator known = knowledge.get(kelm.getType().getIndex() - 2);
354 // Knowledges are ordered by type index, from 0 to n-1, the first one being reserved (reason for -2)
355 // RKV:Begin: Find a knowledge iterator for appropriate knowledge type
356 KnowledgeIterator known = null;
357 for (KnowledgeIterator aKnowledgeSection : knowledge) {
358 if (aKnowledgeSection.getIndex().equals(
359 String.valueOf(kelm.getType().getIndex()))) {
360 known = aKnowledgeSection;
364 if (known != null) { // RKV:End
365 knowpres.put(kelm.getIndex(), facade);
366 known.list.add(facade); // Insert the new knowledge at the end of the corresponding knowledge type
370 protected void remove(Publication doctag) {
371 // ------------------------------------------
372 for (Iterator<DocumentFacade> i = contents.iterator(); i.hasNext();) {
373 DocumentFacade facade = i.next();
374 if (!facade.isFacadeOf(doctag))
379 if (contents.size() == 0)
380 this.getMenu().refreshSelectedItem();
383 protected void changeUser(User user) {
384 // -------------------------------------
387 if (getStudyService().isStaffedBy(mystudy, cuser)) {
388 popup = ApplicationSettings.getPopupMenu("steditable");
389 popup.setContext("study", new StudyRights(cuser, mystudy));
391 ustep.setActor(cuser);
392 urightstudy = new StudyRights(cuser, mystudy);
393 urightstep = new StepRights(cuser, ustep);
396 protected void remove(SimulationContext contex) {
397 // ------------------------------------------------
398 for (Iterator<SimulationContextFacade> i = context.iterator(); i
400 SimulationContextFacade facade = i.next();
401 if (!facade.isFacadeOf(contex))
408 protected void remove(KnowledgeElement kelm) {
409 // ---------------------------------------------
410 KnowledgeIterator known = knowledge.get((int) (kelm.getType()
412 // Knowledges are ordered by type index, from 0 to n-1, the first one being reserved (reason for -2)
413 knowpres.remove(kelm.getIndex());
414 for (Iterator<KnowledgeElementFacade> i = known.list.iterator(); i
416 KnowledgeElementFacade facade = i.next();
417 if (!facade.isFacadeOf(kelm))
424 protected void update(Publication doc) {
425 // ---------------------------------------
426 DocumentFacade facade = docpres.get(doc.getIndex());
427 if (facade != null) {
432 protected void update(KnowledgeElement kelm) {
433 // ---------------------------------------------
434 KnowledgeElementFacade facade = knowpres.get(kelm.getIndex());
435 if (facade != null) {
440 protected void updateSimulationContexts() {
441 // ------------------------------------------
443 for (Iterator<Step> i = involving.iterator(); i.hasNext();) {
444 for (Iterator<SimulationContext> j = i.next()
445 .getAllSimulationContexts().iterator(); j.hasNext();) {
446 context.add(new SimulationContextFacade(j.next(),
447 getProjectSettings().getAllSteps()));
452 // ==============================================================================================================================
454 // ==============================================================================================================================
456 private void setupPreviousToSelectedSteps() {
457 // --------------------------------------------
458 String[] item = selection.split("\\x2E");
459 int major = Integer.valueOf(item[0]);
460 int minor = Integer.valueOf(item[1]);
466 Scenario[] branch = mystudy.getScenarii();
467 Scenario scenar = branch[0];
468 for (int i = 0; i < branch.length; i++) {
470 if (scenar.getIndex() == major)
471 break; // Supposed exist
473 step = getProjectElementService().getSteps(scenar);
474 base = step[0].getNumber() - 1;
475 for (int i = 0; i + base < minor; i++) {
476 involving.add(step[i]);
479 step = getProjectElementService().getSteps(mystudy);
480 for (int i = step.length - 1; i > -1; i--) {
481 Step firstep = step[i];
482 if (firstep.getNumber() > base)
484 involving.add(0, firstep);
489 * Get the stepService.
491 * @return the stepService
493 public StepService getStepService() {
498 * Set the stepService.
501 * the stepService to set
503 public void setStepService(StepService stepService) {
504 _stepService = stepService;
508 * Get the repositoryService.
510 * @return the repositoryService
512 public RepositoryService getRepositoryService() {
513 return _repositoryService;
517 * Set the repositoryService.
519 * @param repositoryService
520 * the repositoryService to set
522 public void setRepositoryService(RepositoryService repositoryService) {
523 _repositoryService = repositoryService;
527 * Get the documentTypeService.
528 * @return the documentTypeService
530 public DocumentTypeService getDocumentTypeService() {
531 return _documentTypeService;
535 * Set the documentTypeService.
536 * @param documentTypeService the documentTypeService to set
538 public void setDocumentTypeService(DocumentTypeService documentTypeService) {
539 _documentTypeService = documentTypeService;
543 * Get the studyService.
545 * @return the studyService
547 public StudyService getStudyService() {
548 return _studyService;
552 * Set the studyService.
554 * @param studyService
555 * the studyService to set
557 public void setStudyService(StudyService studyService) {
558 _studyService = studyService;
562 * Get the documentService.
563 * @return the documentService
565 public DocumentService getDocumentService() {
566 return _documentService;
570 * Set the documentService.
571 * @param documentService the documentService to set
573 public void setDocumentService(DocumentService documentService) {
574 _documentService = documentService;