[Carbon-commits] [Carbon-Graphite] svn commit r121307 - in trunk/graphite/components/data-services/org.wso2.carbon.dataservices.core/src/main/java/org/wso2/carbon/dataservices/core/description: config query

dinusha at wso2.com dinusha at wso2.com
Thu Feb 23 14:38:43 PST 2012


Author: dinusha
Date: Thu Feb 23 14:38:42 2012
New Revision: 121307
URL: http://wso2.org/svn/browse/wso2?view=rev&revision=121307

Log:
Fixing CARBON-12443 in trunk.

Added:
   trunk/graphite/components/data-services/org.wso2.carbon.dataservices.core/src/main/java/org/wso2/carbon/dataservices/core/description/config/SparqlEndpointConfig.java
   trunk/graphite/components/data-services/org.wso2.carbon.dataservices.core/src/main/java/org/wso2/carbon/dataservices/core/description/query/RdfFileQuery.java
   trunk/graphite/components/data-services/org.wso2.carbon.dataservices.core/src/main/java/org/wso2/carbon/dataservices/core/description/query/SparqlEndpointQuery.java
   trunk/graphite/components/data-services/org.wso2.carbon.dataservices.core/src/main/java/org/wso2/carbon/dataservices/core/description/query/SparqlQueryBase.java
Modified:
   trunk/graphite/components/data-services/org.wso2.carbon.dataservices.core/src/main/java/org/wso2/carbon/dataservices/core/description/config/ConfigFactory.java
   trunk/graphite/components/data-services/org.wso2.carbon.dataservices.core/src/main/java/org/wso2/carbon/dataservices/core/description/query/QueryFactory.java
   trunk/graphite/components/data-services/org.wso2.carbon.dataservices.core/src/main/java/org/wso2/carbon/dataservices/core/description/query/QuerySerializer.java

Modified: trunk/graphite/components/data-services/org.wso2.carbon.dataservices.core/src/main/java/org/wso2/carbon/dataservices/core/description/config/ConfigFactory.java
URL: http://wso2.org/svn/browse/wso2/trunk/graphite/components/data-services/org.wso2.carbon.dataservices.core/src/main/java/org/wso2/carbon/dataservices/core/description/config/ConfigFactory.java?rev=121307&r1=121306&r2=121307&view=diff
==============================================================================
--- trunk/graphite/components/data-services/org.wso2.carbon.dataservices.core/src/main/java/org/wso2/carbon/dataservices/core/description/config/ConfigFactory.java	(original)
+++ trunk/graphite/components/data-services/org.wso2.carbon.dataservices.core/src/main/java/org/wso2/carbon/dataservices/core/description/config/ConfigFactory.java	Thu Feb 23 14:38:42 2012
@@ -18,23 +18,16 @@
  */
 package org.wso2.carbon.dataservices.core.description.config;
 
-import java.util.Map;
-
-import javax.xml.namespace.QName;
-
 import org.apache.axiom.om.OMElement;
 import org.wso2.carbon.dataservices.common.DBConstants;
-import org.wso2.carbon.dataservices.common.DBConstants.CSV;
-import org.wso2.carbon.dataservices.common.DBConstants.DBSFields;
-import org.wso2.carbon.dataservices.common.DBConstants.DataSourceTypes;
-import org.wso2.carbon.dataservices.common.DBConstants.Excel;
-import org.wso2.carbon.dataservices.common.DBConstants.GSpread;
-import org.wso2.carbon.dataservices.common.DBConstants.RDBMS;
-import org.wso2.carbon.dataservices.common.DBConstants.RDF;
+import org.wso2.carbon.dataservices.common.DBConstants.*;
 import org.wso2.carbon.dataservices.core.DBUtils;
 import org.wso2.carbon.dataservices.core.DataServiceFault;
 import org.wso2.carbon.dataservices.core.engine.DataService;
 
+import javax.xml.namespace.QName;
+import java.util.Map;
+
 /**
  * A factory class to create config objects to the given data.
  */
