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 if (tag.value().getProgressState() == ProgressState.EXTERN) {
97 _documentStates.add(ProgressState.EXTERN);
99 _documentStates.add(ProgressState.inWORK);
100 if (_deftype != null) {
101 // Check if the validation cycle of the document type can has a review state
102 ValidationCycle cycle = getStudyService()
103 .getValidationCycleOf(_mystudy.getMystudy(),
105 if ((cycle != null) && cycle.enables(ValidationStep.REVIEW)) {
106 _documentStates.add(ProgressState.inDRAFT);
111 if (!(Constants.NONE.equals(getToolProperty()))) {
112 initializationFullScreenContext(Constants.STUDY_MENU,
113 Constants.STUDY_MENU, Constants.TRUE, Constants.NONE,
114 Constants.STUDY_MENU);
122 * Try to extract properties from the uploaded file if it is XML.
127 * the document to version
128 * @return true if succeeded or if the file is not XML, otherwise return false
130 private boolean extractProperties(final File upfile, final Document doc) {
132 Reader tool = Toolbox.getReader(upfile);
134 String fileref = tool.extractProperty("reference");
135 String filever = tool.extractProperty("version");
136 if (fileref == null || doc.getReference().equals(fileref)) {
137 if (filever != null) {
139 Revision.Format get = new Revision.Format(
140 getProjectSettings().getRevisionPattern());
141 Revision newver = get.parse(filever);
142 Revision oldver = new Revision(doc.getVersion());
143 if (!newver.isGraterThan(oldver)) {
144 throw new InvalidPropertyException("version");
146 if (newver.isMinor()) {
147 _state = ProgressState.inWORK;
149 _state = ProgressState.inDRAFT;
151 setVersion(newver.toString());
152 } catch (Exception e) {
153 setErrorCode("message.error.version.mismatch");
158 _description = tool.extractProperty("history");
159 res = extractDate(tool);
162 setErrorCode("message.error.reference.mismatch");
170 * Create a new version of the selected document.
172 * @return SUCCESS - if succeeded, "cancel" - if canceled, ERROR - if failed
174 public String doVersion() {
176 initializationScreenContext(Constants.STUDY_MENU, Constants.STUDY_MENU,
179 if (_action == ToDo.cancel) {
184 // Getting user inputs
185 _mystudy = getOpenStudy();
186 User user = getConnectedUser();
187 Step step = _mystudy.getSelectedStep();
189 if (getDocumentDate().length() > 0) {
190 ResourceBundle locale = ResourceBundle.getBundle("som",
191 getApplicationSettings().getCurrentLocale());
192 SimpleDateFormat get = new SimpleDateFormat(locale
193 .getString("date.format"), getApplicationSettings()
194 .getCurrentLocale());
195 aDate = get.parse(getDocumentDate());
198 String[] listDocuses = null;
199 if (_docuses != null) {
200 listDocuses = _docuses.split(",");
202 getPublicationService().versionDocument(step, user, _fileName,
203 Integer.valueOf(_index), getVersion(), _description,
204 _state, aDate, listDocuses, _docusedby);
206 // Update of the open study
210 } catch (FileNotFoundException error) {
211 LOG.error("Reason:", error);
212 setErrorCode("message.error.import.file");
213 } catch (Exception error) {
214 LOG.error("Reason:", error);
215 setErrorCode("message.error.internal");
217 if (!SUCCESS.equals(res)) {
218 initializationFullScreenContext(Constants.STUDY_MENU,
219 Constants.STUDY_MENU, Constants.TRUE, Constants.NONE,
220 Constants.STUDY_MENU);
226 // ==============================================================================================================================
227 // Getters and setters
228 // ==============================================================================================================================
230 public List<Publication> getDependencies() {
234 public String getDescription() {
238 public String getIndex() {
242 public void setDefaultDescription(final String summary) {
243 if (this._description == null) {
244 this._description = summary;
248 public void setDescription(final String summary) {
249 this._description = summary;
252 public void setIndex(final String index) {
256 public void setUsedBy(final long[] list) {
257 this._docusedby = list;
261 * Get the documentStates.
263 * @return the documentStates
265 public List<ProgressState> getDocumentStates() {
266 return _documentStates;