001: package org.geotools.data.sql;
002:
003: import java.io.IOException;
004: import java.util.Collections;
005: import java.util.HashMap;
006: import java.util.Map;
007: import java.util.logging.Logger;
008:
009: import org.geotools.data.DataSourceException;
010: import org.geotools.data.DataStore;
011: import org.geotools.data.DataStoreFactorySpi;
012: import org.geotools.data.DataStoreFinder;
013:
014: /**
015: * A DataStoreFactory that produces any {@link org.geotools.data.sql.SqlDataStore}, whose
016: * only purpose is to simplify the process of defining in-process views by
017: * specifying them as plain SQL SELECT statements in the Map of parameters.
018: * <p>
019: * The only mandatory parameter to use this factory is <code>"wrapper.type"</code>, whose
020: * value must be the fixed literal <code>"sql"</code>. Though a bunch of other parameters
021: * will be needed to actually find the surrogate datastore, those parameters are
022: * dependent on the parameters expected by the surrogate datastore factory.
023: * To prevent parameter name clashes and the surrogate being found prior to this
024: * factory in the lookup process, all surrogate datastre's parameters must be
025: * prepended by the literal <code>"surrogate."</code>, as in the following example:
026: * <pre><code>
027: * wrapper.type=SQL
028: * surrogate.property1=...
029: * surrogate.property2=...
030: * surrogate.property3=...
031: * surrogate.property4=...
032: *
033: * sqlView.1.typeName = ViewType1
034: * sqlView.1.sqlQuery = select gid, the_geom, table2.someField \
035: * from table1, table2 \
036: * where table1.gid = table2.table1_id
037: *
038: * sqlView.2.typeName = ViewType2
039: * sqlView.2.sqlQuery = select ...
040: * </code></pre>
041: * </p>
042: * <p>
043: * As noted above, apart from the surrogate parameters you'll have to specify
044: * which SQL queries to use as FeatureType "views". The process is quite simple:
045: * prepend <code>"sqlView.N."</code> to <code>"typeName"</code> and <code>"sqlQuery"</code>
046: * for each view you want to define.
047: * </p>
048: *
049: * @author Gabriel Roldan, Axios Engineering
050: * @version $id$
051: * @since 2.3.x
052: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/unsupported/community-schemas/sql-datastore/src/org/geotools/data/sql/SqlDataStoreFactory.java $
053: */
054: public class SqlDataStoreFactory implements DataStoreFactorySpi {
055: private static final Logger LOGGER = org.geotools.util.logging.Logging
056: .getLogger(SqlDataStore.class.getPackage().getName());
057:
058: private static final String WRAPPERTYPE_FIXED_VALUE = "sql";
059:
060: private static Param WRAPPERTYPE = new Param("wrapper.type",
061: String.class,
062: "Needs to be the fixed value 'sql' to be recognized", true,
063: WRAPPERTYPE_FIXED_VALUE);
064:
065: public DataStore createDataStore(Map params) throws IOException {
066: if (!canProcess(params)) {
067: throw new DataSourceException("can't process " + params);
068: }
069: Map surrogateParams = ViewRegisteringFactoryHelper
070: .cleanUpSurrogateParams(params);
071: DataStore surrogate = DataStoreFinder
072: .getDataStore(surrogateParams);
073: if (surrogate == null) {
074: throw new DataSourceException(
075: "Can't locate surrogate datastore: "
076: + surrogateParams);
077: }
078: if (!(surrogate instanceof SqlDataStore)) {
079: throw new DataSourceException(
080: "Surrogate datastore is not a SqlDataStore: "
081: + surrogate.getClass());
082: }
083:
084: SqlDataStore sqlDs = (SqlDataStore) surrogate;
085: ViewRegisteringFactoryHelper.registerSqlViews(sqlDs, params);
086:
087: return sqlDs;
088: }
089:
090: public DataStore createNewDataStore(Map params) throws IOException {
091: throw new UnsupportedOperationException();
092: }
093:
094: public String getDisplayName() {
095: return "SQL select based datastore";
096: }
097:
098: public String getDescription() {
099: return "DataStore that adds in-process views to ahother one that implements SqlDataStore";
100: }
101:
102: public Param[] getParametersInfo() {
103: return new Param[] { WRAPPERTYPE };
104: }
105:
106: /**
107: * Checks only if the "wrapper.type" keywords is equal to the
108: * fixed value "sql".
109: */
110: public boolean canProcess(Map params) {
111: try {
112: return WRAPPERTYPE_FIXED_VALUE.equalsIgnoreCase(String
113: .valueOf(WRAPPERTYPE.lookUp(params)));
114: } catch (Exception e) {
115: LOGGER.finer("lookup failed on parameter "
116: + WRAPPERTYPE.key);
117: return false;
118: }
119: }
120:
121: /**
122: * @return <code>true</code>
123: * @REVISIT: checking surrogate's datastore availability
124: * would be costly which goes against this method intentions.
125: */
126: public boolean isAvailable() {
127: return true;
128: }
129:
130: /**
131: * @return an empty Map. No implementation hints
132: * defined for this datastore.
133: */
134: public Map getImplementationHints() {
135: return Collections.EMPTY_MAP;
136: }
137:
138: }
|