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