Salome HOME
Copyrights update 2015.
[tools/siman.git] / Workspace / Siman-Common / src / org / splat / dal / bo / som / File.java
1 package org.splat.dal.bo.som;
2 /**
3  * Class of meta files representing physical files under the control of Study Manager.
4  * Typically, the files represented by this class are source files of Documents and exports in different formats.
5  * The path of such files is relative to the vault of the repository of Study Manager.
6  * When creating a Document, as the source file is produced by the caller which creates the Document, the corresponding
7  * physical file may not exist at instantiation time.
8  * 
9  * @author    Daniel Brunier-Coulin
10  * @copyright OPEN CASCADE 2012-2015
11  */
12
13 import java.util.Calendar;
14 import java.util.Date;
15
16 import org.splat.dal.bo.kernel.Persistent;
17 import org.splat.dal.dao.som.Database;
18
19
20 public class File extends Persistent {
21
22 //  Persistent fields
23     protected String  format;
24     protected String  path;
25     protected Date    date;
26
27 //  Transient fields
28     private   java.io.File  myfile;        // For optimization
29
30 //  ==============================================================================================================================
31 //  Construction
32 //  ==============================================================================================================================
33
34 //  Database fetch constructor
35     protected File () {
36 //  -----------------
37       this.myfile = null;
38     }
39 //  Internal constructors
40     public File (final String path) {
41 //  ----------------------------
42       Calendar  current = Calendar.getInstance();
43       String[]  table   = path.split("\\x2E");
44
45       this.format = table[table.length-1];
46       this.path   = path;                  // The corresponding physical file may not exist yet
47       this.date   = current.getTime();     // Today
48       this.myfile = null;
49     }
50     protected File (final String path, final String format, final Date date) {
51 //  ------------------------------------------------------
52       this.path   = path;                  // The corresponding physical file may not exist yet
53       this.format = format;                // The format name may be different from the physical file extension
54       this.date   = date;
55       if (date == null) {
56         Calendar current = Calendar.getInstance();
57         this.date = current.getTime();     // Today
58       }
59       this.myfile = null;
60     }
61
62 //  ==============================================================================================================================
63 //  Public member functions
64 //  ==============================================================================================================================
65 /**
66  * Returns the data file associated to this meta file.
67  * 
68  * @return the associated data file. If this meta data is an empty document, the returned file does not exist.
69  */
70     public java.io.File asFile () {
71 //  -----------------------------
72       if (myfile == null) {
73                 myfile = new java.io.File(Database.getRepositoryVaultPath() + path);
74         }
75       return myfile;
76     }
77
78     public Date getDate () {
79 //  ----------------------
80       return date;
81     }
82
83     public String getFormat () {
84 //  --------------------------
85       return format;
86     }
87
88     public String getName () {
89 //  ------------------------
90       return this.asFile().getName();
91     }
92
93     public String getRelativePath () {
94 //  --------------------------------
95       return path;
96     }
97
98     public boolean exists () {             // Shortcut
99 //  ------------------------
100       return (this.asFile().exists());
101     }
102
103 //  ==============================================================================================================================
104 //  Protected service
105 //  ==============================================================================================================================
106
107     public void changePath (final String path) {
108 //  ---------------------------------------
109       this.path   = path;
110       this.myfile = null;
111     }
112         /**
113          * Set the date.
114          * @param date the date to set
115          */
116         public void setDate(final Date date) {
117                 this.date = date;
118         }
119 }