1 package org.splat.simer;
4 import java.io.FileNotFoundException;
5 import java.text.SimpleDateFormat;
6 import java.util.ArrayList;
9 import java.util.ResourceBundle;
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;
28 * Action for creating a new version of a document.
30 public class VersionDocumentAction extends BaseUploadDocumentAction {
35 private static final long serialVersionUID = -5702264003232132168L;
38 * Versioned document index.
40 private String _index = null;
42 * List of publications which use the selected document.
44 private transient List<Publication> _usedby = null;
46 * List of selected impacted documents ids.
48 private transient long[] _docusedby = null;
50 * Summary of changes in the new version.
52 private String _description = null;
54 * Applicable document states.
56 private transient final List<ProgressState> _documentStates = new ArrayList<ProgressState>();
59 * Initialize the action form.
61 * @return SUCCESS if succeeded, ERROR if uploaded file is XML and we can't extract properties from it
63 public String doInitialize() {
64 File upfile = commonInitialize(Constants.TRUE);
66 _mystudy = getOpenStudy();
67 _defuses = new ArrayList<Document>();
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>();
77 if (extractProperties(upfile, doc)) {
78 if (_deftype != null) {
79 setupDefaultUses(_deftype);
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(used)) {
88 // Avoid recursive using of the document
89 if (_defuses.contains(doc)) {
93 _usedby.addAll(tag.getRelations(UsedByRelation.class));
95 // Initialize applicable states list
96 _documentStates.add(ProgressState.inWORK);
97 if (_deftype != null) {
98 // Check if the validation cycle of the document type can has a review state
99 ValidationCycle cycle = getStudyService().getValidationCycleOf(
100 _mystudy.getMystudy(), _deftype);
101 if ((cycle != null) && cycle.enables(ValidationStep.REVIEW)) {
102 _documentStates.add(ProgressState.inDRAFT);
107 if (!(Constants.NONE.equals(getToolProperty()))) {
108 initializationFullScreenContext(Constants.STUDY_MENU,
109 Constants.STUDY_MENU, Constants.TRUE, Constants.NONE,
110 Constants.STUDY_MENU);
118 * Try to extract properties from the uploaded file if it is XML.
123 * the document to version
124 * @return true if succeeded or if the file is not XML, otherwise return false
126 private boolean extractProperties(final File upfile, final Document doc) {
128 Reader tool = Toolbox.getReader(upfile);
130 String fileref = tool.extractProperty("reference");
131 String filever = tool.extractProperty("version");
132 if (fileref == null || doc.getReference().equals(fileref)) {
133 if (filever != null) {
135 Revision.Format get = new Revision.Format(
136 getProjectSettings().getRevisionPattern());
137 Revision newver = get.parse(filever);
138 Revision oldver = new Revision(doc.getVersion());
139 if (!newver.isGraterThan(oldver)) {
140 throw new InvalidPropertyException("version");
142 if (newver.isMinor()) {
143 _state = ProgressState.inWORK;
145 _state = ProgressState.inDRAFT;
147 setVersion(newver.toString());
148 } catch (Exception e) {
149 setErrorCode("message.error.version.mismatch");
154 _description = tool.extractProperty("history");
155 res = extractDate(tool);
158 setErrorCode("message.error.reference.mismatch");
166 * Create a new version of the selected document.
168 * @return SUCCESS - if succeeded, "cancel" - if canceled, ERROR - if failed
170 public String doVersion() {
172 initializationScreenContext(Constants.STUDY_MENU, Constants.STUDY_MENU,
175 if (_action == ToDo.cancel) {
180 // Getting user inputs
181 _mystudy = getOpenStudy();
182 User user = getConnectedUser();
183 Step step = _mystudy.getSelectedStep();
185 if (getDocumentDate().length() > 0) {
186 ResourceBundle locale = ResourceBundle.getBundle("som",
187 getApplicationSettings().getCurrentLocale());
188 SimpleDateFormat get = new SimpleDateFormat(locale
189 .getString("date.format"), getApplicationSettings()
190 .getCurrentLocale());
191 aDate = get.parse(getDocumentDate());
194 String[] listDocuses = null;
195 if (_docuses != null) {
196 listDocuses = _docuses.split(",");
198 getPublicationService().versionDocument(step, user, _fileName,
199 Integer.valueOf(_index), getVersion(), _description,
200 _state, aDate, listDocuses, _docusedby);
202 // Update of the open study
203 _mystudy.setSelection(_mystudy.getSelection()); // Rebuilds the presentation
204 // TODO: Look is an optimization is possible (for example by updating the presentation of versioned document)
207 } catch (FileNotFoundException error) {
208 LOG.error("Reason:", error);
209 setErrorCode("message.error.import.file");
210 } catch (Exception error) {
211 LOG.error("Reason:", error);
212 setErrorCode("message.error.internal");
214 if (!SUCCESS.equals(res)) {
215 initializationFullScreenContext(Constants.STUDY_MENU,
216 Constants.STUDY_MENU, Constants.TRUE, Constants.NONE,
217 Constants.STUDY_MENU);
223 // ==============================================================================================================================
224 // Getters and setters
225 // ==============================================================================================================================
227 public List<Publication> getDependencies() {
231 public String getDescription() {
235 public String getIndex() {
239 public void setDefaultDescription(final String summary) {
240 if (this._description == null) {
241 this._description = summary;
245 public void setDescription(final String summary) {
246 this._description = summary;
249 public void setIndex(final String index) {
253 public void setUsedBy(final long[] list) {
254 this._docusedby = list;
258 * Get the documentStates.
260 * @return the documentStates
262 public List<ProgressState> getDocumentStates() {
263 return _documentStates;