@@ -56,7 +49,9 @@
 			return getExcelConfig(dataService, configId, properties);
 		} else if (DataSourceTypes.RDF.equals(configType)) {
 			return getRDFConfig(dataService, configId, properties);
-		} else if (DataSourceTypes.CSV.equals(configType)) {
+		} else if (DataSourceTypes.SPARQL.equals(configType)) {
+            return getSparqlEndpointConfig(dataService, configId, properties);
+        } else if (DataSourceTypes.CSV.equals(configType)) {
 			return getCSVConfig(dataService, configId, properties);
 		} else if (DataSourceTypes.GDATA_SPREADSHEET.equals(configType)) {
 			return getGSpreadConfig(dataService, configId, properties);
@@ -93,6 +88,12 @@
 		RDFConfig config = new RDFConfig(dataService, configId, properties);
 		return config;
 	}
+
+    private static SparqlEndpointConfig getSparqlEndpointConfig(DataService dataService, String configId,
+			Map<String, String> properties) throws DataServiceFault {
+		SparqlEndpointConfig config = new SparqlEndpointConfig(dataService, configId, properties);
+		return config;
+	}
 	
 	private static CSVConfig getCSVConfig(DataService dataService, String configId, 
 			Map<String, String> properties) throws DataServiceFault {
@@ -136,7 +137,9 @@
 		    return DataSourceTypes.EXCEL;
 		} else if  (properties.get(RDF.DATASOURCE) != null) {
 		    return DataSourceTypes.RDF;
-		} else if (properties.get(CSV.DATASOURCE) != null) {
+		} else if (properties.get(SPARQL.DATASOURCE) != null) {
+            return DataSourceTypes.SPARQL;
+        } else if (properties.get(CSV.DATASOURCE) != null) {
 		    return DataSourceTypes.CSV;
 		} else if (properties.get(DBConstants.JNDI.INITIAL_CONTEXT_FACTORY) != null) {
 		    return DataSourceTypes.JNDI;

Added: trunk/graphite/components/data-services/org.wso2.carbon.dataservices.core/src/main/java/org/wso2/carbon/dataservices/core/description/config/SparqlEndpointConfig.java
URL: http://wso2.org/svn/browse/wso2/trunk/graphite/components/data-services/org.wso2.carbon.dataservices.core/src/main/java/org/wso2/carbon/dataservices/core/description/config/SparqlEndpointConfig.java?pathrev=121307
==============================================================================
--- (empty file)
+++ trunk/graphite/components/data-services/org.wso2.carbon.dataservices.core/src/main/java/org/wso2/carbon/dataservices/core/description/config/SparqlEndpointConfig.java	Thu Feb 23 14:38:42 2012
@@ -0,0 +1,57 @@
+/*
+ *  Copyright (c) 2005-2010, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
+ *
+ *  WSO2 Inc. licenses this file to you under the Apache License,
+ *  Version 2.0 (the "License"); you may not use this file except
+ *  in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ *
+ */
+package org.wso2.carbon.dataservices.core.description.config;
+
+import java.util.Map;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.wso2.carbon.dataservices.common.DBConstants;
+import org.wso2.carbon.dataservices.common.DBConstants.DataSourceTypes;
+import org.wso2.carbon.dataservices.core.engine.DataService;
+
+/**
+ * This class represents a RDF based data source configuration.
+ */
+public class SparqlEndpointConfig extends Config {
+
+	private static final Log log = LogFactory.getLog(ExcelConfig.class);
+	
+	private String sparqlEndpointUrl;
+	
+	public SparqlEndpointConfig(DataService dataService, String configId, Map<String, String> properties) {
+		super(dataService, configId, DataSourceTypes.SPARQL, properties);
+		
+		this.sparqlEndpointUrl = this.getProperty(DBConstants.SPARQL.DATASOURCE).trim();
+	}
+	
+	public String getSparqlEndpoint() {
+		return this.sparqlEndpointUrl;
+	}
+	
+	@Override
+	public boolean isActive() {
+		return true;
+	}
+	
+	public void close() {
+		/* nothing to close */
+	}
+	
+}
\ No newline at end of file

Modified: trunk/graphite/components/data-services/org.wso2.carbon.dataservices.core/src/main/java/org/wso2/carbon/dataservices/core/description/query/QueryFactory.java
URL: http://wso2.org/svn/browse/wso2/trunk/graphite/components/data-services/org.wso2.carbon.dataservices.core/src/main/java/org/wso2/carbon/dataservices/core/description/query/QueryFactory.java?rev=121307&r1=121306&r2=121307&view=diff
==============================================================================
--- trunk/graphite/components/data-services/org.wso2.carbon.dataservices.core/src/main/java/org/wso2/carbon/dataservices/core/description/query/QueryFactory.java	(original)
+++ trunk/graphite/components/data-services/org.wso2.carbon.dataservices.core/src/main/java/org/wso2/carbon/dataservices/core/description/query/QueryFactory.java	Thu Feb 23 14:38:42 2012
@@ -66,7 +66,9 @@
 		} else if (DataSourceTypes.GDATA_SPREADSHEET.equals(sourceType)) {
 			query = createGSpreadQuery(dataService, queryEl);
         } else if (DataSourceTypes.RDF.equals(sourceType)) {
-        	query = createSPARQLQuery(dataService, queryEl);
+        	query = createRdfFileQuery(dataService, queryEl);
+        } else if (DataSourceTypes.SPARQL.equals(sourceType)) {
+        	query = createSparqlEndpointQuery(dataService, queryEl);
 		}  else if (DataSourceTypes.WEB.equals(sourceType)) {
 			query = createWebQuery(dataService, queryEl);
 		} else {
@@ -107,7 +109,7 @@
     	return inputNamespace;
     }
     
