Statistics
| Revision:

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 @ 44058

History | View | Annotate | Download (4.69 KB)

1 41536 jjdelcerro
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 41880 jjdelcerro
            this.provider.getHelper().closeConnection(this.connection);
73 41536 jjdelcerro
            this.connection = null;
74
            this.provider.getHelper().getResource().endUse();
75
        }
76
77
    }
78
79
    public void abort() throws DataException {
80
        if (this.connection == null) {
81
            throw new StoreCancelEditingException(null, this.getSourceId());
82
        }
83
        sql = null;
84
        attributes = null;
85
        resetCount();
86
        try {
87
            this.connection.rollback();
88
        } catch (SQLException ex) {
89
            throw new PerformEditingException(this.getSourceId(), ex);
90
        } finally {
91
            this.connection = null;
92
            this.provider.getHelper().getResource().endUse();
93
        }
94
    }
95
96
    public void append(FeatureProvider feature) throws DataException {
97
        if (this.connection == null) {
98
            throw new NeedEditingModeException(this.getSourceId());
99
        }
100
101
        PreparedStatement st = null;
102
        try {
103
            st = connection.prepareStatement(sql);
104
            perfomInsert(connection, st, sql, feature, attributes);
105
            resetCount();
106
107
        } catch (SQLException e) {
108
            throw new JDBCPreparingSQLException(sql, e);
109
        } catch (Exception e) {
110
            throw new PerformEditingException(this.getSourceId(), e);
111
        } finally {
112
            try {
113
                st.close();
114
            } catch (Exception ex) {
115
                // Do nothing
116
            }
117
        }
118
    }
119
120
    protected void prepareSQLAndAttributeListForInsert(StringBuilder sqlb,
121
            List<FeatureAttributeDescriptor> attributes) throws DataException {
122
        this.provider.prepareSQLAndAttributeListForInsert(sqlb, attributes);
123
    }
124
125
    private void perfomInsert(Connection conn, PreparedStatement insertSt,
126
            String sql, FeatureProvider feature, List<FeatureAttributeDescriptor> attributes)
127
            throws DataException {
128
        this.provider.perfomInsert(conn, insertSt, sql, feature, attributes);
129
    }
130
131
    private void resetCount() {
132
        this.provider.resetCount();
133
    }
134
135
    private String getSourceId() {
136
        return this.provider.getSourceId().toString();
137
    }
138
139
}