1 package org.splat.simer;
4 import java.io.FileNotFoundException;
5 import java.text.ParseException;
6 import java.text.SimpleDateFormat;
8 import java.util.HashSet;
9 import java.util.Iterator;
10 import java.util.List;
11 import java.util.ResourceBundle;
12 import java.util.Vector;
14 import org.hibernate.HibernateException;
15 import org.hibernate.Session;
16 import org.hibernate.Transaction;
17 import org.splat.kernel.InvalidPropertyException;
18 import org.splat.dal.bo.kernel.Relation;
19 import org.splat.dal.bo.kernel.User;
20 import org.splat.manox.Reader;
21 import org.splat.manox.Toolbox;
22 import org.splat.dal.dao.som.Database;
23 import org.splat.dal.bo.som.Document;
24 import org.splat.dal.bo.som.ProgressState;
25 import org.splat.service.PublicationService;
26 import org.splat.service.StepService;
27 import org.splat.service.technical.ProjectSettingsService;
28 import org.splat.service.technical.RepositoryService;
29 import org.splat.dal.bo.som.Publication;
30 import org.splat.som.Revision;
31 import org.splat.som.Step;
32 import org.splat.dal.bo.som.UsedByRelation;
33 import org.splat.dal.bo.som.UsesRelation;
35 public class VersionDocumentAction extends UploadBaseNextAction {
37 private String index = null; // Versioned document index
38 private List<Publication> usedby = null;
39 private long[] docusedby = null;
40 private String summary = null; // Summary of changes in the new version
41 private String docver = ""; // Version number extracted from the imported file, if exist
42 private String date = ""; // Date extracted from the imported file, if exist
43 private ProjectSettingsService _projectSettingsService;
44 private PublicationService _publicationService;
45 private StepService _stepService;
47 * Injected repository service.
49 private RepositoryService _repositoryService;
51 * Value of the menu property.
52 * It can be: none, create, open, study, knowledge, sysadmin, help.
54 private String _menuProperty;
57 * Value of the title bar property.
58 * It can be: study, knowledge.
60 private String _titleProperty;
63 * Property that indicates whether the current open study is editable or not.
64 * On the screen it looks like pen on the status icon, pop-up menu also can be called.
65 * It is necessary for correct building the title bar.
67 private String _editDisabledProperty = "false";
72 private static final long serialVersionUID = -5702264003232132168L;
74 // ==============================================================================================================================
76 // ==============================================================================================================================
78 public String doInitialize() {
79 // -----------------------------
81 setMenuProperty("study");
82 setTitleProperty("study");
83 setEditDisabledProperty("true");
84 initializationScreenContext(_menuProperty, _titleProperty, _editDisabledProperty);
86 Session connex = Database.getCurSession();
87 Transaction transax = connex.beginTransaction();
88 User user = getConnectedUser();
89 File updir = getRepositoryService().getDownloadDirectory(user);
90 File upfile = new File(updir.getPath() + "/" + filename);
92 mystudy = getOpenStudy();
94 Publication tag = mystudy.getSelectedStep().getDocument(
95 Integer.valueOf(index));
96 Document doc = tag.value();
97 deftype = doc.getType();
98 docname = doc.getTitle();
99 defuses = new Vector<Document>();
100 usedby = new Vector<Publication>();
102 Reader tool = Toolbox.getReader(upfile);
104 String fileref = tool.extractProperty("reference");
105 String filever = tool.extractProperty("version");
106 if (fileref != null && !doc.getReference().equals(fileref)) {
107 setErrorCode("reference.mismatch");
112 Revision.Format get = new Revision.Format(
113 getProjectSettings().getRevisionPattern());
114 Revision newver = get.parse(filever);
115 Revision oldver = new Revision(doc.getVersion());
116 if (!newver.isGraterThan(oldver))
117 throw new InvalidPropertyException("version");
118 if (newver.isMinor())
119 state = ProgressState.inWORK;
121 state = ProgressState.inDRAFT;
122 docver = newver.toString();
123 } catch (Exception e) {
124 setErrorCode("version.mismatch");
127 summary = tool.extractProperty("history");
128 date = tool.extractProperty("date");
130 ResourceBundle locale = ResourceBundle.getBundle("som",
131 ApplicationSettings.getCurrentLocale());
132 SimpleDateFormat check = new SimpleDateFormat(
133 locale.getString("date.format"));
136 } catch (ParseException e) {
137 setErrorCode("format.date");
143 setupDefaultUses(deftype);
144 // Add additional documents used by the current version
145 List<Relation> uses = doc.getRelations(UsesRelation.class);
146 for (Iterator<Relation> i = uses.iterator(); i.hasNext();) {
147 Document used = (Document) i.next().getTo();
148 if (!defuses.contains(used))
151 // Setup dependencies
152 List<Publication> relist = tag.getRelations(UsedByRelation.class);
153 for (Iterator<Publication> i = relist.iterator(); i.hasNext();) {
154 usedby.add(i.next());
160 public String doVersion() {
161 // -------------------------
162 setMenuProperty("study");
163 setTitleProperty("study");
164 setEditDisabledProperty("true");
165 initializationScreenContext(_menuProperty, _titleProperty, _editDisabledProperty);
167 if (action == ToDo.cancel)
171 // Getting user inputs
172 mystudy = getOpenStudy();
173 User user = getConnectedUser();
174 Step step = mystudy.getSelectedStep();
175 // List<Step> steps = mystudy.getInvolvedSteps();
177 if (date.length() > 0) {
178 ResourceBundle locale = ResourceBundle.getBundle("som",
179 ApplicationSettings.getCurrentLocale());
180 SimpleDateFormat get = new SimpleDateFormat(
181 locale.getString("date.format"));
182 aDate = get.parse(date);
185 String[] listDocuses = null;
186 if (docuses != null) {
187 listDocuses = docuses.split(",");
189 getPublicationService().versionDocument(step, user, filename, Integer.valueOf(index), docver,
190 summary, state, aDate, listDocuses, docusedby/*, steps*/);
191 /* File updir = getRepositoryService().getDownloadDirectory(user);
192 File upfile = new File(updir.getPath() + "/" + filename);
194 // Versioning of the document
195 Document.Properties dprop = new Document.Properties();
196 Publication current = step.getDocument(Integer.valueOf(index));
199 if (docver.length() == 0) { // Importation of a foreign document
200 next = getStepService().versionDocument(step, current, dprop.setAuthor(user)
201 .setDescription(summary));
202 updir = next.getSourceFile().asFile();
203 if (logger.isInfoEnabled())
204 logger.info("Moving \"" + upfile.getName() + "\" to \""
205 + updir.getPath() + "\".");
206 upfile.renameTo(updir);
208 getPublicationService().saveAs(next, state); // May throw FileNotFound if rename was not done
209 } catch (FileNotFoundException saverror) {
211 logger.info("Waiting for the file.");
212 upfile.renameTo(updir);
213 getPublicationService().saveAs(next, state); // Forget it if throw again FileNotFound
216 if (date.length() > 0) {
217 ResourceBundle locale = ResourceBundle.getBundle("som",
218 ApplicationSettings.getCurrentLocale());
219 SimpleDateFormat get = new SimpleDateFormat(
220 locale.getString("date.format"));
221 dprop.setDate(get.parse(date));
223 next = getStepService().versionDocument(step, current, dprop.setAuthor(user)
224 .setDescription(summary));
225 updir = next.getSourceFile().asFile();
226 if (logger.isInfoEnabled())
227 logger.info("Moving \"" + upfile.getName() + "\" to \""
228 + updir.getPath() + "\".");
229 upfile.renameTo(updir);
231 getPublicationService().saveAs(next, new Revision(docver));
232 } catch (FileNotFoundException saverror) {
234 logger.info("Waiting for the file.");
235 upfile.renameTo(updir);
236 getPublicationService().saveAs(next, state);
239 // TODO: Remove current document details from the contents of open study
241 // Creation of uses relations
242 if (docuses != null) {
243 String[] list = docuses.split(",");
244 for (int i = 0; i < list.length; i++) {
245 Integer index = Integer.valueOf(list[i].trim());
246 Publication used = getPublication(index);
247 next.addDependency(used);
250 // Outdating impacted document
251 HashSet<Integer> compatible = new HashSet<Integer>();
252 if (docusedby != null) {
253 String[] list = docusedby.split(",");
254 for (int i = 0; i < list.length; i++)
255 compatible.add(Integer.valueOf(list[i].trim()));
257 List<Publication> relist = current
258 .getRelations(UsedByRelation.class);
259 for (Iterator<Publication> i = relist.iterator(); i.hasNext();) {
260 Publication using = i.next();
261 if (!compatible.contains(using.getIndex()))
262 getPublicationService().outdate(using);
265 // Update of the open study
266 mystudy.setSelection(mystudy.getSelection()); // Rebuilds the presentation
267 // TODO: Look is an optimization is possible (for example by updating the presentation of versioned document)
270 } catch (FileNotFoundException error) {
271 logger.error("Reason:", error);
272 setErrorCode("import.file");
273 } catch (Exception error) {
274 logger.error("Reason:", error);
275 setErrorCode("internal");
280 // ==============================================================================================================================
281 // Getters and setters
282 // ==============================================================================================================================
284 public String getDate() {
285 // ------------------------
289 public List<Publication> getDependencies() {
290 // -------------------------------------------
294 public String getDescription() {
295 // -------------------------------
299 public String getIndex() {
300 // -------------------------
304 public String getVersion() {
305 // ---------------------------
309 public void setDate(String date) {
310 // ---------------------------------
314 public void setDefaultDescription(String summary) {
315 // --------------------------------------------------
316 if (this.summary == null)
317 this.summary = summary;
320 public void setDescription(String summary) {
321 // -------------------------------------------
322 this.summary = summary;
325 public void setIndex(String index) {
326 // -----------------------------------
330 public void setUsedBy(long[] list) {
331 // -----------------------------------
332 this.docusedby = list;
335 public void setVersion(String value) {
336 // -------------------------------------
341 * Get project settings.
343 * @return Project settings service
345 private ProjectSettingsService getProjectSettings() {
346 return _projectSettingsService;
350 * Set project settings service.
352 * @param projectSettingsService
353 * project settings service
355 public void setProjectSettings(ProjectSettingsService projectSettingsService) {
356 _projectSettingsService = projectSettingsService;
360 * Get the publicationService.
362 * @return the publicationService
364 public PublicationService getPublicationService() {
365 return _publicationService;
369 * Set the publicationService.
371 * @param publicationService
372 * the publicationService to set
374 public void setPublicationService(PublicationService publicationService) {
375 _publicationService = publicationService;
379 * Get the stepService.
380 * @return the stepService
382 public StepService getStepService() {
387 * Set the stepService.
388 * @param stepService the stepService to set
390 public void setStepService(StepService stepService) {
391 _stepService = stepService;
395 * Get the repositoryService.
396 * @return the repositoryService
398 public RepositoryService getRepositoryService() {
399 return _repositoryService;
403 * Set the repositoryService.
404 * @param repositoryService the repositoryService to set
406 public void setRepositoryService(RepositoryService repositoryService) {
407 _repositoryService = repositoryService;
411 * Get the menuProperty.
412 * @return the menuProperty
414 public String getMenuProperty() {
415 return _menuProperty;
419 * Set the menuProperty.
420 * @param menuProperty the menuProperty to set
422 public void setMenuProperty(String menuProperty) {
423 this._menuProperty = menuProperty;
427 * Get the _titleProperty.
428 * @return the _titleProperty
430 public String getTitleProperty() {
431 return _titleProperty;
435 * Set the _titleProperty.
436 * @param _titleProperty the titleProperty to set
438 public void setTitleProperty(String titleProperty) {
439 _titleProperty = titleProperty;
443 * Get the _editDisabledProperty.
444 * @return the _editDisabledProperty
446 public String getEditDisabledProperty() {
447 return _editDisabledProperty;
451 * Set the _editDisabledProperty.
452 * @param _editDisabledProperty the _editDisabledProperty to set
454 public void setEditDisabledProperty(String _editDisabledProperty) {
455 this._editDisabledProperty = _editDisabledProperty;