-	private static SPARQLQuery createSPARQLQuery(DataService dataService,
+	private static RdfFileQuery createRdfFileQuery(DataService dataService,
 			OMElement queryEl) throws DataServiceFault {
 		String queryId, configId, sparql, inputNamespace;
 		EventTrigger[] eventTriggers;
@@ -122,7 +124,29 @@
 		} catch (Exception e) {
 			throw new DataServiceFault(e, "Error in parsing SPARQL query element");
 		}		
-		SPARQLQuery query = new SPARQLQuery(dataService, queryId, configId,
+		RdfFileQuery query = new RdfFileQuery(dataService, queryId, configId,
+				sparql, getQueryParamsFromQueryElement(queryEl), result,
+				eventTriggers[0], eventTriggers[1],
+				extractAdvancedProps(queryEl), inputNamespace);
+		return query;
+	}
+	
+	private static SparqlEndpointQuery createSparqlEndpointQuery(DataService dataService,
+			OMElement queryEl) throws DataServiceFault {
+		String queryId, configId, sparql, inputNamespace;
+		EventTrigger[] eventTriggers;
+		Result result;
+		try {
+		    queryId = getQueryId(queryEl);
+		    configId = getConfigId(queryEl);
+		    sparql = queryEl.getFirstChildWithName(new QName(DBSFields.SPARQL)).getText();
+		    eventTriggers = getEventTriggers(dataService, queryEl);
+		    result = getResultFromQueryElement(dataService, queryEl);
+		    inputNamespace = extractQueryInputNamespace(dataService, result, queryEl);
+		} catch (Exception e) {
+			throw new DataServiceFault(e, "Error in parsing SPARQL query element");
+		}		
+		SparqlEndpointQuery query = new SparqlEndpointQuery(dataService, queryId, configId,
 				sparql, getQueryParamsFromQueryElement(queryEl), result,
 				eventTriggers[0], eventTriggers[1],
 				extractAdvancedProps(queryEl), inputNamespace);

Modified: trunk/graphite/components/data-services/org.wso2.carbon.dataservices.core/src/main/java/org/wso2/carbon/dataservices/core/description/query/QuerySerializer.java
URL: http://wso2.org/svn/browse/wso2/trunk/graphite/components/data-services/org.wso2.carbon.dataservices.core/src/main/java/org/wso2/carbon/dataservices/core/description/query/QuerySerializer.java?rev=121307&r1=121306&r2=121307&view=diff
==============================================================================
--- trunk/graphite/components/data-services/org.wso2.carbon.dataservices.core/src/main/java/org/wso2/carbon/dataservices/core/description/query/QuerySerializer.java	(original)
+++ trunk/graphite/components/data-services/org.wso2.carbon.dataservices.core/src/main/java/org/wso2/carbon/dataservices/core/description/query/QuerySerializer.java	Thu Feb 23 14:38:42 2012
@@ -18,13 +18,6 @@
  */
 package org.wso2.carbon.dataservices.core.description.query;
 
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
-import java.util.Map.Entry;
-
-import javax.xml.namespace.QName;
-
 import org.apache.axiom.om.OMElement;
 import org.apache.axiom.om.OMFactory;
 import org.wso2.carbon.dataservices.common.DBConstants;
@@ -32,22 +25,16 @@
 import org.wso2.carbon.dataservices.core.DBUtils;
 import org.wso2.carbon.dataservices.core.XSLTTransformer;
 import org.wso2.carbon.dataservices.core.description.event.EventTrigger;
-import org.wso2.carbon.dataservices.core.engine.CallQuery;
-import org.wso2.carbon.dataservices.core.engine.CallQueryGroup;
-import org.wso2.carbon.dataservices.core.engine.OutputElement;
-import org.wso2.carbon.dataservices.core.engine.OutputElementGroup;
-import org.wso2.carbon.dataservices.core.engine.ParamValue;
-import org.wso2.carbon.dataservices.core.engine.QueryParam;
-import org.wso2.carbon.dataservices.core.engine.Result;
-import org.wso2.carbon.dataservices.core.engine.StaticOutputElement;
+import org.wso2.carbon.dataservices.core.engine.*;
 import org.wso2.carbon.dataservices.core.engine.CallQuery.WithParam;
 import org.wso2.carbon.dataservices.core.validation.Validator;
-import org.wso2.carbon.dataservices.core.validation.standard.ArrayTypeValidator;
-import org.wso2.carbon.dataservices.core.validation.standard.DoubleRangeValidator;
-import org.wso2.carbon.dataservices.core.validation.standard.LengthValidator;
-import org.wso2.carbon.dataservices.core.validation.standard.LongRangeValidator;
-import org.wso2.carbon.dataservices.core.validation.standard.PatternValidator;
-import org.wso2.carbon.dataservices.core.validation.standard.ScalarTypeValidator;
+import org.wso2.carbon.dataservices.core.validation.standard.*;
+
+import javax.xml.namespace.QName;
+import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.Set;
 
 /**
  * This class represents the serializing functionality of a Query.
@@ -73,8 +60,10 @@
 			serializeExcelQueryProps((ExcelQuery) query, queryEl, fac);
 		} else if (query instanceof GSpreadQuery) {
 			serializeGSpreadQueryProps((GSpreadQuery) query, queryEl, fac);
-		} else if (query instanceof SPARQLQuery) {
-			serializeSparqlQueryProps((SPARQLQuery) query, queryEl, fac);
+		} else if (query instanceof RdfFileQuery) {
+			serializeRdfFileQueryProps((RdfFileQuery) query, queryEl, fac);
+		} else if (query instanceof SparqlEndpointQuery) {
+			serializeSparqlEndpointQueryProps((SparqlEndpointQuery) query, queryEl, fac);
 		} else if (query instanceof WebQuery) {
 			serializeWebQueryProps((WebQuery) query, queryEl, fac);
 		}
@@ -166,12 +155,20 @@
 		queryEl.addChild(gspreadEl);
 	}
 	
-	private static void serializeSparqlQueryProps (SPARQLQuery sparqlQuery, OMElement queryEl, OMFactory fac)  {
+	private static void serializeSparqlQueryProps(SparqlQueryBase sparqlQuery, OMElement queryEl, OMFactory fac) {
 		OMElement sparqlEl = fac.createOMElement(new QName(DBSFields.SPARQL));
 		sparqlEl.setText(sparqlQuery.getQuery());
 		queryEl.addChild(sparqlEl);
 	}
 	
+	private static void serializeRdfFileQueryProps (RdfFileQuery sparqlQuery, OMElement queryEl, OMFactory fac)  {
+		serializeSparqlQueryProps((SparqlQueryBase)sparqlQuery, queryEl, fac);
+	}
+	
+	private static void serializeSparqlEndpointQueryProps (SparqlEndpointQuery sparqlQuery, OMElement queryEl, OMFactory fac) {
+		serializeSparqlQueryProps((SparqlQueryBase)sparqlQuery, queryEl, fac);
+	}
+	
 	private static void serializeWebQueryProps (WebQuery webQuery, OMElement queryEl, OMFactory fac)  {
 		OMElement weblEl = fac.createOMElement(new QName(DBSFields.SCRAPER_VARIABLE));
 		weblEl.setText(webQuery.getScraperVariable());

Added: trunk/graphite/components/data-services/org.wso2.carbon.dataservices.core/src/main/java/org/wso2/carbon/dataservices/core/description/query/RdfFileQuery.java
URL: http://wso2.org/svn/browse/wso2/trunk/graphite/components/data-services/org.wso2.carbon.dataservices.core/src/main/java/org/wso2/carbon/dataservices/core/description/query/RdfFileQuery.java?pathrev=121307
==============================================================================
--- (empty file)
+++ trunk/graphite/components/data-services/org.wso2.carbon.dataservices.core/src/main/java/org/wso2/carbon/dataservices/core/description/query/RdfFileQuery.java	Thu Feb 23 14:38:42 2012
@@ -0,0 +1,94 @@
+/*
+ *  Copyright (c) 2005-2010, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
+ *
+ *  WSO2 Inc. licenses this file to you under the Apache License,
+ *  Version 2.0 (the "License"); you may not use this file except
+ *  in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ *
+ */
+package org.wso2.carbon.dataservices.core.description.query;
+
+import com.hp.hpl.jena.query.*;
+import com.hp.hpl.jena.rdf.model.Model;
+import org.wso2.carbon.dataservices.core.DataServiceFault;
+import org.wso2.carbon.dataservices.core.description.config.RDFConfig;
+import org.wso2.carbon.dataservices.core.description.event.EventTrigger;
+import org.wso2.carbon.dataservices.core.engine.*;
+
+import javax.xml.stream.XMLStreamWriter;
+import java.io.IOException;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * This class represents a SPARQL data services query over a single RDF file
+ */
+public class RdfFileQuery extends SparqlQueryBase {
+
+	private RDFConfig config;
+	
+	public RdfFileQuery(DataService dataService, String queryId,
+			String configId, String query, List<QueryParam> queryParams,
+			Result result, EventTrigger inputEventTrigger,
+			EventTrigger outputEventTrigger,
+			Map<String, String> advancedProperties,
+			String inputNamespace) throws DataServiceFault {
+		super(dataService, queryId, configId, query, queryParams, result,
+				inputEventTrigger, outputEventTrigger, advancedProperties,
+				inputNamespace);
+		try {
+			this.config = (RDFConfig) this.getDataService().getConfig(
+					this.getConfigId());
+		} catch (ClassCastException e) {
+			throw new DataServiceFault(e, "Configuration is not a RDF config:"
+					+ this.getConfigId());
+		}
+
+	}	
+
+	public RDFConfig getConfig() {
+		return config;
+	}
+
+	@Override
+	public QueryExecution getQueryExecution() throws IOException, DataServiceFault {
+		return QueryExecutionFactory.create(this.getQuery(), this.config.createRDFModel());
+	}
+
+    public void processQuery(XMLStreamWriter xmlWriter,
+			InternalParamCollection params, int queryLevel) throws DataServiceFault {
+		try {
+			QuerySolutionMap queryMap = new QuerySolutionMap();
+			Model model = this.getModelForValidation();
+			/* process the query params */
+			for (InternalParam param : params.getParams()) {
+				/* set parameters to the query map */
+				queryMap.add(param.getName(), convertTypeLiteral(model, param));
+			}
+
+			QueryExecution qe = this.getQueryExecution();
+            qe.setInitialBinding(queryMap) ;
+
+			/* execute query as a select query */
+			ResultSet results = qe.execSelect();
+			DataEntry dataEntry;
+			while (results.hasNext()) {
+				dataEntry = this.getDataEntryFromRS(results);
+				this.writeResultEntry(xmlWriter, dataEntry, params, queryLevel);
+			}
+		} catch (Exception e) {
+			throw new DataServiceFault(e, "Error in 'SparqlQueryBase.processQuery'");
+		}
+	}
+
+}

Added: trunk/graphite/components/data-services/org.wso2.carbon.dataservices.core/src/main/java/org/wso2/carbon/dataservices/core/description/query/SparqlEndpointQuery.java
URL: http://wso2.org/svn/browse/wso2/trunk/graphite/components/data-services/org.wso2.carbon.dataservices.core/src/main/java/org/wso2/carbon/dataservices/core/description/query/SparqlEndpointQuery.java?pathrev=121307
==============================================================================
--- (empty file)
+++ trunk/graphite/components/data-services/org.wso2.carbon.dataservices.core/src/main/java/org/wso2/carbon/dataservices/core/description/query/SparqlEndpointQuery.java	Thu Feb 23 14:38:42 2012
@@ -0,0 +1,86 @@
+/*
+ *  Copyright (c) 2005-2010, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
+ *
+ *  WSO2 Inc. licenses this file to you under the Apache License,
+ *  Version 2.0 (the "License"); you may not use this file except
+ *  in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ *
+ */
+package org.wso2.carbon.dataservices.core.description.query;
+
+import com.hp.hpl.jena.query.QueryExecution;
+import com.hp.hpl.jena.query.QueryExecutionFactory;
+import com.hp.hpl.jena.query.ResultSet;
+import org.wso2.carbon.dataservices.core.DataServiceFault;
+import org.wso2.carbon.dataservices.core.description.config.SparqlEndpointConfig;
+import org.wso2.carbon.dataservices.core.description.event.EventTrigger;
+import org.wso2.carbon.dataservices.core.engine.*;
+
+import javax.xml.stream.XMLStreamWriter;
+import java.io.IOException;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * This class represents a SPARQL data services query made to an arbitrary SPARQL endpoint via HTTP
+ */
+public class SparqlEndpointQuery extends SparqlQueryBase {
+
+	private SparqlEndpointConfig config;
+	
+	public SparqlEndpointQuery(DataService dataService, String queryId,
+			String configId, String query, List<QueryParam> queryParams,
+			Result result, EventTrigger inputEventTrigger,
+			EventTrigger outputEventTrigger,
+			Map<String, String> advancedProperties,
+			String inputNamespace) throws DataServiceFault {
+		super(dataService, queryId, configId, query, queryParams, result,
+				inputEventTrigger, outputEventTrigger, advancedProperties,
+				inputNamespace);
+		try {
+			this.config = (SparqlEndpointConfig) this.getDataService().getConfig(
+					this.getConfigId());
+		} catch (ClassCastException e) {
+			throw new DataServiceFault(e, "Configuration is not a SPARQL Endpoint config:"
+					+ this.getConfigId());
+		}
+
+	}	
+
+	public SparqlEndpointConfig getConfig() {
+		return config;
+	}
+
+	@Override
+	public QueryExecution getQueryExecution() throws IOException, DataServiceFault {
+		return QueryExecutionFactory.sparqlService(this.getConfig().getSparqlEndpoint(), this.getQuery());
+	}
+
+    public void processQuery(XMLStreamWriter xmlWriter,
+			InternalParamCollection params, int queryLevel) throws DataServiceFault {
+		try {
+			QueryExecution qe = this.getQueryExecution();
+
+			/* execute query as a select query */
+			ResultSet results = qe.execSelect();
+			DataEntry dataEntry;
+			while (results.hasNext()) {
+				dataEntry = this.getDataEntryFromRS(results);
+				this.writeResultEntry(xmlWriter, dataEntry, params, queryLevel);
+			}
+		} catch (Exception e) {
+			throw new DataServiceFault(e, "Error in 'SparqlQueryBase.processQuery'");
+		}
+	}
+    
+}

Added: trunk/graphite/components/data-services/org.wso2.carbon.dataservices.core/src/main/java/org/wso2/carbon/dataservices/core/description/query/SparqlQueryBase.java
URL: http://wso2.org/svn/browse/wso2/trunk/graphite/components/data-services/org.wso2.carbon.dataservices.core/src/main/java/org/wso2/carbon/dataservices/core/description/query/SparqlQueryBase.java?pathrev=121307
==============================================================================
--- (empty file)
+++ trunk/graphite/components/data-services/org.wso2.carbon.dataservices.core/src/main/java/org/wso2/carbon/dataservices/core/description/query/SparqlQueryBase.java	Thu Feb 23 14:38:42 2012
@@ -0,0 +1,204 @@
+/*
+ *  Copyright (c) 2005-2010, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
+ *
+ *  WSO2 Inc. licenses this file to you under the Apache License,
+ *  Version 2.0 (the "License"); you may not use this file except
+ *  in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ *
+ */
+package org.wso2.carbon.dataservices.core.description.query;
+
+import com.hp.hpl.jena.datatypes.xsd.XSDDatatype;
+import com.hp.hpl.jena.query.QueryExecution;
+import com.hp.hpl.jena.query.QuerySolution;
+import com.hp.hpl.jena.query.ResultSet;
+import com.hp.hpl.jena.rdf.model.Model;
+import com.hp.hpl.jena.rdf.model.ModelFactory;
+import com.hp.hpl.jena.rdf.model.RDFNode;
+import org.apache.axis2.databinding.utils.ConverterUtil;
+import org.wso2.carbon.dataservices.common.DBConstants;
+import org.wso2.carbon.dataservices.core.DataServiceFault;
+import org.wso2.carbon.dataservices.core.description.event.EventTrigger;
+import org.wso2.carbon.dataservices.core.engine.*;
+
+import javax.xml.stream.XMLStreamWriter;
+import java.io.IOException;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * This class represents a SPARQL data services query.
+ */
+public abstract class SparqlQueryBase extends Query {
+
+	private String query;
+
+	public SparqlQueryBase(DataService dataService, String queryId,
+			String configId, String query, List<QueryParam> queryParams,
+			Result result, EventTrigger inputEventTrigger,
+			EventTrigger outputEventTrigger,
+			Map<String, String> advancedProperties,
+			String inputNamespace) throws DataServiceFault {
+		super(dataService, queryId, queryParams, result, configId,
+				inputEventTrigger, outputEventTrigger, advancedProperties,
+				inputNamespace);
+		this.query = query;
+	}
+
+	@Override
+	public void runQuery(XMLStreamWriter xmlWriter,
+			InternalParamCollection params, int queryLevel)
+			throws DataServiceFault {
+		this.processQuery(xmlWriter, params, queryLevel);
+	}
+
+	public RDFNode convertTypeLiteral(Model model, InternalParam param)
+			throws DataServiceFault {
+		String value = param.getValue().getScalarValue();
+		String type = param.getSqlType();
+		if (type == null) {
+			return model.createTypedLiteral(value);
+		} else if (DBConstants.XSDTypes.INTEGER.equals(type) ) {
+			return model.createTypedLiteral(value,XSDDatatype.XSDinteger);
+		} else if (DBConstants.XSDTypes.ANYURI.equals(type)) {
+			return model.createResource(value);
+		} else if (DBConstants.XSDTypes.DECIMAL.equals(type)) {
+			return model.createTypedLiteral(value,XSDDatatype.XSDdecimal);
+		} else if (DBConstants.XSDTypes.DOUBLE.equals(type)) {
+			return model.createTypedLiteral(value,XSDDatatype.XSDdouble);
+		} else if (DBConstants.XSDTypes.FLOAT.equals(type)) {
+			return model.createTypedLiteral(value,XSDDatatype.XSDfloat);
+		} else if (DBConstants.XSDTypes.STRING.equals(type)) {
+			return model.createTypedLiteral(value);
+		} else if (DBConstants.XSDTypes.BOOLEAN.equals(type)) {
+			return model.createTypedLiteral(value,XSDDatatype.XSDboolean);
+		} else if (DBConstants.XSDTypes.LONG.equals(type)) {
+			return model.createTypedLiteral(value,XSDDatatype.XSDlong);
+		} else if (DBConstants.XSDTypes.DATE.equals(type) ) {
+			return model.createTypedLiteral(value,XSDDatatype.XSDdate);
+		} else if (DBConstants.XSDTypes.DATETIME.equals(type) ) {
+			return model.createTypedLiteral(value,XSDDatatype.XSDdateTime);
+		} else if (DBConstants.XSDTypes.TIME.equals(type)) {
+			return model.createTypedLiteral(value,XSDDatatype.XSDtime);
+		} else if (DBConstants.XSDTypes.GYEARMONTH.equals(type) ) {
+			return model.createTypedLiteral(value,XSDDatatype.XSDgYearMonth);
+		} else if (DBConstants.XSDTypes.GYEAR.equals(type) ) {
+			return model.createTypedLiteral(value,XSDDatatype.XSDgYear);
+		} else if (DBConstants.XSDTypes.GMONTHDAY.equals(type)) {
+			return model.createTypedLiteral(value,XSDDatatype.XSDgMonthDay);
+		} else if (DBConstants.XSDTypes.GDAY.equals(type)) {
+			return model.createTypedLiteral(value,XSDDatatype.XSDgDay);
+		} else if (DBConstants.XSDTypes.GMONTH.equals(type)) {
+			return model.createTypedLiteral(value,XSDDatatype.XSDgMonth);
+		} else if (DBConstants.XSDTypes.HEXBINARY.equals(type)) {
+			return model.createTypedLiteral(value,XSDDatatype.XSDhexBinary);
+		} else if (DBConstants.XSDTypes.BASE64BINARY.equals(type)) {
+			return model.createTypedLiteral(value,XSDDatatype.XSDbase64Binary);
+		} else if (DBConstants.XSDTypes.QNAME.equals(type)) {
+			return model.createTypedLiteral(value,XSDDatatype.XSDQName);
+		} else if (DBConstants.XSDTypes.NOTATION.equals(type)) {
+			return model.createTypedLiteral(value,XSDDatatype.XSDNOTATION);
+		} else {
+			throw new DataServiceFault("[" + this.getDataService().getName()
+					+ "]  Found Unsupported data type : " + type
+					+ " as input parameter.");
+		}
+
+	}
+ 
+	private String convertRSToString (QuerySolution soln, String colName) {
+		if (soln.getLiteral(colName).getDatatype() == null) {
+			return soln.getLiteral(colName).getString();
+		} else {
+			String colType = soln.getLiteral(colName).getDatatype().getURI();
+			if (colType.equals(XSDDatatype.XSDdecimal.getURI())) {
+				return ConverterUtil.convertToString(ConverterUtil.convertToDecimal(soln
+								.getLiteral(colName).getString()));
+			} else if (colType.equals(XSDDatatype.XSDdouble.getURI())) {
+				return ConverterUtil.convertToString(ConverterUtil
+						.convertToDouble(soln.getLiteral(colName).getString()));
+			} else if (colType.equals(XSDDatatype.XSDfloat.getURI())) {
+				return ConverterUtil.convertToString(ConverterUtil
+						.convertToFloat(soln.getLiteral(colName).getString()));
+			} else if (colType.equals(XSDDatatype.XSDstring.getURI())) {
+				return soln.getLiteral(colName).getString();
+			} else if (colType.equals(XSDDatatype.XSDboolean.getURI())) {
+				return ConverterUtil
+						.convertToString(ConverterUtil.convertToBoolean(soln
+								.getLiteral(colName).getString()));
+			} else if (colType.equals(XSDDatatype.XSDlong.getURI())) {
+				return ConverterUtil.convertToString(ConverterUtil
+						.convertToLong(soln.getLiteral(colName).getString()));
+			} else if (colType.equals(XSDDatatype.XSDdate.getURI())) {
+				return ConverterUtil.convertToString(ConverterUtil
+						.convertToDate(soln.getLiteral(colName).getString()));
+			} else if (colType.equals(XSDDatatype.XSDdateTime.getURI())) {
+				return ConverterUtil
+						.convertToString(ConverterUtil.convertToDateTime(soln
+								.getLiteral(colName).getString()));
+			} else if (colType.equals(XSDDatatype.XSDtime.getURI())) {
+				return ConverterUtil.convertToString(ConverterUtil
+						.convertToTime(soln.getLiteral(colName).getString()));
+			} else {
+				return soln.getLiteral(colName).getString();
+			}
+		}	
+	}
+	
+	public DataEntry getDataEntryFromRS(ResultSet rs) {
+		DataEntry dataEntry = new DataEntry();
+		QuerySolution soln = rs.nextSolution();
+		String colName, value;
+		boolean useColumnNumbers = this.isUsingColumnNumbers();
+		/* for each column get the colName and colValue and add to the data entry */
+		for (int i = 0; i < rs.getResultVars().size(); i++) {
+			colName = rs.getResultVars().get(i);
+			RDFNode node = soln.get(colName) ;  			
+			if (node.isLiteral()) {
+				value = convertRSToString(soln, colName);
+			} else {
+				value = soln.getResource(colName).getURI();
+			}			
+			dataEntry.addValue(useColumnNumbers ? Integer.toString(i + 1) : 
+				colName, new ParamValue(value));
+		}
+		return dataEntry;
+	}
+	
+	public abstract void processQuery(XMLStreamWriter xmlWriter,
+			InternalParamCollection params, int queryLevel) throws DataServiceFault ;
+	/**
+	 * Gets the Query string
+	 * @return
+	 */
+	public String getQuery() {
+		return query;
+	}
+	
+	/**
+	 * Gets a Query Execution for the query
+	 * @return
+	 * @throws DataServiceFault 
+	 * @throws IOException 
+	 */
+	public abstract QueryExecution getQueryExecution() throws IOException, DataServiceFault;
+	
+	/**
+	 * Gets the model used to validate input parameters
+	 * @return
+	 */
+	public Model getModelForValidation()
+	{
+		return ModelFactory.createDefaultModel();
+	}
+}


More information about the Carbon-commits mailing list