Statistics
| Revision:

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

History | View | Annotate | Download (5.19 KB)

1 3199 fjp
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 5568 jmvivo
import com.hardcode.driverManager.Driver;
8 4050 fjp
import com.hardcode.driverManager.DriverLoadException;
9 3199 fjp
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 4050 fjp
 *
21 3199 fjp
 * @author Fernando Gonz?lez Cort?s
22
 */
23 4050 fjp
class FileDataSourceAdapter extends AbstractFileDataSource implements
24
                FileDataSource {
25 3199 fjp
        private File file;
26 4050 fjp
27 3199 fjp
        private FileDriver driver;
28 4050 fjp
29 3199 fjp
        private int sem = 0;
30 4050 fjp
31 3199 fjp
        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 11007 jmvivo
                                //System.out.println("-----" + hashCode() + " Abrir el fichero: ["+ sem + "]"+ file.getAbsolutePath() );
40 3199 fjp
                                driver.open(file);
41
                        }
42
43
                        sem++;
44
                } catch (IOException e) {
45
                        throw new DriverException(e);
46
                }
47
        }
48
49
        /**
50
         * @see com.hardcode.gdbms.engine.data.DataSource#stop()
51
         */
52
        public void stop() throws DriverException {
53
                try {
54
                        sem--;
55
56
                        if (sem == 0) {
57 11007 jmvivo
                                //System.out.println("----" + hashCode() + " Cerrar el fichero: ["+ sem + "]"+ file.getAbsolutePath() );
58 3199 fjp
                                driver.close();
59
                        } else if (sem < 0) {
60 9887 fjp
                                sem=0;
61
                                driver.close();
62 3199 fjp
                                throw new RuntimeException("DataSource closed too many times");
63
                        }
64
                } catch (IOException e) {
65
                        throw new DriverException(e);
66
                }
67
        }
68
69
        /**
70
         * Asigna el driver al adaptador
71 4050 fjp
         *
72
         * @param driver
73
         *            The driver to set.
74 3199 fjp
         */
75
        public void setDriver(FileDriver driver) {
76
                this.driver = driver;
77
        }
78
79
        /**
80
         * Sets the source information of the DataSource
81 4050 fjp
         *
82
         * @param sourceInfo
83
         *            The file to set.
84 3199 fjp
         */
85
        public void setSourceInfo(SourceInfo sourceInfo) {
86
                super.setSourceInfo(sourceInfo);
87
                file = new File(((FileSourceInfo) sourceInfo).file);
88
        }
89
90
        /**
91
         * @see com.hardcode.gdbms.engine.data.DataSource#getDriver()
92
         */
93
        public ReadAccess getReadDriver() {
94
                return driver;
95
        }
96
97
        /**
98
         * @see com.hardcode.gdbms.engine.data.DataSource#getName()
99
         */
100
        public String getName() {
101
                return sourceInfo.name;
102
        }
103
104 4050 fjp
        /**
105 3199 fjp
         * @see com.hardcode.gdbms.engine.data.DataSource#getPrimaryKeys()
106
         */
107
        public int[] getPrimaryKeys() throws DriverException {
108 4050 fjp
                // The last field is the pk/row in FileDataSources
109
                return new int[] { getFieldCount() - 1 };
110 3199 fjp
        }
111
112 4050 fjp
        /**
113
         * @see com.hardcode.gdbms.engine.data.driver.ObjectDriver#getFieldCount()
114
         */
115
        public int getFieldCount() throws DriverException {
116
                if (fieldCount == -1) {
117
                        fieldCount = getReadDriver().getFieldCount() + 1;
118 3199 fjp
119 4050 fjp
                }
120 3199 fjp
121 4050 fjp
                return fieldCount;
122
        }
123 3199 fjp
124 4050 fjp
        /**
125
         * @see com.hardcode.gdbms.engine.data.driver.ObjectDriver#getFieldName(int)
126
         */
127
        public String getFieldName(int fieldId) throws DriverException {
128
                // last field is the virtual primary key
129
                if (fieldId == getFieldCount() - 1)
130
                        return "PK";
131
                return getReadDriver().getFieldName(fieldId);
132
        }
133 3199 fjp
134 4050 fjp
        /**
135
         * @see com.hardcode.gdbms.engine.data.driver.ObjectDriver#getFieldType(int)
136
         */
137
        public int getFieldType(int i) throws DriverException {
138
                // last field is the virtual primary key
139
                if (i == getFieldCount() - 1)
140
                        return Types.BIGINT;
141
                return getReadDriver().getFieldType(i);
142
        }
143 3199 fjp
144 4050 fjp
        /**
145
         * @see com.hardcode.gdbms.engine.data.driver.ObjectDriver#getFieldValue(long,
146
         *      int)
147
         */
148
        public Value getFieldValue(long rowIndex, int fieldId)
149
                        throws DriverException {
150
                // last field is the virtual primary key
151
                if (fieldId == getFieldCount() - 1)
152
                        return ValueFactory.createValue(rowIndex);
153
                Value v = getReadDriver().getFieldValue(rowIndex, fieldId);
154
                return (v == null) ? ValueFactory.createNullValue() : v;
155
        }
156
157
        /**
158
         * @see com.hardcode.gdbms.engine.data.file.FileDataSource#getDriver()
159
         */
160 5568 jmvivo
        public Driver getDriver() {
161 4050 fjp
                return driver;
162
        }
163
164
        /**
165
         * @throws DriverException
166
         * @see com.hardcode.gdbms.engine.data.DataSource#getDataWare(int)
167
         */
168
        public DataWare getDataWare(int mode) throws DriverException {
169
                try {
170
                        FileDataWare dw = FileDataSourceFactory.newDataWareInstance();
171
                        FileDriver driver;
172
                        driver = (FileDriver) getDataSourceFactory().getDriverManager()
173
                                        .getDriver(getDriver().getName());
174
                        ((GDBMSDriver) driver).setDataSourceFactory(getDataSourceFactory());
175
                        dw.setDriver(driver);
176
                        dw.setDataSourceFactory(dsf);
177
                        dw.setSourceInfo(getSourceInfo());
178
                        return dw;
179
                } catch (DriverLoadException e) {
180
                        throw new DriverException(e);
181
                }
182
183
        }
184
185 4851 jmvivo
        public int getFieldWidth(int i) throws DriverException {
186
                return getReadDriver().getFieldWidth(i);
187
        }
188 4859 jmvivo
189
    public boolean isVirtualField(int fieldId) throws DriverException {
190
                // last field is the virtual primary key
191
                if (fieldId == this.getFieldCount() - 1)
192
                        return true;
193
                return false;
194
    }
195 4851 jmvivo
196 6314 jmvivo
        public void reload() throws DriverException {
197
                try {
198
                        sem = 0;
199
                        driver.close();
200
                } catch (IOException e) {
201
                        throw new DriverException(e);
202
                }
203
                this.fieldCount = -1;
204
205
                this.start();
206
207
                this.raiseEventReloaded();
208
209
        }
210
211 3199 fjp
}