1 package org.splat.simer;
4 import java.io.FileNotFoundException;
5 import java.text.ParseException;
6 import java.text.SimpleDateFormat;
8 import java.util.Iterator;
10 import java.util.ResourceBundle;
11 import java.util.Vector;
13 import org.splat.kernel.InvalidPropertyException;
14 import org.splat.dal.bo.kernel.Relation;
15 import org.splat.dal.bo.kernel.User;
16 import org.splat.manox.Reader;
17 import org.splat.manox.Toolbox;
18 import org.splat.dal.bo.som.Document;
19 import org.splat.dal.bo.som.ProgressState;
20 import org.splat.service.PublicationService;
21 import org.splat.service.StepService;
22 import org.splat.service.technical.ProjectSettingsService;
23 import org.splat.service.technical.RepositoryService;
24 import org.splat.dal.bo.som.Publication;
25 import org.splat.som.Revision;
26 import org.splat.som.Step;
27 import org.splat.dal.bo.som.UsedByRelation;
28 import org.splat.dal.bo.som.UsesRelation;
31 * Action for creating a new version of a document.
33 public class VersionDocumentAction extends UploadBaseNextAction {
38 private static final long serialVersionUID = -5702264003232132168L;
41 * Versioned document index.
43 private String index = null;
45 * List of publications which use the selected document.
47 private List<Publication> usedby = null;
49 * List of selected impacted documents ids.
51 private long[] docusedby = null;
53 * Summary of changes in the new version.
55 private String summary = null;
57 * Version number extracted from the imported file, if exist.
59 private String docver = "";
61 * Date extracted from the imported file, if exist.
63 private String date = "";
65 * Injected project settings service.
67 private ProjectSettingsService _projectSettingsService;
69 * Injected publication service.
71 private PublicationService _publicationService;
73 * Injected step service.
75 private StepService _stepService;
77 * Injected repository service.
79 private RepositoryService _repositoryService;
81 * Value of the menu property. It can be: none, create, open, study, knowledge, sysadmin, help.
83 private String _menuProperty;
86 * Value of the title bar property. It can be: study, knowledge.
88 private String _titleProperty;
91 * Property that indicates whether the current open study is editable or not. On the screen it looks like pen on the status icon, pop-up
92 * menu also can be called. It is necessary for correct building the title bar.
94 private String _editDisabledProperty = "false";
96 // ==============================================================================================================================
98 // ==============================================================================================================================
101 * Initialize the action form.
103 * @return SUCCESS if succeeded, ERROR if uploaded file is XML and we can't extract properties from it
105 public String doInitialize() {
107 setMenuProperty("study");
108 setTitleProperty("study");
109 setEditDisabledProperty("true");
110 initializationScreenContext(_menuProperty, _titleProperty,
111 _editDisabledProperty);
113 User user = getConnectedUser();
114 File updir = getRepositoryService().getDownloadDirectory(user);
115 File upfile = new File(updir.getPath() + "/" + filename);
117 mystudy = getOpenStudy();
119 Publication tag = mystudy.getSelectedStep().getDocument(
120 Integer.valueOf(index));
121 Document doc = tag.value();
122 deftype = doc.getType();
123 docname = doc.getTitle();
124 defuses = new Vector<Document>();
125 usedby = new Vector<Publication>();
127 if (!extractProperties(upfile, doc)) {
130 setupDefaultUses(deftype);
131 // Add additional documents used by the current version
132 List<Relation> uses = doc.getRelations(UsesRelation.class);
133 for (Iterator<Relation> i = uses.iterator(); i.hasNext();) {
134 Document used = (Document) i.next().getTo();
135 if (!defuses.contains(used))
138 // Setup dependencies
139 List<Publication> relist = tag.getRelations(UsedByRelation.class);
140 for (Iterator<Publication> i = relist.iterator(); i.hasNext();) {
141 usedby.add(i.next());
147 * Try to extract properties from the uploaded file if it is XML.
152 * the document to version
153 * @return true if succeeded or if the file is not XML, otherwise return false
155 private boolean extractProperties(File upfile, Document doc) {
157 Reader tool = Toolbox.getReader(upfile);
159 String fileref = tool.extractProperty("reference");
160 String filever = tool.extractProperty("version");
161 if (fileref != null && !doc.getReference().equals(fileref)) {
162 setErrorCode("reference.mismatch");
165 if (filever != null) {
167 Revision.Format get = new Revision.Format(
168 getProjectSettings().getRevisionPattern());
169 Revision newver = get.parse(filever);
170 Revision oldver = new Revision(doc.getVersion());
171 if (!newver.isGraterThan(oldver)) {
172 throw new InvalidPropertyException("version");
174 if (newver.isMinor()) {
175 state = ProgressState.inWORK;
177 state = ProgressState.inDRAFT;
179 docver = newver.toString();
180 } catch (Exception e) {
181 setErrorCode("version.mismatch");
186 summary = tool.extractProperty("history");
187 date = tool.extractProperty("date");
189 ResourceBundle locale = ResourceBundle.getBundle("som",
190 getApplicationSettings().getCurrentLocale());
191 SimpleDateFormat check = new SimpleDateFormat(locale
192 .getString("date.format"));
195 } catch (ParseException e) {
196 setErrorCode("format.date");
209 * Create a new version of the selected document.
211 * @return SUCCESS - if succeeded, "cancel" - if canceled, ERROR - if failed
213 public String doVersion() {
214 setMenuProperty("study");
215 setTitleProperty("study");
216 setEditDisabledProperty("true");
217 initializationScreenContext(_menuProperty, _titleProperty,
218 _editDisabledProperty);
220 if (action == ToDo.cancel)
224 // Getting user inputs
225 mystudy = getOpenStudy();
226 User user = getConnectedUser();
227 Step step = mystudy.getSelectedStep();
229 if (date.length() > 0) {
230 ResourceBundle locale = ResourceBundle.getBundle("som",
231 getApplicationSettings().getCurrentLocale());
232 SimpleDateFormat get = new SimpleDateFormat(locale
233 .getString("date.format"));
234 aDate = get.parse(date);
237 String[] listDocuses = null;
238 if (docuses != null) {
239 listDocuses = docuses.split(",");
241 getPublicationService().versionDocument(step, user, filename,
242 Integer.valueOf(index), docver, summary, state, aDate,
243 listDocuses, docusedby);
245 // Update of the open study
246 mystudy.setSelection(mystudy.getSelection()); // Rebuilds the presentation
247 // TODO: Look is an optimization is possible (for example by updating the presentation of versioned document)
250 } catch (FileNotFoundException error) {
251 logger.error("Reason:", error);
252 setErrorCode("import.file");
253 } catch (Exception error) {
254 logger.error("Reason:", error);
255 setErrorCode("internal");
260 // ==============================================================================================================================
261 // Getters and setters
262 // ==============================================================================================================================
264 public String getDate() {
268 public List<Publication> getDependencies() {
272 public String getDescription() {
276 public String getIndex() {
280 public String getVersion() {
284 public void setDate(String date) {
288 public void setDefaultDescription(String summary) {
289 if (this.summary == null)
290 this.summary = summary;
293 public void setDescription(String summary) {
294 this.summary = summary;
297 public void setIndex(String index) {
301 public void setUsedBy(long[] list) {
302 this.docusedby = list;
305 public void setVersion(String value) {
310 * Get project settings.
312 * @return Project settings service
314 private ProjectSettingsService getProjectSettings() {
315 return _projectSettingsService;
319 * Set project settings service.
321 * @param projectSettingsService
322 * project settings service
324 public void setProjectSettings(ProjectSettingsService projectSettingsService) {
325 _projectSettingsService = projectSettingsService;
329 * Get the publicationService.
331 * @return the publicationService
333 public PublicationService getPublicationService() {
334 return _publicationService;
338 * Set the publicationService.
340 * @param publicationService
341 * the publicationService to set
343 public void setPublicationService(PublicationService publicationService) {
344 _publicationService = publicationService;
348 * Get the stepService.
350 * @return the stepService
352 public StepService getStepService() {
357 * Set the stepService.
360 * the stepService to set
362 public void setStepService(StepService stepService) {
363 _stepService = stepService;
367 * Get the repositoryService.
369 * @return the repositoryService
371 public RepositoryService getRepositoryService() {
372 return _repositoryService;
376 * Set the repositoryService.
378 * @param repositoryService
379 * the repositoryService to set
381 public void setRepositoryService(RepositoryService repositoryService) {
382 _repositoryService = repositoryService;
386 * Get the menuProperty.
388 * @return the menuProperty
390 public String getMenuProperty() {
391 return _menuProperty;
395 * Set the menuProperty.
397 * @param menuProperty
398 * the menuProperty to set
400 public void setMenuProperty(String menuProperty) {
401 this._menuProperty = menuProperty;
405 * Get the _titleProperty.
407 * @return the _titleProperty
409 public String getTitleProperty() {
410 return _titleProperty;
414 * Set the _titleProperty.
416 * @param titleProperty
417 * the titleProperty to set
419 public void setTitleProperty(String titleProperty) {
420 _titleProperty = titleProperty;
424 * Get the _editDisabledProperty.
426 * @return the _editDisabledProperty
428 public String getEditDisabledProperty() {
429 return _editDisabledProperty;
433 * Set the _editDisabledProperty.
435 * @param _editDisabledProperty
436 * the _editDisabledProperty to set
438 public void setEditDisabledProperty(String _editDisabledProperty) {
439 this._editDisabledProperty = _editDisabledProperty;