Salome HOME
Actions menu properties are refactored, unnecessary code is removed.
[tools/siman.git] / Workspace / Siman / src / org / splat / simer / VersionDocumentAction.java
1 package org.splat.simer;
2
3 import java.io.File;
4 import java.io.FileNotFoundException;
5 import java.text.ParseException;
6 import java.text.SimpleDateFormat;
7 import java.util.ArrayList;
8 import java.util.Date;
9 import java.util.Iterator;
10 import java.util.List;
11 import java.util.ResourceBundle;
12
13 import org.splat.dal.bo.kernel.Relation;
14 import org.splat.dal.bo.kernel.User;
15 import org.splat.dal.bo.som.Document;
16 import org.splat.dal.bo.som.ProgressState;
17 import org.splat.dal.bo.som.Publication;
18 import org.splat.dal.bo.som.UsedByRelation;
19 import org.splat.dal.bo.som.UsesRelation;
20 import org.splat.kernel.InvalidPropertyException;
21 import org.splat.manox.Reader;
22 import org.splat.manox.Toolbox;
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;
30
31 /**
32  * Action for creating a new version of a document.
33  */
34 public class VersionDocumentAction extends UploadBaseNextAction {
35
36         /**
37          * Serial version ID.
38          */
39         private static final long serialVersionUID = -5702264003232132168L;
40
41         /**
42          * Versioned document index.
43          */
44         private String _index = null;
45         /**
46          * List of publications which use the selected document.
47          */
48         private transient List<Publication> _usedby = null;
49         /**
50          * List of selected impacted documents ids.
51          */
52         private transient long[] _docusedby = null;
53         /**
54          * Summary of changes in the new version.
55          */
56         private String _description = null;
57         /**
58          * Version number extracted from the imported file, if exist.
59          */
60         private String _version = "";
61         /**
62          * Date extracted from the imported file, if exist.
63          */
64         private String _date = "";
65         /**
66          * Injected project settings service.
67          */
68         private ProjectSettingsService _projectSettings;
69         /**
70          * Injected publication service.
71          */
72         private PublicationService _publicationService;
73         /**
74          * Injected step service.
75          */
76         private StepService _stepService;
77         /**
78          * Injected repository service.
79          */
80         private RepositoryService _repositoryService;
81
82         // ==============================================================================================================================
83         // Action methods
84         // ==============================================================================================================================
85
86         /**
87          * Initialize the action form.
88          * 
89          * @return SUCCESS if succeeded, ERROR if uploaded file is XML and we can't extract properties from it
90          */
91         public String doInitialize() {
92
93                 if ("true".equals(getWriteAccess())) {
94                         setToolProperty(Constants.STUDY_MENU);
95                 } else {
96                         setToolProperty(Constants.NONE);
97                 }
98                 initializationFullScreenContext(Constants.STUDY_MENU,
99                                 Constants.STUDY_MENU, Constants.TRUE, getToolProperty(),
100                                 Constants.STUDY_MENU);
101
102                 User user = getConnectedUser();
103                 File updir = getRepositoryService().getDownloadDirectory(user);
104                 File upfile = new File(updir.getPath() + "/" + filename);
105
106                 mystudy = getOpenStudy();
107
108                 Publication tag = mystudy.getSelectedStep().getDocument(
109                                 Integer.valueOf(_index));
110                 Document doc = tag.value();
111                 deftype = doc.getType();
112                 docname = doc.getTitle();
113                 defuses = new ArrayList<Document>();
114                 _usedby = new ArrayList<Publication>();
115
116                 String res = SUCCESS;
117                 if (extractProperties(upfile, doc)) {
118                         setupDefaultUses(deftype);
119                         // Add additional documents used by the current version
120                         List<Relation> uses = doc.getRelations(UsesRelation.class);
121                         for (Iterator<Relation> i = uses.iterator(); i.hasNext();) {
122                                 Document used = (Document) i.next().getTo();
123                                 if (!defuses.contains(used)) {
124                                         defuses.add(used);
125                                 }
126                         }
127                         // Setup dependencies
128                         _usedby.addAll(tag.getRelations(UsedByRelation.class));
129                 } else {
130                         if (!(Constants.NONE.equals(getToolProperty()))) {
131                                 initializationFullScreenContext(Constants.STUDY_MENU,
132                                                 Constants.STUDY_MENU, Constants.TRUE, Constants.NONE,
133                                                 Constants.STUDY_MENU);
134                         }
135                         res = ERROR;
136                 }
137                 return res;
138         }
139
140         /**
141          * Try to extract properties from the uploaded file if it is XML.
142          * 
143          * @param upfile
144          *            the file to parse
145          * @param doc
146          *            the document to version
147          * @return true if succeeded or if the file is not XML, otherwise return false
148          */
149         private boolean extractProperties(final File upfile, final Document doc) {
150                 boolean res = true;
151                 Reader tool = Toolbox.getReader(upfile);
152                 if (tool != null) {
153                         String fileref = tool.extractProperty("reference");
154                         String filever = tool.extractProperty("version");
155                         if (fileref == null || doc.getReference().equals(fileref)) {
156                                 if (filever != null) {
157                                         try {
158                                                 Revision.Format get = new Revision.Format(
159                                                                 getProjectSettings().getRevisionPattern());
160                                                 Revision newver = get.parse(filever);
161                                                 Revision oldver = new Revision(doc.getVersion());
162                                                 if (!newver.isGraterThan(oldver)) {
163                                                         throw new InvalidPropertyException("version");
164                                                 }
165                                                 if (newver.isMinor()) {
166                                                         state = ProgressState.inWORK;
167                                                 } else {
168                                                         state = ProgressState.inDRAFT;
169                                                 }
170                                                 _version = newver.toString();
171                                         } catch (Exception e) {
172                                                 setErrorCode("message.error.version.mismatch");
173                                                 res = false;
174                                         }
175                                 }
176                                 if (res) {
177                                         _description = tool.extractProperty("history");
178                                         _date = tool.extractProperty("date");
179                                         if (_date == null) {
180                                                 _date = "";
181                                         } else {
182                                                 ResourceBundle locale = ResourceBundle.getBundle("som",
183                                                                 getApplicationSettings().getCurrentLocale());
184                                                 SimpleDateFormat check = new SimpleDateFormat(locale
185                                                                 .getString("date.format"));
186                                                 try {
187                                                         check.parse(_date);
188                                                 } catch (ParseException e) {
189                                                         setErrorCode("message.error.format.date");
190                                                         res = false;
191                                                 }
192                                         }
193                                 }
194                         } else {
195                                 setErrorCode("message.error.reference.mismatch");
196                                 res = false;
197                         }
198                 }
199                 return res;
200         }
201
202         /**
203          * Create a new version of the selected document.
204          * 
205          * @return SUCCESS - if succeeded, "cancel" - if canceled, ERROR - if failed
206          */
207         public String doVersion() {
208                 String res = ERROR;
209                 initializationScreenContext(Constants.STUDY_MENU, Constants.STUDY_MENU,
210                                 Constants.TRUE);
211
212                 if (action == ToDo.cancel) {
213                         res = "cancel";
214                 } else {
215
216                         try {
217                                 // Getting user inputs
218                                 mystudy = getOpenStudy();
219                                 User user = getConnectedUser();
220                                 Step step = mystudy.getSelectedStep();
221                                 Date aDate = null;
222                                 if (_date.length() > 0) {
223                                         ResourceBundle locale = ResourceBundle.getBundle("som",
224                                                         getApplicationSettings().getCurrentLocale());
225                                         SimpleDateFormat get = new SimpleDateFormat(locale
226                                                         .getString("date.format"));
227                                         aDate = get.parse(_date);
228                                 }
229
230                                 String[] listDocuses = null;
231                                 if (docuses != null) {
232                                         listDocuses = docuses.split(",");
233                                 }
234                                 getPublicationService().versionDocument(step, user, filename,
235                                                 Integer.valueOf(_index), _version, _description, state,
236                                                 aDate, listDocuses, _docusedby);
237
238                                 // Update of the open study
239                                 mystudy.setSelection(mystudy.getSelection()); // Rebuilds the presentation
240                                 // TODO: Look is an optimization is possible (for example by updating the presentation of versioned document)
241
242                                 res = SUCCESS;
243                         } catch (FileNotFoundException error) {
244                                 LOG.error("Reason:", error);
245                                 setErrorCode("message.error.import.file");
246                         } catch (Exception error) {
247                                 LOG.error("Reason:", error);
248                                 setErrorCode("message.error.internal");
249                         }
250                         if (!SUCCESS.equals(res)) {
251                                 initializationFullScreenContext(Constants.STUDY_MENU,
252                                                 Constants.STUDY_MENU, Constants.TRUE, Constants.NONE,
253                                                 Constants.STUDY_MENU);
254                         }
255                 }
256                 return res;
257         }
258
259         // ==============================================================================================================================
260         // Getters and setters
261         // ==============================================================================================================================
262
263         public String getDate() {
264                 return _date;
265         }
266
267         public List<Publication> getDependencies() {
268                 return _usedby;
269         }
270
271         public String getDescription() {
272                 return _description;
273         }
274
275         public String getIndex() {
276                 return _index;
277         }
278
279         public String getVersion() {
280                 return _version;
281         }
282
283         public void setDate(final String date) {
284                 this._date = date;
285         }
286
287         public void setDefaultDescription(final String summary) {
288                 if (this._description == null) {
289                         this._description = summary;
290                 }
291         }
292
293         public void setDescription(final String summary) {
294                 this._description = summary;
295         }
296
297         public void setIndex(final String index) {
298                 this._index = index;
299         }
300
301         public void setUsedBy(final long[] list) {
302                 this._docusedby = list;
303         }
304
305         public void setVersion(final String value) {
306                 this._version = value;
307         }
308
309         /**
310          * Get project settings.
311          * 
312          * @return Project settings service
313          */
314         private ProjectSettingsService getProjectSettings() {
315                 return _projectSettings;
316         }
317
318         /**
319          * Set project settings service.
320          * 
321          * @param projectSettingsService
322          *            project settings service
323          */
324         public void setProjectSettings(
325                         final ProjectSettingsService projectSettingsService) {
326                 _projectSettings = projectSettingsService;
327         }
328
329         /**
330          * Get the publicationService.
331          * 
332          * @return the publicationService
333          */
334         public PublicationService getPublicationService() {
335                 return _publicationService;
336         }
337
338         /**
339          * Set the publicationService.
340          * 
341          * @param publicationService
342          *            the publicationService to set
343          */
344         public void setPublicationService(
345                         final PublicationService publicationService) {
346                 _publicationService = publicationService;
347         }
348
349         /**
350          * Get the stepService.
351          * 
352          * @return the stepService
353          */
354         public StepService getStepService() {
355                 return _stepService;
356         }
357
358         /**
359          * Set the stepService.
360          * 
361          * @param stepService
362          *            the stepService to set
363          */
364         public void setStepService(final StepService stepService) {
365                 _stepService = stepService;
366         }
367
368         /**
369          * Get the repositoryService.
370          * 
371          * @return the repositoryService
372          */
373         public RepositoryService getRepositoryService() {
374                 return _repositoryService;
375         }
376
377         /**
378          * Set the repositoryService.
379          * 
380          * @param repositoryService
381          *            the repositoryService to set
382          */
383         public void setRepositoryService(final RepositoryService repositoryService) {
384                 _repositoryService = repositoryService;
385         }
386 }