Statistics
| Revision:

svn-gvsig-desktop / branches / v10 / libraries / libGDBMS / src / main / java / com / hardcode / gdbms / engine / data / file / FileDataSourceAdapter.java @ 14594

History | View | Annotate | Download (5.31 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 org.apache.log4j.Logger;
8

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

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

    
29
        private FileDriver driver;
30

    
31
        private int sem = 0;
32

    
33
        private int fieldCount = -1;
34

    
35
        /**
36
         * @see com.hardcode.gdbms.engine.data.DataSource#start()
37
         */
38
        public void start() throws DriverException {
39
                try {
40
                        if (sem == 0) {
41
                                //System.out.println("-----" + hashCode() + " Abrir el fichero: ["+ sem + "]"+ file.getAbsolutePath() );
42
                                driver.open(file);
43
                        }
44

    
45
                        sem++;
46
                } catch (IOException e) {
47
                        throw new DriverException(e);
48
                }
49
        }
50

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

    
58
                        if (sem == 0) {
59
                                //System.out.println("----" + hashCode() + " Cerrar el fichero: ["+ sem + "]"+ file.getAbsolutePath() );
60
                                driver.close();
61
                        } else if (sem < 0) {
62
                                sem=0;
63
                                driver.close();
64
                                Logger.getLogger(this.getClass()).debug("DataSource closed too many times  =  "+ driver.getName());
65
//                                throw new RuntimeException("DataSource closed too many times");
66
                        }
67
                } catch (IOException e) {
68
                        throw new DriverException(e);
69
                }
70
        }
71

    
72
        /**
73
         * Asigna el driver al adaptador
74
         *
75
         * @param driver
76
         *            The driver to set.
77
         */
78
        public void setDriver(FileDriver driver) {
79
                this.driver = driver;
80
        }
81

    
82
        /**
83
         * Sets the source information of the DataSource
84
         *
85
         * @param sourceInfo
86
         *            The file to set.
87
         */
88
        public void setSourceInfo(SourceInfo sourceInfo) {
89
                super.setSourceInfo(sourceInfo);
90
                file = new File(((FileSourceInfo) sourceInfo).file);
91
        }
92

    
93
        /**
94
         * @see com.hardcode.gdbms.engine.data.DataSource#getDriver()
95
         */
96
        public ReadAccess getReadDriver() {
97
                return driver;
98
        }
99

    
100
        /**
101
         * @see com.hardcode.gdbms.engine.data.DataSource#getName()
102
         */
103
        public String getName() {
104
                return sourceInfo.name;
105
        }
106

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

    
115
        /**
116
         * @see com.hardcode.gdbms.engine.data.driver.ObjectDriver#getFieldCount()
117
         */
118
        public int getFieldCount() throws DriverException {
119
                if (fieldCount == -1) {
120
                        fieldCount = getReadDriver().getFieldCount() + 1;
121

    
122
                }
123

    
124
                return fieldCount;
125
        }
126

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

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

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

    
160
        /**
161
         * @see com.hardcode.gdbms.engine.data.file.FileDataSource#getDriver()
162
         */
163
        public Driver getDriver() {
164
                return driver;
165
        }
166

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

    
186
        }
187

    
188
        public int getFieldWidth(int i) throws DriverException {
189
                return getReadDriver().getFieldWidth(i);
190
        }
191

    
192
    public boolean isVirtualField(int fieldId) throws DriverException {
193
                // last field is the virtual primary key
194
                if (fieldId == this.getFieldCount() - 1)
195
                        return true;
196
                return false;
197
    }
198

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

    
208
                this.start();
209

    
210
                this.raiseEventReloaded();
211

    
212
        }
213

    
214
}