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