summaryrefslogtreecommitdiffstats
path: root/das-java/branches/das-java-beta2/samples/dbconfig/src/main/java/org/apache/tuscany/das/rdb/dbconfig/DBDataHelper.java
blob: 2ef25bdf8156e7ae5d3f5a5d3a67ce03d8b25430 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.    
 */

package org.apache.tuscany.das.rdb.dbconfig;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Iterator;
import java.util.List;

import org.apache.log4j.Level;
import org.apache.log4j.Logger;

public class DBDataHelper {
    private static final String CLASS_NAME = "DBDataHelper";

    private final Logger logger = Logger.getLogger(DBDataHelper.class);

    private final DBConfig dbConfig;

    protected DBDataHelper(DBConfig dbConfig) {
        if (logger.isDebugEnabled()) {
            this.logger.log(Level.DEBUG, "DBDataHelper()");
        }
        
        this.dbConfig = dbConfig;
    }

    public boolean isDatabasePopulated() {
        boolean isPopulated = true;

        if (logger.isDebugEnabled()) {
            this.logger.log(Level.DEBUG, CLASS_NAME + ".isDatabasePopulated()");
        }

        Iterator tableIterator = dbConfig.getTable().iterator();
        while (tableIterator.hasNext()) {
            Table table = (Table) tableIterator.next();

            if (logger.isDebugEnabled()) {
                this.logger.log(Level.DEBUG, CLASS_NAME + ".isDatabasePopulated() calling isTablePopulated() for '" + table.getName() + "'");
            }
            
            isPopulated = this.isTablePopulated(table.getName());
            if (isPopulated == false) {
                break;
            }
        }
        return isPopulated;
    }

    /**
     * 
     * @param tableName
     * @return Count of rows present in the table specified by tableName
     */
    protected boolean isTablePopulated(String tableName) {
        boolean isPopulated = false;
        Connection dbConnection = null;
        Statement dbStatement = null;

        try {
            dbConnection = DBConnectionHelper.createConnection(dbConfig.getConnectionInfo());
            dbStatement = dbConnection.createStatement();
            String sqlString = "select count(*) from " + tableName;

            this.logger.log(Level.DEBUG, CLASS_NAME + ".isTablePopulated()=> sqlString => '" + sqlString + "'");

            ResultSet rs = dbStatement.executeQuery(sqlString);
            rs.next();

            if (rs != null) {
                if (logger.isDebugEnabled()) {
                    this.logger.log(Level.DEBUG, CLASS_NAME + ".isTablePopulated()=> pointer set");
                }
            }

            int count = rs.getInt(1);
            
            if (logger.isDebugEnabled()) {
                this.logger.log(Level.DEBUG, CLASS_NAME + ".isTablePopulated()=> '" + tableName + "' => " + count);
            }

            if (count > 0) {
                isPopulated = true;
            }

        } catch (SQLException e) {
            // ignore and return false
        } finally {
            try {
                dbStatement.close();
                dbConnection.close();
            } catch (SQLException e1) {
                // ignore and return false
            }
        }

        return isPopulated;
    }

    protected String generateInsertSQL(Connection dbConnection, String tableName, String rowValues) throws SQLException {
        StringBuffer sqlBuffer = new StringBuffer(50); 
        
        Statement dbStatement = dbConnection.createStatement();
        ResultSet dummyRS = dbStatement.executeQuery("SELECT * FROM "+tableName);
        ResultSetMetaData rsMetaData = dummyRS.getMetaData(); 
        	
        
        sqlBuffer.append("INSERT INTO ").append(tableName).append(" (");

        int numberOfColumns = rsMetaData.getColumnCount();
        String columnName = null;
        int i;
        for (i = 1; i <= numberOfColumns -1; i++) {
            //get the column's name.
        	columnName = rsMetaData.getColumnName(i);
          if(!rsMetaData.isAutoIncrement(i)){
              sqlBuffer.append(columnName).append(",");
          	}
          	else{
                if (logger.isDebugEnabled()) {
                    this.logger.log(Level.DEBUG, CLASS_NAME + ".initializeDatabaseData()=> auto increment column => " + i + " "+columnName);
                }
          	}
        }
        sqlBuffer.append(rsMetaData.getColumnName(i)).append(") ");
        sqlBuffer.append("VALUES (").append(rowValues).append(")");

        if (logger.isDebugEnabled()) {
            this.logger.log(Level.DEBUG, CLASS_NAME + ".initializeDatabaseData()=> SQL Clause => " + sqlBuffer);
        }
        
        return sqlBuffer.toString();
    }
    
