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 * Property that indicates whether the current open study is editable or not.
82 * On the screen it looks like pen on the status icon, pop-up menu also can be called.
83 * It is necessary for correct building the title bar.
85 private String _editDisabledProperty = "false";
87 // ==============================================================================================================================
89 // ==============================================================================================================================
92 * Initialize the operation.
93 * @return SUCCESS in success, otherwise - ERROR
95 public String doInitialize() {
97 setMenuProperty("study");
98 setTitleProperty("study");
99 setEditDisabledProperty("true");
100 initializationScreenContext(_menuProperty, _titleProperty, _editDisabledProperty);
102 User user = getConnectedUser();
103 File updir = getRepositoryService().getDownloadDirectory(user);
104 File upfile = new File(updir.getPath() + "/" + filename);
105 String[] table = filename.split("\\x2E");
106 String filext = table[table.length - 1].toLowerCase();
108 mystudy = getOpenStudy();
109 Step step = mystudy.getSelectedStep();
110 doctypes = getStepService().getValidDocumentTypes(step);
111 deftype = getApplicationSettings().getDefaultDocumentType(step, filext);
112 defuses = new Vector<Document>();
113 state = ProgressState.inWORK;
115 Reader tool = Toolbox.getReader(upfile);
117 String fileref = tool.extractProperty("reference");
118 String filever = tool.extractProperty("version"); // Property kept even if the file is not referenced
119 String filetype = tool.extractProperty("type"); // Property kept even if the file is not referenced
120 for (Iterator<DocumentType> i = doctypes.iterator(); i.hasNext();) {
121 DocumentType type = i.next();
122 if (!type.getName().equals(filetype))
125 doctype = type.getIndex(); // Disables the document type field
128 if (fileref != null) {
129 Document slot = getDocumentService().selectDocument(fileref,
130 new Revision().toString());
132 setErrorCode("reference.undefined");
135 if (!slot.isUndefined()) {
136 setErrorCode("reference.duplicate");
139 docref = fileref; // Disables document name and state fields
140 deftype = slot.getType(); // Just in case
141 doctype = deftype.getIndex(); // Disables the document type field
146 Revision.Format get = new Revision.Format(
147 getProjectSettings().getRevisionPattern());
148 Revision version = get.parse(filever);
149 if (version.isNull())
150 throw new ParseException(filever, filever.length() - 1);
151 if (!version.isMinor())
152 state = ProgressState.inCHECK;
153 docver = version.toString();
154 } catch (ParseException e) {
155 setErrorCode("format.version");
158 docname = tool.extractProperty("title"); // Property kept even if the file is not referenced
159 date = tool.extractProperty("date");
161 ResourceBundle locale = ResourceBundle.getBundle("som",
162 getApplicationSettings().getCurrentLocale());
163 SimpleDateFormat check = new SimpleDateFormat(
164 locale.getString("date.format"));
167 } catch (ParseException e) {
168 setErrorCode("format.date");
173 } else if (filext.equals("pdf"))
174 state = ProgressState.EXTERN; // TODO: Should external extensions be configurable ?
175 if (docname == null) {
177 for (int i = 1; i < table.length - 1; i++)
178 docname = docname + "." + table[i];
181 setupDefaultUses(deftype);
183 DocumentType[] types = doctypes.toArray(new DocumentType[doctypes
185 DocumentTypeComparator compare = new DocumentTypeComparator();
186 Arrays.sort(types, compare);
187 doctypes = Arrays.asList(types);
193 * Perform import of a document.
194 * @return SUCCESS if ok, "cancel" - if canceled, ERROR - if error
196 public String doImport() {
197 // -------------------------
199 setMenuProperty("study");
200 setTitleProperty("study");
201 setEditDisabledProperty("true");
202 initializationScreenContext(_menuProperty, _titleProperty, _editDisabledProperty);
204 if (action == ToDo.cancel)
207 setErrorCode("import.type");
211 // Getting user inputs
212 mystudy = getOpenStudy();
213 User user = getConnectedUser();
214 Step step = mystudy.getSelectedStep();
215 DocumentType type = getDocumentTypeService().selectType((int)doctype);
216 File updir = getRepositoryService().getDownloadDirectory(user);
217 File upfile = new File(updir.getPath() + "/" + filename);
218 String[] table = filename.split("\\x2E");
220 // Creation of the document
221 Document.Properties dprop = new Document.Properties();
224 if (docref.length() == 0) { // Importation of a foreign document
225 // TODO: Extract property of supported documents (DOCX, ODT...)
226 addoc = getStepService().createDocument(step, dprop.setName(docname)
227 .setType(type).setFormat(table[table.length - 1])
229 updir = addoc.getSourceFile().asFile();
230 if (logger.isInfoEnabled())
231 logger.info("Moving \"" + upfile.getName() + "\" to \""
232 + updir.getPath() + "\".");
233 upfile.renameTo(updir);
235 getPublicationService().saveAs(addoc, state); // May throw FileNotFound if rename was not done
236 } catch (FileNotFoundException saverror) {
238 logger.info("Waiting for the file.");
239 upfile.renameTo(updir);
240 getPublicationService().saveAs(addoc, state); // Forget it if throw again FileNotFound
242 } else { // Importation of a previously created template-based document
243 if (date.length() > 0) {
244 ResourceBundle locale = ResourceBundle.getBundle("som",
245 getApplicationSettings().getCurrentLocale());
246 SimpleDateFormat get = new SimpleDateFormat(
247 locale.getString("date.format"));
248 dprop.setDate(get.parse(date));
250 addoc = getStepService().assignDocument(step, dprop.setReference(docref).setName(
252 updir = addoc.getSourceFile().asFile();
253 if (logger.isInfoEnabled())
254 logger.info("Moving \"" + upfile.getName() + "\" to \""
255 + updir.getPath() + "\".");
256 upfile.renameTo(updir);
258 if (docver.length() > 0)
259 getPublicationService().saveAs(addoc,
260 new Revision(docver));
262 getPublicationService().saveAs(addoc, state);
263 } catch (FileNotFoundException saverror) {
265 logger.info("Waiting for the file.");
266 upfile.renameTo(updir);
267 if (docver.length() > 0)
268 getPublicationService().saveAs(addoc,
269 new Revision(docver));
271 getPublicationService().saveAs(addoc, state);
273 mystudy.updateSimulationContexts(); // In case of simulation contexts extracted from the imported document
275 // Creation of uses relations
276 if (docuses != null) {
277 String[] list = docuses.split(",");
278 for (int i = 0; i < list.length; i++) {
279 Integer index = Integer.valueOf(list[i].trim());
280 Publication used = getPublication(index);
281 addoc.addDependency(used);
284 // Creation of derived the document formats
285 // Document ndoc = addoc.value();
286 // Converter send = getApplicationSettings().getConverter(ndoc.getType(), ndoc.getFormat());
288 // if (send != null) send.converts(addoc); // Asynchronous process
290 mystudy.add(addoc); // Updates the presentation
292 } catch (FileNotFoundException error) {
293 logger.error("Reason:", error);
294 setErrorCode("import.file");
295 } catch (Exception error) {
296 logger.error("Reason:", error);
297 setErrorCode("internal");
302 // ==============================================================================================================================
303 // Getters and setters
304 // ==============================================================================================================================
306 public String getDocumentDate() {
307 // --------------------------------
311 public List<DocumentType> getDocumentTypes() {
312 // ---------------------------------------------
316 public long getDocumentType() {
317 // -----------------------------
321 public String getReference() {
322 // -----------------------------
326 public String getVersion() {
327 // ---------------------------
331 public void setDocumentDate(String date) {
332 // -----------------------------------------
336 public void setDocumentName(String name) {
337 // -----------------------------------------
338 this.docname = name; // Name entered by the user if enabled
341 public void setDocumentTitle(String name) { // Called even if DocumentName is enabled
342 // -----------------------------------------
343 if (this.docname == null)
347 public void setDocumentType(String value) {
348 // ------------------------------------------
349 this.doctype = Integer.valueOf(value);
352 public void setDefaultDocumentState(String state) { // Called even if DocumentState is enabled
353 // --------------------------------------------------
354 if (this.state == null)
355 this.state = ProgressState.valueOf(state);
358 public void setDefaultDocumentType(String value) { // Called even if DocumentType is enabled
359 // --------------------------------------------------
360 if (this.doctype == 0)
361 this.doctype = Integer.valueOf(value);
364 public void setReference(String value) {
365 // ---------------------------------------
369 public void setVersion(String value) {
370 // -------------------------------------
375 * Get project settings.
377 * @return Project settings service
379 private ProjectSettingsService getProjectSettings() {
380 return _projectSettingsService;
384 * Set project settings service.
386 * @param projectSettingsService
387 * project settings service
389 public void setProjectSettings(ProjectSettingsService projectSettingsService) {
390 _projectSettingsService = projectSettingsService;
394 * Get the publicationService.
396 * @return the publicationService
398 public PublicationService getPublicationService() {
399 return _publicationService;
403 * Set the publicationService.
405 * @param publicationService
406 * the publicationService to set
408 public void setPublicationService(PublicationService publicationService) {
409 _publicationService = publicationService;
413 * Get the stepService.
414 * @return the stepService
416 public StepService getStepService() {
421 * Set the stepService.
422 * @param stepService the stepService to set
424 public void setStepService(StepService stepService) {
425 _stepService = stepService;
429 * Get the documentService.
430 * @return the documentService
432 public DocumentService getDocumentService() {
433 return _documentService;
437 * Set the documentService.
438 * @param documentService the documentService to set
440 public void setDocumentService(DocumentService documentService) {
441 _documentService = documentService;
445 * Get the repositoryService.
446 * @return the repositoryService
448 public RepositoryService getRepositoryService() {
449 return _repositoryService;
453 * Set the repositoryService.
454 * @param repositoryService the repositoryService to set
456 public void setRepositoryService(RepositoryService repositoryService) {
457 _repositoryService = repositoryService;
461 * Get the documentTypeService.
462 * @return the documentTypeService
464 public DocumentTypeService getDocumentTypeService() {
465 return _documentTypeService;
469 * Set the documentTypeService.
470 * @param documentTypeService the documentTypeService to set
472 public void setDocumentTypeService(DocumentTypeService documentTypeService) {
473 _documentTypeService = documentTypeService;
477 * Get the menuProperty.
478 * @return the menuProperty
480 public String getMenuProperty() {
481 return _menuProperty;
485 * Set the menuProperty.
486 * @param menuProperty the menuProperty to set
488 public void setMenuProperty(String menuProperty) {
489 this._menuProperty = menuProperty;
493 * Get the _titleProperty.
494 * @return the _titleProperty
496 public String getTitleProperty() {
497 return _titleProperty;
501 * Set the _titleProperty.
502 * @param _titleProperty the titleProperty to set
504 public void setTitleProperty(String titleProperty) {
505 _titleProperty = titleProperty;
509 * Get the editDisabledProperty.
510 * @return the editDisabledProperty
512 public final String getEditDisabledProperty() {
513 return _editDisabledProperty;
517 * Set the editDisabledProperty.
518 * @param editDisabledProperty the editDisabledProperty to set
520 public final void setEditDisabledProperty(String editDisabledProperty) {
521 _editDisabledProperty = editDisabledProperty;