aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSteve Pitschke2013-09-30 13:03:52 (EDT)
committerSteve Pitschke2013-10-08 09:21:08 (EDT)
commit9254846a2d96f41a7d6651761d12b84735f3cb0b (patch)
tree06379d8cce0f609c7b636c5c5fb1842d2af832e7
parent14dbc8c0079ab03d1647fdcd8381f777825d11bd (diff)
downloadorg.eclipse.lyo.client-9254846a2d96f41a7d6651761d12b84735f3cb0b.zip
org.eclipse.lyo.client-9254846a2d96f41a7d6651761d12b84735f3cb0b.tar.gz
org.eclipse.lyo.client-9254846a2d96f41a7d6651761d12b84735f3cb0b.tar.bz2
bug 418371: Add getMembers() Accessor to OslcQueryResult refs/changes/91/16891/2
Change-Id: Id1147b66dab1a8f084676bb5b63d0135aa15b4f3 Signed-off-by: Steve Pitschke <pitschke@us.ibm.com>
-rw-r--r--org.eclipse.lyo.client.java/src/main/java/org/eclipse/lyo/client/oslc/resources/OslcQueryResult.java423
-rw-r--r--org.eclipse.lyo.samples.clients/src/main/java/org/eclipse/lyo/client/oslc/samples/RTCFormSample.java48
2 files changed, 256 insertions, 215 deletions
diff --git a/org.eclipse.lyo.client.java/src/main/java/org/eclipse/lyo/client/oslc/resources/OslcQueryResult.java b/org.eclipse.lyo.client.java/src/main/java/org/eclipse/lyo/client/oslc/resources/OslcQueryResult.java
index 556327d..5acf64b 100644
--- a/org.eclipse.lyo.client.java/src/main/java/org/eclipse/lyo/client/oslc/resources/OslcQueryResult.java
+++ b/org.eclipse.lyo.client.java/src/main/java/org/eclipse/lyo/client/oslc/resources/OslcQueryResult.java
@@ -1,179 +1,244 @@
-/*******************************************************************************
- * Copyright (c) 2012 IBM Corporation.
- *
- * All rights reserved. This program and the accompanying materials
- * are made available under the terms of the Eclipse Public License v1.0
- * and Eclipse Distribution License v. 1.0 which accompanies this distribution.
- *
- * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
- * and the Eclipse Distribution License is available at
- * http://www.eclipse.org/org/documents/edl-v10.php.
- *
- * Contributors:
- *
- * Sean Kennedy - initial API and implementation
- *******************************************************************************/
-package org.eclipse.lyo.client.oslc.resources;
-
-import java.io.InputStream;
-import java.util.ArrayList;
-import java.util.Iterator;
-
-import org.apache.wink.client.ClientResponse;
-import org.eclipse.lyo.client.oslc.OSLCConstants;
-import org.eclipse.lyo.oslc4j.core.model.OslcConstants;
-
-import com.hp.hpl.jena.rdf.model.Model;
-import com.hp.hpl.jena.rdf.model.ModelFactory;
-import com.hp.hpl.jena.rdf.model.Property;
-import com.hp.hpl.jena.rdf.model.RDFNode;
-import com.hp.hpl.jena.rdf.model.ResIterator;
-import com.hp.hpl.jena.rdf.model.Resource;
-import com.hp.hpl.jena.rdf.model.Selector;
-import com.hp.hpl.jena.rdf.model.SimpleSelector;
-import com.hp.hpl.jena.rdf.model.Statement;
-import com.hp.hpl.jena.rdf.model.StmtIterator;
-
-
-/**
- * The results of an OSLC query. If the query was paged, subsequent pages can be retrieved using the Iterator interface.
- *
- * This class is not currently thread safe.
- */
-public class OslcQueryResult implements Iterator<OslcQueryResult> {
-
- private final OslcQuery query;
-
- private final ClientResponse response;
-
- private final int pageNumber;
-
- private Model rdfModel;
-
- private Resource infoResource, membersResource;
-
- private String nextPageUrl = "";
-
- private boolean rdfInitialized = false;
-
- public OslcQueryResult(OslcQuery query, ClientResponse response) {
- this.query = query;
- this.response = response;
-
- this.pageNumber = 1;
-
-
- }
-
- private OslcQueryResult(OslcQueryResult prev) {
- this.query = new OslcQuery(prev);
- this.response = this.query.getResponse();
-
- this.pageNumber = prev.pageNumber + 1;
-
- }
-
- private synchronized void initializeRdf() {
- if (!rdfInitialized) {
- rdfInitialized = true;
- rdfModel = ModelFactory.createDefaultModel();
- rdfModel.read(response.getEntity(InputStream.class), query.getCapabilityUrl());
-
- //Find a resource with rdf:type of oslc:ResourceInfo
- Property rdfType = rdfModel.createProperty(OslcConstants.RDF_NAMESPACE, "type");
- Property responseInfo = rdfModel.createProperty(OslcConstants.OSLC_CORE_NAMESPACE, "ResponseInfo");
- ResIterator iter = rdfModel.listResourcesWithProperty(rdfType, responseInfo);
-
- //There should only be one - take the first
- infoResource = null;
- while (iter.hasNext()) {
- infoResource = iter.next();
- break;
- }
- membersResource = rdfModel.getResource(query.getCapabilityUrl());
- }
- }
-
- String getNextPageUrl() {
- initializeRdf();
- if ((nextPageUrl == null || nextPageUrl.isEmpty()) && infoResource != null) {
- Property predicate = rdfModel.getProperty(OslcConstants.OSLC_CORE_NAMESPACE, "nextPage");
- Selector select = new SimpleSelector(infoResource, predicate, (RDFNode) null);
- StmtIterator iter = rdfModel.listStatements(select);
- if (iter.hasNext()) {
- Statement nextPage = iter.next();
- nextPageUrl = nextPage.getResource().getURI();
- } else {
- nextPageUrl = "";
- }
- }
- return nextPageUrl;
- }
-
- /**
- * @return whether there is another page of results after this
- */
- public boolean hasNext() {
- return (!"".equals(getNextPageUrl()));
- }
-
- /**
- * @return the next page of results
- * @throws NoSuchElementException if there is no next page
- */
- public OslcQueryResult next() {
- return new OslcQueryResult(this);
- }
-
- /**
- * @throws UnsupportedOperationException always
- */
- public void remove() {
- throw new UnsupportedOperationException();
- }
-
- public OslcQuery getQuery() {
- return query;
- }
-
- /**
- * Get the raw Wink client response to a query.
- *
- * NOTE: Using this method and consuming the response will make other methods
- * which examine the response unavailable (Examples: getMemberUrls(), next() and hasNext()).
- * When this method is invoked, the consumer is responsible for OSLC page processing
- *
- * @return
- */
- public ClientResponse getRawResponse() {
- return response;
- }
-
- /**
- * Return the subject URLs of the query response. The URLs are the location of all artifacts
- * which satisfy the query conditions.
- *
- * NOTE: Using this method consumes the query response and makes other methods
- * which examine the response unavailable (Example: getRawResponse().
- * @return
- */
- public String[] getMembersUrls() {
- initializeRdf();
- ArrayList<String> membersUrls = new ArrayList<String>();
- Selector select = new SimpleSelector(membersResource, (Property) null, (RDFNode) null);
- StmtIterator iter = rdfModel.listStatements(select);
- while (iter.hasNext()) {
- Statement member = iter.next();
- try {
- String fqPredicateName = member.getPredicate().getNameSpace() + member.getPredicate().getLocalName();
- if (fqPredicateName != null && !fqPredicateName.equals(OSLCConstants.RDF_TYPE_PROP)) {
- membersUrls.add(member.getResource().getURI());
- }
- } catch (Throwable t) {
- //FIXME
- System.err.println("Member was not a resource");
- }
- }
- return membersUrls.toArray(new String[membersUrls.size()]);
- }
-}
+/*******************************************************************************
+ * Copyright (c) 2012, 2013 IBM Corporation.
+ *
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * and Eclipse Distribution License v. 1.0 which accompanies this distribution.
+ *
+ * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
+ * and the Eclipse Distribution License is available at
+ * http://www.eclipse.org/org/documents/edl-v10.php.
+ *
+ * Contributors:
+ *
+ * Sean Kennedy - initial API and implementation
+ * Steve Pitschke - added getMembers() method
+ *******************************************************************************/
+package org.eclipse.lyo.client.oslc.resources;
+
+import java.io.InputStream;
+import java.lang.reflect.InvocationTargetException;
+import java.net.URISyntaxException;
+import java.util.ArrayList;
+import java.util.Iterator;
+
+import javax.xml.datatype.DatatypeConfigurationException;
+
+import org.apache.wink.client.ClientResponse;
+import org.eclipse.lyo.client.oslc.OSLCConstants;
+import org.eclipse.lyo.oslc4j.core.exception.OslcCoreApplicationException;
+import org.eclipse.lyo.oslc4j.core.model.OslcConstants;
+import org.eclipse.lyo.oslc4j.provider.jena.JenaModelHelper;
+
+import com.hp.hpl.jena.rdf.model.Model;
+import com.hp.hpl.jena.rdf.model.ModelFactory;
+import com.hp.hpl.jena.rdf.model.Property;
+import com.hp.hpl.jena.rdf.model.RDFNode;
+import com.hp.hpl.jena.rdf.model.ResIterator;
+import com.hp.hpl.jena.rdf.model.Resource;
+import com.hp.hpl.jena.rdf.model.Selector;
+import com.hp.hpl.jena.rdf.model.SimpleSelector;
+import com.hp.hpl.jena.rdf.model.Statement;
+import com.hp.hpl.jena.rdf.model.StmtIterator;
+
+
+/**
+ * The results of an OSLC query. If the query was paged, subsequent pages can be retrieved using the Iterator interface.
+ *
+ * This class is not currently thread safe.
+ */
+public class OslcQueryResult implements Iterator<OslcQueryResult> {
+
+ private final OslcQuery query;
+
+ private final ClientResponse response;
+
+ private final int pageNumber;
+
+ private Model rdfModel;
+
+ private Resource infoResource, membersResource;
+
+ private String nextPageUrl = "";
+
+ private boolean rdfInitialized = false;
+
+ public OslcQueryResult(OslcQuery query, ClientResponse response) {
+ this.query = query;
+ this.response = response;
+
+ this.pageNumber = 1;
+
+
+ }
+
+ private OslcQueryResult(OslcQueryResult prev) {
+ this.query = new OslcQuery(prev);
+ this.response = this.query.getResponse();
+
+ this.pageNumber = prev.pageNumber + 1;
+
+ }
+
+ private synchronized void initializeRdf() {
+ if (!rdfInitialized) {
+ rdfInitialized = true;
+ rdfModel = ModelFactory.createDefaultModel();
+ rdfModel.read(response.getEntity(InputStream.class), query.getCapabilityUrl());
+
+ //Find a resource with rdf:type of oslc:ResourceInfo
+ Property rdfType = rdfModel.createProperty(OslcConstants.RDF_NAMESPACE, "type");
+ Property responseInfo = rdfModel.createProperty(OslcConstants.OSLC_CORE_NAMESPACE, "ResponseInfo");
+ ResIterator iter = rdfModel.listResourcesWithProperty(rdfType, responseInfo);
+
+ //There should only be one - take the first
+ infoResource = null;
+ while (iter.hasNext()) {
+ infoResource = iter.next();
+ break;
+ }
+ membersResource = rdfModel.getResource(query.getCapabilityUrl());
+ }
+ }
+
+ String getNextPageUrl() {
+ initializeRdf();
+ if ((nextPageUrl == null || nextPageUrl.isEmpty()) && infoResource != null) {
+ Property predicate = rdfModel.getProperty(OslcConstants.OSLC_CORE_NAMESPACE, "nextPage");
+ Selector select = new SimpleSelector(infoResource, predicate, (RDFNode) null);
+ StmtIterator iter = rdfModel.listStatements(select);
+ if (iter.hasNext()) {
+ Statement nextPage = iter.next();
+ nextPageUrl = nextPage.getResource().getURI();
+ } else {
+ nextPageUrl = "";
+ }
+ }
+ return nextPageUrl;
+ }
+
+ /**
+ * @return whether there is another page of results after this
+ */
+ public boolean hasNext() {
+ return (!"".equals(getNextPageUrl()));
+ }
+
+ /**
+ * @return the next page of results
+ * @throws NoSuchElementException if there is no next page
+ */
+ public OslcQueryResult next() {
+ return new OslcQueryResult(this);
+ }
+
+ /**
+ * @throws UnsupportedOperationException always
+ */
+ public void remove() {
+ throw new UnsupportedOperationException();
+ }
+
+ public OslcQuery getQuery() {
+ return query;
+ }
+
+ /**
+ * Get the raw Wink client response to a query.
+ *
+ * NOTE: Using this method and consuming the response will make other methods
+ * which examine the response unavailable (Examples: getMemberUrls(), next() and hasNext()).
+ * When this method is invoked, the consumer is responsible for OSLC page processing
+ *
+ * @return
+ */
+ public ClientResponse getRawResponse() {
+ return response;
+ }
+
+ /**
+ * Return the subject URLs of the query response. The URLs are the location of all artifacts
+ * which satisfy the query conditions.
+ *
+ * NOTE: Using this method consumes the query response and makes other methods
+ * which examine the response unavailable (Example: getRawResponse().
+ * @return
+ */
+ public String[] getMembersUrls() {
+ initializeRdf();
+ ArrayList<String> membersUrls = new ArrayList<String>();
+ Selector select = new SimpleSelector(membersResource, (Property) null, (RDFNode) null);
+ StmtIterator iter = rdfModel.listStatements(select);
+ while (iter.hasNext()) {
+ Statement member = iter.next();
+ try {
+ String fqPredicateName = member.getPredicate().getNameSpace() + member.getPredicate().getLocalName();
+ if (fqPredicateName != null && !fqPredicateName.equals(OSLCConstants.RDF_TYPE_PROP)) {
+ membersUrls.add(member.getResource().getURI());
+ }
+ } catch (Throwable t) {
+ //FIXME
+ System.err.println("Member was not a resource");
+ }
+ }
+ return membersUrls.toArray(new String[membersUrls.size()]);
+ }
+
+ /**
+ * Return the enumeration of queried results from this page
+ *
+ * @param T
+ * @param clazz
+ *
+ * @return member statements from current page.
+ */
+ public <T> Iterable<T> getMembers(final Class<T> clazz) {
+ initializeRdf();
+
+ Selector select = new SimpleSelector(membersResource, (Property) null, (RDFNode) null);
+ final StmtIterator iter = rdfModel.listStatements(select);
+ Iterable<T> result = new Iterable<T>() {
+ public Iterator<T>
+ iterator() {
+ return new Iterator<T>() {
+ public boolean hasNext() {
+ return iter.hasNext();
+ }
+
+ @SuppressWarnings("unchecked")
+ public T next() {
+ Statement member = iter.next();
+
+ try {
+ return (T)JenaModelHelper.fromJenaResource((Resource)member.getObject(), clazz);
+ } catch (IllegalArgumentException e) {
+ throw new IllegalStateException(e.getMessage());
+ } catch (SecurityException e) {
+ throw new IllegalStateException(e.getMessage());
+ } catch (DatatypeConfigurationException e) {
+ throw new IllegalStateException(e.getMessage());
+ } catch (IllegalAccessException e) {
+ throw new IllegalStateException(e.getMessage());
+ } catch (InstantiationException e) {
+ throw new IllegalStateException(e.getMessage());
+ } catch (InvocationTargetException e) {
+ throw new IllegalStateException(e.getMessage());
+ } catch (OslcCoreApplicationException e) {
+ throw new IllegalStateException(e.getMessage());
+ } catch (URISyntaxException e) {
+ throw new IllegalStateException(e.getMessage());
+ } catch (NoSuchMethodException e) {
+ throw new IllegalStateException(e.getMessage());
+ }
+ }
+
+ public void remove() {
+ iter.remove();
+ }
+ };
+ }
+ };
+
+ return result;
+ }
+}
diff --git a/org.eclipse.lyo.samples.clients/src/main/java/org/eclipse/lyo/client/oslc/samples/RTCFormSample.java b/org.eclipse.lyo.samples.clients/src/main/java/org/eclipse/lyo/client/oslc/samples/RTCFormSample.java
index eff863c..cd82632 100644
--- a/org.eclipse.lyo.samples.clients/src/main/java/org/eclipse/lyo/client/oslc/samples/RTCFormSample.java
+++ b/org.eclipse.lyo.samples.clients/src/main/java/org/eclipse/lyo/client/oslc/samples/RTCFormSample.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2012 IBM Corporation.
+ * Copyright (c) 2012, 2013 IBM Corporation.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
@@ -12,6 +12,7 @@
* Contributors:
*
* Michael Fiedler - initial API and implementation
+ * Steve Pitschke - added use of new OslcQueryResult.getMembers()
*******************************************************************************/
package org.eclipse.lyo.client.oslc.samples;
@@ -19,10 +20,14 @@ import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
+import java.lang.reflect.InvocationTargetException;
import java.net.URI;
+import java.net.URISyntaxException;
import java.util.logging.Level;
import java.util.logging.Logger;
+import javax.xml.datatype.DatatypeConfigurationException;
+
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.GnuParser;
@@ -40,6 +45,7 @@ import org.eclipse.lyo.client.oslc.resources.ChangeRequest;
import org.eclipse.lyo.client.oslc.resources.OslcQuery;
import org.eclipse.lyo.client.oslc.resources.OslcQueryParameters;
import org.eclipse.lyo.client.oslc.resources.OslcQueryResult;
+import org.eclipse.lyo.oslc4j.core.exception.OslcCoreApplicationException;
import org.eclipse.lyo.oslc4j.core.model.Link;
import org.eclipse.lyo.oslc4j.core.model.OslcMediaType;
@@ -114,7 +120,8 @@ public class RTCFormSample {
//page turned on and list the members of the result
OslcQueryParameters queryParams = new OslcQueryParameters();
queryParams.setWhere("oslc_cm:closed=false");
- OslcQuery query = new OslcQuery(client, queryCapability, 10, queryParams);
+ queryParams.setSelect("dcterms:identifier,dcterms:title,oslc_cm:status");
+ OslcQuery query = new OslcQuery(client, queryCapability, 10, queryParams);
OslcQueryResult result = query.submit();
@@ -203,33 +210,9 @@ public class RTCFormSample {
private static void processCurrentPage(OslcQueryResult result, OslcClient client, boolean asJavaObjects) {
- for (String resultsUrl : result.getMembersUrls()) {
- System.out.println(resultsUrl);
-
- ClientResponse response = null;
- try {
-
- //Get a single artifact by its URL
- response = client.getResource(resultsUrl, OSLCConstants.CT_RDF);
-
- if (response != null) {
- //De-serialize it as a Java object
- if (asJavaObjects) {
- ChangeRequest cr = response.getEntity(ChangeRequest.class);
- printChangeRequestInfo(cr); //print a few attributes
- } else {
-
- //Just print the raw RDF/XML (or process the XML as desired)
- processRawResponse(response);
-
- }
- }
- } catch (Exception e) {
- logger.log(Level.SEVERE, "Unable to process artfiact at url: " + resultsUrl, e);
- }
-
- }
-
+ for (ChangeRequest cr : result.getMembers(ChangeRequest.class)) {
+ System.out.println("id: " + cr.getIdentifier() + ", title: " + cr.getTitle() + ", status: " + cr.getStatus());
+ }
}
private static void processRawResponse(ClientResponse response) throws IOException {
@@ -244,13 +227,6 @@ public class RTCFormSample {
response.consumeContent();
}
- private static void printChangeRequestInfo(ChangeRequest cr) {
- //See the OSLC4J ChangeRequest class for a full list of attributes you can access.
- if (cr != null) {
- System.out.println("ID: " + cr.getIdentifier() + ", Title: " + cr.getTitle() + ", Status: " + cr.getStatus());
- }
- }
-
private static boolean validateOptions(CommandLine cmd) {
boolean isValid = true;