Salome HOME
Fix:
[tools/siman.git] / Workspace / Siman / src / org / splat / simer / BaseUploadDocumentAction.java
1 package org.splat.simer;
2
3 import java.io.File;
4 import java.text.ParseException;
5 import java.text.SimpleDateFormat;
6 import java.util.ArrayList;
7 import java.util.Iterator;
8 import java.util.List;
9 import java.util.ResourceBundle;
10 import java.util.Set;
11
12 import org.splat.dal.bo.kernel.User;
13 import org.splat.dal.bo.som.Document;
14 import org.splat.dal.bo.som.DocumentType;
15 import org.splat.dal.bo.som.ProgressState;
16 import org.splat.dal.bo.som.Publication;
17 import org.splat.manox.Reader;
18 import org.splat.service.PublicationService;
19 import org.splat.service.StepService;
20 import org.splat.service.StudyService;
21 import org.splat.service.technical.ProjectSettingsService;
22 import org.splat.som.Step;
23 import org.splat.wapp.Constants;
24
25 /**
26  * Base action for adding a document into a study step.
27  */
28 public class BaseUploadDocumentAction extends AbstractUploadNextAction {
29
30         /**
31          * Serial version ID.
32          */
33         private static final long serialVersionUID = 7920982168638437486L;
34         /**
35          * Version number extracted from the imported file, if exist.
36          */
37         private String _version = "";
38         /**
39          * Date extracted from the imported file, if exist.
40          */
41         private String _documentDate = "";
42         /**
43          * Injected project settings service.
44          */
45         private ProjectSettingsService _projectSettings;
46         /**
47          * Injected publication service.
48          */
49         private PublicationService _publicationService;
50         /**
51          * Injected step service.
52          */
53         private StepService _stepService;
54         /**
55          * Injected study service.
56          */
57         private StudyService _studyService;
58         protected DocumentType _deftype = null;
59         protected List<Document> _defuses = null;
60         protected String _docname = null;
61         protected String _docuses = null;
62         protected OpenStudy _mystudy = null;
63         protected ProgressState _state = null;
64
65         /**
66          * Common initialization of the action form.
67          * 
68          * @param editDisabled
69          *            if the study editable or not
70          * @return downloaded file
71          */
72         protected File commonInitialize(final String editDisabled) {
73
74                 if (Constants.TRUE.equals(getWriteAccess())) {
75                         setToolProperty(Constants.STUDY_MENU);
76                 } else {
77                         setToolProperty(Constants.NONE);
78                 }
79                 initializationFullScreenContext(Constants.STUDY_MENU,
80                                 Constants.STUDY_MENU, editDisabled, getToolProperty(),
81                                 Constants.STUDY_MENU);
82
83                 User user = getConnectedUser();
84                 File updir = getRepositoryService().getDownloadDirectory(user);
85                 File upfile = new File(updir.getPath() + "/" + _fileName);
86
87                 _mystudy = getOpenStudy();
88                 _defuses = new ArrayList<Document>();
89                 return upfile;
90         }
91
92         /**
93          * Extract document date.
94          * 
95          * @param tool
96          *            file reader
97          * @return true if ok, false if error
98          */
99         protected boolean extractDate(final Reader tool) {
100                 boolean isOk = true;
101                 setDocumentDate(tool.extractProperty("date"));
102                 if (getDocumentDate() == null) {
103                         setDocumentDate("");
104                 } else {
105                         ResourceBundle locale = ResourceBundle.getBundle("som",
106                                         getApplicationSettings().getCurrentLocale());
107                         SimpleDateFormat check = new SimpleDateFormat(locale
108                                         .getString("date.format"), getApplicationSettings()
109                                         .getCurrentLocale());
110                         try {
111                                 check.parse(getDocumentDate());
112                         } catch (ParseException e) {
113                                 setErrorCode("message.error.format.date");
114                                 isOk = false;
115                         }
116                 }
117                 return isOk;
118         }
119
120         /**
121          * Set error message and menus.
122          * 
123          * @param errorCode
124          *            error message key
125          */
126         protected void setError(final String errorCode) {
127                 setErrorCode(errorCode);
128
129                 initializationFullScreenContext(Constants.STUDY_MENU,
130                                 Constants.STUDY_MENU, Constants.FALSE, Constants.NONE,
131                                 Constants.STUDY_MENU);
132         }
133
134         /**
135          * Date extracted from the imported file, if exist.
136          * 
137          * @return the file date
138          */
139         public String getDocumentDate() {
140                 return _documentDate;
141         }
142
143         /**
144          * Get version number extracted from the imported file, if exist.
145          * 
146          * @return the document version
147          */
148         public String getVersion() {
149                 return _version;
150         }
151
152         /**
153          * Set date extracted from the imported file.
154          * 
155          * @param date
156          *            the date to set
157          */
158         public void setDocumentDate(final String date) {
159                 this._documentDate = date;
160         }
161
162         /**
163          * Set version number extracted from the imported file, if exist.
164          * 
165          * @param value
166          *            the version
167          */
168         public void setVersion(final String value) {
169                 this._version = value;
170         }
171
172         /**
173          * Get project settings.
174          * 
175          * @return Project settings service
176          */
177         protected ProjectSettingsService getProjectSettings() {
178                 return _projectSettings;
179         }
180
181         /**
182          * Set project settings service.
183          * 
184          * @param projectSettingsService
185          *            project settings service
186          */
187         public void setProjectSettings(
188                         final ProjectSettingsService projectSettingsService) {
189                 _projectSettings = projectSettingsService;
190         }
191
192         /**
193          * Get the publicationService.
194          * 
195          * @return the publicationService
196          */
197         public PublicationService getPublicationService() {
198                 return _publicationService;
199         }
200
201         /**
202          * Set the publicationService.
203          * 
204          * @param publicationService
205          *            the publicationService to set
206          */
207         public void setPublicationService(
208                         final PublicationService publicationService) {
209                 _publicationService = publicationService;
210         }
211
212         /**
213          * Get the stepService.
214          * 
215          * @return the stepService
216          */
217         public StepService getStepService() {
218                 return _stepService;
219         }
220
221         /**
222          * Set the stepService.
223          * 
224          * @param stepService
225          *            the stepService to set
226          */
227         public void setStepService(final StepService stepService) {
228                 _stepService = stepService;
229         }
230
231         /**
232          * Get the studyService.
233          * 
234          * @return the studyService
235          */
236         public StudyService getStudyService() {
237                 return _studyService;
238         }
239
240         /**
241          * Set the studyService.
242          * 
243          * @param studyService
244          *            the studyService to set
245          */
246         public void setStudyService(final StudyService studyService) {
247                 _studyService = studyService;
248         }
249
250         // ==============================================================================================================================
251         // Getters and setters
252         // ==============================================================================================================================
253         
254         public DocumentType getDefaultDocumentType() {
255                 return _deftype;
256         }
257
258         public List<Document> getDefaultDocumentUses() {
259                 return _defuses;
260         }
261
262         public String getDocumentName() {
263                 return _docname;
264         }
265
266         public String getDocumentState() {
267                 return _state.toString();
268         }
269
270         protected Publication getPublication(final int index) {
271                 List<Step> steps = _mystudy.getInvolvedSteps();
272                 Publication found = null, pub;
273                 for (Iterator<Step> i = steps.iterator(); i.hasNext();) {
274                         List<Publication> published = i.next().getAllDocuments();
275                         for (Iterator<Publication> j = published.iterator(); j.hasNext();) {
276                                 pub = j.next(); // In a given study step,
277                                 if (pub.value().getIndex() == index) {
278                                         found = pub; // there is only one publication of a given document
279                                         break;
280                                 }
281                         }
282                 }
283                 return found;
284         }
285
286         public void setDocumentState(final String state) {
287                 this._state = ProgressState.valueOf(state);
288         }
289
290         // ==============================================================================================================================
291         // Protected services
292         // ==============================================================================================================================
293         
294         protected void setupDefaultUses(final DocumentType type) {
295                 Set<DocumentType> uses = type.getDefaultUses();
296         
297                 for (Iterator<DocumentType> i = uses.iterator(); i.hasNext();) {
298                         DocumentType usetype = i.next();
299                         List<Document> usedoc = _mystudy.collectInvolvedDocuments(usetype);
300                         if (usedoc.isEmpty() && (!usetype.equals(type))) {
301                                 setupDefaultUses(usetype);
302                         } else {
303                                 _defuses.addAll(usedoc);
304                         }
305                 }
306         }
307
308         public void setUses(final String list) {
309                 this._docuses = list;
310         }
311         
312         public String getWriteAccess() {
313                 Boolean res = (_mystudy != null);
314                 if (res) {
315                         res = _mystudy.isOpenForWriting();
316                 }
317                 return res.toString();
318         }
319 }