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 _defuses = new ArrayList<Document>();
70 Publication tag = _mystudy.getSelectedStep().getDocument(
71 Integer.valueOf(_index));
72 Document doc = tag.value();
73 _deftype = doc.getType();
74 _docname = doc.getTitle();
75 _usedby = new ArrayList<Publication>();
78 if (extractProperties(upfile, doc)) {
79 if (_deftype != null) {
80 setupDefaultUses(_deftype);
82 // Add additional documents used by the current version
83 for (Relation usesRel : doc.getRelations(UsesRelation.class)) {
84 Document used = (Document) usesRel.getTo();
85 Document lastVersion = getPublicationService().getLastVersion(used, tag.getOwner());
86 if (lastVersion != null && !_defuses.contains(lastVersion)) {
87 _defuses.add(lastVersion);
90 // Avoid recursive using of the document
91 if (_defuses.contains(doc)) {
95 // Avoid using of documents dependent on the current version of the document being versioned
96 // (This case is possible only if both documents belong to the step of the same Project Element)
97 for(Iterator<Document> document = _defuses.iterator(); document.hasNext(); ) {
98 Publication pub = tag.getOwner().getPublication(document.next());
99 if(pub != null && pub.getRelations(UsesRelation.class).contains(tag)) {
104 // Setup dependencies
105 _usedby.addAll(tag.getRelations(UsedByRelation.class));
107 // Initialize applicable states list
108 if (tag.value().getProgressState() == ProgressState.EXTERN) {
109 _documentStates.add(ProgressState.EXTERN);
111 _documentStates.add(ProgressState.inWORK);
112 if (_deftype != null) {
113 // Check if the validation cycle of the document type can has a review state
114 ValidationCycle cycle = getStudyService()
115 .getValidationCycleOf(_mystudy.getMystudy(),
117 if ((cycle != null) && cycle.enables(ValidationStep.REVIEW)) {
118 _documentStates.add(ProgressState.inDRAFT);
123 if (!(Constants.NONE.equals(getToolProperty()))) {
124 initializationFullScreenContext(Constants.STUDY_MENU,
125 Constants.STUDY_MENU, Constants.TRUE, Constants.NONE,
126 Constants.STUDY_MENU);
134 * Try to extract properties from the uploaded file if it is XML.
139 * the document to version
140 * @return true if succeeded or if the file is not XML, otherwise return false
142 private boolean extractProperties(final File upfile, final Document doc) {
144 Reader tool = Toolbox.getReader(upfile);
146 String fileref = tool.extractProperty("reference");
147 String filever = tool.extractProperty("version");
148 if (fileref == null || doc.getReference().equals(fileref)) {
149 if (filever != null) {
151 Revision.Format get = new Revision.Format(
152 getProjectSettings().getRevisionPattern());
153 Revision newver = get.parse(filever);
154 Revision oldver = new Revision(doc.getVersion());
155 if (!newver.isGraterThan(oldver)) {
156 throw new InvalidPropertyException("version");
158 if (newver.isMinor()) {
159 _state = ProgressState.inWORK;
161 _state = ProgressState.inDRAFT;
163 setVersion(newver.toString());
164 } catch (Exception e) {
165 setErrorCode("message.error.version.mismatch");
170 _description = tool.extractProperty("history");
171 res = extractDate(tool);
174 setErrorCode("message.error.reference.mismatch");
182 * Create a new version of the selected document.
184 * @return SUCCESS - if succeeded, "cancel" - if canceled, ERROR - if failed
186 public String doVersion() {
188 initializationScreenContext(Constants.STUDY_MENU, Constants.STUDY_MENU,
191 if (_action == ToDo.cancel) {
196 // Getting user inputs
197 _mystudy = getOpenStudy();
198 User user = getConnectedUser();
199 Step step = _mystudy.getSelectedStep();
201 if (getDocumentDate().length() > 0) {
202 ResourceBundle locale = ResourceBundle.getBundle("som",
203 getApplicationSettings().getCurrentLocale());
204 SimpleDateFormat get = new SimpleDateFormat(locale
205 .getString("date.format"), getApplicationSettings()
206 .getCurrentLocale());
207 aDate = get.parse(getDocumentDate());
210 String[] listDocuses = null;
211 if (_docuses != null) {
212 listDocuses = _docuses.split(",");
214 getPublicationService().versionDocument(step, user, _fileName,
215 Integer.valueOf(_index), getVersion(), _description,
216 _state, aDate, listDocuses, _docusedby);
218 // Update of the open study
222 } catch (FileNotFoundException error) {
223 LOG.error("Reason:", error);
224 setErrorCode("message.error.import.file");
225 } catch (Exception error) {
226 LOG.error("Reason:", error);
227 setErrorCode("message.error.internal");
229 if (!SUCCESS.equals(res)) {
230 initializationFullScreenContext(Constants.STUDY_MENU,
231 Constants.STUDY_MENU, Constants.TRUE, Constants.NONE,
232 Constants.STUDY_MENU);
238 // ==============================================================================================================================
239 // Getters and setters
240 // ==============================================================================================================================
242 public List<Publication> getDependencies() {
246 public String getDescription() {
250 public String getIndex() {
254 public void setDefaultDescription(final String summary) {
255 if (this._description == null) {
256 this._description = summary;
260 public void setDescription(final String summary) {
261 this._description = summary;
264 public void setIndex(final String index) {
268 public void setUsedBy(final long[] list) {
269 this._docusedby = list;
273 * Get the documentStates.
275 * @return the documentStates
277 public List<ProgressState> getDocumentStates() {
278 return _documentStates;