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.dal.bo.som.ValidationCycle;
20 import org.splat.dal.bo.som.ValidationStep;
21 import org.splat.manox.Reader;
22 import org.splat.manox.Toolbox;
23 import org.splat.service.DocumentService;
24 import org.splat.service.DocumentTypeService;
25 import org.splat.som.Revision;
26 import org.splat.som.Step;
27 import org.splat.wapp.Constants;
30 * Action for adding a document into a study step.
32 public class ImportDocumentAction extends BaseUploadDocumentAction {
37 private static final long serialVersionUID = 2587822564883588556L;
40 * Sorted list of document types for the selected study step.
42 private transient List<DocumentType> _documentTypes = null;
44 * Sorted list of document types for the selected study step.
46 private transient final List<Boolean> _reviewable = new ArrayList<Boolean>();
48 * The selected document type.
50 private long _documentType = 0;
52 * Reference extracted from the imported file, if exist.
54 private String _reference = null;
56 * Injected document service.
58 private DocumentService _documentService;
60 * Injected document type service.
62 private DocumentTypeService _documentTypeService;
65 * Initialize the operation.
67 * @return SUCCESS in success, otherwise - ERROR
69 public String doInitialize() {
71 File upfile = commonInitialize(Constants.FALSE);
72 String[] table = _fileName.split("\\x2E");
73 String filext = table[table.length - 1].toLowerCase();
75 _mystudy = getOpenStudy();
76 Step step = _mystudy.getSelectedStep();
77 _documentTypes = getStepService().getValidDocumentTypes(step);
78 // Set the document type by default
79 _deftype = getApplicationSettings().getDefaultDocumentType(step, filext);
80 if (_deftype != null) {
81 setDefaultDocumentType(_deftype.getIndex());
83 _defuses = new ArrayList<Document>();
84 _state = ProgressState.inWORK;
87 if (extractProperties(upfile, filext)) {
88 if (_docname == null) {
90 for (int i = 1; i < table.length - 1; i++) {
91 _docname = _docname + "." + table[i];
94 if (_deftype != null) {
95 setupDefaultUses(_deftype);
98 DocumentType[] types = _documentTypes
99 .toArray(new DocumentType[_documentTypes.size()]);
100 DocumentTypeComparator compare = new DocumentTypeComparator();
101 Arrays.sort(types, compare);
102 _documentTypes = Arrays.asList(types);
106 // Initialize isReviewable: if a type has review state or not
107 ValidationCycle cycle;
109 for (DocumentType dtype : _documentTypes) {
110 cycle = getStudyService().getValidationCycleOf(
111 _mystudy.getMystudy(), dtype);
112 hasReview = (cycle != null) && cycle.enables(ValidationStep.REVIEW);
113 _reviewable.add(hasReview);
119 * Extract properties from the uploaded file.
125 * @return true if succeeded, false if error
127 private boolean extractProperties(final File upfile, final String filext) {
129 Reader tool = Toolbox.getReader(upfile);
131 if ("pdf".equals(filext)) {
132 _state = ProgressState.EXTERN; // TODO: Should external extensions be configurable ?
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 = _documentTypes.iterator(); i
140 DocumentType type = i.next();
141 if (type.getName().equals(filetype)) {
143 _documentType = type.getIndex(); // Disables the document type field
147 if (fileref != null) {
148 isOk = findTypeByDocRef(fileref);
151 if (filever != null) {
153 Revision.Format get = new Revision.Format(
154 getProjectSettings().getRevisionPattern());
155 Revision version = get.parse(filever);
156 if (version.isNull()) {
157 throw new ParseException(filever,
158 filever.length() - 1);
160 if (!version.isMinor()) {
161 _state = ProgressState.inCHECK;
163 setVersion(version.toString());
164 } catch (ParseException e) {
165 setErrorCode("message.error.format.version");
170 _docname = tool.extractProperty("title"); // Property kept even if the file is not referenced
171 isOk = extractDate(tool);
179 * Find document type by document reference.
182 * the document reference
183 * @return true if succeeded, false if error
185 private boolean findTypeByDocRef(final String fileref) {
187 Document slot = getDocumentService().selectDocument(fileref,
188 new Revision().toString());
190 setError("message.error.reference.undefined");
193 if (slot.isUndefined()) {
194 _reference = fileref; // Disables document name and state fields
195 _deftype = slot.getType(); // Just in case
196 _documentType = _deftype.getIndex(); // Disables the document type field
198 setError("message.error.reference.duplicate");
206 * Perform import of a document.
208 * @return SUCCESS if ok, "cancel" - if canceled, ERROR - if error
210 public String doImport() {
213 initializationScreenContext(Constants.STUDY_MENU, Constants.STUDY_MENU,
216 if (_action == ToDo.cancel) {
218 } else if (_documentType == 0) {
219 setErrorCode("message.error.import.type");
221 initializationFullScreenContext(Constants.STUDY_MENU,
222 Constants.STUDY_MENU, Constants.TRUE, Constants.NONE,
223 Constants.STUDY_MENU);
226 // Getting user inputs
227 _mystudy = getOpenStudy();
228 User user = getConnectedUser();
229 Step step = _mystudy.getSelectedStep();
231 if (getDocumentDate().length() > 0) {
232 ResourceBundle locale = ResourceBundle.getBundle("som",
233 getApplicationSettings().getCurrentLocale());
234 SimpleDateFormat get = new SimpleDateFormat(locale
235 .getString("date.format"), getApplicationSettings()
236 .getCurrentLocale());
237 aDate = get.parse(getDocumentDate());
239 // Creation of uses relations
240 List<Long> uses = new ArrayList<Long>();
241 if (_docuses != null) {
242 String[] list = _docuses.split(",");
243 for (int i = 0; i < list.length; i++) {
244 uses.add(Long.valueOf(list[i].trim()));
247 if (LOG.isDebugEnabled()) {
249 .debug("Document to be imported uses documents with following ids:");
250 for (Long usesId : uses) {
251 LOG.debug("#" + usesId);
254 Publication addoc = getPublicationService().createDoc(
255 _mystudy.getIndex(), step, _documentType,
256 user.getIndex(), _fileName, _docname, _state, _reference,
257 getVersion(), aDate, uses);
259 if (_reference.length() > 0) { // Importation of a not foreign document
260 _mystudy.updateSimulationContexts(); // In case of simulation contexts extracted from the imported document
263 // Creation of derived the document formats
264 // Document ndoc = addoc.value();
265 // Converter send = getApplicationSettings().getConverter(ndoc.getType(), ndoc.getFormat());
267 // if (send != null) send.converts(addoc); // Asynchronous process
269 _mystudy.add(addoc); // Updates the presentation
271 } catch (FileNotFoundException error) {
272 LOG.error("Reason:", error);
273 setErrorCode("message.error.import.file");
274 } catch (Exception error) {
275 LOG.error("Reason:", error);
276 setErrorCode("message.error.internal");
279 if (!SUCCESS.equals(res)) {
280 initializationFullScreenContext(Constants.STUDY_MENU,
281 Constants.STUDY_MENU, Constants.TRUE, Constants.NONE,
282 Constants.STUDY_MENU);
289 // ==============================================================================================================================
290 // Getters and setters
291 // ==============================================================================================================================
294 * Get sorted list of document types valid for the selected study step.
296 * @return sorted list of document types
298 public List<DocumentType> getDocumentTypes() {
299 return _documentTypes;
303 * Get document type id.
305 * @return document type id
307 public long getDocumentType() {
308 return _documentType;
312 * Get reference extracted from the imported file, if exist.
314 * @return the document reference
316 public String getReference() {
321 * Set document name entered by the user if enabled.
326 public void setDocumentName(final String name) {
327 this._docname = name; // Name entered by the user if enabled
331 * Set the default title if no title was defined.
334 * the default document title
336 public void setDocumentTitle(final String name) { // Called even if DocumentName is enabled
337 if (this._docname == null) {
338 this._docname = name;
343 * Set document type id.
348 public void setDocumentType(final Long value) {
349 this._documentType = value;
353 * Set the default state if no state was selected.
358 public void setDefaultDocumentState(final String state) { // Called even if DocumentState is enabled
359 if (this._state == null) {
360 this._state = ProgressState.valueOf(state);
365 * Set the default type if no type was selected.
368 * the default document type id
370 public void setDefaultDocumentType(final Long value) { // Called even if DocumentType is enabled
371 if (this._documentType == 0) {
372 this._documentType = value;
377 * Set document reference extracted from the imported file, if exist.
382 public void setReference(final String value) {
383 this._reference = value;
387 * Get the documentService.
389 * @return the documentService
391 public DocumentService getDocumentService() {
392 return _documentService;
396 * Set the documentService.
398 * @param documentService
399 * the documentService to set
401 public void setDocumentService(final DocumentService documentService) {
402 _documentService = documentService;
406 * Get the documentTypeService.
408 * @return the documentTypeService
410 public DocumentTypeService getDocumentTypeService() {
411 return _documentTypeService;
415 * Set the documentTypeService.
417 * @param documentTypeService
418 * the documentTypeService to set
420 public void setDocumentTypeService(
421 final DocumentTypeService documentTypeService) {
422 _documentTypeService = documentTypeService;
426 * Get sorted list of available document states.
428 * @return the documentStates
430 public List<ProgressState> getDocumentStates() {
431 List<ProgressState> states = new ArrayList<ProgressState>();
432 states.add(ProgressState.inWORK);
433 states.add(ProgressState.inDRAFT);
434 states.add(ProgressState.EXTERN);
439 * Get the isReviewable.
441 * @return the isReviewable
443 public List<Boolean> getReviewable() {