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