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 // ==============================================================================================================================
71 // ==============================================================================================================================
74 * Initialize the operation.
75 * @return SUCCESS in success, otherwise - ERROR
77 public String doInitialize() {
78 User user = getConnectedUser();
79 File updir = getRepositoryService().getDownloadDirectory(user);
80 File upfile = new File(updir.getPath() + "/" + filename);
81 String[] table = filename.split("\\x2E");
82 String filext = table[table.length - 1].toLowerCase();
84 mystudy = getOpenStudy();
85 Step step = mystudy.getSelectedStep();
86 doctypes = getStepService().getValidDocumentTypes(step);
87 deftype = ApplicationSettings.getDefaultDocumentType(step, filext);
88 defuses = new Vector<Document>();
89 state = ProgressState.inWORK;
91 Reader tool = Toolbox.getReader(upfile);
93 String fileref = tool.extractProperty("reference");
94 String filever = tool.extractProperty("version"); // Property kept even if the file is not referenced
95 String filetype = tool.extractProperty("type"); // Property kept even if the file is not referenced
96 for (Iterator<DocumentType> i = doctypes.iterator(); i.hasNext();) {
97 DocumentType type = i.next();
98 if (!type.getName().equals(filetype))
101 doctype = type.getIndex(); // Disables the document type field
104 if (fileref != null) {
105 Document slot = getDocumentService().selectDocument(fileref,
106 new Revision().toString());
108 setErrorCode("reference.undefined");
111 if (!slot.isUndefined()) {
112 setErrorCode("reference.duplicate");
115 docref = fileref; // Disables document name and state fields
116 deftype = slot.getType(); // Just in case
117 doctype = deftype.getIndex(); // Disables the document type field
122 Revision.Format get = new Revision.Format(
123 getProjectSettings().getRevisionPattern());
124 Revision version = get.parse(filever);
125 if (version.isNull())
126 throw new ParseException(filever, filever.length() - 1);
127 if (!version.isMinor())
128 state = ProgressState.inCHECK;
129 docver = version.toString();
130 } catch (ParseException e) {
131 setErrorCode("format.version");
134 docname = tool.extractProperty("title"); // Property kept even if the file is not referenced
135 date = tool.extractProperty("date");
137 ResourceBundle locale = ResourceBundle.getBundle("som",
138 ApplicationSettings.getCurrentLocale());
139 SimpleDateFormat check = new SimpleDateFormat(
140 locale.getString("date.format"));
143 } catch (ParseException e) {
144 setErrorCode("format.date");
149 } else if (filext.equals("pdf"))
150 state = ProgressState.EXTERN; // TODO: Should external extensions be configurable ?
151 if (docname == null) {
153 for (int i = 1; i < table.length - 1; i++)
154 docname = docname + "." + table[i];
157 setupDefaultUses(deftype);
159 DocumentType[] types = doctypes.toArray(new DocumentType[doctypes
161 DocumentTypeComparator compare = new DocumentTypeComparator();
162 Arrays.sort(types, compare);
163 doctypes = Arrays.asList(types);
169 * Perform import of a document.
170 * @return SUCCESS if ok, "cancel" - if canceled, ERROR - if error
172 public String doImport() {
173 // -------------------------
174 if (action == ToDo.cancel)
177 setErrorCode("import.type");
181 // Getting user inputs
182 mystudy = getOpenStudy();
183 User user = getConnectedUser();
184 Step step = mystudy.getSelectedStep();
185 DocumentType type = getDocumentTypeService().selectType((int)doctype);
186 File updir = getRepositoryService().getDownloadDirectory(user);
187 File upfile = new File(updir.getPath() + "/" + filename);
188 String[] table = filename.split("\\x2E");
190 // Creation of the document
191 Document.Properties dprop = new Document.Properties();
194 if (docref.length() == 0) { // Importation of a foreign document
195 // TODO: Extract property of supported documents (DOCX, ODT...)
196 addoc = getStepService().createDocument(step, dprop.setName(docname)
197 .setType(type).setFormat(table[table.length - 1])
199 updir = addoc.getSourceFile().asFile();
200 if (logger.isInfoEnabled())
201 logger.info("Moving \"" + upfile.getName() + "\" to \""
202 + updir.getPath() + "\".");
203 upfile.renameTo(updir);
205 getPublicationService().saveAs(addoc, state); // May throw FileNotFound if rename was not done
206 } catch (FileNotFoundException saverror) {
208 logger.info("Waiting for the file.");
209 upfile.renameTo(updir);
210 getPublicationService().saveAs(addoc, state); // Forget it if throw again FileNotFound
212 } else { // Importation of a previously created template-based document
213 if (date.length() > 0) {
214 ResourceBundle locale = ResourceBundle.getBundle("som",
215 ApplicationSettings.getCurrentLocale());
216 SimpleDateFormat get = new SimpleDateFormat(
217 locale.getString("date.format"));
218 dprop.setDate(get.parse(date));
220 addoc = getStepService().assignDocument(step, dprop.setReference(docref).setName(
222 updir = addoc.getSourceFile().asFile();
223 if (logger.isInfoEnabled())
224 logger.info("Moving \"" + upfile.getName() + "\" to \""
225 + updir.getPath() + "\".");
226 upfile.renameTo(updir);
228 if (docver.length() > 0)
229 getPublicationService().saveAs(addoc,
230 new Revision(docver));
232 getPublicationService().saveAs(addoc, state);
233 } catch (FileNotFoundException saverror) {
235 logger.info("Waiting for the file.");
236 upfile.renameTo(updir);
237 if (docver.length() > 0)
238 getPublicationService().saveAs(addoc,
239 new Revision(docver));
241 getPublicationService().saveAs(addoc, state);
243 mystudy.updateSimulationContexts(); // In case of simulation contexts extracted from the imported document
245 // Creation of uses relations
246 if (docuses != null) {
247 String[] list = docuses.split(",");
248 for (int i = 0; i < list.length; i++) {
249 Integer index = Integer.valueOf(list[i].trim());
250 Publication used = getPublication(index);
251 addoc.addDependency(used);
254 // Creation of derived the document formats
255 // Document ndoc = addoc.value();
256 // Converter send = ApplicationSettings.getConverter(ndoc.getType(), ndoc.getFormat());
258 // if (send != null) send.converts(addoc); // Asynchronous process
260 mystudy.add(addoc); // Updates the presentation
262 } catch (FileNotFoundException error) {
263 logger.error("Reason:", error);
264 setErrorCode("import.file");
265 } catch (Exception error) {
266 logger.error("Reason:", error);
267 setErrorCode("internal");
272 // ==============================================================================================================================
273 // Getters and setters
274 // ==============================================================================================================================
276 public String getDocumentDate() {
277 // --------------------------------
281 public List<DocumentType> getDocumentTypes() {
282 // ---------------------------------------------
286 public long getDocumentType() {
287 // -----------------------------
291 public String getReference() {
292 // -----------------------------
296 public String getVersion() {
297 // ---------------------------
301 public void setDocumentDate(String date) {
302 // -----------------------------------------
306 public void setDocumentName(String name) {
307 // -----------------------------------------
308 this.docname = name; // Name entered by the user if enabled
311 public void setDocumentTitle(String name) { // Called even if DocumentName is enabled
312 // -----------------------------------------
313 if (this.docname == null)
317 public void setDocumentType(String value) {
318 // ------------------------------------------
319 this.doctype = Integer.valueOf(value);
322 public void setDefaultDocumentState(String state) { // Called even if DocumentState is enabled
323 // --------------------------------------------------
324 if (this.state == null)
325 this.state = ProgressState.valueOf(state);
328 public void setDefaultDocumentType(String value) { // Called even if DocumentType is enabled
329 // --------------------------------------------------
330 if (this.doctype == 0)
331 this.doctype = Integer.valueOf(value);
334 public void setReference(String value) {
335 // ---------------------------------------
339 public void setVersion(String value) {
340 // -------------------------------------
345 * Get project settings.
347 * @return Project settings service
349 private ProjectSettingsService getProjectSettings() {
350 return _projectSettingsService;
354 * Set project settings service.
356 * @param projectSettingsService
357 * project settings service
359 public void setProjectSettings(ProjectSettingsService projectSettingsService) {
360 _projectSettingsService = projectSettingsService;
364 * Get the publicationService.
366 * @return the publicationService
368 public PublicationService getPublicationService() {
369 return _publicationService;
373 * Set the publicationService.
375 * @param publicationService
376 * the publicationService to set
378 public void setPublicationService(PublicationService publicationService) {
379 _publicationService = publicationService;
383 * Get the stepService.
384 * @return the stepService
386 public StepService getStepService() {
391 * Set the stepService.
392 * @param stepService the stepService to set
394 public void setStepService(StepService stepService) {
395 _stepService = stepService;
399 * Get the documentService.
400 * @return the documentService
402 public DocumentService getDocumentService() {
403 return _documentService;
407 * Set the documentService.
408 * @param documentService the documentService to set
410 public void setDocumentService(DocumentService documentService) {
411 _documentService = documentService;
415 * Get the repositoryService.
416 * @return the repositoryService
418 public RepositoryService getRepositoryService() {
419 return _repositoryService;
423 * Set the repositoryService.
424 * @param repositoryService the repositoryService to set
426 public void setRepositoryService(RepositoryService repositoryService) {
427 _repositoryService = repositoryService;
431 * Get the documentTypeService.
432 * @return the documentTypeService
434 public DocumentTypeService getDocumentTypeService() {
435 return _documentTypeService;
439 * Set the documentTypeService.
440 * @param documentTypeService the documentTypeService to set
442 public void setDocumentTypeService(DocumentTypeService documentTypeService) {
443 _documentTypeService = documentTypeService;