1 package org.splat.simer;
4 import java.io.FileNotFoundException;
5 import java.text.ParseException;
6 import java.text.SimpleDateFormat;
7 import java.util.ArrayList;
9 import java.util.Iterator;
10 import java.util.List;
11 import java.util.ResourceBundle;
13 import org.splat.dal.bo.kernel.Relation;
14 import org.splat.dal.bo.kernel.User;
15 import org.splat.dal.bo.som.Document;
16 import org.splat.dal.bo.som.ProgressState;
17 import org.splat.dal.bo.som.Publication;
18 import org.splat.dal.bo.som.UsedByRelation;
19 import org.splat.dal.bo.som.UsesRelation;
20 import org.splat.kernel.InvalidPropertyException;
21 import org.splat.manox.Reader;
22 import org.splat.manox.Toolbox;
23 import org.splat.service.PublicationService;
24 import org.splat.service.StepService;
25 import org.splat.service.technical.ProjectSettingsService;
26 import org.splat.service.technical.RepositoryService;
27 import org.splat.som.Revision;
28 import org.splat.som.Step;
29 import org.splat.wapp.Constants;
32 * Action for creating a new version of a document.
34 public class VersionDocumentAction extends UploadBaseNextAction {
39 private static final long serialVersionUID = -5702264003232132168L;
42 * Versioned document index.
44 private String _index = null;
46 * List of publications which use the selected document.
48 private transient List<Publication> _usedby = null;
50 * List of selected impacted documents ids.
52 private transient long[] _docusedby = null;
54 * Summary of changes in the new version.
56 private String _description = null;
58 * Version number extracted from the imported file, if exist.
60 private String _version = "";
62 * Date extracted from the imported file, if exist.
64 private String _date = "";
66 * Injected project settings service.
68 private ProjectSettingsService _projectSettings;
70 * Injected publication service.
72 private PublicationService _publicationService;
74 * Injected step service.
76 private StepService _stepService;
78 * Injected repository service.
80 private RepositoryService _repositoryService;
82 // ==============================================================================================================================
84 // ==============================================================================================================================
87 * Initialize the action form.
89 * @return SUCCESS if succeeded, ERROR if uploaded file is XML and we can't extract properties from it
91 public String doInitialize() {
93 if ("true".equals(getWriteAccess())) {
94 setToolProperty(Constants.STUDY_MENU);
96 setToolProperty(Constants.NONE);
98 initializationFullScreenContext(Constants.STUDY_MENU,
99 Constants.STUDY_MENU, Constants.TRUE, getToolProperty(),
100 Constants.STUDY_MENU);
102 User user = getConnectedUser();
103 File updir = getRepositoryService().getDownloadDirectory(user);
104 File upfile = new File(updir.getPath() + "/" + filename);
106 mystudy = getOpenStudy();
108 Publication tag = mystudy.getSelectedStep().getDocument(
109 Integer.valueOf(_index));
110 Document doc = tag.value();
111 deftype = doc.getType();
112 docname = doc.getTitle();
113 defuses = new ArrayList<Document>();
114 _usedby = new ArrayList<Publication>();
116 String res = SUCCESS;
117 if (extractProperties(upfile, doc)) {
118 setupDefaultUses(deftype);
119 // Add additional documents used by the current version
120 List<Relation> uses = doc.getRelations(UsesRelation.class);
121 for (Iterator<Relation> i = uses.iterator(); i.hasNext();) {
122 Document used = (Document) i.next().getTo();
123 if (!defuses.contains(used)) {
127 // Setup dependencies
128 _usedby.addAll(tag.getRelations(UsedByRelation.class));
130 if (!(Constants.NONE.equals(getToolProperty()))) {
131 initializationFullScreenContext(Constants.STUDY_MENU,
132 Constants.STUDY_MENU, Constants.TRUE, Constants.NONE,
133 Constants.STUDY_MENU);
141 * Try to extract properties from the uploaded file if it is XML.
146 * the document to version
147 * @return true if succeeded or if the file is not XML, otherwise return false
149 private boolean extractProperties(final File upfile, final Document doc) {
151 Reader tool = Toolbox.getReader(upfile);
153 String fileref = tool.extractProperty("reference");
154 String filever = tool.extractProperty("version");
155 if (fileref == null || doc.getReference().equals(fileref)) {
156 if (filever != null) {
158 Revision.Format get = new Revision.Format(
159 getProjectSettings().getRevisionPattern());
160 Revision newver = get.parse(filever);
161 Revision oldver = new Revision(doc.getVersion());
162 if (!newver.isGraterThan(oldver)) {
163 throw new InvalidPropertyException("version");
165 if (newver.isMinor()) {
166 state = ProgressState.inWORK;
168 state = ProgressState.inDRAFT;
170 _version = newver.toString();
171 } catch (Exception e) {
172 setErrorCode("message.error.version.mismatch");
177 _description = tool.extractProperty("history");
178 _date = tool.extractProperty("date");
182 ResourceBundle locale = ResourceBundle.getBundle("som",
183 getApplicationSettings().getCurrentLocale());
184 SimpleDateFormat check = new SimpleDateFormat(locale
185 .getString("date.format"));
188 } catch (ParseException e) {
189 setErrorCode("message.error.format.date");
195 setErrorCode("message.error.reference.mismatch");
203 * Create a new version of the selected document.
205 * @return SUCCESS - if succeeded, "cancel" - if canceled, ERROR - if failed
207 public String doVersion() {
209 initializationScreenContext(Constants.STUDY_MENU, Constants.STUDY_MENU,
212 if (action == ToDo.cancel) {
217 // Getting user inputs
218 mystudy = getOpenStudy();
219 User user = getConnectedUser();
220 Step step = mystudy.getSelectedStep();
222 if (_date.length() > 0) {
223 ResourceBundle locale = ResourceBundle.getBundle("som",
224 getApplicationSettings().getCurrentLocale());
225 SimpleDateFormat get = new SimpleDateFormat(locale
226 .getString("date.format"));
227 aDate = get.parse(_date);
230 String[] listDocuses = null;
231 if (docuses != null) {
232 listDocuses = docuses.split(",");
234 getPublicationService().versionDocument(step, user, filename,
235 Integer.valueOf(_index), _version, _description, state,
236 aDate, listDocuses, _docusedby);
238 // Update of the open study
239 mystudy.setSelection(mystudy.getSelection()); // Rebuilds the presentation
240 // TODO: Look is an optimization is possible (for example by updating the presentation of versioned document)
243 } catch (FileNotFoundException error) {
244 LOG.error("Reason:", error);
245 setErrorCode("message.error.import.file");
246 } catch (Exception error) {
247 LOG.error("Reason:", error);
248 setErrorCode("message.error.internal");
250 if (!SUCCESS.equals(res)) {
251 initializationFullScreenContext(Constants.STUDY_MENU,
252 Constants.STUDY_MENU, Constants.TRUE, Constants.NONE,
253 Constants.STUDY_MENU);
259 // ==============================================================================================================================
260 // Getters and setters
261 // ==============================================================================================================================
263 public String getDate() {
267 public List<Publication> getDependencies() {
271 public String getDescription() {
275 public String getIndex() {
279 public String getVersion() {
283 public void setDate(final String date) {
287 public void setDefaultDescription(final String summary) {
288 if (this._description == null) {
289 this._description = summary;
293 public void setDescription(final String summary) {
294 this._description = summary;
297 public void setIndex(final String index) {
301 public void setUsedBy(final long[] list) {
302 this._docusedby = list;
305 public void setVersion(final String value) {
306 this._version = value;
310 * Get project settings.
312 * @return Project settings service
314 private ProjectSettingsService getProjectSettings() {
315 return _projectSettings;
319 * Set project settings service.
321 * @param projectSettingsService
322 * project settings service
324 public void setProjectSettings(
325 final ProjectSettingsService projectSettingsService) {
326 _projectSettings = projectSettingsService;
330 * Get the publicationService.
332 * @return the publicationService
334 public PublicationService getPublicationService() {
335 return _publicationService;
339 * Set the publicationService.
341 * @param publicationService
342 * the publicationService to set
344 public void setPublicationService(
345 final PublicationService publicationService) {
346 _publicationService = publicationService;
350 * Get the stepService.
352 * @return the stepService
354 public StepService getStepService() {
359 * Set the stepService.
362 * the stepService to set
364 public void setStepService(final StepService stepService) {
365 _stepService = stepService;
369 * Get the repositoryService.
371 * @return the repositoryService
373 public RepositoryService getRepositoryService() {
374 return _repositoryService;
378 * Set the repositoryService.
380 * @param repositoryService
381 * the repositoryService to set
383 public void setRepositoryService(final RepositoryService repositoryService) {
384 _repositoryService = repositoryService;