root / branches / v2_0_0_prep / libraries / libFMap_dataDB / src / org / gvsig / fmap / data / feature / db / jdbc / JDBCFeaturesWriter.java @ 24250
History | View | Annotate | Download (3.48 KB)
1 |
package org.gvsig.fmap.data.feature.db.jdbc; |
---|---|
2 |
|
3 |
import java.sql.Connection; |
4 |
import java.sql.PreparedStatement; |
5 |
import java.sql.SQLException; |
6 |
import java.sql.Statement; |
7 |
|
8 |
import org.gvsig.fmap.data.exceptions.CloseException; |
9 |
import org.gvsig.fmap.data.exceptions.DataException; |
10 |
import org.gvsig.fmap.data.exceptions.OpenException; |
11 |
import org.gvsig.fmap.data.exceptions.ReadException; |
12 |
import org.gvsig.fmap.data.exceptions.WriteException; |
13 |
import org.gvsig.fmap.data.feature.Feature; |
14 |
import org.gvsig.fmap.data.feature.FeatureAttributeDescriptor; |
15 |
import org.gvsig.fmap.data.feature.FeatureStore; |
16 |
import org.gvsig.fmap.data.feature.db.DBAttributeDescriptor; |
17 |
import org.gvsig.fmap.data.feature.db.DBFeatureType; |
18 |
import org.gvsig.fmap.data.feature.db.jdbc.h2.H2StoreParameters; |
19 |
import org.gvsig.fmap.data.feature.db.jdbc.postgresql.PostgresqlStore; |
20 |
import org.gvsig.fmap.data.feature.exceptions.InitializeWriterException; |
21 |
import org.gvsig.fmap.data.feature.impl.SelectiveWriter; |
22 |
import org.gvsig.fmap.geom.Geometry; |
23 |
import org.gvsig.fmap.geom.operation.GeometryOperationException; |
24 |
import org.gvsig.fmap.geom.operation.GeometryOperationNotSupportedException; |
25 |
import org.gvsig.fmap.geom.operation.towkb.ToWKB; |
26 |
|
27 |
public abstract class JDBCFeaturesWriter implements SelectiveWriter{ |
28 |
|
29 |
protected Connection conex; |
30 |
protected JDBCStore store;
|
31 |
protected boolean previousAutocommit; |
32 |
|
33 |
|
34 |
public void init(FeatureStore store) throws InitializeWriterException{ |
35 |
this.store = (JDBCStore)store;
|
36 |
try {
|
37 |
this.conex = this.store.getWriterConnection(); |
38 |
} catch (ReadException e) {
|
39 |
throw new InitializeWriterException(this.store.getName(),e); |
40 |
} |
41 |
} |
42 |
|
43 |
/* (non-Javadoc)
|
44 |
* @see org.gvsig.fmap.data.feature.FeaturesWriter#dispose()
|
45 |
*/
|
46 |
public void dispose() throws DataException { |
47 |
try {
|
48 |
this.conex.close();
|
49 |
} catch (SQLException e) { |
50 |
throw new CloseException(this.store.getName(),e); |
51 |
} |
52 |
this.conex = null; |
53 |
|
54 |
} |
55 |
|
56 |
|
57 |
public void preProcess() throws WriteException, ReadException { |
58 |
try {
|
59 |
previousAutocommit = conex.getAutoCommit(); |
60 |
conex.setAutoCommit(false);
|
61 |
} catch (SQLException e) { |
62 |
throw new WriteException(this.store.getName(),e); |
63 |
} |
64 |
|
65 |
} |
66 |
|
67 |
public void postProcess() throws OpenException, WriteException { |
68 |
try {
|
69 |
conex.commit(); |
70 |
if (previousAutocommit){
|
71 |
conex.setAutoCommit(true);
|
72 |
} |
73 |
} catch (SQLException e) { |
74 |
throw new WriteException(this.store.getName(),e); |
75 |
} |
76 |
} |
77 |
|
78 |
public void cancelProcess() throws WriteException { |
79 |
try {
|
80 |
conex.rollback(); |
81 |
if (previousAutocommit){
|
82 |
conex.setAutoCommit(true);
|
83 |
} |
84 |
} catch (SQLException e) { |
85 |
throw new WriteException(this.store.getName(),e); |
86 |
} |
87 |
} |
88 |
|
89 |
protected void loadValueInPreparedStatement(PreparedStatement ps, int paramIndex, DBAttributeDescriptor attr, Feature feature) throws java.sql.SQLException, WriteException { |
90 |
Object value = feature.getAttribute(attr.ordinal());
|
91 |
if (value == null){ |
92 |
ps.setNull(paramIndex, attr.getSqlType()); |
93 |
return;
|
94 |
} |
95 |
|
96 |
if (attr.getDataType() == FeatureAttributeDescriptor.GEOMETRY){
|
97 |
Geometry geom =(Geometry)feature.getAttribute(attr.ordinal()); |
98 |
try {
|
99 |
// TODO Falta pasar el SRS de la geometr?a para pasarla a WKB.
|
100 |
ps.setBytes( |
101 |
paramIndex, (byte[])geom.invokeOperation(ToWKB.CODE,null) |
102 |
); |
103 |
} catch (GeometryOperationNotSupportedException e) {
|
104 |
throw new WriteException(this.store.getName(),e); |
105 |
} catch (GeometryOperationException e) {
|
106 |
throw new WriteException(this.store.getName(),e); |
107 |
} |
108 |
return;
|
109 |
} |
110 |
ps.setObject(paramIndex, feature.getAttribute(attr.ordinal())); |
111 |
} |
112 |
|
113 |
} |