1 package org.splat.simer;
4 import java.io.FileNotFoundException;
5 import java.text.ParseException;
6 import java.text.SimpleDateFormat;
7 import java.util.ArrayList;
8 import java.util.Arrays;
10 import java.util.Iterator;
11 import java.util.List;
12 import java.util.ResourceBundle;
14 import org.splat.dal.bo.kernel.User;
15 import org.splat.dal.bo.som.Document;
16 import org.splat.dal.bo.som.DocumentType;
17 import org.splat.dal.bo.som.ProgressState;
18 import org.splat.dal.bo.som.Publication;
19 import org.splat.manox.Reader;
20 import org.splat.manox.Toolbox;
21 import org.splat.service.DocumentService;
22 import org.splat.service.DocumentTypeService;
23 import org.splat.service.PublicationService;
24 import org.splat.service.StepService;
25 import org.splat.service.technical.ProjectSettingsService;
26 import org.splat.service.technical.RepositoryService;
27 import org.splat.som.Revision;
28 import org.splat.som.Step;
29 import org.splat.wapp.Constants;
32 * Action for adding a document into a study step.
34 public class ImportDocumentAction extends UploadBaseNextAction {
39 private static final long serialVersionUID = 2587822564883588556L;
41 private transient List<DocumentType> _documentTypes = null;
42 private long _documentType = 0;
44 * Reference extracted from the imported file, if exist.
46 private String _reference = null;
48 * Version number extracted from the imported file, if exist.
50 private String _version = "";
52 * Date extracted from the imported file, if exist.
54 private String _documentDate = "";
56 * Injected project settings service.
58 private ProjectSettingsService _projectSettings;
60 * Injected publication service.
62 private PublicationService _publicationService;
64 * Injected step service.
66 private StepService _stepService;
68 * Injected document service.
70 private DocumentService _documentService;
72 * Injected document type service.
74 private DocumentTypeService _documentTypeService;
76 * Injected repository service.
78 private RepositoryService _repositoryService;
80 // ==============================================================================================================================
82 // ==============================================================================================================================
85 * Initialize the operation.
87 * @return SUCCESS in success, otherwise - ERROR
89 public String doInitialize() {
91 if (Constants.TRUE.equals(getWriteAccess())) {
92 setToolProperty(Constants.STUDY_MENU);
94 setToolProperty(Constants.NONE);
96 initializationFullScreenContext(Constants.STUDY_MENU,
97 Constants.STUDY_MENU, Constants.FALSE, getToolProperty(),
98 Constants.STUDY_MENU);
100 User user = getConnectedUser();
101 File updir = getRepositoryService().getDownloadDirectory(user);
102 File upfile = new File(updir.getPath() + "/" + filename);
103 String[] table = filename.split("\\x2E");
104 String filext = table[table.length - 1].toLowerCase();
106 mystudy = getOpenStudy();
107 Step step = mystudy.getSelectedStep();
108 _documentTypes = getStepService().getValidDocumentTypes(step);
109 // Set the document type by default
110 deftype = getApplicationSettings().getDefaultDocumentType(step, filext);
111 if (deftype != null) {
112 setDefaultDocumentType(Long.toString(deftype.getIndex()));
114 defuses = new ArrayList<Document>();
115 state = ProgressState.inWORK;
118 if (extractProperties(upfile, filext)) {
119 if (docname == null) {
121 for (int i = 1; i < table.length - 1; i++) {
122 docname = docname + "." + table[i];
125 if (deftype != null) {
126 setupDefaultUses(deftype);
129 DocumentType[] types = _documentTypes
130 .toArray(new DocumentType[_documentTypes.size()]);
131 DocumentTypeComparator compare = new DocumentTypeComparator();
132 Arrays.sort(types, compare);
133 _documentTypes = Arrays.asList(types);
140 * Extract properties from the uploaded file.
146 * @return true if succeeded, false if error
148 private boolean extractProperties(final File upfile, final String filext) {
150 Reader tool = Toolbox.getReader(upfile);
152 if ("pdf".equals(filext)) {
153 state = ProgressState.EXTERN; // TODO: Should external extensions be configurable ?
156 String fileref = tool.extractProperty("reference");
157 String filever = tool.extractProperty("version"); // Property kept even if the file is not referenced
158 String filetype = tool.extractProperty("type"); // Property kept even if the file is not referenced
159 for (Iterator<DocumentType> i = _documentTypes.iterator(); i
161 DocumentType type = i.next();
162 if (type.getName().equals(filetype)) {
164 _documentType = type.getIndex(); // Disables the document type field
168 if (fileref != null) {
169 isOk = findTypeByDocRef(fileref);
172 if (filever != null) {
174 Revision.Format get = new Revision.Format(
175 getProjectSettings().getRevisionPattern());
176 Revision version = get.parse(filever);
177 if (version.isNull()) {
178 throw new ParseException(filever,
179 filever.length() - 1);
181 if (!version.isMinor()) {
182 state = ProgressState.inCHECK;
184 _version = version.toString();
185 } catch (ParseException e) {
186 setError("message.error.format.version");
191 docname = tool.extractProperty("title"); // Property kept even if the file is not referenced
192 _documentDate = tool.extractProperty("date");
193 if (_documentDate == null) {
196 ResourceBundle locale = ResourceBundle.getBundle("som",
197 getApplicationSettings().getCurrentLocale());
198 SimpleDateFormat check = new SimpleDateFormat(locale
199 .getString("date.format"));
201 check.parse(_documentDate);
202 } catch (ParseException e) {
203 setError("message.error.format.date");
214 * Find document type by document reference.
217 * the document reference
218 * @return true if succeeded, false if error
220 private boolean findTypeByDocRef(final String fileref) {
222 Document slot = getDocumentService().selectDocument(fileref,
223 new Revision().toString());
225 setError("message.error.reference.undefined");
228 if (slot.isUndefined()) {
229 _reference = fileref; // Disables document name and state fields
230 deftype = slot.getType(); // Just in case
231 _documentType = deftype.getIndex(); // Disables the document type field
233 setError("message.error.reference.duplicate");
241 * Set error message and menus.
246 private void setError(final String errorCode) {
247 setErrorCode(errorCode);
249 initializationFullScreenContext(Constants.STUDY_MENU,
250 Constants.STUDY_MENU, Constants.FALSE, Constants.NONE,
251 Constants.STUDY_MENU);
255 * Perform import of a document.
257 * @return SUCCESS if ok, "cancel" - if canceled, ERROR - if error
259 public String doImport() {
261 initializationScreenContext(Constants.STUDY_MENU, Constants.STUDY_MENU,
264 if (action == ToDo.cancel) {
267 if (_documentType == 0) {
268 setErrorCode("message.error.import.type");
270 initializationFullScreenContext(Constants.STUDY_MENU,
271 Constants.STUDY_MENU, Constants.TRUE, Constants.NONE,
272 Constants.STUDY_MENU);
277 // Getting user inputs
278 mystudy = getOpenStudy();
279 User user = getConnectedUser();
280 Step step = mystudy.getSelectedStep();
282 if (_documentDate.length() > 0) {
283 ResourceBundle locale = ResourceBundle.getBundle("som",
284 getApplicationSettings().getCurrentLocale());
285 SimpleDateFormat get = new SimpleDateFormat(locale
286 .getString("date.format"));
287 docdate = get.parse(_documentDate);
289 // Creation of uses relations
290 List<Long> uses = new ArrayList<Long>();
291 if (docuses != null) {
292 String[] list = docuses.split(",");
293 for (int i = 0; i < list.length; i++) {
294 uses.add(Long.valueOf(list[i].trim()));
297 if (LOG.isDebugEnabled()) {
299 .debug("Document to be imported uses documents with following ids:");
300 for (Long usesId : uses) {
301 LOG.debug("#" + usesId);
304 Publication addoc = getPublicationService().createDoc(
305 mystudy.getIndex(), step, _documentType, user.getIndex(),
306 filename, docname, state, _reference, _version, docdate,
309 if (_reference.length() > 0) { // Importation of a not foreign document
310 mystudy.updateSimulationContexts(); // In case of simulation contexts extracted from the imported document
313 // Creation of derived the document formats
314 // Document ndoc = addoc.value();
315 // Converter send = getApplicationSettings().getConverter(ndoc.getType(), ndoc.getFormat());
317 // if (send != null) send.converts(addoc); // Asynchronous process
319 mystudy.add(addoc); // Updates the presentation
321 } catch (FileNotFoundException error) {
322 LOG.error("Reason:", error);
323 setErrorCode("message.error.import.file");
324 } catch (Exception error) {
325 LOG.error("Reason:", error);
326 setErrorCode("message.error.internal");
329 initializationFullScreenContext(Constants.STUDY_MENU,
330 Constants.STUDY_MENU, Constants.TRUE, Constants.NONE,
331 Constants.STUDY_MENU);
336 // ==============================================================================================================================
337 // Getters and setters
338 // ==============================================================================================================================
341 * Date extracted from the imported file, if exist.
343 * @return the file date
345 public String getDocumentDate() {
346 return _documentDate;
349 public List<DocumentType> getDocumentTypes() {
350 return _documentTypes;
354 * Get document type id.
356 * @return document type id
358 public long getDocumentType() {
359 return _documentType;
363 * Get reference extracted from the imported file, if exist.
365 * @return the document reference
367 public String getReference() {
372 * Get version number extracted from the imported file, if exist.
374 * @return the document version
376 public String getVersion() {
381 * Set date extracted from the imported file.
386 public void setDocumentDate(final String date) {
387 this._documentDate = date;
391 * Set document name entered by the user if enabled.
396 public void setDocumentName(final String name) {
397 this.docname = name; // Name entered by the user if enabled
400 public void setDocumentTitle(final String name) { // Called even if DocumentName is enabled
401 if (this.docname == null) {
407 * Set document type id.
412 public void setDocumentType(final String value) {
413 this._documentType = Integer.valueOf(value);
416 public void setDefaultDocumentState(final String state) { // Called even if DocumentState is enabled
417 if (this.state == null) {
418 this.state = ProgressState.valueOf(state);
422 public void setDefaultDocumentType(final String value) { // Called even if DocumentType is enabled
423 if (this._documentType == 0) {
424 this._documentType = Integer.valueOf(value);
429 * Set document reference extracted from the imported file, if exist.
434 public void setReference(final String value) {
435 this._reference = value;
439 * Set version number extracted from the imported file, if exist.
444 public void setVersion(final String value) {
445 this._version = value;
449 * Get project settings.
451 * @return Project settings service
453 private ProjectSettingsService getProjectSettings() {
454 return _projectSettings;
458 * Set project settings service.
460 * @param projectSettingsService
461 * project settings service
463 public void setProjectSettings(
464 final ProjectSettingsService projectSettingsService) {
465 _projectSettings = projectSettingsService;
469 * Get the publicationService.
471 * @return the publicationService
473 public PublicationService getPublicationService() {
474 return _publicationService;
478 * Set the publicationService.
480 * @param publicationService
481 * the publicationService to set
483 public void setPublicationService(
484 final PublicationService publicationService) {
485 _publicationService = publicationService;
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(final StepService stepService) {
504 _stepService = stepService;
508 * Get the documentService.
510 * @return the documentService
512 public DocumentService getDocumentService() {
513 return _documentService;
517 * Set the documentService.
519 * @param documentService
520 * the documentService to set
522 public void setDocumentService(final DocumentService documentService) {
523 _documentService = documentService;
527 * Get the repositoryService.
529 * @return the repositoryService
531 public RepositoryService getRepositoryService() {
532 return _repositoryService;
536 * Set the repositoryService.
538 * @param repositoryService
539 * the repositoryService to set
541 public void setRepositoryService(final RepositoryService repositoryService) {
542 _repositoryService = repositoryService;
546 * Get the documentTypeService.
548 * @return the documentTypeService
550 public DocumentTypeService getDocumentTypeService() {
551 return _documentTypeService;
555 * Set the documentTypeService.
557 * @param documentTypeService
558 * the documentTypeService to set
560 public void setDocumentTypeService(
561 final DocumentTypeService documentTypeService) {
562 _documentTypeService = documentTypeService;