}
- public ClientResponse<AccountsCommonList> readSearchList(String screenName) {
- return accountProxy.readSearchList(screenName);
+ public ClientResponse<AccountsCommonList> readSearchList(String screenName, String uid, String email) {
+ return accountProxy.readSearchList(screenName, uid, email);
}
@GET
@Produces({"application/xml"})
- ClientResponse<AccountsCommonList> readSearchList(@QueryParam("sn") String screenName);
+ ClientResponse<AccountsCommonList> readSearchList(@QueryParam("sn") String screenName, @QueryParam("uid") String uid, @QueryParam("email") String email);
//(C)reate
@POST
private String knownResourceId = null;
private String resource1Id = null;
private String resource2Id = null;
-
+ private String resource3Id = null;
/*
* This method is called only by the parent class, AbstractServiceTest
*/
+
@Override
protected String getServicePathComponent() {
return client.getServicePathComponent();
invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
resource2Id = extractId(res);
+
+ AccountsCommon account3 =
+ createAccountInstance("dj", "hithere10", "dj@dinoland.com", true, true, true);
+ res = client.create(account3);
+ statusCode = res.getStatus();
+ Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
+ invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
+ Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
+ resource3Id = extractId(res);
}
// Failure outcomes
@Test(dataProvider = "testName", dataProviderClass = AbstractServiceTest.class,
dependsOnMethods = {"createList", "read"})
- public void readSearchList(String testName) throws Exception {
+ public void searchScreenName(String testName) throws Exception {
+
+ // Perform setup.
+ setupReadList(testName);
+
+ // Submit the request to the service and store the response.
+ ClientResponse<AccountsCommonList> res = client.readSearchList("tom", null, null);
+ AccountsCommonList list = res.getEntity();
+ int statusCode = res.getStatus();
+
+ // Check the status code of the response: does it match
+ // the expected response(s)?
+ if (logger.isDebugEnabled()) {
+ logger.debug(testName + ": status = " + statusCode);
+ }
+ Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
+ invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
+ Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
+ Assert.assertEquals(1, list.getAccountListItem().size());
+ // Optionally output additional data about list members for debugging.
+ boolean iterateThroughList = true;
+ if (iterateThroughList && logger.isDebugEnabled()) {
+ printList(testName, list);
+ }
+ }
+
+ @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTest.class,
+ dependsOnMethods = {"createList", "read"})
+ public void searchUserId(String testName) throws Exception {
+
+ // Perform setup.
+ setupReadList(testName);
+
+ // Submit the request to the service and store the response.
+ ClientResponse<AccountsCommonList> res = client.readSearchList(null, "tom", null);
+ AccountsCommonList list = res.getEntity();
+ int statusCode = res.getStatus();
+
+ // Check the status code of the response: does it match
+ // the expected response(s)?
+ if (logger.isDebugEnabled()) {
+ logger.debug(testName + ": status = " + statusCode);
+ }
+ Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
+ invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
+ Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
+ Assert.assertEquals(1, list.getAccountListItem().size());
+ // Optionally output additional data about list members for debugging.
+ boolean iterateThroughList = true;
+ if (iterateThroughList && logger.isDebugEnabled()) {
+ printList(testName, list);
+ }
+ }
+
+ @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTest.class,
+ dependsOnMethods = {"createList", "read"})
+ public void searchEmail(String testName) throws Exception {
// Perform setup.
setupReadList(testName);
// Submit the request to the service and store the response.
- ClientResponse<AccountsCommonList> res = client.readSearchList("tom");
+ ClientResponse<AccountsCommonList> res = client.readSearchList(null, null, "dinoland");
AccountsCommonList list = res.getEntity();
int statusCode = res.getStatus();
Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
+ Assert.assertEquals(2, list.getAccountListItem().size());
+ // Optionally output additional data about list members for debugging.
+ boolean iterateThroughList = true;
+ if (iterateThroughList && logger.isDebugEnabled()) {
+ printList(testName, list);
+ }
+ }
+ @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTest.class,
+ dependsOnMethods = {"createList", "read"})
+ public void searchScreenNameEmail(String testName) throws Exception {
+
+ // Perform setup.
+ setupReadList(testName);
+
+ // Submit the request to the service and store the response.
+ ClientResponse<AccountsCommonList> res = client.readSearchList("tom", null, "jerry");
+ AccountsCommonList list = res.getEntity();
+ int statusCode = res.getStatus();
+
+ // Check the status code of the response: does it match
+ // the expected response(s)?
+ if (logger.isDebugEnabled()) {
+ logger.debug(testName + ": status = " + statusCode);
+ }
+ Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
+ invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
+ Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
+ Assert.assertEquals(1, list.getAccountListItem().size());
// Optionally output additional data about list members for debugging.
boolean iterateThroughList = true;
if (iterateThroughList && logger.isDebugEnabled()) {
Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
+
+ res = client.delete(resource3Id);
+ statusCode = res.getStatus();
+ Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
+ invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
+ Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
}
// Failure outcomes
import org.collectionspace.services.common.document.BadRequestException;
import org.collectionspace.services.common.document.DocumentFilter;
import org.collectionspace.services.common.document.DocumentWrapper;
-import org.collectionspace.services.common.storage.jpa.JpaDocumentFilter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Override
public DocumentFilter createDocumentFilter() {
- return new JpaDocumentFilter();
+ return new AccountJpaFilter();
}
- private void setWhereForGetAll(StringBuilder strBld) {
- DocumentFilter filter = getDocumentFilter();
- String screenName = null;
- if (screenName != null && !screenName.isEmpty()) {
- String ptClause =
- "WHERE UPPER(a.screenName)"
- + " LIKE "
- + ":sn";
- filter.addQueryParam("sn", "%" + screenName.toUpperCase() + "%");
- filter.setWhereClause(ptClause);
- }
- }
/**
* sanitize removes data not needed to be sent to the consumer
* @param account
*/
private void sanitize(AccountsCommon account) {
account.setPassword(null);
+
}
}
--- /dev/null
+/**
+ * This document is a part of the source code and related artifacts
+ * for CollectionSpace, an open source collections management system
+ * for museums and related institutions:
+
+ * http://www.collectionspace.org
+ * http://wiki.collectionspace.org
+
+ * Copyright 2009 University of California at Berkeley
+
+ * Licensed under the Educational Community License (ECL), Version 2.0.
+ * You may not use this file except in compliance with this License.
+
+ * You may obtain a copy of the ECL 2.0 License at
+
+ * https://source.collectionspace.org/collection-space/LICENSE.txt
+
+ * 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.
+ *//**
+ * This document is a part of the source code and related artifacts
+ * for CollectionSpace, an open source collections management system
+ * for museums and related institutions:
+
+ * http://www.collectionspace.org
+ * http://wiki.collectionspace.org
+
+ * Copyright 2009 University of California at Berkeley
+
+ * Licensed under the Educational Community License (ECL), Version 2.0.
+ * You may not use this file except in compliance with this License.
+
+ * You may obtain a copy of the ECL 2.0 License at
+
+ * https://source.collectionspace.org/collection-space/LICENSE.txt
+
+ * 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.
+ */
+/*
+ * To change this template, choose Tools | Templates
+ * and open the template in the editor.
+ */
+package org.collectionspace.services.account.storage;
+
+import java.util.ArrayList;
+import java.util.List;
+import org.collectionspace.services.common.storage.jpa.JpaDocumentFilter;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ *
+ * @author
+ */
+public class AccountJpaFilter extends JpaDocumentFilter {
+
+ private final Logger logger = LoggerFactory.getLogger(AccountJpaFilter.class);
+
+ @Override
+ public List<ParamBinding> buildWhereForSearch(StringBuilder queryStrBldr) {
+
+ List<ParamBinding> paramList = new ArrayList<ParamBinding>();
+ boolean hasWhere = false;
+ //TODO: add tenant id
+
+ String screenName = null;
+ List<String> snvals = getQueryParam(AccountStorageConstants.Q_SCREEN_NAME);
+ if (snvals != null) {
+ screenName = snvals.get(0);
+ }
+ if (null != screenName && !screenName.isEmpty()) {
+ hasWhere = true;
+ queryStrBldr.append(" WHERE");
+ queryStrBldr.append(" UPPER(a." + AccountStorageConstants.SCREEN_NAME + ")");
+ queryStrBldr.append(" LIKE");
+ queryStrBldr.append(" :" + AccountStorageConstants.Q_SCREEN_NAME);
+ paramList.add(new ParamBinding(AccountStorageConstants.Q_SCREEN_NAME, "%"
+ + screenName.toUpperCase() + "%"));
+ }
+
+ String uid = null;
+ List<String> uidvals = getQueryParam(AccountStorageConstants.Q_USER_ID);
+ if (uidvals != null) {
+ uid = uidvals.get(0);
+ }
+ if (null != uid && !uid.isEmpty()) {
+ if (hasWhere) {
+ queryStrBldr.append(" AND");
+ } else {
+ queryStrBldr.append(" WHERE");
+ }
+ queryStrBldr.append(" UPPER(a." + AccountStorageConstants.USER_ID + ")");
+ queryStrBldr.append(" LIKE");
+ queryStrBldr.append(" :" + AccountStorageConstants.Q_USER_ID);
+ paramList.add(new ParamBinding(AccountStorageConstants.Q_USER_ID, "%"
+ + uid.toUpperCase() + "%"));
+ hasWhere = true;
+ }
+
+ String email = null;
+ List<String> emailvals = getQueryParam(AccountStorageConstants.Q_EMAIL);
+ if (emailvals != null) {
+ email = emailvals.get(0);
+ }
+ if (null != email && !email.isEmpty()) {
+ if (hasWhere) {
+ queryStrBldr.append(" AND");
+ } else {
+ queryStrBldr.append(" WHERE");
+ }
+ queryStrBldr.append(" UPPER(a." + AccountStorageConstants.EMAIL + ")");
+ queryStrBldr.append(" LIKE");
+ queryStrBldr.append(" :" + AccountStorageConstants.Q_EMAIL);
+ paramList.add(new ParamBinding(AccountStorageConstants.Q_EMAIL, "%"
+ + email.toUpperCase() + "%"));
+ hasWhere = true;
+ }
+
+ if (logger.isDebugEnabled()) {
+ logger.debug("query=" + queryStrBldr.toString());
+ }
+
+ return paramList;
+ }
+
+ @Override
+ public List<ParamBinding> buildWhere(StringBuilder queryStrBldr) {
+ return new ArrayList<ParamBinding>();
+ }
+}
*/
package org.collectionspace.services.account.storage;
-import java.util.ArrayList;
-import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Query;
--- /dev/null
+/**
+ * This document is a part of the source code and related artifacts
+ * for CollectionSpace, an open source collections management system
+ * for museums and related institutions:
+
+ * http://www.collectionspace.org
+ * http://wiki.collectionspace.org
+
+ * Copyright 2009 University of California at Berkeley
+
+ * Licensed under the Educational Community License (ECL), Version 2.0.
+ * You may not use this file except in compliance with this License.
+
+ * You may obtain a copy of the ECL 2.0 License at
+
+ * https://source.collectionspace.org/collection-space/LICENSE.txt
+
+ * 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.
+ *//**
+ * This document is a part of the source code and related artifacts
+ * for CollectionSpace, an open source collections management system
+ * for museums and related institutions:
+
+ * http://www.collectionspace.org
+ * http://wiki.collectionspace.org
+
+ * Copyright 2009 University of California at Berkeley
+
+ * Licensed under the Educational Community License (ECL), Version 2.0.
+ * You may not use this file except in compliance with this License.
+
+ * You may obtain a copy of the ECL 2.0 License at
+
+ * https://source.collectionspace.org/collection-space/LICENSE.txt
+
+ * 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.
+ */
+/*
+ * To change this template, choose Tools | Templates
+ * and open the template in the editor.
+ */
+
+package org.collectionspace.services.account.storage;
+
+/**
+ * AccountStorageConstants declares query params, etc.
+ * @author
+ */
+public class AccountStorageConstants {
+
+ final public static String Q_SCREEN_NAME = "sn";
+ final public static String Q_USER_ID= "uid";
+ final public static String Q_EMAIL = "email";
+
+
+ final public static String SCREEN_NAME = "screenName";
+ final public static String USER_ID = "userId";
+ final public static String EMAIL = "email";
+}
*/\r
package org.collectionspace.services.common.document;\r
\r
-import java.util.HashMap;\r
+import java.util.ArrayList;\r
import java.util.List;\r
-import java.util.Map;\r
import javax.ws.rs.core.MultivaluedMap;\r
import org.collectionspace.services.common.query.IQueryManager;\r
\r
+//TODO: would be great to not rely on resteasy directly\r
+import org.jboss.resteasy.specimpl.MultivaluedMapImpl;\r
+\r
/**\r
* DocumentFilter bundles simple query filtering parameters. \r
* It is designed to be used with filtered get and search calls to RepositoryClient.\r
protected String whereClause; // Filtering clause. Omit the "WHERE".\r
protected int startPage; // Pagination offset for list results\r
protected int pageSize; // Pagination limit for list results\r
- private MultivaluedMap<String, String> queryParams;\r
+ private MultivaluedMap<String, String> queryParams = new MultivaluedMapImpl<String, String>();\r
+\r
+\r
+ /**\r
+ * ParamBinding encapsulates parameter binding for query\r
+ */\r
+ public static class ParamBinding {\r
+\r
+ private String name;\r
+ private Object value;\r
+\r
+ public ParamBinding(String name, Object value) {\r
+ this.name = name;\r
+ this.value = value;\r
+ }\r
+\r
+ /**\r
+ * @return the name\r
+ */\r
+ public String getName() {\r
+ return name;\r
+ }\r
+\r
+ /**\r
+ * @param name the name to set\r
+ */\r
+ public void setName(String name) {\r
+ this.name = name;\r
+ }\r
\r
+ /**\r
+ * @return the value\r
+ */\r
+ public Object getValue() {\r
+ return value;\r
+ }\r
+\r
+ /**\r
+ * @param value the value to set\r
+ */\r
+ public void setValue(Object value) {\r
+ this.value = value;\r
+ }\r
+ }\r
+ \r
public DocumentFilter() {\r
this("", 0, defaultPageSize); // Use empty string for easy concatenation\r
}\r
}\r
}\r
\r
+ /**\r
+ * buildWhereClause builds where clause for search query\r
+ * @param queryStrBldr query string to append with where clause\r
+ * @return parameter binding\r
+ */\r
+ public List<ParamBinding> buildWhereForSearch(StringBuilder queryStrBldr) {\r
+ return new ArrayList<ParamBinding>();\r
+ }\r
+\r
+ /**\r
+ * buildWhereClause builds where clause for get, update or delete\r
+ * @param queryStrBldr query string to append with where clause\r
+ * @return parameter binding\r
+ */\r
+ public List<ParamBinding> buildWhere(StringBuilder queryStrBldr) {\r
+ return new ArrayList<ParamBinding>();\r
+ }\r
+\r
/**\r
* @return the specified (0-based) page offset\r
*/\r
EntityManager em = null;
try {
handler.prepare(Action.GET);
- StringBuilder queryStr = new StringBuilder("SELECT a FROM ");
- queryStr.append(getEntityName(ctx));
- queryStr.append(" a");
- queryStr.append(" WHERE csid = :csid");
+ StringBuilder queryStrBldr = new StringBuilder("SELECT a FROM ");
+ queryStrBldr.append(getEntityName(ctx));
+ queryStrBldr.append(" a");
+ queryStrBldr.append(" WHERE csid = :csid");
//TODO: add tenant id
String where = docFilter.getWhereClause();
if ((null != where) && (where.length() > 0)) {
- queryStr.append(" AND " + where);
+ queryStrBldr.append(" AND " + where);
}
emf = getEntityManagerFactory();
em = emf.createEntityManager();
- Query q = em.createQuery(queryStr.toString());
+ String queryStr = queryStrBldr.toString(); //for debugging
+ Query q = em.createQuery(queryStr);
q.setParameter("csid", id);
//TODO: add tenant id
try {
handler.prepare(Action.GET_ALL);
- StringBuilder strBld = new StringBuilder("SELECT a FROM ");
- strBld.append(getEntityName(ctx));
- strBld.append(" a");
+ StringBuilder queryStrBldr = new StringBuilder("SELECT a FROM ");
+ queryStrBldr.append(getEntityName(ctx));
+ queryStrBldr.append(" a");
+ List<DocumentFilter.ParamBinding> params = docFilter.buildWhereForSearch(queryStrBldr);
//TODO: add tenant id
emf = getEntityManagerFactory();
em = emf.createEntityManager();
- String queryStr = strBld.toString(); //for debugging
+ String queryStr = queryStrBldr.toString(); //for debugging
Query q = em.createQuery(queryStr);
- //TODO: add tenant id
- String where = docFilter.getWhereClause();
- if ((null != where) && (where.length() > 0)) {
- strBld.append(where);
+ //bind parameters
+ for(DocumentFilter.ParamBinding p : params) {
+ q.setParameter(p.getName(), p.getValue());
}
if (docFilter.getOffset() > 0) {
q.setFirstResult(docFilter.getOffset());