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;
74 // ==============================================================================================================================
76 // ==============================================================================================================================
79 * Initialize the operation.
80 * @return SUCCESS in success, otherwise - ERROR
82 public String doInitialize() {
84 setMenuProperty("study");
85 initializationScreenContext(_menuProperty);
87 User user = getConnectedUser();
88 File updir = getRepositoryService().getDownloadDirectory(user);
89 File upfile = new File(updir.getPath() + "/" + filename);
90 String[] table = filename.split("\\x2E");
91 String filext = table[table.length - 1].toLowerCase();
93 mystudy = getOpenStudy();
94 Step step = mystudy.getSelectedStep();
95 doctypes = getStepService().getValidDocumentTypes(step);
96 deftype = ApplicationSettings.getDefaultDocumentType(step, filext);
97 defuses = new Vector<Document>();
98 state = ProgressState.inWORK;
100 Reader tool = Toolbox.getReader(upfile);
102 String fileref = tool.extractProperty("reference");
103 String filever = tool.extractProperty("version"); // Property kept even if the file is not referenced
104 String filetype = tool.extractProperty("type"); // Property kept even if the file is not referenced
105 for (Iterator<DocumentType> i = doctypes.iterator(); i.hasNext();) {
106 DocumentType type = i.next();
107 if (!type.getName().equals(filetype))
110 doctype = type.getIndex(); // Disables the document type field
113 if (fileref != null) {
114 Document slot = getDocumentService().selectDocument(fileref,
115 new Revision().toString());
117 setErrorCode("reference.undefined");
120 if (!slot.isUndefined()) {
121 setErrorCode("reference.duplicate");
124 docref = fileref; // Disables document name and state fields
125 deftype = slot.getType(); // Just in case
126 doctype = deftype.getIndex(); // Disables the document type field
131 Revision.Format get = new Revision.Format(
132 getProjectSettings().getRevisionPattern());
133 Revision version = get.parse(filever);
134 if (version.isNull())
135 throw new ParseException(filever, filever.length() - 1);
136 if (!version.isMinor())
137 state = ProgressState.inCHECK;
138 docver = version.toString();
139 } catch (ParseException e) {
140 setErrorCode("format.version");
143 docname = tool.extractProperty("title"); // Property kept even if the file is not referenced
144 date = tool.extractProperty("date");
146 ResourceBundle locale = ResourceBundle.getBundle("som",
147 ApplicationSettings.getCurrentLocale());
148 SimpleDateFormat check = new SimpleDateFormat(
149 locale.getString("date.format"));
152 } catch (ParseException e) {
153 setErrorCode("format.date");
158 } else if (filext.equals("pdf"))
159 state = ProgressState.EXTERN; // TODO: Should external extensions be configurable ?
160 if (docname == null) {
162 for (int i = 1; i < table.length - 1; i++)
163 docname = docname + "." + table[i];
166 setupDefaultUses(deftype);
168 DocumentType[] types = doctypes.toArray(new DocumentType[doctypes
170 DocumentTypeComparator compare = new DocumentTypeComparator();
171 Arrays.sort(types, compare);
172 doctypes = Arrays.asList(types);
178 * Perform import of a document.
179 * @return SUCCESS if ok, "cancel" - if canceled, ERROR - if error
181 public String doImport() {
182 // -------------------------
184 setMenuProperty("study");
185 initializationScreenContext(_menuProperty);
187 if (action == ToDo.cancel)
190 setErrorCode("import.type");
194 // Getting user inputs
195 mystudy = getOpenStudy();
196 User user = getConnectedUser();
197 Step step = mystudy.getSelectedStep();
198 DocumentType type = getDocumentTypeService().selectType((int)doctype);
199 File updir = getRepositoryService().getDownloadDirectory(user);
200 File upfile = new File(updir.getPath() + "/" + filename);
201 String[] table = filename.split("\\x2E");
203 // Creation of the document
204 Document.Properties dprop = new Document.Properties();
207 if (docref.length() == 0) { // Importation of a foreign document
208 // TODO: Extract property of supported documents (DOCX, ODT...)
209 addoc = getStepService().createDocument(step, dprop.setName(docname)
210 .setType(type).setFormat(table[table.length - 1])
212 updir = addoc.getSourceFile().asFile();
213 if (logger.isInfoEnabled())
214 logger.info("Moving \"" + upfile.getName() + "\" to \""
215 + updir.getPath() + "\".");
216 upfile.renameTo(updir);
218 getPublicationService().saveAs(addoc, state); // May throw FileNotFound if rename was not done
219 } catch (FileNotFoundException saverror) {
221 logger.info("Waiting for the file.");
222 upfile.renameTo(updir);
223 getPublicationService().saveAs(addoc, state); // Forget it if throw again FileNotFound
225 } else { // Importation of a previously created template-based document
226 if (date.length() > 0) {
227 ResourceBundle locale = ResourceBundle.getBundle("som",
228 ApplicationSettings.getCurrentLocale());
229 SimpleDateFormat get = new SimpleDateFormat(
230 locale.getString("date.format"));
231 dprop.setDate(get.parse(date));
233 addoc = getStepService().assignDocument(step, dprop.setReference(docref).setName(
235 updir = addoc.getSourceFile().asFile();
236 if (logger.isInfoEnabled())
237 logger.info("Moving \"" + upfile.getName() + "\" to \""
238 + updir.getPath() + "\".");
239 upfile.renameTo(updir);
241 if (docver.length() > 0)
242 getPublicationService().saveAs(addoc,
243 new Revision(docver));
245 getPublicationService().saveAs(addoc, state);
246 } catch (FileNotFoundException saverror) {
248 logger.info("Waiting for the file.");
249 upfile.renameTo(updir);
250 if (docver.length() > 0)
251 getPublicationService().saveAs(addoc,
252 new Revision(docver));
254 getPublicationService().saveAs(addoc, state);
256 mystudy.updateSimulationContexts(); // In case of simulation contexts extracted from the imported document
258 // Creation of uses relations
259 if (docuses != null) {
260 String[] list = docuses.split(",");
261 for (int i = 0; i < list.length; i++) {
262 Integer index = Integer.valueOf(list[i].trim());
263 Publication used = getPublication(index);
264 addoc.addDependency(used);
267 // Creation of derived the document formats
268 // Document ndoc = addoc.value();
269 // Converter send = ApplicationSettings.getConverter(ndoc.getType(), ndoc.getFormat());
271 // if (send != null) send.converts(addoc); // Asynchronous process
273 mystudy.add(addoc); // Updates the presentation
275 } catch (FileNotFoundException error) {
276 logger.error("Reason:", error);
277 setErrorCode("import.file");
278 } catch (Exception error) {
279 logger.error("Reason:", error);
280 setErrorCode("internal");
285 // ==============================================================================================================================
286 // Getters and setters
287 // ==============================================================================================================================
289 public String getDocumentDate() {
290 // --------------------------------
294 public List<DocumentType> getDocumentTypes() {
295 // ---------------------------------------------
299 public long getDocumentType() {
300 // -----------------------------
304 public String getReference() {
305 // -----------------------------
309 public String getVersion() {
310 // ---------------------------
314 public void setDocumentDate(String date) {
315 // -----------------------------------------
319 public void setDocumentName(String name) {
320 // -----------------------------------------
321 this.docname = name; // Name entered by the user if enabled
324 public void setDocumentTitle(String name) { // Called even if DocumentName is enabled
325 // -----------------------------------------
326 if (this.docname == null)
330 public void setDocumentType(String value) {
331 // ------------------------------------------
332 this.doctype = Integer.valueOf(value);
335 public void setDefaultDocumentState(String state) { // Called even if DocumentState is enabled
336 // --------------------------------------------------
337 if (this.state == null)
338 this.state = ProgressState.valueOf(state);
341 public void setDefaultDocumentType(String value) { // Called even if DocumentType is enabled
342 // --------------------------------------------------
343 if (this.doctype == 0)
344 this.doctype = Integer.valueOf(value);
347 public void setReference(String value) {
348 // ---------------------------------------
352 public void setVersion(String value) {
353 // -------------------------------------
358 * Get project settings.
360 * @return Project settings service
362 private ProjectSettingsService getProjectSettings() {
363 return _projectSettingsService;
367 * Set project settings service.
369 * @param projectSettingsService
370 * project settings service
372 public void setProjectSettings(ProjectSettingsService projectSettingsService) {
373 _projectSettingsService = projectSettingsService;
377 * Get the publicationService.
379 * @return the publicationService
381 public PublicationService getPublicationService() {
382 return _publicationService;
386 * Set the publicationService.
388 * @param publicationService
389 * the publicationService to set
391 public void setPublicationService(PublicationService publicationService) {
392 _publicationService = publicationService;
396 * Get the stepService.
397 * @return the stepService
399 public StepService getStepService() {
404 * Set the stepService.
405 * @param stepService the stepService to set
407 public void setStepService(StepService stepService) {
408 _stepService = stepService;
412 * Get the documentService.
413 * @return the documentService
415 public DocumentService getDocumentService() {
416 return _documentService;
420 * Set the documentService.
421 * @param documentService the documentService to set
423 public void setDocumentService(DocumentService documentService) {
424 _documentService = documentService;
428 * Get the repositoryService.
429 * @return the repositoryService
431 public RepositoryService getRepositoryService() {
432 return _repositoryService;
436 * Set the repositoryService.
437 * @param repositoryService the repositoryService to set
439 public void setRepositoryService(RepositoryService repositoryService) {
440 _repositoryService = repositoryService;
444 * Get the documentTypeService.
445 * @return the documentTypeService
447 public DocumentTypeService getDocumentTypeService() {
448 return _documentTypeService;
452 * Set the documentTypeService.
453 * @param documentTypeService the documentTypeService to set
455 public void setDocumentTypeService(DocumentTypeService documentTypeService) {
456 _documentTypeService = documentTypeService;
460 * Get the menuProperty.
461 * @return the menuProperty
463 public String getMenuProperty() {
464 return _menuProperty;
468 * Set the menuProperty.
469 * @param menuProperty the menuProperty to set
471 public void setMenuProperty(String menuProperty) {
472 this._menuProperty = menuProperty;