1 package org.splat.simer;
4 import java.io.FileNotFoundException;
5 import java.text.SimpleDateFormat;
6 import java.util.ArrayList;
8 import java.util.Iterator;
10 import java.util.ResourceBundle;
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;
31 * Action for creating a new version of a document.
33 public class VersionDocumentAction extends BaseUploadDocumentAction {
38 private static final long serialVersionUID = -5702264003232132168L;
41 * Versioned document index.
43 private String _index = null;
45 * List of publications which use the selected document.
47 private transient List<Publication> _usedby = null;
49 * List of selected impacted documents ids.
51 private transient long[] _docusedby = null;
53 * Summary of changes in the new version.
55 private String _description = null;
57 * Applicable document states.
59 private transient final List<ProgressState> _documentStates = new ArrayList<ProgressState>();
62 * Initialize the action form.
64 * @return SUCCESS if succeeded, ERROR if uploaded file is XML and we can't extract properties from it
66 public String doInitialize() {
67 File upfile = commonInitialize(Constants.TRUE);
69 _mystudy = getOpenStudy();
70 //updating relations of docs
71 _mystudy.updateCurrentStep();
72 _defuses = new ArrayList<Document>();
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>();
82 if (extractProperties(upfile, doc)) {
83 if (_deftype != null) {
84 setupDefaultUses(_deftype);
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);
94 // Avoid recursive using of the document
95 if (_defuses.contains(doc)) {
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,
104 for (Document document : toDeleteFromDefuses) {
105 if (_defuses.contains(document)) {
106 _defuses.remove(document);
110 // Setup dependencies
111 _usedby.addAll(tag.getRelations(UsedByRelation.class));
113 // Initialize applicable states list
114 if (tag.value().getProgressState() == ProgressState.EXTERN) {
115 _documentStates.add(ProgressState.EXTERN);
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(),
123 if ((cycle != null) && cycle.enables(ValidationStep.REVIEW)) {
124 _documentStates.add(ProgressState.inDRAFT);
129 if (!(Constants.NONE.equals(getToolProperty()))) {
130 initializationFullScreenContext(Constants.STUDY_MENU,
131 Constants.STUDY_MENU, Constants.TRUE, Constants.NONE,
132 Constants.STUDY_MENU);
140 * Try to extract properties from the uploaded file if it is XML.
145 * the document to version
146 * @return true if succeeded or if the file is not XML, otherwise return false
148 private boolean extractProperties(final File upfile, final Document doc) {
150 Reader tool = Toolbox.getReader(upfile);
152 String fileref = tool.extractProperty("reference");
153 String filever = tool.extractProperty("version");
154 if (fileref == null || doc.getReference().equals(fileref)) {
155 if (filever != null) {
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");
164 if (newver.isMinor()) {
165 _state = ProgressState.inWORK;
167 _state = ProgressState.inDRAFT;
169 setVersion(newver.toString());
170 } catch (Exception e) {
171 setErrorCode("message.error.version.mismatch");
176 _description = tool.extractProperty("history");
177 res = extractDate(tool);
180 setErrorCode("message.error.reference.mismatch");
188 * Create a new version of the selected document.
190 * @return SUCCESS - if succeeded, "cancel" - if canceled, ERROR - if failed
192 public String doVersion() {
194 initializationScreenContext(Constants.STUDY_MENU, Constants.STUDY_MENU,
197 if (_action == ToDo.cancel) {
202 // Getting user inputs
203 _mystudy = getOpenStudy();
204 User user = getConnectedUser();
205 Step step = _mystudy.getSelectedStep();
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());
216 String[] listDocuses = null;
217 if (_docuses != null) {
218 listDocuses = _docuses.split(",");
220 getPublicationService().versionDocument(step, user, _fileName,
221 Integer.valueOf(_index), getVersion(), _description,
222 _state, aDate, listDocuses, _docusedby);
224 // Update of the open study
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");
235 if (!SUCCESS.equals(res)) {
236 initializationFullScreenContext(Constants.STUDY_MENU,
237 Constants.STUDY_MENU, Constants.TRUE, Constants.NONE,
238 Constants.STUDY_MENU);
244 // ==============================================================================================================================
245 // Getters and setters
246 // ==============================================================================================================================
248 public List<Publication> getDependencies() {
252 public String getDescription() {
256 public String getIndex() {
260 public void setDefaultDescription(final String summary) {
261 if (this._description == null) {
262 this._description = summary;
266 public void setDescription(final String summary) {
267 this._description = summary;
270 public void setIndex(final String index) {
274 public void setUsedBy(final long[] list) {
275 this._docusedby = list;
279 * Get the documentStates.
281 * @return the documentStates
283 public List<ProgressState> getDocumentStates() {
284 return _documentStates;