From 30261f2819d7f7fe6e6df58948b19db2261d7bbf Mon Sep 17 00:00:00 2001 From: jomu Date: Thu, 5 Feb 2015 13:11:21 +0000 Subject: [PATCH] added filter options to list item queries --- sharepoint/api/pom.xml | 4 +- .../sharepoint/api/AbstractSpJaxbObject.java | 40 +++++++++++++++++++ .../shared/sharepoint/api/SPJaxbObject.java | 11 ++--- .../shared/sharepoint/api/lists/SPList.java | 26 +++++++++--- .../sharepoint/api/lists/SPListTest.java | 26 +++++++++++- .../sharepoint/api/lists/batchimporttest.xml} | 0 .../sharepoint/api/lists/batchtest.xml} | 0 7 files changed, 91 insertions(+), 16 deletions(-) create mode 100644 sharepoint/api/src/main/java/de/muehlencord/shared/sharepoint/api/AbstractSpJaxbObject.java rename sharepoint/api/src/test/resources/{BatchImportTest.xml => de/muehlencord/shared/sharepoint/api/lists/batchimporttest.xml} (100%) rename sharepoint/api/src/test/resources/{BatchTest.xml => de/muehlencord/shared/sharepoint/api/lists/batchtest.xml} (100%) diff --git a/sharepoint/api/pom.xml b/sharepoint/api/pom.xml index 801969c..829f0b4 100644 --- a/sharepoint/api/pom.xml +++ b/sharepoint/api/pom.xml @@ -228,7 +228,7 @@ - usergroup + usergroup2010 xjc @@ -247,7 +247,7 @@ usergroup.xml true - + diff --git a/sharepoint/api/src/main/java/de/muehlencord/shared/sharepoint/api/AbstractSpJaxbObject.java b/sharepoint/api/src/main/java/de/muehlencord/shared/sharepoint/api/AbstractSpJaxbObject.java new file mode 100644 index 0000000..9ca34a0 --- /dev/null +++ b/sharepoint/api/src/main/java/de/muehlencord/shared/sharepoint/api/AbstractSpJaxbObject.java @@ -0,0 +1,40 @@ +package de.muehlencord.shared.sharepoint.api; + +import java.io.IOException; +import java.io.StringReader; +import javax.xml.bind.JAXBException; +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; +import org.w3c.dom.Document; +import org.w3c.dom.Node; +import org.xml.sax.InputSource; +import org.xml.sax.SAXException; + +/** + * + * @author joern.muehlencord + */ +public abstract class AbstractSpJaxbObject extends SPObject { + + public AbstractSpJaxbObject(SPContext context) { + super(context); + } + + public static Node createSharePointCAMLNode(String xmlString) throws ParserConfigurationException, SAXException, IOException, JAXBException { + DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); + documentBuilderFactory.setValidating(false); + DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder(); + Document document = documentBuilder.parse(new InputSource(new StringReader(xmlString))); + Node node = document.getDocumentElement(); + return node; + } + +} + +/** + * History: + * + * $$Log$$ + * + */ diff --git a/sharepoint/api/src/main/java/de/muehlencord/shared/sharepoint/api/SPJaxbObject.java b/sharepoint/api/src/main/java/de/muehlencord/shared/sharepoint/api/SPJaxbObject.java index 461974a..9b4b387 100644 --- a/sharepoint/api/src/main/java/de/muehlencord/shared/sharepoint/api/SPJaxbObject.java +++ b/sharepoint/api/src/main/java/de/muehlencord/shared/sharepoint/api/SPJaxbObject.java @@ -26,7 +26,7 @@ import org.xml.sax.SAXException; * @author joern.muehlencord * @param */ -public abstract class SPJaxbObject extends SPObject { +public abstract class SPJaxbObject extends AbstractSpJaxbObject { private static JAXBContext jaxbContext = null; @@ -75,7 +75,7 @@ public abstract class SPJaxbObject extends SPObject { } schema = sf.newSchema(streamSources); } catch (Exception ex) { - throw new SAXException ("Cannot convert to object. Reason: "+ex.getMessage(), ex); + throw new SAXException("Cannot convert to object. Reason: " + ex.getMessage(), ex); } return schema; } @@ -112,12 +112,7 @@ public abstract class SPJaxbObject extends SPObject { public Node createSharePointCAMLNode() throws ParserConfigurationException, SAXException, IOException, JAXBException { String xmlString = getValueXmlString(); - DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); - documentBuilderFactory.setValidating(false); - DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder(); - Document document = documentBuilder.parse(new InputSource(new StringReader(xmlString))); - Node node = document.getDocumentElement(); - return node; + return AbstractSpJaxbObject.createSharePointCAMLNode(xmlString); } protected T getValue() { diff --git a/sharepoint/api/src/main/java/de/muehlencord/shared/sharepoint/api/lists/SPList.java b/sharepoint/api/src/main/java/de/muehlencord/shared/sharepoint/api/lists/SPList.java index 5ce7d22..a94fcc1 100644 --- a/sharepoint/api/src/main/java/de/muehlencord/shared/sharepoint/api/lists/SPList.java +++ b/sharepoint/api/src/main/java/de/muehlencord/shared/sharepoint/api/lists/SPList.java @@ -6,6 +6,7 @@ import com.microsoft.schemas.sharepoint.soap.lists.GetListItemsResponse; import com.microsoft.schemas.sharepoint.soap.lists.GetListResponse; import com.microsoft.schemas.sharepoint.soap.lists.ListsSoap; import com.microsoft.schemas.sharepoint.soap.lists.UpdateListItems; +import de.muehlencord.shared.sharepoint.api.AbstractSpJaxbObject; import de.muehlencord.shared.sharepoint.api.SPContext; import de.muehlencord.shared.sharepoint.api.SPJaxbObject; import de.muehlencord.shared.sharepoint.api.SPObject; @@ -45,7 +46,7 @@ public class SPList extends SPJaxbObject { @Override protected java.util.List getSchemaLocation() { java.util.List schemaList = new ArrayList(); - schemaList.add("/xsd/list.xsd"); + schemaList.add("/xsd/lists.xsd"); return schemaList; } @@ -65,12 +66,23 @@ public class SPList extends SPJaxbObject { /* *** queries *** */ public java.util.List> getListItems(java.util.List listColumnNames, String rowLimit) throws NoSuchAlgorithmException, KeyManagementException, JAXBException, MalformedURLException, - SAXException { + SAXException, ParserConfigurationException, IOException { + return getListItems(listColumnNames, rowLimit, null); + } + + public java.util.List> getListItems(java.util.List listColumnNames, String rowLimit, String queryString) throws + NoSuchAlgorithmException, KeyManagementException, JAXBException, MalformedURLException, + SAXException, ParserConfigurationException, IOException { //Here are additional parameters that may be set String viewName = null; GetListItems.ViewFields viewFields = null; + GetListItems.Query query = null; + if (queryString != null) { + query = new GetListItems.Query(); + query.getContent().add(AbstractSpJaxbObject.createSharePointCAMLNode(queryString)); + } QueryOptions queryOptions = new QueryOptions(); String webId = null; @@ -153,7 +165,7 @@ public class SPList extends SPJaxbObject { /* *** information & tools *** */ public Map getLookupValueMap(String value, String rowLimit) throws NoSuchAlgorithmException, - KeyManagementException, JAXBException, MalformedURLException, SAXException { + KeyManagementException, JAXBException, MalformedURLException, SAXException, ParserConfigurationException, IOException { // get list values from sharepoint java.util.List listColumnNames = new ArrayList<>(); @@ -204,14 +216,18 @@ public class SPList extends SPJaxbObject { private ListsSoap getListsPort() throws NoSuchAlgorithmException, KeyManagementException, MalformedURLException { java.net.URL contextURL = getContext().getSiteURL(); java.net.URL wsURL = new java.net.URL(contextURL.toString() + "/_vti_bin/Lists.asmx"); - java.net.URL wsdlURL = new java.net.URL(SPContext.class.getResource("/2010/wsdl/lists.wsdl").toExternalForm()); + java.net.URL wsdlURL = new java.net.URL(SPContext.class + .getResource("/2010/wsdl/lists.wsdl").toExternalForm()); com.microsoft.schemas.sharepoint.soap.lists.Lists service = new com.microsoft.schemas.sharepoint.soap.lists.Lists(wsdlURL); ListsSoap listsPort = service.getListsSoap(); + ((BindingProvider) listsPort).getRequestContext().put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, wsURL.toString()); // log all incoming and outgoing communication - TODO make this configurable by DEBUG switch java.util.List handlers = ((BindingProvider) listsPort).getBinding().getHandlerChain(); - handlers.add(new ServiceLogHandler()); + + handlers.add( + new ServiceLogHandler()); ((BindingProvider) listsPort).getBinding().setHandlerChain(handlers); return listsPort; diff --git a/sharepoint/api/src/test/java/de/muehlencord/shared/sharepoint/api/lists/SPListTest.java b/sharepoint/api/src/test/java/de/muehlencord/shared/sharepoint/api/lists/SPListTest.java index 12309b6..93307cd 100644 --- a/sharepoint/api/src/test/java/de/muehlencord/shared/sharepoint/api/lists/SPListTest.java +++ b/sharepoint/api/src/test/java/de/muehlencord/shared/sharepoint/api/lists/SPListTest.java @@ -106,8 +106,32 @@ public class SPListTest extends BaseTest { assertEquals("Size smallList", 30, smallList.size()); java.util.List> bigList = list.getListItems(columns, "300"); assertNotNull(bigList); - assertEquals("Size bigList", 250, bigList.size()); + assertEquals("Size bigList", 249, bigList.size()); } + + @Test + @Ignore // Depends on available sharepoint currently + public void testGetListItemsQuery() throws Exception { + SPLists instance = new SPLists(context); + SPList list = instance.getSpListByTitle("Questionnaire_Countries"); + assertNotNull(list); + java.util.List columns = new ArrayList<>(); + columns.add("Title"); + columns.add("ID"); + + // create query + String queryString = "" + + "" + + "DE" + + ""; + + java.util.List> smallList = list.getListItems(columns, "30",queryString); + // the list contains more than one item but title is a unique value, so it can only + // return one row if filtered to one country + assertNotNull(smallList); + assertEquals("Size smallList", 1, smallList.size()); + + } @Test @Ignore // Depends on available sharepoint currently diff --git a/sharepoint/api/src/test/resources/BatchImportTest.xml b/sharepoint/api/src/test/resources/de/muehlencord/shared/sharepoint/api/lists/batchimporttest.xml similarity index 100% rename from sharepoint/api/src/test/resources/BatchImportTest.xml rename to sharepoint/api/src/test/resources/de/muehlencord/shared/sharepoint/api/lists/batchimporttest.xml diff --git a/sharepoint/api/src/test/resources/BatchTest.xml b/sharepoint/api/src/test/resources/de/muehlencord/shared/sharepoint/api/lists/batchtest.xml similarity index 100% rename from sharepoint/api/src/test/resources/BatchTest.xml rename to sharepoint/api/src/test/resources/de/muehlencord/shared/sharepoint/api/lists/batchtest.xml