svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.dal / org.gvsig.fmap.dal.db / org.gvsig.fmap.dal.db.jdbc / src / main / java / org / gvsig / fmap / dal / store / jdbc / JDBCAppender.java @ 41536
History | View | Annotate | Download (4.62 KB)
1 |
package org.gvsig.fmap.dal.store.jdbc; |
---|---|
2 |
|
3 |
import java.sql.Connection; |
4 |
import java.sql.PreparedStatement; |
5 |
import java.sql.SQLException; |
6 |
import java.util.ArrayList; |
7 |
import java.util.List; |
8 |
import java.util.logging.Level; |
9 |
import org.gvsig.fmap.dal.exception.DataException; |
10 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
11 |
import org.gvsig.fmap.dal.feature.exception.AlreadyEditingException; |
12 |
import org.gvsig.fmap.dal.feature.exception.NeedEditingModeException; |
13 |
import org.gvsig.fmap.dal.feature.exception.PerformEditingException; |
14 |
import org.gvsig.fmap.dal.feature.exception.StoreCancelEditingException; |
15 |
import org.gvsig.fmap.dal.feature.spi.FeatureProvider; |
16 |
import org.gvsig.fmap.dal.store.jdbc.exception.JDBCPreparingSQLException; |
17 |
import org.slf4j.Logger; |
18 |
import org.slf4j.LoggerFactory; |
19 |
|
20 |
public class JDBCAppender implements Appender { |
21 |
|
22 |
final static private Logger logger = LoggerFactory |
23 |
.getLogger(JDBCAppender.class); |
24 |
|
25 |
public String sql; |
26 |
public List<FeatureAttributeDescriptor> attributes; |
27 |
public Connection connection = null; |
28 |
protected JDBCStoreProviderWriter provider;
|
29 |
|
30 |
public JDBCAppender(JDBCStoreProviderWriter provider) {
|
31 |
this.provider = provider;
|
32 |
} |
33 |
|
34 |
public void begin() throws DataException { |
35 |
if (this.connection != null) { |
36 |
throw new AlreadyEditingException(this.getSourceId()); |
37 |
} |
38 |
try {
|
39 |
StringBuilder sqlb = new StringBuilder(); |
40 |
List<FeatureAttributeDescriptor> attrs = new ArrayList<FeatureAttributeDescriptor>(); |
41 |
|
42 |
prepareSQLAndAttributeListForInsert(sqlb, attrs); |
43 |
|
44 |
sql = sqlb.toString(); |
45 |
attributes = attrs; |
46 |
this.connection = this.provider.getHelper().getConnection(); |
47 |
this.provider.getHelper().getResource().beginUse();
|
48 |
this.connection.setAutoCommit(false); |
49 |
} catch (SQLException ex) { |
50 |
try {
|
51 |
this.connection = null; |
52 |
this.provider.getHelper().getResource().endUse();
|
53 |
} catch(Exception ex1) { |
54 |
// Ignore errors
|
55 |
} |
56 |
throw new PerformEditingException(this.getSourceId(), ex); |
57 |
} |
58 |
} |
59 |
|
60 |
public void end() throws DataException { |
61 |
if (this.connection == null) { |
62 |
throw new NeedEditingModeException(this.getSourceId()); |
63 |
} |
64 |
sql = null;
|
65 |
attributes = null;
|
66 |
resetCount(); |
67 |
try {
|
68 |
this.connection.commit();
|
69 |
} catch (SQLException ex) { |
70 |
throw new PerformEditingException(this.getSourceId().toString(), ex); |
71 |
} finally {
|
72 |
this.connection = null; |
73 |
this.provider.getHelper().getResource().endUse();
|
74 |
} |
75 |
|
76 |
} |
77 |
|
78 |
public void abort() throws DataException { |
79 |
if (this.connection == null) { |
80 |
throw new StoreCancelEditingException(null, this.getSourceId()); |
81 |
} |
82 |
sql = null;
|
83 |
attributes = null;
|
84 |
resetCount(); |
85 |
try {
|
86 |
this.connection.rollback();
|
87 |
} catch (SQLException ex) { |
88 |
throw new PerformEditingException(this.getSourceId(), ex); |
89 |
} finally {
|
90 |
this.connection = null; |
91 |
this.provider.getHelper().getResource().endUse();
|
92 |
} |
93 |
} |
94 |
|
95 |
public void append(FeatureProvider feature) throws DataException { |
96 |
if (this.connection == null) { |
97 |
throw new NeedEditingModeException(this.getSourceId()); |
98 |
} |
99 |
|
100 |
PreparedStatement st = null; |
101 |
try {
|
102 |
st = connection.prepareStatement(sql); |
103 |
perfomInsert(connection, st, sql, feature, attributes); |
104 |
resetCount(); |
105 |
|
106 |
} catch (SQLException e) { |
107 |
throw new JDBCPreparingSQLException(sql, e); |
108 |
} catch (Exception e) { |
109 |
throw new PerformEditingException(this.getSourceId(), e); |
110 |
} finally {
|
111 |
try {
|
112 |
st.close(); |
113 |
} catch (Exception ex) { |
114 |
// Do nothing
|
115 |
} |
116 |
} |
117 |
} |
118 |
|
119 |
protected void prepareSQLAndAttributeListForInsert(StringBuilder sqlb, |
120 |
List<FeatureAttributeDescriptor> attributes) throws DataException { |
121 |
this.provider.prepareSQLAndAttributeListForInsert(sqlb, attributes);
|
122 |
} |
123 |
|
124 |
private void perfomInsert(Connection conn, PreparedStatement insertSt, |
125 |
String sql, FeatureProvider feature, List<FeatureAttributeDescriptor> attributes) |
126 |
throws DataException {
|
127 |
this.provider.perfomInsert(conn, insertSt, sql, feature, attributes);
|
128 |
} |
129 |
|
130 |
private void resetCount() { |
131 |
this.provider.resetCount();
|
132 |
} |
133 |
|
134 |
private String getSourceId() { |
135 |
return this.provider.getSourceId().toString(); |
136 |
} |
137 |
|
138 |
} |