1 package org.splat.simer;
4 import java.io.FileNotFoundException;
5 import java.text.ParseException;
6 import java.text.SimpleDateFormat;
7 import java.util.HashSet;
8 import java.util.Iterator;
10 import java.util.ResourceBundle;
11 import java.util.Vector;
13 import org.hibernate.HibernateException;
14 import org.hibernate.Session;
15 import org.hibernate.Transaction;
16 import org.splat.kernel.InvalidPropertyException;
17 import org.splat.dal.bo.kernel.Relation;
18 import org.splat.dal.bo.kernel.User;
19 import org.splat.manox.Reader;
20 import org.splat.manox.Toolbox;
21 import org.splat.dal.dao.som.Database;
22 import org.splat.dal.bo.som.Document;
23 import org.splat.dal.bo.som.ProgressState;
24 import org.splat.service.PublicationService;
25 import org.splat.service.StepService;
26 import org.splat.service.technical.ProjectSettingsService;
27 import org.splat.service.technical.RepositoryService;
28 import org.splat.dal.bo.som.Publication;
29 import org.splat.som.Revision;
30 import org.splat.som.Step;
31 import org.splat.dal.bo.som.UsedByRelation;
32 import org.splat.dal.bo.som.UsesRelation;
34 public class VersionDocumentAction extends UploadBaseNextAction {
36 private String index = null; // Versioned document index
37 private List<Publication> usedby = null;
38 private String docusedby = null;
39 private String summary = null; // Summary of changes in the new version
40 private String docver = ""; // Version number extracted from the imported file, if exist
41 private String date = ""; // Date extracted from the imported file, if exist
42 private ProjectSettingsService _projectSettingsService;
43 private PublicationService _publicationService;
44 private StepService _stepService;
46 * Injected repository service.
48 private RepositoryService _repositoryService;
50 * Value of the menu property.
51 * It can be: none, create, open, study, knowledge, sysadmin, help.
53 private String _menuProperty;
56 * Value of the title bar property.
57 * It can be: study, knowledge.
59 private String _titleProperty;
62 * Property that indicates whether the current open study is editable or not.
63 * On the screen it looks like pen on the status icon, pop-up menu also can be called.
64 * It is necessary for correct building the title bar.
66 private String _editDisabledProperty = "false";
71 private static final long serialVersionUID = -5702264003232132168L;
73 // ==============================================================================================================================
75 // ==============================================================================================================================
77 public String doInitialize() {
78 // -----------------------------
80 setMenuProperty("study");
81 setTitleProperty("study");
82 setEditDisabledProperty("true");
83 initializationScreenContext(_menuProperty, _titleProperty, _editDisabledProperty);
85 Session connex = Database.getCurSession();
86 Transaction transax = connex.beginTransaction();
87 User user = getConnectedUser();
88 File updir = getRepositoryService().getDownloadDirectory(user);
89 File upfile = new File(updir.getPath() + "/" + filename);
91 mystudy = getOpenStudy();
93 Publication tag = mystudy.getSelectedStep().getDocument(
94 Integer.valueOf(index));
95 Document doc = tag.value();
96 deftype = doc.getType();
97 docname = doc.getTitle();
98 defuses = new Vector<Document>();
99 usedby = new Vector<Publication>();
101 Reader tool = Toolbox.getReader(upfile);
103 String fileref = tool.extractProperty("reference");
104 String filever = tool.extractProperty("version");
105 if (fileref != null && !doc.getReference().equals(fileref)) {
106 setErrorCode("reference.mismatch");
111 Revision.Format get = new Revision.Format(
112 getProjectSettings().getRevisionPattern());
113 Revision newver = get.parse(filever);
114 Revision oldver = new Revision(doc.getVersion());
115 if (!newver.isGraterThan(oldver))
116 throw new InvalidPropertyException("version");
117 if (newver.isMinor())
118 state = ProgressState.inWORK;
120 state = ProgressState.inDRAFT;
121 docver = newver.toString();
122 } catch (Exception e) {
123 setErrorCode("version.mismatch");
126 summary = tool.extractProperty("history");
127 date = tool.extractProperty("date");
129 ResourceBundle locale = ResourceBundle.getBundle("som",
130 ApplicationSettings.getCurrentLocale());
131 SimpleDateFormat check = new SimpleDateFormat(
132 locale.getString("date.format"));
135 } catch (ParseException e) {
136 setErrorCode("format.date");
142 setupDefaultUses(deftype);
143 // Add additional documents used by the current version
144 List<Relation> uses = doc.getRelations(UsesRelation.class);
145 for (Iterator<Relation> i = uses.iterator(); i.hasNext();) {
146 Document used = (Document) i.next().getTo();
147 if (!defuses.contains(used))
150 // Setup dependencies
151 List<Publication> relist = tag.getRelations(UsedByRelation.class);
152 for (Iterator<Publication> i = relist.iterator(); i.hasNext();) {
153 usedby.add(i.next());
159 public String doVersion() {
160 // -------------------------
161 setMenuProperty("study");
162 setTitleProperty("study");
163 setEditDisabledProperty("true");
164 initializationScreenContext(_menuProperty, _titleProperty, _editDisabledProperty);
166 if (action == ToDo.cancel)
169 Session connex = Database.getCurSession();
170 Transaction transax = connex.beginTransaction();
172 // Getting user inputs
173 mystudy = getOpenStudy();
174 User user = getConnectedUser();
175 Step step = mystudy.getSelectedStep();
176 File updir = getRepositoryService().getDownloadDirectory(user);
177 File upfile = new File(updir.getPath() + "/" + filename);
179 // Versioning of the document
180 Document.Properties dprop = new Document.Properties();
181 Publication current = step.getDocument(Integer.valueOf(index));
184 if (docver.length() == 0) { // Importation of a foreign document
185 next = getStepService().versionDocument(step, current, dprop.setAuthor(user)
186 .setDescription(summary));
187 updir = next.getSourceFile().asFile();
188 if (logger.isInfoEnabled())
189 logger.info("Moving \"" + upfile.getName() + "\" to \""
190 + updir.getPath() + "\".");
191 upfile.renameTo(updir);
193 getPublicationService().saveAs(next, state); // May throw FileNotFound if rename was not done
194 } catch (FileNotFoundException saverror) {
196 logger.info("Waiting for the file.");
197 upfile.renameTo(updir);
198 getPublicationService().saveAs(next, state); // Forget it if throw again FileNotFound
201 if (date.length() > 0) {
202 ResourceBundle locale = ResourceBundle.getBundle("som",
203 ApplicationSettings.getCurrentLocale());
204 SimpleDateFormat get = new SimpleDateFormat(
205 locale.getString("date.format"));
206 dprop.setDate(get.parse(date));
208 next = getStepService().versionDocument(step, current, dprop.setAuthor(user)
209 .setDescription(summary));
210 updir = next.getSourceFile().asFile();
211 if (logger.isInfoEnabled())
212 logger.info("Moving \"" + upfile.getName() + "\" to \""
213 + updir.getPath() + "\".");
214 upfile.renameTo(updir);
216 getPublicationService().saveAs(next, new Revision(docver));
217 } catch (FileNotFoundException saverror) {
219 logger.info("Waiting for the file.");
220 upfile.renameTo(updir);
221 getPublicationService().saveAs(next, state);
224 // TODO: Remove current document details from the contents of open study
226 // Creation of uses relations
227 if (docuses != null) {
228 String[] list = docuses.split(",");
229 for (int i = 0; i < list.length; i++) {
230 Integer index = Integer.valueOf(list[i].trim());
231 Publication used = getPublication(index);
232 next.addDependency(used);
235 // Outdating impacted document
236 HashSet<Integer> compatible = new HashSet<Integer>();
237 if (docusedby != null) {
238 String[] list = docusedby.split(",");
239 for (int i = 0; i < list.length; i++)
240 compatible.add(Integer.valueOf(list[i].trim()));
242 List<Publication> relist = current
243 .getRelations(UsedByRelation.class);
244 for (Iterator<Publication> i = relist.iterator(); i.hasNext();) {
245 Publication using = i.next();
246 if (!compatible.contains(using.getIndex()))
247 getPublicationService().outdate(using);
249 // Update of the open study
250 mystudy.setSelection(mystudy.getSelection()); // Rebuilds the presentation
251 // TODO: Look is an optimization is possible (for example by updating the presentation of versioned document)
255 } catch (FileNotFoundException error) {
256 logger.error("Reason:", error);
257 setErrorCode("import.file");
258 } catch (Exception error) {
259 logger.error("Reason:", error);
260 setErrorCode("internal");
262 if (transax != null && transax.isActive()) {
263 // Second try-catch as the rollback could fail as well
266 } catch (HibernateException backerror) {
267 logger.debug("Error rolling back transaction", backerror);
273 // ==============================================================================================================================
274 // Getters and setters
275 // ==============================================================================================================================
277 public String getDate() {
278 // ------------------------
282 public List<Publication> getDependencies() {
283 // -------------------------------------------
287 public String getDescription() {
288 // -------------------------------
292 public String getIndex() {
293 // -------------------------
297 public String getVersion() {
298 // ---------------------------
302 public void setDate(String date) {
303 // ---------------------------------
307 public void setDefaultDescription(String summary) {
308 // --------------------------------------------------
309 if (this.summary == null)
310 this.summary = summary;
313 public void setDescription(String summary) {
314 // -------------------------------------------
315 this.summary = summary;
318 public void setIndex(String index) {
319 // -----------------------------------
323 public void setUsedBy(String list) {
324 // -----------------------------------
325 this.docusedby = list;
328 public void setVersion(String value) {
329 // -------------------------------------
334 * Get project settings.
336 * @return Project settings service
338 private ProjectSettingsService getProjectSettings() {
339 return _projectSettingsService;
343 * Set project settings service.
345 * @param projectSettingsService
346 * project settings service
348 public void setProjectSettings(ProjectSettingsService projectSettingsService) {
349 _projectSettingsService = projectSettingsService;
353 * Get the publicationService.
355 * @return the publicationService
357 public PublicationService getPublicationService() {
358 return _publicationService;
362 * Set the publicationService.
364 * @param publicationService
365 * the publicationService to set
367 public void setPublicationService(PublicationService publicationService) {
368 _publicationService = publicationService;
372 * Get the stepService.
373 * @return the stepService
375 public StepService getStepService() {
380 * Set the stepService.
381 * @param stepService the stepService to set
383 public void setStepService(StepService stepService) {
384 _stepService = stepService;
388 * Get the repositoryService.
389 * @return the repositoryService
391 public RepositoryService getRepositoryService() {
392 return _repositoryService;
396 * Set the repositoryService.
397 * @param repositoryService the repositoryService to set
399 public void setRepositoryService(RepositoryService repositoryService) {
400 _repositoryService = repositoryService;
404 * Get the menuProperty.
405 * @return the menuProperty
407 public String getMenuProperty() {
408 return _menuProperty;
412 * Set the menuProperty.
413 * @param menuProperty the menuProperty to set
415 public void setMenuProperty(String menuProperty) {
416 this._menuProperty = menuProperty;
420 * Get the _titleProperty.
421 * @return the _titleProperty
423 public String getTitleProperty() {
424 return _titleProperty;
428 * Set the _titleProperty.
429 * @param _titleProperty the titleProperty to set
431 public void setTitleProperty(String titleProperty) {
432 _titleProperty = titleProperty;
436 * Get the _editDisabledProperty.
437 * @return the _editDisabledProperty
439 public String getEditDisabledProperty() {
440 return _editDisabledProperty;
444 * Set the _editDisabledProperty.
445 * @param _editDisabledProperty the _editDisabledProperty to set
447 public void setEditDisabledProperty(String _editDisabledProperty) {
448 this._editDisabledProperty = _editDisabledProperty;