1 package org.splat.simer;
4 import java.io.FileNotFoundException;
5 import java.text.ParseException;
6 import java.text.SimpleDateFormat;
7 import java.util.Arrays;
8 import java.util.Iterator;
10 import java.util.ResourceBundle;
11 import java.util.Vector;
13 import org.splat.dal.bo.kernel.User;
14 import org.splat.manox.Reader;
15 import org.splat.manox.Toolbox;
16 import org.splat.dal.bo.som.Document;
17 import org.splat.dal.bo.som.ProgressState;
18 import org.splat.service.DocumentService;
19 import org.splat.service.DocumentTypeService;
20 import org.splat.service.PublicationService;
21 import org.splat.service.StepService;
22 import org.splat.service.technical.ProjectSettingsService;
23 import org.splat.service.technical.RepositoryService;
24 import org.splat.dal.bo.som.Publication;
25 import org.splat.dal.bo.som.DocumentType;
26 import org.splat.som.Revision;
27 import org.splat.som.Step;
30 * Action for adding a document into a study step.
32 public class ImportDocumentAction extends UploadBaseNextAction {
37 private static final long serialVersionUID = 2587822564883588556L;
39 private List<DocumentType> doctypes = null;
40 private long doctype = 0;
41 private String docref = null; // Reference extracted from the imported file, if exist
42 private String docver = ""; // Version number extracted from the imported file, if exist
43 private String date = ""; // Date extracted from the imported file, if exist
45 * Injected project settings service.
47 private ProjectSettingsService _projectSettingsService;
49 * Injected publication service.
51 private PublicationService _publicationService;
53 * Injected step service.
55 private StepService _stepService;
57 * Injected document service.
59 private DocumentService _documentService;
61 * Injected document type service.
63 private DocumentTypeService _documentTypeService;
65 * Injected repository service.
67 private RepositoryService _repositoryService;
69 * Value of the menu property.
70 * It can be: none, create, open, study, knowledge, sysadmin, help.
72 private String _menuProperty;
75 * Value of the title bar property.
76 * It can be: study, knowledge.
78 private String _titleProperty;
81 * Value of the tool bar property.
82 * It can be: none, standard, study, back.
84 private String _toolProperty;
87 * Value of the left menu property.
88 * It can be: open, study, knowledge, scenario.
90 private String _leftMenuProperty;
93 * Property that indicates whether the current open study is editable or not.
94 * On the screen it looks like pen on the status icon, pop-up menu also can be called.
95 * It is necessary for correct building the title bar.
97 private String _editDisabledProperty = "false";
99 // ==============================================================================================================================
101 // ==============================================================================================================================
104 * Initialize the operation.
105 * @return SUCCESS in success, otherwise - ERROR
107 public String doInitialize() {
109 setMenuProperty("study");
110 setTitleProperty("study");
111 setEditDisabledProperty("true");
112 if ("true".equals(getWriteAccess())) {
113 setToolProperty("study");
115 setToolProperty("none");
117 setLeftMenuProperty("study");
118 initializationFullScreenContext(_menuProperty, _titleProperty, _editDisabledProperty, _toolProperty, _leftMenuProperty);
120 User user = getConnectedUser();
121 File updir = getRepositoryService().getDownloadDirectory(user);
122 File upfile = new File(updir.getPath() + "/" + filename);
123 String[] table = filename.split("\\x2E");
124 String filext = table[table.length - 1].toLowerCase();
126 mystudy = getOpenStudy();
127 Step step = mystudy.getSelectedStep();
128 doctypes = getStepService().getValidDocumentTypes(step);
129 deftype = getApplicationSettings().getDefaultDocumentType(step, filext);
130 defuses = new Vector<Document>();
131 state = ProgressState.inWORK;
133 Reader tool = Toolbox.getReader(upfile);
135 String fileref = tool.extractProperty("reference");
136 String filever = tool.extractProperty("version"); // Property kept even if the file is not referenced
137 String filetype = tool.extractProperty("type"); // Property kept even if the file is not referenced
138 for (Iterator<DocumentType> i = doctypes.iterator(); i.hasNext();) {
139 DocumentType type = i.next();
140 if (!type.getName().equals(filetype))
143 doctype = type.getIndex(); // Disables the document type field
146 if (fileref != null) {
147 Document slot = getDocumentService().selectDocument(fileref,
148 new Revision().toString());
150 setErrorCode("reference.undefined");
152 setToolProperty("none");
153 initializationFullScreenContext(_menuProperty, _titleProperty, _editDisabledProperty, _toolProperty, _leftMenuProperty);
157 if (!slot.isUndefined()) {
158 setErrorCode("reference.duplicate");
160 setToolProperty("none");
161 initializationFullScreenContext(_menuProperty, _titleProperty, _editDisabledProperty, _toolProperty, _leftMenuProperty);
165 docref = fileref; // Disables document name and state fields
166 deftype = slot.getType(); // Just in case
167 doctype = deftype.getIndex(); // Disables the document type field
172 Revision.Format get = new Revision.Format(
173 getProjectSettings().getRevisionPattern());
174 Revision version = get.parse(filever);
175 if (version.isNull())
176 throw new ParseException(filever, filever.length() - 1);
177 if (!version.isMinor())
178 state = ProgressState.inCHECK;
179 docver = version.toString();
180 } catch (ParseException e) {
181 setErrorCode("format.version");
183 setToolProperty("none");
184 initializationFullScreenContext(_menuProperty, _titleProperty, _editDisabledProperty, _toolProperty, _leftMenuProperty);
188 docname = tool.extractProperty("title"); // Property kept even if the file is not referenced
189 date = tool.extractProperty("date");
191 ResourceBundle locale = ResourceBundle.getBundle("som",
192 getApplicationSettings().getCurrentLocale());
193 SimpleDateFormat check = new SimpleDateFormat(
194 locale.getString("date.format"));
197 } catch (ParseException e) {
198 setErrorCode("format.date");
200 setToolProperty("none");
201 initializationFullScreenContext(_menuProperty, _titleProperty, _editDisabledProperty, _toolProperty, _leftMenuProperty);
207 } else if (filext.equals("pdf"))
208 state = ProgressState.EXTERN; // TODO: Should external extensions be configurable ?
209 if (docname == null) {
211 for (int i = 1; i < table.length - 1; i++)
212 docname = docname + "." + table[i];
215 setupDefaultUses(deftype);
217 DocumentType[] types = doctypes.toArray(new DocumentType[doctypes
219 DocumentTypeComparator compare = new DocumentTypeComparator();
220 Arrays.sort(types, compare);
221 doctypes = Arrays.asList(types);
227 * Perform import of a document.
228 * @return SUCCESS if ok, "cancel" - if canceled, ERROR - if error
230 public String doImport() {
231 // -------------------------
233 setMenuProperty("study");
234 setTitleProperty("study");
235 setEditDisabledProperty("true");
236 initializationScreenContext(_menuProperty, _titleProperty, _editDisabledProperty);
238 if (action == ToDo.cancel)
241 setErrorCode("import.type");
243 setToolProperty("none");
244 setLeftMenuProperty("study");
245 initializationFullScreenContext(_menuProperty, _titleProperty, _editDisabledProperty, _toolProperty, _leftMenuProperty);
250 // Getting user inputs
251 mystudy = getOpenStudy();
252 User user = getConnectedUser();
253 Step step = mystudy.getSelectedStep();
254 DocumentType type = getDocumentTypeService().selectType((int)doctype);
255 File updir = getRepositoryService().getDownloadDirectory(user);
256 File upfile = new File(updir.getPath() + "/" + filename);
257 String[] table = filename.split("\\x2E");
259 // Creation of the document
260 Document.Properties dprop = new Document.Properties();
263 if (docref.length() == 0) { // Importation of a foreign document
264 // TODO: Extract property of supported documents (DOCX, ODT...)
265 addoc = getStepService().createDocument(step, dprop.setName(docname)
266 .setType(type).setFormat(table[table.length - 1])
268 updir = addoc.getSourceFile().asFile();
269 if (logger.isInfoEnabled())
270 logger.info("Moving \"" + upfile.getName() + "\" to \""
271 + updir.getPath() + "\".");
272 upfile.renameTo(updir);
274 getPublicationService().saveAs(addoc, state); // May throw FileNotFound if rename was not done
275 } catch (FileNotFoundException saverror) {
277 logger.info("Waiting for the file.");
278 upfile.renameTo(updir);
279 getPublicationService().saveAs(addoc, state); // Forget it if throw again FileNotFound
281 } else { // Importation of a previously created template-based document
282 if (date.length() > 0) {
283 ResourceBundle locale = ResourceBundle.getBundle("som",
284 getApplicationSettings().getCurrentLocale());
285 SimpleDateFormat get = new SimpleDateFormat(
286 locale.getString("date.format"));
287 dprop.setDate(get.parse(date));
289 addoc = getStepService().assignDocument(step, dprop.setReference(docref).setName(
291 updir = addoc.getSourceFile().asFile();
292 if (logger.isInfoEnabled())
293 logger.info("Moving \"" + upfile.getName() + "\" to \""
294 + updir.getPath() + "\".");
295 upfile.renameTo(updir);
297 if (docver.length() > 0)
298 getPublicationService().saveAs(addoc,
299 new Revision(docver));
301 getPublicationService().saveAs(addoc, state);
302 } catch (FileNotFoundException saverror) {
304 logger.info("Waiting for the file.");
305 upfile.renameTo(updir);
306 if (docver.length() > 0)
307 getPublicationService().saveAs(addoc,
308 new Revision(docver));
310 getPublicationService().saveAs(addoc, state);
312 mystudy.updateSimulationContexts(); // In case of simulation contexts extracted from the imported document
314 // Creation of uses relations
315 if (docuses != null) {
316 String[] list = docuses.split(",");
317 for (int i = 0; i < list.length; i++) {
318 Integer index = Integer.valueOf(list[i].trim());
319 Publication used = getPublication(index);
320 addoc.addDependency(used);
323 // Creation of derived the document formats
324 // Document ndoc = addoc.value();
325 // Converter send = getApplicationSettings().getConverter(ndoc.getType(), ndoc.getFormat());
327 // if (send != null) send.converts(addoc); // Asynchronous process
329 mystudy.add(addoc); // Updates the presentation
331 } catch (FileNotFoundException error) {
332 logger.error("Reason:", error);
333 setErrorCode("import.file");
334 } catch (Exception error) {
335 logger.error("Reason:", error);
336 setErrorCode("internal");
339 setToolProperty("none");
340 setLeftMenuProperty("study");
341 initializationFullScreenContext(_menuProperty, _titleProperty, _editDisabledProperty, _toolProperty, _leftMenuProperty);
346 // ==============================================================================================================================
347 // Getters and setters
348 // ==============================================================================================================================
350 public String getDocumentDate() {
351 // --------------------------------
355 public List<DocumentType> getDocumentTypes() {
356 // ---------------------------------------------
360 public long getDocumentType() {
361 // -----------------------------
365 public String getReference() {
366 // -----------------------------
370 public String getVersion() {
371 // ---------------------------
375 public void setDocumentDate(String date) {
376 // -----------------------------------------
380 public void setDocumentName(String name) {
381 // -----------------------------------------
382 this.docname = name; // Name entered by the user if enabled
385 public void setDocumentTitle(String name) { // Called even if DocumentName is enabled
386 // -----------------------------------------
387 if (this.docname == null)
391 public void setDocumentType(String value) {
392 // ------------------------------------------
393 this.doctype = Integer.valueOf(value);
396 public void setDefaultDocumentState(String state) { // Called even if DocumentState is enabled
397 // --------------------------------------------------
398 if (this.state == null)
399 this.state = ProgressState.valueOf(state);
402 public void setDefaultDocumentType(String value) { // Called even if DocumentType is enabled
403 // --------------------------------------------------
404 if (this.doctype == 0)
405 this.doctype = Integer.valueOf(value);
408 public void setReference(String value) {
409 // ---------------------------------------
413 public void setVersion(String value) {
414 // -------------------------------------
419 * Get project settings.
421 * @return Project settings service
423 private ProjectSettingsService getProjectSettings() {
424 return _projectSettingsService;
428 * Set project settings service.
430 * @param projectSettingsService
431 * project settings service
433 public void setProjectSettings(ProjectSettingsService projectSettingsService) {
434 _projectSettingsService = projectSettingsService;
438 * Get the publicationService.
440 * @return the publicationService
442 public PublicationService getPublicationService() {
443 return _publicationService;
447 * Set the publicationService.
449 * @param publicationService
450 * the publicationService to set
452 public void setPublicationService(PublicationService publicationService) {
453 _publicationService = publicationService;
457 * Get the stepService.
458 * @return the stepService
460 public StepService getStepService() {
465 * Set the stepService.
466 * @param stepService the stepService to set
468 public void setStepService(StepService stepService) {
469 _stepService = stepService;
473 * Get the documentService.
474 * @return the documentService
476 public DocumentService getDocumentService() {
477 return _documentService;
481 * Set the documentService.
482 * @param documentService the documentService to set
484 public void setDocumentService(DocumentService documentService) {
485 _documentService = documentService;
489 * Get the repositoryService.
490 * @return the repositoryService
492 public RepositoryService getRepositoryService() {
493 return _repositoryService;
497 * Set the repositoryService.
498 * @param repositoryService the repositoryService to set
500 public void setRepositoryService(RepositoryService repositoryService) {
501 _repositoryService = repositoryService;
505 * Get the documentTypeService.
506 * @return the documentTypeService
508 public DocumentTypeService getDocumentTypeService() {
509 return _documentTypeService;
513 * Set the documentTypeService.
514 * @param documentTypeService the documentTypeService to set
516 public void setDocumentTypeService(DocumentTypeService documentTypeService) {
517 _documentTypeService = documentTypeService;
521 * Get the menuProperty.
522 * @return the menuProperty
524 public String getMenuProperty() {
525 return _menuProperty;
529 * Set the menuProperty.
530 * @param menuProperty the menuProperty to set
532 public void setMenuProperty(String menuProperty) {
533 this._menuProperty = menuProperty;
537 * Get the _titleProperty.
538 * @return the _titleProperty
540 public String getTitleProperty() {
541 return _titleProperty;
545 * Set the _titleProperty.
546 * @param _titleProperty the titleProperty to set
548 public void setTitleProperty(String titleProperty) {
549 _titleProperty = titleProperty;
553 * Get the editDisabledProperty.
554 * @return the editDisabledProperty
556 public final String getEditDisabledProperty() {
557 return _editDisabledProperty;
561 * Set the editDisabledProperty.
562 * @param editDisabledProperty the editDisabledProperty to set
564 public final void setEditDisabledProperty(String editDisabledProperty) {
565 _editDisabledProperty = editDisabledProperty;
569 * Get the toolProperty.
570 * @return the toolProperty
572 public String getToolProperty() {
573 return _toolProperty;
577 * Set the toolProperty.
578 * @param toolProperty the toolProperty to set
580 public void setToolProperty(final String toolProperty) {
581 _toolProperty = toolProperty;
585 * Get the leftMenuProperty.
586 * @return the leftMenuProperty
588 public String getLeftMenuProperty() {
589 return _leftMenuProperty;
593 * Set the leftMenuProperty.
594 * @param leftMenuProperty the leftMenuProperty to set
596 public void setLeftMenuProperty(final String leftMenuProperty) {
597 _leftMenuProperty = leftMenuProperty;