Salome HOME
Refactoring: configuration files are moved into Siman web project.
[tools/siman.git] / Workspace / Siman-Common / src / org / splat / som / File.java
1 package org.splat.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
11  */
12
13 import java.util.Calendar;
14 import java.util.Date;
15
16 import org.splat.kernel.Persistent;
17
18
19 public class File extends Persistent {
20
21 //  Persistent fields
22     protected String  format;
23     protected String  path;
24     protected Date    date;
25
26 //  Transient fields
27     private   java.io.File  myfile;        // For optimization
28
29 //  ==============================================================================================================================
30 //  Construction
31 //  ==============================================================================================================================
32
33 //  Database fetch constructor
34     protected File () {
35 //  -----------------
36       this.myfile = null;
37     }
38 //  Internal constructors
39     protected File (String path) {
40 //  ----------------------------
41       Calendar  current = Calendar.getInstance();
42       String[]  table   = path.split("\\x2E");
43
44       this.format = table[table.length-1];
45       this.path   = path;                  // The corresponding physical file may not exist yet
46       this.date   = current.getTime();     // Today
47       this.myfile = null;
48     }
49     protected File (String path, String format, Date date) {
50 //  ------------------------------------------------------
51       this.path   = path;                  // The corresponding physical file may not exist yet
52       this.format = format;                // The format name may be different from the physical file extension
53       this.date   = date;
54       if (date == null) {
55         Calendar current = Calendar.getInstance();
56         this.date = current.getTime();     // Today
57       }
58       this.myfile = null;
59     }
60
61 //  ==============================================================================================================================
62 //  Public member functions
63 //  ==============================================================================================================================
64 /**
65  * Returns the data file associated to this meta file.
66  * 
67  * @return the associated data file. If this meta data is an empty document, the returned file does not exist.
68  */
69     public java.io.File asFile () {
70 //  -----------------------------
71       if (myfile == null) myfile = new java.io.File(Database.getRepositoryVaultPath() + path);
72       return myfile;
73     }
74
75     public Date getDate () {
76 //  ----------------------
77       return date;
78     }
79
80     public String getFormat () {
81 //  --------------------------
82       return format;
83     }
84
85     public String getName () {
86 //  ------------------------
87       return this.asFile().getName();
88     }
89
90     public String getRelativePath () {
91 //  --------------------------------
92       return path;
93     }
94
95     public boolean exists () {             // Shortcut
96 //  ------------------------
97       return (this.asFile().exists());
98     }
99
100 //  ==============================================================================================================================
101 //  Protected service
102 //  ==============================================================================================================================
103
104     protected void changePath (String path) {
105 //  ---------------------------------------
106       this.path   = path;
107       this.myfile = null;
108     }
109 }