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.io.ByteArrayInputStream;
020: import java.io.InputStream;
021: import java.util.Iterator;
022: import javax.sql.DataSource;
023:
024: import org.compass.core.CompassException;
025: import org.compass.core.Property;
026: import org.compass.core.config.InputStreamMappingResolver;
027: import org.compass.gps.device.jdbc.mapping.AutoGenerateMapping;
028: import org.compass.gps.device.jdbc.mapping.ColumnToPropertyMapping;
029: import org.compass.gps.device.jdbc.mapping.DataColumnToPropertyMapping;
030: import org.compass.gps.device.jdbc.mapping.IdColumnToPropertyMapping;
031: import org.compass.gps.device.jdbc.mapping.ResultSetToResourceMapping;
032:
033: /**
034: * A Compass::Core <code>MappingResolver</code>, which can generate compass
035: * mappings (<code>Resource</code> mapping)
036: * {@link org.compass.gps.device.jdbc.mapping.ResultSetToResourceMapping}.
037: * <p/>
038: * Other relevant mapping settings can be set as well, such as subIndex, all,
039: * allMetaData, and the allTermVector.
040: * <p/>
041: * The required property is the
042: * {@link org.compass.gps.device.jdbc.mapping.ResultSetToResourceMapping}, and
043: * the <code>DataSource</code>.
044: * <p/>
045: * Using the mapping builder helps automatically generate compass mapping files (<code>Resource</code>
046: * mappings) based on the configuration of the
047: * {@link org.compass.gps.device.jdbc.mapping.ResultSetToResourceMapping} or one
048: * of it's sub classes (like
049: * {@link org.compass.gps.device.jdbc.mapping.TableToResourceMapping}).
050: *
051: * @author kimchy
052: */
053: public class ResultSetResourceMappingResolver implements
054: InputStreamMappingResolver {
055:
056: private ResultSetToResourceMapping mapping;
057:
058: private DataSource dataSource;
059:
060: private String subIndex;
061:
062: private boolean all = true;
063:
064: private String allMetaData;
065:
066: private Property.TermVector allTermVector;
067:
068: /**
069: * Creates a new mapping builder. Must set the
070: * {@link #setMapping(ResultSetToResourceMapping)}, and the
071: * {@link #setDataSource(DataSource)}.
072: */
073: public ResultSetResourceMappingResolver() {
074:
075: }
076:
077: /**
078: * Creates a new mapping builder, using the mapping and the data source.
079: *
080: * @param mapping The mapping that will be used to generate compass mapping
081: * definition.
082: */
083: public ResultSetResourceMappingResolver(
084: ResultSetToResourceMapping mapping, DataSource dataSource) {
085: this .mapping = mapping;
086: this .dataSource = dataSource;
087: }
088:
089: public String getName() {
090: return "" + subIndex + ".cpm.xml";
091: }
092:
093: /**
094: * Generates the compass mapping definitions.
095: */
096: public InputStream getMappingAsInputStream()
097: throws CompassException {
098: StringBuffer sb = new StringBuffer();
099: sb.append("<?xml version=\"1.0\"?>");
100: sb.append("<!DOCTYPE compass-core-mapping PUBLIC ");
101: sb
102: .append(" \"-//Compass/Compass Core Mapping DTD 1.0//EN\"");
103: sb
104: .append(" \"http://www.opensymphony.com/compass/dtd/compass-core-mapping.dtd\">");
105: sb.append("<compass-core-mapping>");
106: sb.append(" <resource alias=\"").append(mapping.getAlias())
107: .append("\"");
108: if (subIndex != null) {
109: sb.append(" sub-index=\"").append(subIndex).append("\"");
110: }
111: sb.append(" >");
112:
113: sb.append("<all enable=\"").append(all).append("\" ");
114: if (allMetaData != null) {
115: sb.append(" name=\"").append(allMetaData).append("\"");
116: }
117: if (allTermVector != null) {
118: sb.append(" term-vector=\"").append(
119: Property.TermVector.toString(allTermVector))
120: .append("\"");
121: }
122: sb.append(" />");
123:
124: if (mapping.idMappingsSize() == 0) {
125: if (mapping instanceof AutoGenerateMapping) {
126: ((AutoGenerateMapping) mapping)
127: .generateMappings(this .dataSource);
128: }
129: if (mapping.idMappingsSize() == 0) {
130: throw new JdbcGpsDeviceException(
131: "Can not generate resource mapping with no id to column mappings defined");
132: }
133: }
134: for (Iterator it = mapping.idMappingsIt(); it.hasNext();) {
135: IdColumnToPropertyMapping idMapping = (IdColumnToPropertyMapping) it
136: .next();
137: generateResourceIdMapping("resource-id", idMapping, sb);
138: }
139: for (Iterator it = mapping.dataMappingsIt(); it.hasNext();) {
140: DataColumnToPropertyMapping dataMapping = (DataColumnToPropertyMapping) it
141: .next();
142: generateResourceProperyMapping("resource-property",
143: dataMapping, sb);
144: }
145: sb.append(" </resource>");
146: sb.append("</compass-core-mapping>");
147: return new ByteArrayInputStream(sb.toString().getBytes());
148: }
149:
150: private void generateResourceIdMapping(String tag,
151: ColumnToPropertyMapping mapping, StringBuffer sb) {
152: sb.append("<").append(tag).append(" ");
153: sb.append("name=\"").append(mapping.getPropertyName()).append(
154: "\"").append(" ");
155: if (mapping.getAnalyzer() != null) {
156: sb.append("analyzer=\"").append(mapping.getAnalyzer())
157: .append("\"").append(" ");
158: }
159: if (mapping.getConverter() != null) {
160: sb.append("converter=\"").append(mapping.getConverter())
161: .append("\"").append(" ");
162: }
163: sb.append(" />");
164: }
165:
166: private void generateResourceProperyMapping(String tag,
167: ColumnToPropertyMapping mapping, StringBuffer sb) {
168: sb.append("<").append(tag).append(" ");
169: sb.append("name=\"").append(mapping.getPropertyName()).append(
170: "\"").append(" ");
171: sb.append("index=\"").append(
172: Property.Index.toString(mapping.getPropertyIndex()))
173: .append("\"").append(" ");
174: sb.append("store=\"").append(
175: Property.Store.toString(mapping.getPropertyStore()))
176: .append("\"").append(" ");
177: sb.append("term-vector=\"").append(
178: Property.TermVector.toString(mapping
179: .getPropertyTermVector())).append("\"").append(
180: " ");
181: sb.append("exclude-from-all=\"").append(
182: mapping.isExcludeFromAll()).append("\"").append(" ");
183: if (mapping.getAnalyzer() != null) {
184: sb.append("analyzer=\"").append(mapping.getAnalyzer())
185: .append("\"").append(" ");
186: }
187: if (mapping.getConverter() != null) {
188: sb.append("converter=\"").append(mapping.getConverter())
189: .append("\"").append(" ");
190: }
191: sb.append(" />");
192: }
193:
194: /**
195: * Returns the mapping that will be used to generate the compass mapping
196: * definitions.
197: */
198: public ResultSetToResourceMapping getMapping() {
199: return mapping;
200: }
201:
202: /**
203: * Sets the mapping that will be used to generate the compass mapping
204: * definitions.
205: */
206: public void setMapping(ResultSetToResourceMapping mapping) {
207: this .mapping = mapping;
208: }
209:
210: /**
211: * Returns the subIndex that the <code>Resource</code> (alias) will be
212: * mapped to. Optional - null value means no subIndex part in the compass
213: * mapping definition.
214: */
215: public String getSubIndex() {
216: return subIndex;
217: }
218:
219: /**
220: * Sets the subIndex that the <code>Resource</code> (alias) will be mapped
221: * to. Optional - null value means no subIndex part in the compass mapping
222: * definition.
223: */
224: public void setSubIndex(String subIndex) {
225: this .subIndex = subIndex;
226: }
227:
228: /**
229: * Returns the all option of the compass mapping (if the all property will
230: * be saved or not). Optional, defaults to <code>true</code>.
231: */
232: public boolean isAll() {
233: return all;
234: }
235:
236: /**
237: * Sets the all option of the compass mapping (if the all property will be
238: * saved or not). Optional, defaults to <code>true</code>.
239: */
240: public void setAll(boolean all) {
241: this .all = all;
242: }
243:
244: /**
245: * Returns the all meta data name that will be used in the compass mapping.
246: * Optional, defaults to not be added to the compass mapping definitions.
247: */
248: public String getAllMetaData() {
249: return allMetaData;
250: }
251:
252: /**
253: * Sets the all meta data name that will be used in the compass mapping.
254: * Optional, defaults to not be added to the compass mapping definitions.
255: */
256: public void setAllMetaData(String allMetaData) {
257: this .allMetaData = allMetaData;
258: }
259:
260: /**
261: * Returns the term vector setting for the all property. Optional, defaults
262: * to not be added to the compass mapping definitions.
263: */
264: public Property.TermVector getAllTermVector() {
265: return allTermVector;
266: }
267:
268: /**
269: * Sets the term vector setting for the all property. Optional, defaults to
270: * not be added to the compass mapping definitions.
271: */
272: public void setAllTermVector(Property.TermVector allTermVector) {
273: this .allTermVector = allTermVector;
274: }
275:
276: /**
277: * Sets the term vector setting (as a parsable string) for the all property.
278: * Optional, defaults to not be added to the compass mapping definitions.
279: */
280: public void setAllTermVectorString(String allTermVector) {
281: this .allTermVector = Property.TermVector
282: .fromString(allTermVector);
283: }
284:
285: /**
286: * Returns the jdbc data source.
287: */
288: public DataSource getDataSource() {
289: return dataSource;
290: }
291:
292: /**
293: * Sets the jdbc data source.
294: */
295: public void setDataSource(DataSource dataSource) {
296: this.dataSource = dataSource;
297: }
298: }
|