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;
018:
019: import java.sql.ResultSet;
020: import java.sql.ResultSetMetaData;
021: import java.sql.SQLException;
022: import java.util.Iterator;
023:
024: import org.compass.core.Compass;
025: import org.compass.core.CompassSession;
026: import org.compass.core.Property;
027: import org.compass.core.Resource;
028: import org.compass.core.mapping.ResourceMapping;
029: import org.compass.core.mapping.ResourcePropertyMapping;
030: import org.compass.core.spi.InternalCompass;
031: import org.compass.core.spi.InternalCompassSession;
032: import org.compass.gps.device.jdbc.dialect.JdbcDialect;
033: import org.compass.gps.device.jdbc.mapping.ColumnToPropertyMapping;
034: import org.compass.gps.device.jdbc.mapping.ResultSetToResourceMapping;
035: import org.compass.gps.device.jdbc.mapping.VersionColumnMapping;
036: import org.compass.gps.device.jdbc.snapshot.JdbcAliasRowSnapshot;
037:
038: /**
039: * A helper marshaller from a <code>ResultSet</code> current row to a
040: * <code>Resource</code> and/or to a
041: * {@link org.compass.gps.device.jdbc.snapshot.JdbcAliasRowSnapshot}.
042: *
043: * @author kimchy
044: */
045: public class ResultSetRowMarshallHelper {
046:
047: private ResultSetToResourceMapping mapping;
048:
049: private InternalCompassSession session;
050:
051: private JdbcDialect dialect;
052:
053: private JdbcAliasRowSnapshot rowSnapshot;
054:
055: private Resource resource;
056:
057: private boolean marshallResource = false;
058:
059: private boolean marshallVersioning = false;
060:
061: private ResourceMapping resourceMapping;
062:
063: /**
064: * Creates a new marshaller helper that will marhsall the
065: * <code>ResultSet</code> to the given <code>Resource</code>.
066: */
067: public ResultSetRowMarshallHelper(
068: ResultSetToResourceMapping mapping, CompassSession session,
069: JdbcDialect dialect, Resource resource) {
070: this (mapping, session, dialect, resource, null);
071: }
072:
073: /**
074: * Creates a new marshaller helper that will marshall the
075: * <code>ResultSet</code> to the given {@link JdbcAliasRowSnapshot}.
076: */
077: public ResultSetRowMarshallHelper(
078: ResultSetToResourceMapping mapping, JdbcDialect dialect,
079: JdbcAliasRowSnapshot rowSnapshot, Compass compass) {
080: this (mapping, null, dialect, null, rowSnapshot, compass);
081: }
082:
083: public ResultSetRowMarshallHelper(
084: ResultSetToResourceMapping mapping, CompassSession session,
085: JdbcDialect dialect, Resource resource,
086: JdbcAliasRowSnapshot rowSnapshot) {
087: this (mapping, session, dialect, resource, rowSnapshot,
088: ((InternalCompassSession) session).getCompass());
089: }
090:
091: /**
092: * Creates a new marshaller helper that will marhsall that
093: * <code>ResultSet</code> to both the given <code>Resource</code> and
094: * {@link JdbcAliasRowSnapshot}.
095: */
096: public ResultSetRowMarshallHelper(
097: ResultSetToResourceMapping mapping, CompassSession session,
098: JdbcDialect dialect, Resource resource,
099: JdbcAliasRowSnapshot rowSnapshot, Compass compass) {
100: this .mapping = mapping;
101: this .session = (InternalCompassSession) session;
102: this .dialect = dialect;
103: this .rowSnapshot = rowSnapshot;
104: resourceMapping = ((InternalCompass) compass).getMapping()
105: .getMappingByAlias(mapping.getAlias());
106: if (rowSnapshot == null || !mapping.supportsVersioning()) {
107: marshallVersioning = false;
108: } else {
109: marshallVersioning = true;
110: }
111: this .resource = resource;
112: if (resource == null) {
113: marshallResource = false;
114: } else {
115: marshallResource = true;
116: }
117: }
118:
119: /**
120: * Marshalls the <code>ResultSet</code>.
121: */
122: public void marshallResultSet(ResultSet rs) throws SQLException {
123: marshallIds(rs);
124: marshallVersionsIfNeeded(rs);
125: marshallMappedData(rs);
126: marshallUnMappedIfNeeded(rs);
127: }
128:
129: public void marshallIds(ResultSet rs) throws SQLException {
130: for (Iterator it = mapping.idMappingsIt(); it.hasNext();) {
131: ColumnToPropertyMapping ctpMapping = (ColumnToPropertyMapping) it
132: .next();
133: String value = dialect.getStringValue(rs, ctpMapping);
134: if (value == null) {
135: throw new JdbcGpsDeviceException("Id [" + ctpMapping
136: + "] for alias [" + mapping.getAlias()
137: + "] can not be null");
138: }
139: if (marshallResource) {
140: marshallProperty(ctpMapping, value);
141: }
142: if (marshallVersioning) {
143: rowSnapshot.addIdValue(value);
144: }
145: }
146: }
147:
148: public void marshallMappedData(ResultSet rs) throws SQLException {
149: if (!marshallResource) {
150: return;
151: }
152: for (Iterator it = mapping.dataMappingsIt(); it.hasNext();) {
153: ColumnToPropertyMapping ctpMapping = (ColumnToPropertyMapping) it
154: .next();
155: String value = dialect.getStringValue(rs, ctpMapping);
156: if (value == null) {
157: continue;
158: }
159: marshallProperty(ctpMapping, value);
160: }
161: }
162:
163: public void marshallVersionsIfNeeded(ResultSet rs)
164: throws SQLException {
165: if (!marshallVersioning) {
166: return;
167: }
168: for (Iterator it = mapping.versionMappingsIt(); it.hasNext();) {
169: VersionColumnMapping versionMapping = (VersionColumnMapping) it
170: .next();
171: Long version = dialect.getVersion(rs, versionMapping);
172: rowSnapshot.addVersionValue(version);
173: }
174: }
175:
176: public void marshallUnMappedIfNeeded(ResultSet rs)
177: throws SQLException {
178: if (!marshallResource || !mapping.isIndexUnMappedColumns()) {
179: return;
180: }
181: ResultSetMetaData metaData = rs.getMetaData();
182: int count = metaData.getColumnCount();
183: for (int i = 1; i <= count; i++) {
184: String columnName = metaData.getColumnName(i);
185: if (mapping.getMappingsForColumn(columnName) == null
186: && mapping.getMappingsForColumn(i) == null) {
187: String value = dialect.getStringValue(rs, i);
188: if (value == null) {
189: continue;
190: }
191: Property p = session.getCompass().getResourceFactory()
192: .createProperty(columnName, value,
193: Property.Store.YES,
194: Property.Index.TOKENIZED);
195: resource.addProperty(p);
196: }
197: }
198: }
199:
200: public void marshallProperty(ColumnToPropertyMapping ctpMapping,
201: String value) {
202: ResourcePropertyMapping propertyMapping = resourceMapping
203: .getResourcePropertyMapping(ctpMapping
204: .getPropertyName());
205: if (propertyMapping == null) {
206: Property p = session.getCompass().getResourceFactory()
207: .createProperty(ctpMapping.getPropertyName(),
208: value, ctpMapping.getPropertyStore(),
209: ctpMapping.getPropertyIndex(),
210: ctpMapping.getPropertyTermVector());
211: p.setBoost(ctpMapping.getBoost());
212: resource.addProperty(p);
213: } else {
214: // has explicit mappings (not auto generated), use additional settings (like analyzer and such).
215: resource.addProperty(ctpMapping.getPropertyName(), value);
216: }
217: }
218:
219: }
|