Statistics
| Revision:

svn-gvsig-desktop / tags / v1_0_2_Build_906 / libraries / libGDBMS / src / main / java / com / hardcode / gdbms / engine / data / file / FileDataSourceAdapter.java @ 10972

History | View | Annotate | Download (4.98 KB)

1
package com.hardcode.gdbms.engine.data.file;
2

    
3
import java.io.File;
4
import java.io.IOException;
5
import java.sql.Types;
6

    
7
import com.hardcode.driverManager.Driver;
8
import com.hardcode.driverManager.DriverLoadException;
9
import com.hardcode.gdbms.engine.data.SourceInfo;
10
import com.hardcode.gdbms.engine.data.driver.DriverException;
11
import com.hardcode.gdbms.engine.data.driver.FileDriver;
12
import com.hardcode.gdbms.engine.data.driver.GDBMSDriver;
13
import com.hardcode.gdbms.engine.data.driver.ReadAccess;
14
import com.hardcode.gdbms.engine.data.edition.DataWare;
15
import com.hardcode.gdbms.engine.values.Value;
16
import com.hardcode.gdbms.engine.values.ValueFactory;
17

    
18
/**
19
 * Adapta la interfaz FileDriver a la interfaz DataSource
20
 * 
21
 * @author Fernando Gonz?lez Cort?s
22
 */
23
class FileDataSourceAdapter extends AbstractFileDataSource implements
24
                FileDataSource {
25
        private File file;
26

    
27
        private FileDriver driver;
28

    
29
        private int sem = 0;
30

    
31
        private int fieldCount = -1;
32

    
33
        /**
34
         * @see com.hardcode.gdbms.engine.data.DataSource#start()
35
         */
36
        public void start() throws DriverException {
37
                try {
38
                        if (sem == 0) {
39
                                driver.open(file);
40
                        }
41

    
42
                        sem++;
43
                } catch (IOException e) {
44
                        throw new DriverException(e);
45
                }
46
        }
47

    
48
        /**
49
         * @see com.hardcode.gdbms.engine.data.DataSource#stop()
50
         */
51
        public void stop() throws DriverException {
52
                try {
53
                        sem--;
54

    
55
                        if (sem == 0) {
56
                                driver.close();
57
                        } else if (sem < 0) {
58
                                sem=0;
59
                                driver.close();
60
                                throw new RuntimeException("DataSource closed too many times");
61
                        }
62
                } catch (IOException e) {
63
                        throw new DriverException(e);
64
                }
65
        }
66

    
67
        /**
68
         * Asigna el driver al adaptador
69
         * 
70
         * @param driver
71
         *            The driver to set.
72
         */
73
        public void setDriver(FileDriver driver) {
74
                this.driver = driver;
75
        }
76

    
77
        /**
78
         * Sets the source information of the DataSource
79
         * 
80
         * @param sourceInfo
81
         *            The file to set.
82
         */
83
        public void setSourceInfo(SourceInfo sourceInfo) {
84
                super.setSourceInfo(sourceInfo);
85
                file = new File(((FileSourceInfo) sourceInfo).file);
86
        }
87

    
88
        /**
89
         * @see com.hardcode.gdbms.engine.data.DataSource#getDriver()
90
         */
91
        public ReadAccess getReadDriver() {
92
                return driver;
93
        }
94

    
95
        /**
96
         * @see com.hardcode.gdbms.engine.data.DataSource#getName()
97
         */
98
        public String getName() {
99
                return sourceInfo.name;
100
        }
101

    
102
        /**
103
         * @see com.hardcode.gdbms.engine.data.DataSource#getPrimaryKeys()
104
         */
105
        public int[] getPrimaryKeys() throws DriverException {
106
                // The last field is the pk/row in FileDataSources
107
                return new int[] { getFieldCount() - 1 };
108
        }
109

    
110
        /**
111
         * @see com.hardcode.gdbms.engine.data.driver.ObjectDriver#getFieldCount()
112
         */
113
        public int getFieldCount() throws DriverException {
114
                if (fieldCount == -1) {
115
                        fieldCount = getReadDriver().getFieldCount() + 1;
116

    
117
                }
118

    
119
                return fieldCount;
120
        }
121

    
122
        /**
123
         * @see com.hardcode.gdbms.engine.data.driver.ObjectDriver#getFieldName(int)
124
         */
125
        public String getFieldName(int fieldId) throws DriverException {
126
                // last field is the virtual primary key
127
                if (fieldId == getFieldCount() - 1)
128
                        return "PK";
129
                return getReadDriver().getFieldName(fieldId);
130
        }
131

    
132
        /**
133
         * @see com.hardcode.gdbms.engine.data.driver.ObjectDriver#getFieldType(int)
134
         */
135
        public int getFieldType(int i) throws DriverException {
136
                // last field is the virtual primary key
137
                if (i == getFieldCount() - 1)
138
                        return Types.BIGINT;
139
                return getReadDriver().getFieldType(i);
140
        }
141

    
142
        /**
143
         * @see com.hardcode.gdbms.engine.data.driver.ObjectDriver#getFieldValue(long,
144
         *      int)
145
         */
146
        public Value getFieldValue(long rowIndex, int fieldId)
147
                        throws DriverException {
148
                // last field is the virtual primary key
149
                if (fieldId == getFieldCount() - 1)
150
                        return ValueFactory.createValue(rowIndex);
151
                Value v = getReadDriver().getFieldValue(rowIndex, fieldId);
152
                return (v == null) ? ValueFactory.createNullValue() : v;
153
        }
154

    
155
        /**
156
         * @see com.hardcode.gdbms.engine.data.file.FileDataSource#getDriver()
157
         */
158
        public Driver getDriver() {
159
                return driver;
160
        }
161

    
162
        /**
163
         * @throws DriverException 
164
         * @see com.hardcode.gdbms.engine.data.DataSource#getDataWare(int)
165
         */
166
        public DataWare getDataWare(int mode) throws DriverException {
167
                try {
168
                        FileDataWare dw = FileDataSourceFactory.newDataWareInstance();
169
                        FileDriver driver;
170
                        driver = (FileDriver) getDataSourceFactory().getDriverManager()
171
                                        .getDriver(getDriver().getName());
172
                        ((GDBMSDriver) driver).setDataSourceFactory(getDataSourceFactory());
173
                        dw.setDriver(driver);
174
                        dw.setDataSourceFactory(dsf);
175
                        dw.setSourceInfo(getSourceInfo());
176
                        return dw;
177
                } catch (DriverLoadException e) {
178
                        throw new DriverException(e);
179
                }
180

    
181
        }
182

    
183
        public int getFieldWidth(int i) throws DriverException {                
184
                return getReadDriver().getFieldWidth(i);
185
        }
186
        
187
    public boolean isVirtualField(int fieldId) throws DriverException {
188
                // last field is the virtual primary key
189
                if (fieldId == this.getFieldCount() - 1)
190
                        return true;                
191
                return false;
192
    }
193

    
194
        public void reload() throws DriverException {
195
                try {
196
                        sem = 0;                
197
                        driver.close();
198
                } catch (IOException e) {
199
                        throw new DriverException(e);
200
                }
201
                this.fieldCount = -1;
202
                
203
                this.start();
204
                
205
                this.raiseEventReloaded();
206
                
207
        }
208

    
209
}