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