Salome HOME
64f2998d4d42e0712e887d1575780f7d49860bf9
[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
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     protected File (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 (String path, String format, 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) myfile = new java.io.File(Database.getRepositoryVaultPath() + path);
73       return myfile;
74     }
75
76     public Date getDate () {
77 //  ----------------------
78       return date;
79     }
80
81     public String getFormat () {
82 //  --------------------------
83       return format;
84     }
85
86     public String getName () {
87 //  ------------------------
88       return this.asFile().getName();
89     }
90
91     public String getRelativePath () {
92 //  --------------------------------
93       return path;
94     }
95
96     public boolean exists () {             // Shortcut
97 //  ------------------------
98       return (this.asFile().exists());
99     }
100
101 //  ==============================================================================================================================
102 //  Protected service
103 //  ==============================================================================================================================
104
105     public void changePath (String path) {
106 //  ---------------------------------------
107       this.path   = path;
108       this.myfile = null;
109     }
110 }