Salome HOME
Mapping is fixed. Relation in the mapping is inherited from Persistent now. Versionin...
[tools/siman.git] / Workspace / Siman / src / org / splat / simer / VersionDocumentAction.java
1 package org.splat.simer;
2
3 import java.io.File;
4 import java.io.FileNotFoundException;
5 import java.text.ParseException;
6 import java.text.SimpleDateFormat;
7 import java.util.Date;
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;
13
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;
34
35 public class VersionDocumentAction extends UploadBaseNextAction {
36
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;
46         /**
47          * Injected repository service.
48          */
49         private RepositoryService _repositoryService;
50         /**
51          * Value of the menu property. 
52          * It can be: none, create, open, study, knowledge, sysadmin, help.
53          */
54         private String _menuProperty;
55         
56         /**
57          * Value of the title bar property. 
58          * It can be: study, knowledge.
59          */
60         private String _titleProperty;
61         
62         /**
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.
66          */
67         private String _editDisabledProperty = "false";
68
69         /**
70          * Serial version ID.
71          */
72         private static final long serialVersionUID = -5702264003232132168L;
73
74         // ==============================================================================================================================
75         // Action methods
76         // ==============================================================================================================================
77
78         public String doInitialize() {
79                 // -----------------------------
80                 
81                 setMenuProperty("study");
82                 setTitleProperty("study");
83                 setEditDisabledProperty("true");
84         initializationScreenContext(_menuProperty, _titleProperty, _editDisabledProperty);
85             
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);
91
92                 mystudy = getOpenStudy();
93
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>();
101
102                 Reader tool = Toolbox.getReader(upfile);
103                 if (tool != null) {
104                         String fileref = tool.extractProperty("reference");
105                         String filever = tool.extractProperty("version");
106                         if (fileref != null && !doc.getReference().equals(fileref)) {
107                                 setErrorCode("reference.mismatch");
108                                 return ERROR;
109                         }
110                         if (filever != null)
111                                 try {
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;
120                                         else
121                                                 state = ProgressState.inDRAFT;
122                                         docver = newver.toString();
123                                 } catch (Exception e) {
124                                         setErrorCode("version.mismatch");
125                                         return ERROR;
126                                 }
127                         summary = tool.extractProperty("history");
128                         date = tool.extractProperty("date");
129                         if (date != null) {
130                                 ResourceBundle locale = ResourceBundle.getBundle("som",
131                                                 ApplicationSettings.getCurrentLocale());
132                                 SimpleDateFormat check = new SimpleDateFormat(
133                                                 locale.getString("date.format"));
134                                 try {
135                                         check.parse(date);
136                                 } catch (ParseException e) {
137                                         setErrorCode("format.date");
138                                         return ERROR;
139                                 }
140                         } else
141                                 date = "";
142                 }
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))
149                                 defuses.add(used);
150                 }
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());
155                 }
156                 transax.commit();
157                 return SUCCESS;
158         }
159
160         public String doVersion() {
161                 // -------------------------
162                 setMenuProperty("study");
163                 setTitleProperty("study");
164                 setEditDisabledProperty("true");
165         initializationScreenContext(_menuProperty, _titleProperty, _editDisabledProperty);
166             
167                 if (action == ToDo.cancel)
168                         return "cancel";
169
170                 try {
171                         // Getting user inputs
172                         mystudy = getOpenStudy();
173                         User user = getConnectedUser();
174                         Step step = mystudy.getSelectedStep();
175 //                      List<Step> steps = mystudy.getInvolvedSteps();
176                         Date aDate = null;
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);
183                         }
184                         
185                         String[] listDocuses = null;
186                         if (docuses != null) {
187                                 listDocuses = docuses.split(",");
188                         }
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);
193
194                         // Versioning of the document
195                         Document.Properties dprop = new Document.Properties();
196                         Publication current = step.getDocument(Integer.valueOf(index));
197                         Publication next;
198
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);
207                                 try {
208                                         getPublicationService().saveAs(next, state); // May throw FileNotFound if rename was not done
209                                 } catch (FileNotFoundException saverror) {
210                                         Thread.sleep(1000);
211                                         logger.info("Waiting for the file.");
212                                         upfile.renameTo(updir);
213                                         getPublicationService().saveAs(next, state); // Forget it if throw again FileNotFound
214                                 }
215                         } else {
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));
222                                 }
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);
230                                 try {
231                                         getPublicationService().saveAs(next, new Revision(docver));
232                                 } catch (FileNotFoundException saverror) {
233                                         Thread.sleep(1000);
234                                         logger.info("Waiting for the file.");
235                                         upfile.renameTo(updir);
236                                         getPublicationService().saveAs(next, state);
237                                 }
238                         }
239                         // TODO: Remove current document details from the contents of open study
240
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);
248                                 }
249                         }
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()));
256                         }
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);
263                         }
264 */
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)
268
269                         return SUCCESS;
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");
276                 }
277                 return ERROR;
278         }
279
280         // ==============================================================================================================================
281         // Getters and setters
282         // ==============================================================================================================================
283
284         public String getDate() {
285                 // ------------------------
286                 return date;
287         }
288
289         public List<Publication> getDependencies() {
290                 // -------------------------------------------
291                 return usedby;
292         }
293
294         public String getDescription() {
295                 // -------------------------------
296                 return summary;
297         }
298
299         public String getIndex() {
300                 // -------------------------
301                 return index;
302         }
303
304         public String getVersion() {
305                 // ---------------------------
306                 return docver;
307         }
308
309         public void setDate(String date) {
310                 // ---------------------------------
311                 this.date = date;
312         }
313
314         public void setDefaultDescription(String summary) {
315                 // --------------------------------------------------
316                 if (this.summary == null)
317                         this.summary = summary;
318         }
319
320         public void setDescription(String summary) {
321                 // -------------------------------------------
322                 this.summary = summary;
323         }
324
325         public void setIndex(String index) {
326                 // -----------------------------------
327                 this.index = index;
328         }
329
330         public void setUsedBy(long[] list) {
331                 // -----------------------------------
332                 this.docusedby = list;
333         }
334
335         public void setVersion(String value) {
336                 // -------------------------------------
337                 this.docver = value;
338         }
339
340         /**
341          * Get project settings.
342          * 
343          * @return Project settings service
344          */
345         private ProjectSettingsService getProjectSettings() {
346                 return _projectSettingsService;
347         }
348
349         /**
350          * Set project settings service.
351          * 
352          * @param projectSettingsService
353          *            project settings service
354          */
355         public void setProjectSettings(ProjectSettingsService projectSettingsService) {
356                 _projectSettingsService = projectSettingsService;
357         }
358
359         /**
360          * Get the publicationService.
361          * 
362          * @return the publicationService
363          */
364         public PublicationService getPublicationService() {
365                 return _publicationService;
366         }
367
368         /**
369          * Set the publicationService.
370          * 
371          * @param publicationService
372          *            the publicationService to set
373          */
374         public void setPublicationService(PublicationService publicationService) {
375                 _publicationService = publicationService;
376         }
377
378         /**
379          * Get the stepService.
380          * @return the stepService
381          */
382         public StepService getStepService() {
383                 return _stepService;
384         }
385
386         /**
387          * Set the stepService.
388          * @param stepService the stepService to set
389          */
390         public void setStepService(StepService stepService) {
391                 _stepService = stepService;
392         }
393
394         /**
395          * Get the repositoryService.
396          * @return the repositoryService
397          */
398         public RepositoryService getRepositoryService() {
399                 return _repositoryService;
400         }
401
402         /**
403          * Set the repositoryService.
404          * @param repositoryService the repositoryService to set
405          */
406         public void setRepositoryService(RepositoryService repositoryService) {
407                 _repositoryService = repositoryService;
408         }
409         
410         /**
411          * Get the menuProperty.
412          * @return the menuProperty
413          */
414         public String getMenuProperty() {
415                 return _menuProperty;
416         }
417
418         /**
419          * Set the menuProperty.
420          * @param menuProperty the menuProperty to set
421          */
422         public void setMenuProperty(String menuProperty) {
423                 this._menuProperty = menuProperty;
424         }
425         
426         /**
427          * Get the _titleProperty.
428          * @return the _titleProperty
429          */
430         public String getTitleProperty() {
431                 return _titleProperty;
432         }
433
434         /**
435          * Set the _titleProperty.
436          * @param _titleProperty the titleProperty to set
437          */
438         public void setTitleProperty(String titleProperty) {
439                 _titleProperty = titleProperty;
440         }
441
442         /**
443          * Get the _editDisabledProperty.
444          * @return the _editDisabledProperty
445          */
446         public String getEditDisabledProperty() {
447                 return _editDisabledProperty;
448         }
449
450         /**
451          * Set the _editDisabledProperty.
452          * @param _editDisabledProperty the _editDisabledProperty to set
453          */
454         public void setEditDisabledProperty(String _editDisabledProperty) {
455                 this._editDisabledProperty = _editDisabledProperty;
456         }
457 }