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.som.Revision;
25 import org.splat.som.Step;
26 import org.splat.wapp.Constants;
29 * Action for creating a new version of a document.
31 public class VersionDocumentAction extends BaseUploadDocumentAction {
36 private static final long serialVersionUID = -5702264003232132168L;
39 * Versioned document index.
41 private String _index = null;
43 * List of publications which use the selected document.
45 private transient List<Publication> _usedby = null;
47 * List of selected impacted documents ids.
49 private transient long[] _docusedby = null;
51 * Summary of changes in the new version.
53 private String _description = null;
55 * Applicable document states.
57 private transient final List<ProgressState> _documentStates = new ArrayList<ProgressState>();
60 * Initialize the action form.
62 * @return SUCCESS if succeeded, ERROR if uploaded file is XML and we can't extract properties from it
64 public String doInitialize() {
65 File upfile = commonInitialize(Constants.TRUE);
67 _mystudy = getOpenStudy();
68 _mystudy.updateCurrentStep();
69 _defuses = new ArrayList<Document>();
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>();
79 if (extractProperties(upfile, doc)) {
80 if (_deftype != null) {
81 setupDefaultUses(_deftype);
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);
91 // Avoid recursive using of the document
92 if (_defuses.contains(doc)) {
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)) {
105 // Setup dependencies
106 _usedby.addAll(tag.getRelations(UsedByRelation.class));
108 // Initialize applicable states list
109 if (tag.value().getProgressState() == ProgressState.EXTERN) {
110 _documentStates.add(ProgressState.EXTERN);
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(),
118 if ((cycle != null) && cycle.enables(ValidationStep.REVIEW)) {
119 _documentStates.add(ProgressState.inDRAFT);
124 if (!(Constants.NONE.equals(getToolProperty()))) {
125 initializationFullScreenContext(Constants.STUDY_MENU,
126 Constants.STUDY_MENU, Constants.TRUE, Constants.NONE,
127 Constants.STUDY_MENU);
135 * Try to extract properties from the uploaded file if it is XML.
140 * the document to version
141 * @return true if succeeded or if the file is not XML, otherwise return false
143 private boolean extractProperties(final File upfile, final Document doc) {
145 Reader tool = Toolbox.getReader(upfile);
147 String fileref = tool.extractProperty("reference");
148 String filever = tool.extractProperty("version");
149 if (fileref == null || doc.getReference().equals(fileref)) {
150 if (filever != null) {
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");
159 if (newver.isMinor()) {
160 _state = ProgressState.inWORK;
162 _state = ProgressState.inDRAFT;
164 setVersion(newver.toString());
165 } catch (Exception e) {
166 setErrorCode("message.error.version.mismatch");
171 _description = tool.extractProperty("history");
172 res = extractDate(tool);
175 setErrorCode("message.error.reference.mismatch");
183 * Create a new version of the selected document.
185 * @return SUCCESS - if succeeded, "cancel" - if canceled, ERROR - if failed
187 public String doVersion() {
189 initializationScreenContext(Constants.STUDY_MENU, Constants.STUDY_MENU,
192 if (_action == ToDo.cancel) {
197 // Getting user inputs
198 _mystudy = getOpenStudy();
199 User user = getConnectedUser();
200 Step step = _mystudy.getSelectedStep();
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());
211 String[] listDocuses = null;
212 if (_docuses != null) {
213 listDocuses = _docuses.split(",");
215 getPublicationService().versionDocument(step, user, _fileName,
216 Integer.valueOf(_index), getVersion(), _description,
217 _state, aDate, listDocuses, _docusedby);
219 // Update of the open study
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");
230 if (!SUCCESS.equals(res)) {
231 initializationFullScreenContext(Constants.STUDY_MENU,
232 Constants.STUDY_MENU, Constants.TRUE, Constants.NONE,
233 Constants.STUDY_MENU);
239 // ==============================================================================================================================
240 // Getters and setters
241 // ==============================================================================================================================
243 public List<Publication> getDependencies() {
247 public String getDescription() {
251 public String getIndex() {
255 public void setDefaultDescription(final String summary) {
256 if (this._description == null) {
257 this._description = summary;
261 public void setDescription(final String summary) {
262 this._description = summary;
265 public void setIndex(final String index) {
269 public void setUsedBy(final long[] list) {
270 this._docusedby = list;
274 * Get the documentStates.
276 * @return the documentStates
278 public List<ProgressState> getDocumentStates() {
279 return _documentStates;