Salome HOME
Fix:
[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.SimpleDateFormat;
6 import java.util.ArrayList;
7 import java.util.Date;
8 import java.util.List;
9 import java.util.ResourceBundle;
10
11 import org.splat.dal.bo.kernel.Relation;
12 import org.splat.dal.bo.kernel.User;
13 import org.splat.dal.bo.som.Document;
14 import org.splat.dal.bo.som.ProgressState;
15 import org.splat.dal.bo.som.Publication;
16 import org.splat.dal.bo.som.UsedByRelation;
17 import org.splat.dal.bo.som.UsesRelation;
18 import org.splat.dal.bo.som.ValidationCycle;
19 import org.splat.dal.bo.som.ValidationStep;
20 import org.splat.kernel.InvalidPropertyException;
21 import org.splat.manox.Reader;
22 import org.splat.manox.Toolbox;
23 import org.splat.som.Revision;
24 import org.splat.som.Step;
25 import org.splat.wapp.Constants;
26
27 /**
28  * Action for creating a new version of a document.
29  */
30 public class VersionDocumentAction extends BaseUploadDocumentAction {
31
32         /**
33          * Serial version ID.
34          */
35         private static final long serialVersionUID = -5702264003232132168L;
36
37         /**
38          * Versioned document index.
39          */
40         private String _index = null;
41         /**
42          * List of publications which use the selected document.
43          */
44         private transient List<Publication> _usedby = null;
45         /**
46          * List of selected impacted documents ids.
47          */
48         private transient long[] _docusedby = null;
49         /**
50          * Summary of changes in the new version.
51          */
52         private String _description = null;
53         /**
54          * Applicable document states.
55          */
56         private transient final List<ProgressState> _documentStates = new ArrayList<ProgressState>();
57
58         /**
59          * Initialize the action form.
60          * 
61          * @return SUCCESS if succeeded, ERROR if uploaded file is XML and we can't extract properties from it
62          */
63         public String doInitialize() {
64                 File upfile = commonInitialize(Constants.TRUE);
65
66                 _mystudy = getOpenStudy();
67                 _defuses = new ArrayList<Document>();
68
69                 Publication tag = _mystudy.getSelectedStep().getDocument(
70                                 Integer.valueOf(_index));
71                 Document doc = tag.value();
72                 _deftype = doc.getType();
73                 _docname = doc.getTitle();
74                 _usedby = new ArrayList<Publication>();
75
76                 String res = SUCCESS;
77                 if (extractProperties(upfile, doc)) {
78                         if (_deftype != null) {
79                                 setupDefaultUses(_deftype);
80                         }
81                         // Add additional documents used by the current version
82                         for (Relation usesRel: doc.getRelations(UsesRelation.class)) {
83                                 Document used = (Document) usesRel.getTo();
84                                 if (!_defuses.contains(used)) {
85                                         _defuses.add(used);
86                                 }
87                         }
88                         // Avoid recursive using of the document
89                         if (_defuses.contains(doc)) {
90                                 _defuses.remove(doc);
91                         }
92                         // Setup dependencies
93                         _usedby.addAll(tag.getRelations(UsedByRelation.class));
94
95                         // Initialize applicable states list
96                         if(tag.value().getProgressState() == ProgressState.EXTERN) {
97                                 _documentStates.add(ProgressState.EXTERN);
98                         } else {
99                         _documentStates.add(ProgressState.inWORK);
100                         if (_deftype != null) {
101                                 // Check if the validation cycle of the document type can has a review state
102                                 ValidationCycle cycle = getStudyService().getValidationCycleOf(
103                                                 _mystudy.getMystudy(), _deftype);
104                                 if ((cycle != null) && cycle.enables(ValidationStep.REVIEW)) {
105                                         _documentStates.add(ProgressState.inDRAFT);
106                                 }
107                         }
108                         }
109                 } else {
110                         if (!(Constants.NONE.equals(getToolProperty()))) {
111                                 initializationFullScreenContext(Constants.STUDY_MENU,
112                                                 Constants.STUDY_MENU, Constants.TRUE, Constants.NONE,
113                                                 Constants.STUDY_MENU);
114                         }
115                         res = ERROR;
116                 }
117                 return res;
118         }
119
120         /**
121          * Try to extract properties from the uploaded file if it is XML.
122          * 
123          * @param upfile
124          *            the file to parse
125          * @param doc
126          *            the document to version
127          * @return true if succeeded or if the file is not XML, otherwise return false
128          */
129         private boolean extractProperties(final File upfile, final Document doc) {
130                 boolean res = true;
131                 Reader tool = Toolbox.getReader(upfile);
132                 if (tool != null) {
133                         String fileref = tool.extractProperty("reference");
134                         String filever = tool.extractProperty("version");
135                         if (fileref == null || doc.getReference().equals(fileref)) {
136                                 if (filever != null) {
137                                         try {
138                                                 Revision.Format get = new Revision.Format(
139                                                                 getProjectSettings().getRevisionPattern());
140                                                 Revision newver = get.parse(filever);
141                                                 Revision oldver = new Revision(doc.getVersion());
142                                                 if (!newver.isGraterThan(oldver)) {
143                                                         throw new InvalidPropertyException("version");
144                                                 }
145                                                 if (newver.isMinor()) {
146                                                         _state = ProgressState.inWORK;
147                                                 } else {
148                                                         _state = ProgressState.inDRAFT;
149                                                 }
150                                                 setVersion(newver.toString());
151                                         } catch (Exception e) {
152                                                 setErrorCode("message.error.version.mismatch");
153                                                 res = false;
154                                         }
155                                 }
156                                 if (res) {
157                                         _description = tool.extractProperty("history");
158                                         res = extractDate(tool);
159                                 }
160                         } else {
161                                 setErrorCode("message.error.reference.mismatch");
162                                 res = false;
163                         }
164                 }
165                 return res;
166         }
167
168         /**
169          * Create a new version of the selected document.
170          * 
171          * @return SUCCESS - if succeeded, "cancel" - if canceled, ERROR - if failed
172          */
173         public String doVersion() {
174                 String res = ERROR;
175                 initializationScreenContext(Constants.STUDY_MENU, Constants.STUDY_MENU,
176                                 Constants.TRUE);
177
178                 if (_action == ToDo.cancel) {
179                         res = "cancel";
180                 } else {
181
182                         try {
183                                 // Getting user inputs
184                                 _mystudy = getOpenStudy();
185                                 User user = getConnectedUser();
186                                 Step step = _mystudy.getSelectedStep();
187                                 Date aDate = null;
188                                 if (getDocumentDate().length() > 0) {
189                                         ResourceBundle locale = ResourceBundle.getBundle("som",
190                                                         getApplicationSettings().getCurrentLocale());
191                                         SimpleDateFormat get = new SimpleDateFormat(locale
192                                                         .getString("date.format"), getApplicationSettings()
193                                                         .getCurrentLocale());
194                                         aDate = get.parse(getDocumentDate());
195                                 }
196
197                                 String[] listDocuses = null;
198                                 if (_docuses != null) {
199                                         listDocuses = _docuses.split(",");
200                                 }
201                                 getPublicationService().versionDocument(step, user, _fileName,
202                                                 Integer.valueOf(_index), getVersion(), _description,
203                                                 _state, aDate, listDocuses, _docusedby);
204
205                                 // Update of the open study
206                                 _mystudy.setSelection(_mystudy.getSelection()); // Rebuilds the presentation
207                                 // TODO: Look is an optimization is possible (for example by updating the presentation of versioned document)
208
209                                 res = SUCCESS;
210                         } catch (FileNotFoundException error) {
211                                 LOG.error("Reason:", error);
212                                 setErrorCode("message.error.import.file");
213                         } catch (Exception error) {
214                                 LOG.error("Reason:", error);
215                                 setErrorCode("message.error.internal");
216                         }
217                         if (!SUCCESS.equals(res)) {
218                                 initializationFullScreenContext(Constants.STUDY_MENU,
219                                                 Constants.STUDY_MENU, Constants.TRUE, Constants.NONE,
220                                                 Constants.STUDY_MENU);
221                         }
222                 }
223                 return res;
224         }
225
226         // ==============================================================================================================================
227         // Getters and setters
228         // ==============================================================================================================================
229
230         public List<Publication> getDependencies() {
231                 return _usedby;
232         }
233
234         public String getDescription() {
235                 return _description;
236         }
237
238         public String getIndex() {
239                 return _index;
240         }
241
242         public void setDefaultDescription(final String summary) {
243                 if (this._description == null) {
244                         this._description = summary;
245                 }
246         }
247
248         public void setDescription(final String summary) {
249                 this._description = summary;
250         }
251
252         public void setIndex(final String index) {
253                 this._index = index;
254         }
255
256         public void setUsedBy(final long[] list) {
257                 this._docusedby = list;
258         }
259
260         /**
261          * Get the documentStates.
262          * 
263          * @return the documentStates
264          */
265         public List<ProgressState> getDocumentStates() {
266                 return _documentStates;
267         }
268 }