summaryrefslogtreecommitdiffstats
path: root/tags/java-stable-20060304/das/rdb/src/main/java/org/apache/tuscany/das/rdb/graphbuilder/impl/ResultSetRow.java
blob: f68f48fc06a55bde34c5a5771195a1b504ce8879 (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
/**
 *
 *  Copyright 2005 The Apache Software Foundation or its licensors, as applicable.
 *
 *  Licensed 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.graphbuilder.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;

import org.apache.tuscany.das.rdb.util.DebugUtil;


/**
 * 
 * A ResultSetRow is used to transform a single row of a ResultSet into a set of
 * EDataObjects.
 */
public class ResultSetRow {

	private final ResultMetadata metadata;

	private HashMap tableMap = new HashMap();

	private ArrayList allTableData;
	
	private boolean debug = false;

	/**
	 * Method ResultSetRow.
	 * 
	 * @param rs
	 *            A ResultSet positioned on the desired row
	 * @param ePackage
	 *            The package used to create EDataObjects
	 */
	public ResultSetRow(ResultSet rs, ResultMetadata m) throws SQLException {
		this.metadata = m;
		if (m.isRecursive())
			processRecursiveRow(rs);
		else
			processRow(rs);
	}

	/**
	 * Processes a single row in the ResultSet Method processRow.
	 * 
	 * @param rs
	 */
	private void processRow(ResultSet rs) throws SQLException {

		DebugUtil.debugln(getClass(), debug, "");
		for (int i = 1; i <= metadata.getResultSetSize(); i++) {
			Object data = getObject(rs, i);

			TableData table = getRawData(metadata.getTablePropertyName(i));
			DebugUtil.debugln(getClass(), debug, "Adding column: "
					+ metadata.getColumnPropertyName(i) + "\tValue: " + data
					+ "\tTable: " + metadata.getTablePropertyName(i));
			table.addData(metadata.getColumnPropertyName(i), metadata
					.isPKColumn(i), data);
		}

	}

	public void processRecursiveRow(ResultSet rs) throws SQLException {
		this.allTableData = new ArrayList();
		int i = 1;
		DebugUtil.debugln(getClass(), debug, "");
		while (i <= metadata.getResultSetSize()) {
			DebugUtil.debugln(getClass(), debug, "");
			TableData table = new TableData(metadata.getTablePropertyName(i));
			this.allTableData.add(table);
			
			while ( (i <= metadata.getResultSetSize()) && (metadata.isPKColumn(i))) {
				Object data = getObject(rs, i);
				DebugUtil.debugln(getClass(), debug, "Adding column: "
						+ metadata.getColumnPropertyName(i) + "\tValue: " + data
						+ "\tTable: " + metadata.getTablePropertyName(i));
				table.addData(metadata.getColumnPropertyName(i), true, data);
				i++;
			}

			while ( (i <= metadata.getResultSetSize()) &&
					(!metadata.isPKColumn(i))) {
				Object data = getObject(rs, i);
				DebugUtil.debugln(getClass(), debug, "Adding column: "
						+ metadata.getColumnPropertyName(i) + "\tValue: " + data
						+ "\tTable: " + metadata.getTablePropertyName(i));
				table.addData(metadata.getColumnPropertyName(i), false, data);
				i++;
			}
		}
	}

	/**
	 * @param rs
	 * @param metadata
	 * @param i
	 * @return
	 */
	private Object getObject(ResultSet rs,  int i)
			throws SQLException {

		Object data = rs.getObject(i);

		if (rs.wasNull())
			return null;
		else
			return metadata.getConverter(i).getPropertyValue(data);
	}

	/**
	 * Returns a HashMap that holds data for the specified table
	 * 
	 * @param tableName
	 *            The name of the table
	 * @return HashMap
	 */
	public TableData getTable(String tableName) {
		return (TableData) tableMap.get(tableName);
	}

	/**
	 * Returns a HashMap that holds data for the specified table If the HashMap
	 * doesn't exist, it will be created. This is used internally to build the
	 * ResultSetRow, whereas getTable is used externally to retrieve existing
	 * table data.
	 * 
	 * @param tableName
	 *            The name of the table
	 * @return HashMap
	 */
	private TableData getRawData(String tableName) {

		TableData table = (TableData) tableMap.get(tableName);

		if (table == null) {
			table = new TableData(tableName);
			tableMap.put(tableName, table);
		}

		return table;
	}
	
	public ArrayList getAllTableData() {
		if ( this.allTableData == null ) {
			this.allTableData = new ArrayList();
			this.allTableData.addAll(tableMap.values());
		}
		DebugUtil.debugln(getClass(), debug, allTableData);
		
		return this.allTableData;
	}

}