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.technical.ProjectSettingsService;
26 import org.splat.dal.bo.som.Publication;
27 import org.splat.som.Revision;
28 import org.splat.som.Step;
29 import org.splat.dal.bo.som.UsedByRelation;
30 import org.splat.dal.bo.som.UsesRelation;
32 public class VersionDocumentAction extends UploadBaseNextAction {
34 private String index = null; // Versioned document index
35 private List<Publication> usedby = null;
36 private String docusedby = null;
37 private String summary = null; // Summary of changes in the new version
38 private String docver = ""; // Version number extracted from the imported file, if exist
39 private String date = ""; // Date extracted from the imported file, if exist
40 private ProjectSettingsService _projectSettingsService;
41 private PublicationService _publicationService;
43 private static final long serialVersionUID = -5702264003232132168L;
45 // ==============================================================================================================================
47 // ==============================================================================================================================
49 public String doInitialize() {
50 // -----------------------------
51 Session connex = Database.getSession();
52 Transaction transax = connex.beginTransaction();
53 User user = getConnectedUser();
54 File updir = Database.getDownloadDirectory(user);
55 File upfile = new File(updir.getPath() + "/" + filename);
57 mystudy = getOpenStudy();
59 Publication tag = mystudy.getSelectedStep().getDocument(
60 Integer.valueOf(index));
61 Document doc = tag.value();
62 deftype = doc.getType();
63 docname = doc.getTitle();
64 defuses = new Vector<Document>();
65 usedby = new Vector<Publication>();
67 Reader tool = Toolbox.getReader(upfile);
69 String fileref = tool.extractProperty("reference");
70 String filever = tool.extractProperty("version");
71 if (fileref != null && !doc.getReference().equals(fileref)) {
72 setErrorCode("reference.mismatch");
77 Revision.Format get = new Revision.Format(
78 getProjectSettings().getRevisionPattern());
79 Revision newver = get.parse(filever);
80 Revision oldver = new Revision(doc.getVersion());
81 if (!newver.isGraterThan(oldver))
82 throw new InvalidPropertyException("version");
84 state = ProgressState.inWORK;
86 state = ProgressState.inDRAFT;
87 docver = newver.toString();
88 } catch (Exception e) {
89 setErrorCode("version.mismatch");
92 summary = tool.extractProperty("history");
93 date = tool.extractProperty("date");
95 ResourceBundle locale = ResourceBundle.getBundle("som",
96 ApplicationSettings.getCurrentLocale());
97 SimpleDateFormat check = new SimpleDateFormat(
98 locale.getString("date.format"));
101 } catch (ParseException e) {
102 setErrorCode("format.date");
108 setupDefaultUses(deftype);
109 // Add additional documents used by the current version
110 List<Relation> uses = doc.getRelations(UsesRelation.class);
111 for (Iterator<Relation> i = uses.iterator(); i.hasNext();) {
112 Document used = (Document) i.next().getTo();
113 if (!defuses.contains(used))
116 // Setup dependencies
117 List<Publication> relist = tag.getRelations(UsedByRelation.class);
118 for (Iterator<Publication> i = relist.iterator(); i.hasNext();) {
119 usedby.add(i.next());
125 public String doVersion() {
126 // -------------------------
127 if (action == ToDo.cancel)
130 Session connex = Database.getSession();
131 Transaction transax = connex.beginTransaction();
133 // Getting user inputs
134 mystudy = getOpenStudy();
135 User user = getConnectedUser();
136 Step step = mystudy.getSelectedStep();
137 File updir = Database.getDownloadDirectory(user);
138 File upfile = new File(updir.getPath() + "/" + filename);
140 // Versioning of the document
141 Document.Properties dprop = new Document.Properties();
142 Publication current = step.getDocument(Integer.valueOf(index));
145 if (docver.length() == 0) { // Importation of a foreign document
146 next = step.versionDocument(current, dprop.setAuthor(user)
147 .setDescription(summary));
148 updir = next.getSourceFile().asFile();
149 if (logger.isInfoEnabled())
150 logger.info("Moving \"" + upfile.getName() + "\" to \""
151 + updir.getPath() + "\".");
152 upfile.renameTo(updir);
154 getPublicationService().saveAs(next, state); // May throw FileNotFound if rename was not done
155 } catch (FileNotFoundException saverror) {
157 logger.info("Waiting for the file.");
158 upfile.renameTo(updir);
159 getPublicationService().saveAs(next, state); // Forget it if throw again FileNotFound
162 if (date.length() > 0) {
163 ResourceBundle locale = ResourceBundle.getBundle("som",
164 ApplicationSettings.getCurrentLocale());
165 SimpleDateFormat get = new SimpleDateFormat(
166 locale.getString("date.format"));
167 dprop.setDate(get.parse(date));
169 next = step.versionDocument(current, dprop.setAuthor(user)
170 .setDescription(summary));
171 updir = next.getSourceFile().asFile();
172 if (logger.isInfoEnabled())
173 logger.info("Moving \"" + upfile.getName() + "\" to \""
174 + updir.getPath() + "\".");
175 upfile.renameTo(updir);
177 getPublicationService().saveAs(next, new Revision(docver));
178 } catch (FileNotFoundException saverror) {
180 logger.info("Waiting for the file.");
181 upfile.renameTo(updir);
182 getPublicationService().saveAs(next, state);
185 // TODO: Remove current document details from the contents of open study
187 // Creation of uses relations
188 if (docuses != null) {
189 String[] list = docuses.split(",");
190 for (int i = 0; i < list.length; i++) {
191 Integer index = Integer.valueOf(list[i].trim());
192 Publication used = getPublication(index);
193 next.addDependency(used);
196 // Outdating impacted document
197 HashSet<Integer> compatible = new HashSet<Integer>();
198 if (docusedby != null) {
199 String[] list = docusedby.split(",");
200 for (int i = 0; i < list.length; i++)
201 compatible.add(Integer.valueOf(list[i].trim()));
203 List<Publication> relist = current
204 .getRelations(UsedByRelation.class);
205 for (Iterator<Publication> i = relist.iterator(); i.hasNext();) {
206 Publication using = i.next();
207 if (!compatible.contains(using.getIndex()))
210 // Update of the open study
211 mystudy.setSelection(mystudy.getSelection()); // Rebuilds the presentation
212 // TODO: Look is an optimization is possible (for example by updating the presentation of versioned document)
216 } catch (FileNotFoundException error) {
217 logger.error("Reason:", error);
218 setErrorCode("import.file");
219 } catch (Exception error) {
220 logger.error("Reason:", error);
221 setErrorCode("internal");
223 if (transax != null && transax.isActive()) {
224 // Second try-catch as the rollback could fail as well
227 } catch (HibernateException backerror) {
228 logger.debug("Error rolling back transaction", backerror);
234 // ==============================================================================================================================
235 // Getters and setters
236 // ==============================================================================================================================
238 public String getDate() {
239 // ------------------------
243 public List<Publication> getDependencies() {
244 // -------------------------------------------
248 public String getDescription() {
249 // -------------------------------
253 public String getIndex() {
254 // -------------------------
258 public String getVersion() {
259 // ---------------------------
263 public void setDate(String date) {
264 // ---------------------------------
268 public void setDefaultDescription(String summary) {
269 // --------------------------------------------------
270 if (this.summary == null)
271 this.summary = summary;
274 public void setDescription(String summary) {
275 // -------------------------------------------
276 this.summary = summary;
279 public void setIndex(String index) {
280 // -----------------------------------
284 public void setUsedBy(String list) {
285 // -----------------------------------
286 this.docusedby = list;
289 public void setVersion(String value) {
290 // -------------------------------------
295 * Get project settings.
297 * @return Project settings service
299 private ProjectSettingsService getProjectSettings() {
300 return _projectSettingsService;
304 * Set project settings service.
306 * @param projectSettingsService
307 * project settings service
309 public void setProjectSettings(ProjectSettingsService projectSettingsService) {
310 _projectSettingsService = projectSettingsService;
314 * Get the publicationService.
316 * @return the publicationService
318 public PublicationService getPublicationService() {
319 return _publicationService;
323 * Set the publicationService.
325 * @param publicationService
326 * the publicationService to set
328 public void setPublicationService(PublicationService publicationService) {
329 _publicationService = publicationService;