summaryrefslogtreecommitdiffstats
path: root/das-java/branches/das-java-M2/rdb/src/main/java/org/apache/tuscany/das/rdb/graphbuilder/impl/ResultSetProcessor.java
blob: 74a4627fa15a2d6b2127012360f4ffe8dbc1beef (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
/*
 * 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.graphbuilder.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Iterator;

import org.apache.log4j.Logger;
import org.apache.tuscany.das.rdb.util.LoggerFactory;

import commonj.sdo.DataObject;

/**
 * 
 * A ResultSetProcessor is used to transform the data in a ResultSet into a set of inter-related EDataObjects.
 */
public class ResultSetProcessor {
    private final Logger logger = LoggerFactory.INSTANCE.getLogger(ResultSetProcessor.class);

    private TableRegistry registry;

    private GraphBuilderMetadata metadata;

    private final DataObjectMaker doMaker;

    public ResultSetProcessor(DataObject g, GraphBuilderMetadata gbmd) {

        this.metadata = gbmd;
        if (metadata.getRelationships().size() == 0) {
            registry = new SingleTableRegistry();
        } else {
            registry = new MultiTableRegistry();
        }

        doMaker = new DataObjectMaker(g);

        if (this.logger.isDebugEnabled()) {
            this.logger.debug(metadata);
        }

    }

    // private void debug(Object output) {
    // DebugUtil.debugln(getClass(), debug, output);
    // }

    /**
     * Process the ResultSet. For each row in the ResultSet, a
     * 
     * @link ResultSetRow object will be created to represent the row as a set of EDataObjects. Then, 
     * the relevant relationships will be constructed
     *       between each object in the
     * @link ResultSetRow.
     * 
     * @param rs
     *            The ResultSet
     */
    public void processResults(int start, int end) throws SQLException {

        Iterator i = metadata.getResultMetadata().iterator();
        while (i.hasNext()) {
            ResultMetadata resultMetadata = (ResultMetadata) i.next();
            ResultSet results = resultMetadata.getResultSet();

            processResultSet(results, resultMetadata, start, end);

            // TODO These statements HAVE to be closed or we will have major problems
            // results.getStatement().close();
            results.close();
        }

    }

    private void processResultSet(ResultSet rs, ResultMetadata rsMetadata, int start, int end) throws SQLException {

        if (rs.getType() == ResultSet.TYPE_FORWARD_ONLY) {
            while (rs.next() && start < end) {
                ResultSetRow rsr = new ResultSetRow(rs, rsMetadata);
                addRowToGraph(rsr, rsMetadata);
                ++start;
            }
        } else {
            while (rs.absolute(start) && start < end) {
                ResultSetRow rsr = new ResultSetRow(rs, rsMetadata);
                addRowToGraph(rsr, rsMetadata);
                ++start;
            }
        }
    }

    /**
     * @param row
     * @param resultMetadata
     */
    private void addRowToGraph(ResultSetRow row, ResultMetadata resultMetadata) {
        RowObjects tableObjects = new RowObjects(metadata, registry);
        Iterator tables = row.getAllTableData().iterator();
        while (tables.hasNext()) {
            TableData rawDataFromRow = (TableData) tables.next();

            if (!rawDataFromRow.hasValidPrimaryKey()) {
                continue;
            }

            String tableName = rawDataFromRow.getTableName();
            DataObject tableObject = registry.get(tableName, rawDataFromRow.getPrimaryKeyValues());
            if (tableObject == null) {
                tableObject = doMaker.createAndAddDataObject(rawDataFromRow, resultMetadata);

                if (this.logger.isDebugEnabled()) {
                    this.logger.debug("Putting table " + tableName + " with PK " 
                            + rawDataFromRow.getPrimaryKeyValues() + " into registry");
                }

                registry.put(tableName, rawDataFromRow.getPrimaryKeyValues(), tableObject);
            }
            tableObjects.put(tableName, tableObject);
        }

        tableObjects.processRelationships();

    }

}