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.kernel.Relation;
18 import org.splat.kernel.User;
19 import org.splat.manox.Reader;
20 import org.splat.manox.Toolbox;
21 import org.splat.som.Database;
22 import org.splat.som.Document;
23 import org.splat.som.ProgressState;
24 import org.splat.som.ProjectSettings;
25 import org.splat.som.Publication;
26 import org.splat.som.Revision;
27 import org.splat.som.Step;
28 import org.splat.som.UsedByRelation;
29 import org.splat.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
41 private static final long serialVersionUID = -5702264003232132168L;
43 // ==============================================================================================================================
45 // ==============================================================================================================================
47 public String doInitialize () {
48 // -----------------------------
49 Session connex = Database.getSession();
50 Transaction transax = connex.beginTransaction();
51 User user = getConnectedUser();
52 File updir = Database.getDownloadDirectory(user);
53 File upfile = new File(updir.getPath() + "/" + filename);
55 mystudy = getOpenStudy();
57 Publication tag = mystudy.getSelectedStep().getDocument(Integer.valueOf(index));
58 Document doc = tag.value();
59 deftype = doc.getType();
60 docname = doc.getTitle();
61 defuses = new Vector<Document>();
62 usedby = new Vector<Publication>();
64 Reader tool = Toolbox.getReader(upfile);
66 String fileref = tool.extractProperty("reference");
67 String filever = tool.extractProperty("version");
68 if (fileref != null && !doc.getReference().equals(fileref)) {
69 setErrorCode("reference.mismatch");
72 if (filever != null) try {
73 Revision.Format get = new Revision.Format(ProjectSettings.getRevisionPattern());
74 Revision newver = get.parse(filever);
75 Revision oldver = new Revision(doc.getVersion());
76 if (!newver.isGraterThan(oldver)) throw new InvalidPropertyException("version");
77 if ( newver.isMinor() ) state = ProgressState.inWORK;
78 else state = ProgressState.inDRAFT;
79 docver = newver.toString();
80 } catch (Exception e) {
81 setErrorCode("version.mismatch");
84 summary = tool.extractProperty("history");
85 date = tool.extractProperty("date");
87 ResourceBundle locale = ResourceBundle.getBundle("som", ApplicationSettings.getCurrentLocale());
88 SimpleDateFormat check = new SimpleDateFormat(locale.getString("date.format"));
91 } catch (ParseException e) {
92 setErrorCode("format.date");
97 setupDefaultUses(deftype);
98 // Add additional documents used by the current version
99 List<Relation> uses = doc.getRelations(UsesRelation.class);
100 for (Iterator<Relation> i=uses.iterator(); i.hasNext();) {
101 Document used = (Document)i.next().getTo();
102 if (!defuses.contains(used)) defuses.add(used);
104 // Setup dependencies
105 List<Publication> relist = tag.getRelations(UsedByRelation.class);
106 for (Iterator<Publication> i=relist.iterator(); i.hasNext();) {
107 usedby.add(i.next());
113 public String doVersion () {
114 // -------------------------
115 if (action == ToDo.cancel) return "cancel";
117 Session connex = Database.getSession();
118 Transaction transax = connex.beginTransaction();
120 // Getting user inputs
121 mystudy = getOpenStudy();
122 User user = getConnectedUser();
123 Step step = mystudy.getSelectedStep();
124 File updir = Database.getDownloadDirectory(user);
125 File upfile = new File(updir.getPath() + "/" + filename);
127 // Versioning of the document
128 Document.Properties dprop = new Document.Properties();
129 Publication current = step.getDocument(Integer.valueOf(index));
132 if (docver.length() == 0) { // Importation of a foreign document
133 next = step.versionDocument(current, dprop.setAuthor(user).setDescription(summary));
134 updir = next.getSourceFile().asFile();
135 if (logger.isInfoEnabled()) logger.info("Moving \"" + upfile.getName() + "\" to \"" + updir.getPath() + "\".");
136 upfile.renameTo(updir);
138 next.saveAs(state); // May throw FileNotFound if rename was not done
139 } catch (FileNotFoundException saverror) {
141 logger.info("Waiting for the file.");
142 upfile.renameTo(updir);
143 next.saveAs(state); // Forget it if throw again FileNotFound
146 if (date.length() > 0) {
147 ResourceBundle locale = ResourceBundle.getBundle("som", ApplicationSettings.getCurrentLocale());
148 SimpleDateFormat get = new SimpleDateFormat(locale.getString("date.format"));
149 dprop.setDate(get.parse(date));
151 next = step.versionDocument(current, dprop.setAuthor(user).setDescription(summary));
152 updir = next.getSourceFile().asFile();
153 if (logger.isInfoEnabled()) logger.info("Moving \"" + upfile.getName() + "\" to \"" + updir.getPath() + "\".");
154 upfile.renameTo(updir);
156 next.saveAs(new Revision(docver));
157 } catch (FileNotFoundException saverror) {
159 logger.info("Waiting for the file.");
160 upfile.renameTo(updir);
164 //TODO: Remove current document details from the contents of open study
166 // Creation of uses relations
167 if (docuses != null) {
168 String[] list = docuses.split(",");
169 for (int i=0; i<list.length; i++) {
170 Integer index = Integer.valueOf(list[i].trim());
171 Publication used = getPublication(index);
172 next.addDependency(used);
175 // Outdating impacted document
176 HashSet<Integer> compatible = new HashSet<Integer>();
177 if (docusedby != null) {
178 String[] list = docusedby.split(",");
179 for (int i=0; i<list.length; i++) compatible.add(Integer.valueOf(list[i].trim()));
181 List<Publication> relist = current.getRelations(UsedByRelation.class);
182 for (Iterator<Publication> i=relist.iterator(); i.hasNext();) {
183 Publication using = i.next();
184 if (!compatible.contains(using.getIndex())) using.outdate();
186 // Update of the open study
187 mystudy.setSelection(mystudy.getSelection()); // Rebuilds the presentation
188 //TODO: Look is an optimization is possible (for example by updating the presentation of versioned document)
193 catch (FileNotFoundException error) {
194 logger.error("Reason:", error);
195 setErrorCode("import.file");
197 catch (Exception error) {
198 logger.error("Reason:", error);
199 setErrorCode("internal");
201 if (transax != null && transax.isActive()) {
202 // Second try-catch as the rollback could fail as well
205 } catch (HibernateException backerror) {
206 logger.debug("Error rolling back transaction", backerror);
211 // ==============================================================================================================================
212 // Getters and setters
213 // ==============================================================================================================================
215 public String getDate () {
216 // ------------------------
219 public List<Publication> getDependencies () {
220 // -------------------------------------------
223 public String getDescription () {
224 // -------------------------------
227 public String getIndex () {
228 // -------------------------
231 public String getVersion () {
232 // ---------------------------
236 public void setDate (String date) {
237 // ---------------------------------
240 public void setDefaultDescription (String summary) {
241 // --------------------------------------------------
242 if (this.summary == null) this.summary = summary;
244 public void setDescription (String summary) {
245 // -------------------------------------------
246 this.summary = summary;
248 public void setIndex (String index) {
249 // -----------------------------------
252 public void setUsedBy (String list) {
253 // -----------------------------------
254 this.docusedby = list;
256 public void setVersion (String value) {
257 // -------------------------------------