Salome HOME
Fix for mantis #0022093: To exclude the "In-Draft" state from drop-down list during...
[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                         _documentStates.add(ProgressState.inWORK);
97                         if (_deftype != null) {
98                                 // Check if the validation cycle of the document type can has a review state
99                                 ValidationCycle cycle = getStudyService().getValidationCycleOf(
100                                                 _mystudy.getMystudy(), _deftype);
101                                 if ((cycle != null) && cycle.enables(ValidationStep.REVIEW)) {
102                                         _documentStates.add(ProgressState.inDRAFT);
103                                 }
104                         }
105
106                 } else {
107                         if (!(Constants.NONE.equals(getToolProperty()))) {
108                                 initializationFullScreenContext(Constants.STUDY_MENU,
109                                                 Constants.STUDY_MENU, Constants.TRUE, Constants.NONE,
110                                                 Constants.STUDY_MENU);
111                         }
112                         res = ERROR;
113                 }
114                 return res;
115         }
116
117         /**
118          * Try to extract properties from the uploaded file if it is XML.
119          * 
120          * @param upfile
121          *            the file to parse
122          * @param doc
123          *            the document to version
124          * @return true if succeeded or if the file is not XML, otherwise return false
125          */
126         private boolean extractProperties(final File upfile, final Document doc) {
127                 boolean res = true;
128                 Reader tool = Toolbox.getReader(upfile);
129                 if (tool != null) {
130                         String fileref = tool.extractProperty("reference");
131                         String filever = tool.extractProperty("version");
132                         if (fileref == null || doc.getReference().equals(fileref)) {
133                                 if (filever != null) {
134                                         try {
135                                                 Revision.Format get = new Revision.Format(
136                                                                 getProjectSettings().getRevisionPattern());
137                                                 Revision newver = get.parse(filever);
138                                                 Revision oldver = new Revision(doc.getVersion());
139                                                 if (!newver.isGraterThan(oldver)) {
140                                                         throw new InvalidPropertyException("version");
141                                                 }
142                                                 if (newver.isMinor()) {
143                                                         _state = ProgressState.inWORK;
144                                                 } else {
145                                                         _state = ProgressState.inDRAFT;
146                                                 }
147                                                 setVersion(newver.toString());
148                                         } catch (Exception e) {
149                                                 setErrorCode("message.error.version.mismatch");
150                                                 res = false;
151                                         }
152                                 }
153                                 if (res) {
154                                         _description = tool.extractProperty("history");
155                                         res = extractDate(tool);
156                                 }
157                         } else {
158                                 setErrorCode("message.error.reference.mismatch");
159                                 res = false;
160                         }
161                 }
162                 return res;
163         }
164
165         /**
166          * Create a new version of the selected document.
167          * 
168          * @return SUCCESS - if succeeded, "cancel" - if canceled, ERROR - if failed
169          */
170         public String doVersion() {
171                 String res = ERROR;
172                 initializationScreenContext(Constants.STUDY_MENU, Constants.STUDY_MENU,
173                                 Constants.TRUE);
174
175                 if (_action == ToDo.cancel) {
176                         res = "cancel";
177                 } else {
178
179                         try {
180                                 // Getting user inputs
181                                 _mystudy = getOpenStudy();
182                                 User user = getConnectedUser();
183                                 Step step = _mystudy.getSelectedStep();
184                                 Date aDate = null;
185                                 if (getDocumentDate().length() > 0) {
186                                         ResourceBundle locale = ResourceBundle.getBundle("som",
187                                                         getApplicationSettings().getCurrentLocale());
188                                         SimpleDateFormat get = new SimpleDateFormat(locale
189                                                         .getString("date.format"), getApplicationSettings()
190                                                         .getCurrentLocale());
191                                         aDate = get.parse(getDocumentDate());
192                                 }
193
194                                 String[] listDocuses = null;
195                                 if (_docuses != null) {
196                                         listDocuses = _docuses.split(",");
197                                 }
198                                 getPublicationService().versionDocument(step, user, _fileName,
199                                                 Integer.valueOf(_index), getVersion(), _description,
200                                                 _state, aDate, listDocuses, _docusedby);
201
202                                 // Update of the open study
203                                 _mystudy.setSelection(_mystudy.getSelection()); // Rebuilds the presentation
204                                 // TODO: Look is an optimization is possible (for example by updating the presentation of versioned document)
205
206                                 res = SUCCESS;
207                         } catch (FileNotFoundException error) {
208                                 LOG.error("Reason:", error);
209                                 setErrorCode("message.error.import.file");
210                         } catch (Exception error) {
211                                 LOG.error("Reason:", error);
212                                 setErrorCode("message.error.internal");
213                         }
214                         if (!SUCCESS.equals(res)) {
215                                 initializationFullScreenContext(Constants.STUDY_MENU,
216                                                 Constants.STUDY_MENU, Constants.TRUE, Constants.NONE,
217                                                 Constants.STUDY_MENU);
218                         }
219                 }
220                 return res;
221         }
222
223         // ==============================================================================================================================
224         // Getters and setters
225         // ==============================================================================================================================
226
227         public List<Publication> getDependencies() {
228                 return _usedby;
229         }
230
231         public String getDescription() {
232                 return _description;
233         }
234
235         public String getIndex() {
236                 return _index;
237         }
238
239         public void setDefaultDescription(final String summary) {
240                 if (this._description == null) {
241                         this._description = summary;
242                 }
243         }
244
245         public void setDescription(final String summary) {
246                 this._description = summary;
247         }
248
249         public void setIndex(final String index) {
250                 this._index = index;
251         }
252
253         public void setUsedBy(final long[] list) {
254                 this._docusedby = list;
255         }
256
257         /**
258          * Get the documentStates.
259          * 
260          * @return the documentStates
261          */
262         public List<ProgressState> getDocumentStates() {
263                 return _documentStates;
264         }
265 }