001: /*
002: * Copyright 2004-2006 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.compass.gps.device.jdbc.mapping;
018:
019: import java.util.ArrayList;
020: import java.util.HashMap;
021: import java.util.Iterator;
022: import java.util.List;
023: import java.util.Map;
024:
025: /**
026: * Maps a Jdbc <code>ResultSet</code> to a Compass <code>Resource</code>.
027: * <p>
028: * The required mappings are the alias of the <code>Resource</code>, the
029: * select query that generates the <code>ResultSet</code>, and at least one
030: * id mapping that maps the <code>ResultSet</code> identifiers columns ({@link org.compass.gps.device.jdbc.mapping.IdColumnToPropertyMapping}).
031: * <p>
032: * Mapping data columns ({@link org.compass.gps.device.jdbc.mapping.DataColumnToPropertyMapping})
033: * is optional, but provides the meta data (<code>Resource Proeprty</code>)
034: * for searching. You can also enable (which is disabled by default) for
035: * indexing all the un mapped columns of the <code>ResultSet</code> using the
036: * {@link #setIndexUnMappedColumns(boolean)}.
037: * <p>
038: * For real time mirroring, the version query and at least one version column
039: * mapping ({@link org.compass.gps.device.jdbc.mapping.VersionColumnMapping})
040: * is required.
041: *
042: * @author kimchy
043: */
044: public class ResultSetToResourceMapping {
045:
046: private String alias;
047:
048: private String selectQuery;
049:
050: private String versionQuery;
051:
052: private List idMappings = new ArrayList();
053:
054: private List dataMappings = new ArrayList();
055:
056: private Map mappingsMap = new HashMap();
057:
058: private List versionMappings = new ArrayList();
059:
060: private boolean indexUnMappedColumns = false;
061:
062: /**
063: * Creates a new <code>ResultSet</code> to <code>Resource</code>
064: * mapping. Must set at least the alias, the select query, and at lease one
065: * id mapping.
066: * <p>
067: * Indexing of unmapped columns is diasabled by default.
068: */
069: public ResultSetToResourceMapping() {
070:
071: }
072:
073: /**
074: * Creates a new <code>ResultSet</code> to <code>Resource</code> mapping
075: * using the supplied alias and select query. At least one id mapping is
076: * required to be configured as well.
077: * <p>
078: * Indexing of unmapped columns is diasabled by default.
079: *
080: * @param alias
081: * The alias of the mapped <code>Resource</code>.
082: * @param selectQuery
083: * The select query that generates teh <code>ResultSet</code>.
084: */
085: public ResultSetToResourceMapping(String alias, String selectQuery) {
086: this (alias, selectQuery, null);
087: }
088:
089: /**
090: * Creates a new <code>ResultSet</code> to <code>Resource</code> mapping
091: * using the supplied alias and select query and one id column mapping.
092: * <p>
093: * If additional id column mappings are required, use
094: * {@link #addIdMapping(ColumnToPropertyMapping)} to add them.
095: * <p>
096: * Indexing of unmapped columns is diasabled by default.
097: *
098: * @param alias
099: * The alias of the mapped <code>Resource</code>.
100: * @param selectQuery
101: * The select query that generates teh <code>ResultSet</code>.
102: * @param idMapping
103: * Id mapping used to map an id column.
104: */
105: public ResultSetToResourceMapping(String alias, String selectQuery,
106: ColumnToPropertyMapping idMapping) {
107: this .alias = alias;
108: this .selectQuery = selectQuery;
109: if (idMapping != null) {
110: this .idMappings.add(idMapping);
111: }
112: }
113:
114: /**
115: * Returns the alias of the mapped <code>Resource</code>.
116: */
117: public String getAlias() {
118: return alias;
119: }
120:
121: /**
122: * Sets the alias of the mapped <code>Resource</code>.
123: */
124: public void setAlias(String alias) {
125: this .alias = alias;
126: }
127:
128: /**
129: * Returns the select query that generates the <code>ResultSet</code>.
130: */
131: public String getSelectQuery() {
132: return selectQuery;
133: }
134:
135: /**
136: * Sets the select query that generates the <code>ResultSet</code>.
137: */
138: public void setSelectQuery(String selectQuery) {
139: this .selectQuery = selectQuery;
140: }
141:
142: /**
143: * Returns the version query that is used for real time mirror data changes.
144: */
145: public String getVersionQuery() {
146: return versionQuery;
147: }
148:
149: /**
150: * Sets the version query that is used for real time mirror data changes.
151: */
152: public void setVersionQuery(String snapshotQuery) {
153: this .versionQuery = snapshotQuery;
154: }
155:
156: /**
157: * Adds id column mapping.
158: */
159: public void addIdMapping(ColumnToPropertyMapping idMapping) {
160: this .idMappings.add(idMapping);
161: addMappingToMap(idMapping);
162: }
163:
164: /**
165: * Adds data column mapping.
166: */
167: public void addDataMapping(ColumnToPropertyMapping dataMapping) {
168: this .dataMappings.add(dataMapping);
169: addMappingToMap(dataMapping);
170: }
171:
172: /**
173: * Adds version column mapping for real time mirror data changes.
174: */
175: public void addVersionMapping(
176: VersionColumnMapping versionColumnMapping) {
177: this .versionMappings.add(versionColumnMapping);
178: addMappingToMap(versionColumnMapping);
179: }
180:
181: /**
182: * Returns a list of all the {@link ColumnMapping}s that are mapped to the
183: * column name.
184: *
185: * @param columnName
186: * The column name for the mapped columns.
187: * @return A list of all the {@link ColumnMapping}s.
188: */
189: public List getMappingsForColumn(String columnName) {
190: return (List) mappingsMap.get(columnName);
191: }
192:
193: /**
194: * Returns a list of all the {@link ColumnMapping}s that are mapped to the
195: * column index.
196: *
197: * @param columnIndex
198: * The column index for the mapped columns.
199: * @return A list of all the {@link ColumnMapping}s.
200: */
201: public List getMappingsForColumn(int columnIndex) {
202: return (List) mappingsMap.get("" + columnIndex);
203: }
204:
205: /**
206: * Adds the array of {@link ColumnToPropertyMapping}s which acts as the id
207: * column mappings.
208: *
209: * @param idMappingsArr
210: * An array of {@link ColumnToPropertyMapping}s to add.
211: */
212: public void setIdMappings(ColumnToPropertyMapping[] idMappingsArr) {
213: for (int i = 0; i < idMappingsArr.length; i++) {
214: addIdMapping(idMappingsArr[i]);
215: }
216: }
217:
218: /**
219: * Adds of array of {@link ColumnToPropertyMapping}s which acts as the data
220: * column mappings.
221: *
222: * @param dataMappingsArr
223: * An array of {@link ColumnToPropertyMapping}s to add.
224: */
225: public void setDataMappings(
226: ColumnToPropertyMapping[] dataMappingsArr) {
227: for (int i = 0; i < dataMappingsArr.length; i++) {
228: addDataMapping(dataMappingsArr[i]);
229: }
230: }
231:
232: /**
233: * Adds an array of {@link ColumnToPropertyMapping}s which acts as the
234: * version column mappings.
235: *
236: * @param versionColumnMappingsArr
237: * An array of {@link ColumnToPropertyMapping}s to add.
238: */
239: public void setVersionMappings(
240: VersionColumnMapping[] versionColumnMappingsArr) {
241: for (int i = 0; i < versionColumnMappingsArr.length; i++) {
242: addVersionMapping(versionColumnMappingsArr[i]);
243: }
244: }
245:
246: /**
247: * Returns an iterator over the id mappings.
248: */
249: public Iterator idMappingsIt() {
250: return idMappings.iterator();
251: }
252:
253: /**
254: * Returns the size of the id mappings.
255: */
256: public int idMappingsSize() {
257: return idMappings.size();
258: }
259:
260: /**
261: * Returns an iterator over the data mappings.
262: */
263: public Iterator dataMappingsIt() {
264: return dataMappings.iterator();
265: }
266:
267: /**
268: * Returns the size of the data mappings.
269: */
270: public int dataMappingsSize() {
271: return dataMappings.size();
272: }
273:
274: /**
275: * Returns an iterator of the version mappings.
276: */
277: public Iterator versionMappingsIt() {
278: return versionMappings.iterator();
279: }
280:
281: /**
282: * Returns the size of the version mappings.
283: */
284: public int versionMappingsSize() {
285: return versionMappings.size();
286: }
287:
288: public Iterator mappingsIt() {
289: return mappingsMap.values().iterator();
290: }
291:
292: /**
293: * Should the mapping index unmapped columns as data columns.
294: */
295: public boolean isIndexUnMappedColumns() {
296: return indexUnMappedColumns;
297: }
298:
299: /**
300: * Returns if the mapping should set to index unmapped columns as data
301: * columns.
302: */
303: public void setIndexUnMappedColumns(boolean indexUnMappedColumns) {
304: this .indexUnMappedColumns = indexUnMappedColumns;
305: }
306:
307: /**
308: * Is the mapping support versioning. It is a derived property, returning
309: * <code>false</code> if no version column mapping are set.
310: */
311: public boolean supportsVersioning() {
312: return this .versionMappings.size() != 0;
313: }
314:
315: private String getColumnKey(ColumnMapping columnMapping) {
316: if (columnMapping.isUsingColumnIndex()) {
317: return "" + columnMapping.getColumnIndex();
318: }
319: return columnMapping.getColumnName();
320: }
321:
322: private void addMappingToMap(ColumnMapping columnMapping) {
323: String key = getColumnKey(columnMapping);
324: List list = (List) mappingsMap.get(key);
325: if (list == null) {
326: list = new ArrayList();
327: mappingsMap.put(key, list);
328: }
329: list.add(columnMapping);
330: }
331:
332: public String toString() {
333: StringBuffer sb = new StringBuffer();
334: sb.append("alias [" + alias + "] ");
335: sb.append("select query [" + selectQuery + "] ");
336: sb.append("version[" + supportsVersioning() + "] ");
337: if (supportsVersioning()) {
338: sb.append(" version query [" + versionQuery + "] ");
339: }
340: sb
341: .append("indexUnMappedColumns[" + indexUnMappedColumns
342: + "] ");
343: sb.append("Id Mappings [");
344: for (Iterator it = idMappingsIt(); it.hasNext();) {
345: sb.append("{" + it.next() + "}, ");
346: }
347: if (supportsVersioning()) {
348: sb.append("] Version Mappings [");
349: for (Iterator it = versionMappingsIt(); it.hasNext();) {
350: sb.append("{" + it.next() + "}, ");
351: }
352: }
353: sb.append("] Data Mappings [");
354: for (Iterator it = dataMappingsIt(); it.hasNext();) {
355: sb.append("{" + it.next() + "}, ");
356: }
357: sb.append("}");
358: return sb.toString();
359: }
360:
361: }
|