01: /**
02: * Copyright 2006 Webmedia Group Ltd.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: **/package org.araneaframework.backend.list.helper.reader;
16:
17: import java.sql.ResultSet;
18: import java.util.HashMap;
19: import java.util.Map;
20:
21: import org.araneaframework.uilib.list.util.Converter;
22:
23: /**
24: * ResultSetColumnReader that can have custom converters for each column.
25: * <p>
26: * This implementation wraps an existing {@link ResultSetColumnReader}
27: * by adding additional behavior if a certain column is being read.
28: * </p>
29: * To add a custom converter for a column use method
30: * {@link #addResultSetDeconverterForColumn(String, Converter)}.
31: * For that column {@link Converter#reverseConvert(Object)} is invoked
32: * and {@link Converter#getDestinationType()} is used as a Java type
33: * retrieved from the {@link ResultSet}.
34: *
35: * @author Rein Raudjärv
36: *
37: * @since 1.1
38: */
39: public class ConverterBasedColumnReader extends
40: FilterResultSetColumnReader {
41:
42: private final Map converters = new HashMap();
43:
44: public ConverterBasedColumnReader(ResultSetColumnReader child) {
45: super (child);
46: }
47:
48: /**
49: * Adds a deconverter for <code>ResultSet</code>.
50: * <p>
51: * The converter is used to {@link Converter#reverseConvert(Object)}
52: * values from <code>ResultSet</code>.
53: *
54: * @param columnName
55: * ResultSet column name.
56: * @param converter
57: * converter that is used by <code>reverseConvert()</code>
58: * method.
59: */
60: public void addResultSetDeconverterForColumn(String columnName,
61: Converter converter) {
62: this .converters.put(columnName, converter);
63: }
64:
65: public Object readFromResultSet(String columnName,
66: ResultSet resultSet, Class javaType) {
67: // Find converter
68: Converter converter = (Converter) converters.get(columnName);
69: if (converter == null) {
70: // No converter registered
71: return super .readFromResultSet(columnName, resultSet,
72: javaType);
73: }
74: // Converter found
75:
76: // Change type that is used to read data
77: javaType = converter.getDestinationType();
78:
79: // Read data
80: Object value = super .readFromResultSet(columnName, resultSet,
81: javaType);
82:
83: // Convert
84: value = converter.reverseConvert(value);
85:
86: return value;
87: }
88:
89: }
|