Salome HOME
Correction of the uses variants during version operation
[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(getPublicationService().getLastVersion(used, tag.getOwner()))) {
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()
103                                                         .getValidationCycleOf(_mystudy.getMystudy(),
104                                                                         _deftype);
105                                         if ((cycle != null) && cycle.enables(ValidationStep.REVIEW)) {
106                                                 _documentStates.add(ProgressState.inDRAFT);
107                                         }
108                                 }
109                         }
110                 } else {
111                         if (!(Constants.NONE.equals(getToolProperty()))) {
112                                 initializationFullScreenContext(Constants.STUDY_MENU,
113                                                 Constants.STUDY_MENU, Constants.TRUE, Constants.NONE,
114                                                 Constants.STUDY_MENU);
115                         }
116                         res = ERROR;
117                 }
118                 return res;
119         }
120
121         /**
122          * Try to extract properties from the uploaded file if it is XML.
123          * 
124          * @param upfile
125          *            the file to parse
126          * @param doc
127          *            the document to version
128          * @return true if succeeded or if the file is not XML, otherwise return false
129          */
130         private boolean extractProperties(final File upfile, final Document doc) {
131                 boolean res = true;
132                 Reader tool = Toolbox.getReader(upfile);
133                 if (tool != null) {
134                         String fileref = tool.extractProperty("reference");
135                         String filever = tool.extractProperty("version");
136                         if (fileref == null || doc.getReference().equals(fileref)) {
137                                 if (filever != null) {
138                                         try {
139                                                 Revision.Format get = new Revision.Format(
140                                                                 getProjectSettings().getRevisionPattern());
141                                                 Revision newver = get.parse(filever);
142                                                 Revision oldver = new Revision(doc.getVersion());
143                                                 if (!newver.isGraterThan(oldver)) {
144                                                         throw new InvalidPropertyException("version");
145                                                 }
146                                                 if (newver.isMinor()) {
147                                                         _state = ProgressState.inWORK;
148                                                 } else {
149                                                         _state = ProgressState.inDRAFT;
150                                                 }
151                                                 setVersion(newver.toString());
152                                         } catch (Exception e) {
153                                                 setErrorCode("message.error.version.mismatch");
154                                                 res = false;
155                                         }
156                                 }
157                                 if (res) {
158                                         _description = tool.extractProperty("history");
159                                         res = extractDate(tool);
160                                 }
161                         } else {
162                                 setErrorCode("message.error.reference.mismatch");
163                                 res = false;
164                         }
165                 }
166                 return res;
167         }
168
169         /**
170          * Create a new version of the selected document.
171          * 
172          * @return SUCCESS - if succeeded, "cancel" - if canceled, ERROR - if failed
173          */
174         public String doVersion() {
175                 String res = ERROR;
176                 initializationScreenContext(Constants.STUDY_MENU, Constants.STUDY_MENU,
177                                 Constants.TRUE);
178
179                 if (_action == ToDo.cancel) {
180                         res = "cancel";
181                 } else {
182
183                         try {
184                                 // Getting user inputs
185                                 _mystudy = getOpenStudy();
186                                 User user = getConnectedUser();
187                                 Step step = _mystudy.getSelectedStep();
188                                 Date aDate = null;
189                                 if (getDocumentDate().length() > 0) {
190                                         ResourceBundle locale = ResourceBundle.getBundle("som",
191                                                         getApplicationSettings().getCurrentLocale());
192                                         SimpleDateFormat get = new SimpleDateFormat(locale
193                                                         .getString("date.format"), getApplicationSettings()
194                                                         .getCurrentLocale());
195                                         aDate = get.parse(getDocumentDate());
196                                 }
197
198                                 String[] listDocuses = null;
199                                 if (_docuses != null) {
200                                         listDocuses = _docuses.split(",");
201                                 }
202                                 getPublicationService().versionDocument(step, user, _fileName,
203                                                 Integer.valueOf(_index), getVersion(), _description,
204                                                 _state, aDate, listDocuses, _docusedby);
205
206                                 // Update of the open study
207                                 refreshStudy();
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 }