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.
82 * It can be: none, create, open, study, knowledge, sysadmin, help.
84 private String _menuProperty;
87 * Value of the title bar property.
88 * It can be: study, knowledge.
90 private String _titleProperty;
93 * Value of the tool bar property.
94 * It can be: none, standard, study, back.
96 private String _toolProperty;
99 * Value of the left menu property.
100 * It can be: open, study, knowledge, scenario.
102 private String _leftMenuProperty;
105 * Property that indicates whether the current open study is editable or not.
106 * On the screen it looks like pen on the status icon, pop-up menu also can be called.
107 * It is necessary for correct building the title bar.
109 private String _editDisabledProperty = "false";
111 // ==============================================================================================================================
113 // ==============================================================================================================================
116 * Initialize the action form.
118 * @return SUCCESS if succeeded, ERROR if uploaded file is XML and we can't extract properties from it
120 public String doInitialize() {
122 setMenuProperty("study");
123 setTitleProperty("study");
124 setEditDisabledProperty("true");
125 if ("true".equals(getWriteAccess())) {
126 setToolProperty("study");
128 setToolProperty("none");
130 setLeftMenuProperty("study");
131 initializationFullScreenContext(_menuProperty, _titleProperty, _editDisabledProperty, _toolProperty, _leftMenuProperty);
133 User user = getConnectedUser();
134 File updir = getRepositoryService().getDownloadDirectory(user);
135 File upfile = new File(updir.getPath() + "/" + filename);
137 mystudy = getOpenStudy();
139 Publication tag = mystudy.getSelectedStep().getDocument(
140 Integer.valueOf(index));
141 Document doc = tag.value();
142 deftype = doc.getType();
143 docname = doc.getTitle();
144 defuses = new Vector<Document>();
145 usedby = new Vector<Publication>();
147 if (!extractProperties(upfile, doc)) {
148 if (!("none".equals(_toolProperty))) {
149 setToolProperty("none");
150 setLeftMenuProperty("study");
151 initializationFullScreenContext(_menuProperty, _titleProperty, _editDisabledProperty, _toolProperty, _leftMenuProperty);
155 setupDefaultUses(deftype);
156 // Add additional documents used by the current version
157 List<Relation> uses = doc.getRelations(UsesRelation.class);
158 for (Iterator<Relation> i = uses.iterator(); i.hasNext();) {
159 Document used = (Document) i.next().getTo();
160 if (!defuses.contains(used))
163 // Setup dependencies
164 List<Publication> relist = tag.getRelations(UsedByRelation.class);
165 for (Iterator<Publication> i = relist.iterator(); i.hasNext();) {
166 usedby.add(i.next());
172 * Try to extract properties from the uploaded file if it is XML.
177 * the document to version
178 * @return true if succeeded or if the file is not XML, otherwise return false
180 private boolean extractProperties(File upfile, Document doc) {
182 Reader tool = Toolbox.getReader(upfile);
184 String fileref = tool.extractProperty("reference");
185 String filever = tool.extractProperty("version");
186 if (fileref != null && !doc.getReference().equals(fileref)) {
187 setErrorCode("reference.mismatch");
190 if (filever != null) {
192 Revision.Format get = new Revision.Format(
193 getProjectSettings().getRevisionPattern());
194 Revision newver = get.parse(filever);
195 Revision oldver = new Revision(doc.getVersion());
196 if (!newver.isGraterThan(oldver)) {
197 throw new InvalidPropertyException("version");
199 if (newver.isMinor()) {
200 state = ProgressState.inWORK;
202 state = ProgressState.inDRAFT;
204 docver = newver.toString();
205 } catch (Exception e) {
206 setErrorCode("version.mismatch");
211 summary = tool.extractProperty("history");
212 date = tool.extractProperty("date");
214 ResourceBundle locale = ResourceBundle.getBundle("som",
215 getApplicationSettings().getCurrentLocale());
216 SimpleDateFormat check = new SimpleDateFormat(locale
217 .getString("date.format"));
220 } catch (ParseException e) {
221 setErrorCode("format.date");
234 * Create a new version of the selected document.
236 * @return SUCCESS - if succeeded, "cancel" - if canceled, ERROR - if failed
238 public String doVersion() {
239 setMenuProperty("study");
240 setTitleProperty("study");
241 setEditDisabledProperty("true");
242 initializationScreenContext(_menuProperty, _titleProperty, _editDisabledProperty);
244 if (action == ToDo.cancel)
248 // Getting user inputs
249 mystudy = getOpenStudy();
250 User user = getConnectedUser();
251 Step step = mystudy.getSelectedStep();
253 if (date.length() > 0) {
254 ResourceBundle locale = ResourceBundle.getBundle("som",
255 getApplicationSettings().getCurrentLocale());
256 SimpleDateFormat get = new SimpleDateFormat(locale
257 .getString("date.format"));
258 aDate = get.parse(date);
261 String[] listDocuses = null;
262 if (docuses != null) {
263 listDocuses = docuses.split(",");
265 getPublicationService().versionDocument(step, user, filename,
266 Integer.valueOf(index), docver, summary, state, aDate,
267 listDocuses, docusedby);
269 // Update of the open study
270 mystudy.setSelection(mystudy.getSelection()); // Rebuilds the presentation
271 // TODO: Look is an optimization is possible (for example by updating the presentation of versioned document)
274 } catch (FileNotFoundException error) {
275 logger.error("Reason:", error);
276 setErrorCode("import.file");
277 } catch (Exception error) {
278 logger.error("Reason:", error);
279 setErrorCode("internal");
282 setToolProperty("none");
284 setLeftMenuProperty("study");
285 initializationFullScreenContext(_menuProperty, _titleProperty, _editDisabledProperty, _toolProperty, _leftMenuProperty);
290 // ==============================================================================================================================
291 // Getters and setters
292 // ==============================================================================================================================
294 public String getDate() {
298 public List<Publication> getDependencies() {
302 public String getDescription() {
306 public String getIndex() {
310 public String getVersion() {
314 public void setDate(String date) {
318 public void setDefaultDescription(String summary) {
319 if (this.summary == null)
320 this.summary = summary;
323 public void setDescription(String summary) {
324 this.summary = summary;
327 public void setIndex(String index) {
331 public void setUsedBy(long[] list) {
332 this.docusedby = list;
335 public void setVersion(String value) {
340 * Get project settings.
342 * @return Project settings service
344 private ProjectSettingsService getProjectSettings() {
345 return _projectSettingsService;
349 * Set project settings service.
351 * @param projectSettingsService
352 * project settings service
354 public void setProjectSettings(ProjectSettingsService projectSettingsService) {
355 _projectSettingsService = projectSettingsService;
359 * Get the publicationService.
361 * @return the publicationService
363 public PublicationService getPublicationService() {
364 return _publicationService;
368 * Set the publicationService.
370 * @param publicationService
371 * the publicationService to set
373 public void setPublicationService(PublicationService publicationService) {
374 _publicationService = publicationService;
378 * Get the stepService.
380 * @return the stepService
382 public StepService getStepService() {
387 * Set the stepService.
390 * the stepService to set
392 public void setStepService(StepService stepService) {
393 _stepService = stepService;
397 * Get the repositoryService.
399 * @return the repositoryService
401 public RepositoryService getRepositoryService() {
402 return _repositoryService;
406 * Set the repositoryService.
408 * @param repositoryService
409 * the repositoryService to set
411 public void setRepositoryService(RepositoryService repositoryService) {
412 _repositoryService = repositoryService;
416 * Get the menuProperty.
418 * @return the menuProperty
420 public String getMenuProperty() {
421 return _menuProperty;
425 * Set the menuProperty.
427 * @param menuProperty
428 * the menuProperty to set
430 public void setMenuProperty(String menuProperty) {
431 this._menuProperty = menuProperty;
435 * Get the _titleProperty.
437 * @return the _titleProperty
439 public String getTitleProperty() {
440 return _titleProperty;
444 * Set the _titleProperty.
446 * @param titleProperty
447 * the titleProperty to set
449 public void setTitleProperty(String titleProperty) {
450 _titleProperty = titleProperty;
454 * Get the _editDisabledProperty.
456 * @return the _editDisabledProperty
458 public String getEditDisabledProperty() {
459 return _editDisabledProperty;
463 * Set the _editDisabledProperty.
465 * @param _editDisabledProperty
466 * the _editDisabledProperty to set
468 public void setEditDisabledProperty(String _editDisabledProperty) {
469 this._editDisabledProperty = _editDisabledProperty;
473 * Get the toolProperty.
474 * @return the toolProperty
476 public String getToolProperty() {
477 return _toolProperty;
481 * Set the toolProperty.
482 * @param toolProperty the toolProperty to set
484 public void setToolProperty(final String toolProperty) {
485 _toolProperty = toolProperty;
489 * Get the leftMenuProperty.
490 * @return the leftMenuProperty
492 public String getLeftMenuProperty() {
493 return _leftMenuProperty;
497 * Set the leftMenuProperty.
498 * @param leftMenuProperty the leftMenuProperty to set
500 public void setLeftMenuProperty(final String leftMenuProperty) {
501 _leftMenuProperty = leftMenuProperty;