    public void initializeDatabaseData() {
        Connection dbConnection = null;
        Statement dbStatement = null;

        try {
            dbConnection = DBConnectionHelper.createConnection(dbConfig.getConnectionInfo());
            dbStatement = dbConnection.createStatement();

            Iterator tableIterator = dbConfig.getTable().iterator();
            while (tableIterator.hasNext()) {
                Table table = (Table) tableIterator.next();
                
                if (logger.isDebugEnabled()) {
                    this.logger.log(Level.DEBUG, CLASS_NAME + ".initializeDatabaseData()=> INSERT FOR TABLE => '" + table.getName() + "'");
                }
         
                //String columnClause = generateInsertSQL(dbConnection, table.getName());
                                
                String tableName = table.getName();
                Iterator dataIterator = table.getRow().iterator();
                while (dataIterator.hasNext()) {
                    String tableRow = (String) dataIterator.next();                    
                    String sqlString = generateInsertSQL(dbConnection, tableName, tableRow);

                    if (logger.isDebugEnabled()) {
                        this.logger.log(Level.DEBUG, CLASS_NAME + ".initializeDatabaseData()=> sqlString => '" + sqlString + "'");
                    }

                    try {
                        dbStatement.executeUpdate(sqlString);
                    }catch(SQLException e){
                    	//e.printStackTrace();
                        //ignore and jump to new table
                        if (logger.isDebugEnabled()) {
                            this.logger.log(Level.DEBUG, CLASS_NAME + ".initializeDatabaseData() - Error inserting table data : " + e.getMessage(), e);
                        }
                    }
                }
            }
        } catch (SQLException e) {
            // ignore and return false
            if (logger.isDebugEnabled()) {
                this.logger.log(Level.DEBUG, CLASS_NAME + ".initializeDatabaseData() - Internal error : " +  e.getMessage(), e);
            }
        } finally {
            try {
                dbStatement.close();
                dbConnection.close();
            } catch (SQLException e1) {
                // ignore and return false
            }
        }
    }

    public void deleteDatabaseData() {
        Connection dbConnection = null;
        Statement dbStatement = null;

        try {
            dbConnection = DBConnectionHelper.createConnection(dbConfig.getConnectionInfo());
            dbStatement = dbConnection.createStatement();
            //inverse order - to take care of parent-child
            List tables = dbConfig.getTable();
            for(int i=tables.size()-1; i>-1; i-- ){
                Table table = (Table) tables.get(i);
                String sqlString = "DELETE FROM " + table.getName() ;
               
                if (logger.isDebugEnabled()) {
                    this.logger.log(Level.DEBUG, CLASS_NAME + ".deleteDatabaseData()=> sqlString => '" + sqlString + "'");
                }

                try {
                    dbStatement.executeQuery(sqlString);
                }catch(SQLException e){
                	e.printStackTrace();
                    //ignore and jump to new table
                    if (logger.isDebugEnabled()) {
                        this.logger.log(Level.DEBUG, CLASS_NAME + ".deleteDatabaseData() - Error inserting table data : " + e.getMessage(), e);
                    }
                }            	            
            }
        } catch (SQLException e) {
            // ignore and return false
            if (logger.isDebugEnabled()) {
                this.logger.log(Level.DEBUG, CLASS_NAME + ".initializeDatabaseData() - Internal error : " +  e.getMessage(), e);
            }
        } finally {
            try {
                dbStatement.close();
                dbConnection.close();
            } catch (SQLException e1) {
                // ignore and return false
            }
        }
    }
}