Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libFMap_dataDB / src / org / gvsig / fmap / data / feature / db / jdbc / postgresqlbin / PostgresqlBinFeature.java @ 24250

History | View | Annotate | Download (5.61 KB)

1
package org.gvsig.fmap.data.feature.db.jdbc.postgresqlbin;
2

    
3
import java.math.BigDecimal;
4
import java.nio.ByteBuffer;
5
import java.sql.Date;
6
import java.sql.ResultSet;
7
import java.sql.SQLException;
8
import java.sql.Timestamp;
9
import java.sql.Types;
10

    
11
import org.gvsig.fmap.data.exceptions.DataException;
12
import org.gvsig.fmap.data.exceptions.ReadException;
13
import org.gvsig.fmap.data.feature.Feature;
14
import org.gvsig.fmap.data.feature.FeatureAttributeDescriptor;
15
import org.gvsig.fmap.data.feature.FeatureReference;
16
import org.gvsig.fmap.data.feature.FeatureType;
17
import org.gvsig.fmap.data.feature.db.DBAttributeDescriptor;
18
import org.gvsig.fmap.data.feature.db.DBFeatureType;
19
import org.gvsig.fmap.data.feature.db.jdbc.JDBCFeature;
20
import org.gvsig.fmap.data.feature.db.jdbc.JDBCStore;
21
import org.gvsig.fmap.data.feature.exceptions.IsNotFeatureSettingException;
22

    
23

    
24
public class PostgresqlBinFeature extends JDBCFeature{
25

    
26

    
27

    
28
        /**
29
         *
30
         */
31
        private static final long serialVersionUID = -1215877232314904198L;
32

    
33
        PostgresqlBinFeature(FeatureType featureType, JDBCStore store, ResultSet rs) throws ReadException {
34
                super(featureType, store, rs);
35
        }
36

    
37

    
38
        public FeatureReference getID() {
39
                return new PostgresqlBinFeatureID(this.store,featureKey);
40
        }
41
        protected void loading() {
42
                // TODO Auto-generated method stub
43
                super.loading();
44
        }
45

    
46
        protected void stopLoading() {
47
                // TODO Auto-generated method stub
48
                super.stopLoading();
49
        }
50

    
51

    
52
        /* (non-Javadoc)
53
         * @see org.gvsig.fmap.data.feature.db.jdbc.JDBCFeature#loadValueFromResulset(java.sql.ResultSet, org.gvsig.fmap.data.feature.FeatureAttributeDescriptor)
54
         */
55
        protected void loadValueFromResulset(ResultSet rs, FeatureAttributeDescriptor attr) throws ReadException {
56
                String name = attr.getName();
57
                Object value = null;
58
                try {
59
                        value = getFieldValueFromBinaryCursor(rs, (DBAttributeDescriptor)attr);
60
                        if (attr.getDataType().equals(FeatureAttributeDescriptor.GEOMETRY)) {
61
                                this.setGeometry(name,value);
62
                        } else {
63
                                this.setAttribute(name, value);
64
                        }
65
                } catch (java.sql.SQLException e) {
66
                        throw new ReadException("CreateFeature",e);
67
                } catch (IsNotFeatureSettingException e) {
68
                        throw new ReadException(this.store.getName(),e);
69
                }
70

    
71
        }
72

    
73
        private Object getFieldValueFromBinaryCursor(ResultSet aRs, DBAttributeDescriptor attrDescriptor) throws java.sql.SQLException {
74
                int fieldId = attrDescriptor.ordinal();
75
                int sqlType = attrDescriptor.getSqlType();
76
                byte[] byteBuf = aRs.getBytes(fieldId+1);
77
                if (byteBuf == null) {
78
                        return null;
79
                } else {
80

    
81
                        ByteBuffer buf = ByteBuffer.wrap(byteBuf);
82

    
83
                        if (attrDescriptor.getDataType().equals(FeatureAttributeDescriptor.GEOMETRY)){
84
                                if (attrDescriptor.getDataType() == FeatureAttributeDescriptor.GEOMETRY){
85
                                        if (byteBuf == null) {
86
                                                return null;
87
                                        }
88
//                                        return wkbParser.parse(byteBuf);
89
                                        return null;
90
                                }
91

    
92
                        }
93

    
94
                        switch (sqlType) {
95
                        case Types.VARCHAR:
96
                                //FIXME Error
97
                                return aRs.getString(fieldId);
98
//                                return new String(buf.toString());
99
                        case Types.FLOAT:
100
                                return new Float(buf.getFloat());
101
                        case Types.DOUBLE:
102
                                return new Double(buf.getDouble());
103
                        case Types.REAL:
104
                                return new Float(buf.getFloat());
105
                        case Types.INTEGER:
106
                                return new Integer(buf.getInt());
107
                        case Types.BIGINT:
108
                                return new Long(buf.getLong());
109
                        case Types.BIT:
110
                                return new Boolean(byteBuf[0] == 1);
111
                        case Types.BOOLEAN:
112
                                return new Boolean(aRs.getBoolean(fieldId));
113
                        case Types.DATE:
114
                                long daysAfter2000 = buf.getInt() + 1;
115
                                long msecs = daysAfter2000*24*60*60*1000;
116
                                long real_msecs_date1 = (long) (XTypes.NUM_msSecs2000 + msecs);
117
                                Date realDate1 = new Date(real_msecs_date1);
118
                                return realDate1;
119
                        case Types.TIME:
120
                                // TODO:
121
                                // throw new RuntimeException("TIME type not implemented yet");
122
                                return "NOT IMPLEMENTED YET";
123
                        case Types.TIMESTAMP:
124
                                double segsReferredTo2000 = buf.getDouble();
125
                                long real_msecs = (long) (XTypes.NUM_msSecs2000 + segsReferredTo2000*1000);
126
                                Timestamp valTimeStamp = new Timestamp(real_msecs);
127
                                return valTimeStamp;
128
                        case Types.NUMERIC:
129
                                // System.out.println(metaData.getColumnName(fieldId) + " "
130
                                // + metaData.getColumnClassName(fieldId));
131
                                short ndigits = buf.getShort();
132
                                short weight = buf.getShort();
133
                                short sign = buf.getShort();
134
                                short dscale = buf.getShort();
135
                                String strAux;
136
                                if (sign == 0) {
137
                                        strAux = "+";
138
                                } else {
139
                                        strAux = "-";
140
                                }
141

    
142
                                for (int iDigit = 0; iDigit < ndigits; iDigit++) {
143
                                        short digit = buf.getShort();
144
                                        strAux = strAux + digit;
145
                                        if (iDigit == weight) {
146
                                                strAux = strAux + ".";
147
                                        }
148

    
149
                                }
150
                                strAux = strAux + "0";
151
                                BigDecimal dec;
152
                                dec = new BigDecimal(strAux);
153
                                // System.out.println(ndigits + "_" + weight + "_" + dscale
154
                                // + "_" + strAux);
155
                                // System.out.println(strAux + " Big= " + dec);
156
                                return new Double(dec.doubleValue());
157

    
158

    
159
                        default:
160
                                //TODO ???
161
                                throw new RuntimeException("Unsuported Type");
162
                        }
163

    
164
                }
165
        }
166

    
167
        protected Object[] getPkFromResulsetBinary(ResultSet rs, DBFeatureType featureType) throws SQLException {
168
                String[] fieldsId = featureType.getFieldsId();
169
                Object[] result = new Object[fieldsId.length];
170
                for (int i=0;i<fieldsId.length;i++){
171
                        result[i] = getFieldValueFromBinaryCursor(
172
                                        rs,
173
                                        (DBAttributeDescriptor)featureType.get(fieldsId[i])
174
                                );
175

    
176
                }
177
                return result;
178
        }
179

    
180

    
181
        /* (non-Javadoc)
182
         * @see org.gvsig.fmap.data.feature.AbstractFeature#cloneFeature()
183
         */
184
        protected Feature cloneFeature() throws DataException {
185
                // TODO Auto-generated method stub
186
                return null;
187
        }
188

    
189
}