DocumentFilter#getQueryParams returns empty list of query para values but not null
test: all tests, account and authorization-mgt
M account/service/src/main/java/org/collectionspace/services/account/AccountRoleSubResource.java
M account/service/src/main/java/org/collectionspace/services/account/storage/AccountStorageClient.java
M account/service/src/main/java/org/collectionspace/services/account/storage/AccountValidatorHandler.java
M account/service/src/main/java/org/collectionspace/services/account/storage/AccountJpaFilter.java
M authorization-mgt/service/src/main/java/org/collectionspace/services/authorization/storage/RoleJpaFilter.java
M authorization-mgt/service/src/main/java/org/collectionspace/services/authorization/storage/PermissionJpaFilter.java
M common/src/main/java/org/collectionspace/services/common/storage/jpa/JpaStorageClientImpl.java
M common/src/main/java/org/collectionspace/services/common/storage/jpa/JpaRelationshipStorageClient.java
A common/src/main/java/org/collectionspace/services/common/storage/jpa/JpaStorageUtils.java
M common/src/main/java/org/collectionspace/services/common/document/DocumentFilter.java
ctx.setProperty("entity-name", AccountRoleRel.class.getName());
//subject name is necessary to indicate if role or account is a subject
ctx.setProperty("subject", subject);
- //set context that for the relationship query
+ //set context for the relationship query
ctx.setProperty("objectId", "account_id");
return ctx;
}
String screenName = null;
List<String> snvals = getQueryParam(AccountStorageConstants.Q_SCREEN_NAME);
- if (snvals != null) {
+ if (null != snvals && snvals.size() > 0) {
screenName = snvals.get(0);
}
if (null != screenName && !screenName.isEmpty()) {
String uid = null;
List<String> uidvals = getQueryParam(AccountStorageConstants.Q_USER_ID);
- if (uidvals != null) {
+ if (null != uidvals && uidvals.size() > 0) {
uid = uidvals.get(0);
}
if (null != uid && !uid.isEmpty()) {
String email = null;
List<String> emailvals = getQueryParam(AccountStorageConstants.Q_EMAIL);
- if (emailvals != null) {
+ if (null != emailvals && emailvals.size() > 0) {
email = emailvals.get(0);
}
if (null != email && !email.isEmpty()) {
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Query;
-import org.apache.commons.codec.binary.Base64;
import org.collectionspace.services.account.AccountsCommon;
import org.collectionspace.services.authentication.User;
import org.collectionspace.services.common.context.ServiceContext;
import org.collectionspace.services.common.document.DocumentWrapperImpl;
import org.collectionspace.services.common.security.SecurityUtils;
import org.collectionspace.services.common.storage.jpa.JpaStorageClientImpl;
+import org.collectionspace.services.common.storage.jpa.JpaStorageUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
DocumentWrapper<AccountsCommon> wrapDoc =
new DocumentWrapperImpl<AccountsCommon>(account);
handler.handle(Action.CREATE, wrapDoc);
- emf = getEntityManagerFactory();
+ emf = JpaStorageUtils.getEntityManagerFactory();
em = emf.createEntityManager();
em.getTransaction().begin();
//if userid and password are given, add to default id provider
throw new DocumentException(e);
} finally {
if (em != null) {
- releaseEntityManagerFactory(emf);
+ JpaStorageUtils.releaseEntityManagerFactory(emf);
}
}
}
new DocumentWrapperImpl<AccountsCommon>(account);
setCsid(account, id); //set id just in case it was not populated by consumer
handler.handle(Action.UPDATE, wrapDoc);
- emf = getEntityManagerFactory();
+ emf = JpaStorageUtils.getEntityManagerFactory();
em = emf.createEntityManager();
em.getTransaction().begin();
AccountsCommon accountFound = getAccount(em, id);
throw new DocumentException(e);
} finally {
if (emf != null) {
- releaseEntityManagerFactory(emf);
+ JpaStorageUtils.releaseEntityManagerFactory(emf);
}
}
}
EntityManagerFactory emf = null;
EntityManager em = null;
try {
- emf = getEntityManagerFactory();
+ emf = JpaStorageUtils.getEntityManagerFactory();
em = emf.createEntityManager();
//TODO investigate if deep delete is possible
//query an delete is inefficient
throw new DocumentException(e);
} finally {
if (emf != null) {
- releaseEntityManagerFactory(emf);
+ JpaStorageUtils.releaseEntityManagerFactory(emf);
}
}
}
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
-import javax.persistence.EntityManager;
-import javax.persistence.EntityManagerFactory;
import org.collectionspace.services.account.AccountTenant;
import org.collectionspace.services.account.AccountsCommon;
import org.collectionspace.services.account.Tenant;
import org.collectionspace.services.common.document.DocumentHandler.Action;
import org.collectionspace.services.common.document.InvalidDocumentException;
import org.collectionspace.services.common.document.ValidatorHandler;
-import org.collectionspace.services.common.storage.jpa.JpaStorageClientImpl;
+import org.collectionspace.services.common.storage.jpa.JpaStorageUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
private boolean isInvalidTenant(List<AccountTenant> atList, StringBuilder msgBldr) {
boolean invalid = false;
- JpaStorageClientImpl store = new JpaStorageClientImpl();
- EntityManagerFactory emf = store.getEntityManagerFactory();
- EntityManager em = emf.createEntityManager();
- try {
- for (AccountTenant at : atList) {
- String tid = at.getTenantId();
- if (tid == null || tid.isEmpty()) {
- invalid = true;
- msgBldr.append("\n tenant : tenantId is missing");
- break;
- }
- Tenant tenantFound = em.find(Tenant.class, tid);
- if (tenantFound == null) {
- invalid = true;
- msgBldr.append("\n tenant : tenantId=" + tid
- + " not found");
- break;
- }
+ for (AccountTenant at : atList) {
+ String tid = at.getTenantId();
+ if (tid == null || tid.isEmpty()) {
+ invalid = true;
+ msgBldr.append("\n tenant : tenantId is missing");
+ break;
}
- } finally {
- if (em != null) {
- store.releaseEntityManagerFactory(emf);
+ Tenant tenantFound = (Tenant) JpaStorageUtils.getEntity(tid, Tenant.class);
+ if (tenantFound == null) {
+ invalid = true;
+ msgBldr.append("\n tenant : tenantId=" + tid
+ + " not found");
+ break;
}
}
return invalid;
String resName = null;
List<String> rn = getQueryParam(PermissionStorageConstants.Q_RESOURCE_NAME);
- if (null != rn) {
+ if (null != rn && rn.size() > 0) {
resName = rn.get(0);
}
if (null != resName && !resName.isEmpty()) {
String roleName = null;
List<String> rn = getQueryParam(RoleStorageConstants.Q_ROLE_NAME);
- if (null != rn) {
+ if (null != rn && rn.size() > 0) {
roleName = rn.get(0);
}
if (null != roleName && !roleName.isEmpty()) {
protected int startPage; // Pagination offset for list results\r
protected int pageSize; // Pagination limit for list results\r
private boolean pageSizeDirty = false; // True if default page size explicitly set/overridden\r
- private MultivaluedMap<String, String> queryParams = null;\r
\r
+ //queryParams is not initialized as it would require a multi-valued map implementation\r
+ //unless it is used from opensource lib...this variable holds ref to\r
+ //implementation available in JBoss RESTeasy\r
+ private MultivaluedMap<String, String> queryParams = null;\r
\r
/**\r
* ParamBinding encapsulates parameter binding for query\r
this.value = value;\r
}\r
}\r
- \r
+\r
/**\r
* Instantiates a new document filter.\r
* \r
* @param ctx the ctx\r
*/\r
public DocumentFilter(ServiceContext ctx) {\r
- this.setPageSize(ctx.getServiceBindingPropertyValue(\r
- DocumentFilter.PAGE_SIZE_DEFAULT_PROPERTY));\r
+ this.setPageSize(ctx.getServiceBindingPropertyValue(\r
+ DocumentFilter.PAGE_SIZE_DEFAULT_PROPERTY));\r
}\r
- \r
+\r
public DocumentFilter() {\r
this("", 0, defaultPageSize); // Use empty string for easy concatenation\r
}\r
* \r
* @param queryParams the query params\r
*/\r
- public void setPagination(MultivaluedMap<String, String> queryParams) { \r
- //\r
- // Bail if there are no params\r
- //\r
- if (queryParams == null) return;\r
- \r
+ public void setPagination(MultivaluedMap<String, String> queryParams) {\r
//\r
- // Set the page size\r
- //\r
- String pageSizeStr = null;\r
+ // Bail if there are no params\r
+ //\r
+ if (queryParams == null) {\r
+ return;\r
+ }\r
+\r
+ //\r
+ // Set the page size\r
+ //\r
+ String pageSizeStr = null;\r
List<String> list = queryParams.remove(PAGE_SIZE_PARAM);\r
if (list != null) {\r
pageSizeStr = list.get(0);\r
public int getPageSize() {\r
return pageSize;\r
}\r
- \r
+\r
public boolean getPageSizeDirty() {\r
- return this.getPageSizeDirty();\r
+ return this.getPageSizeDirty();\r
}\r
\r
/**\r
* @param pageSize the max number of items to return for list requests\r
*/\r
public void setPageSize(String pageSizeStr) {\r
- int pageSize = this.defaultPageSize;\r
+ int pageSize = this.defaultPageSize;\r
if (pageSizeStr != null) {\r
try {\r
- pageSize = Integer.valueOf(pageSizeStr);\r
+ pageSize = Integer.valueOf(pageSizeStr);\r
} catch (NumberFormatException e) {\r
- //FIXME This should cause a warning in the log file and should result in the\r
- //FIXME page size being set to the default. We don't need to throw an exception here.\r
+ //FIXME This should cause a warning in the log file and should result in the\r
+ //FIXME page size being set to the default. We don't need to throw an exception here.\r
throw new NumberFormatException("Bad value for: " + PAGE_SIZE_PARAM);\r
}\r
}\r
- \r
+\r
setPageSize(pageSize);\r
}\r
\r
protected void setStartPage(String startPageStr) {\r
if (startPageStr != null) {\r
try {\r
- startPage = Integer.valueOf(startPageStr);\r
+ startPage = Integer.valueOf(startPageStr);\r
} catch (NumberFormatException e) {\r
throw new NumberFormatException("Bad value for: " + START_PAGE_PARAM);\r
}\r
}\r
\r
public void addQueryParam(String key, String value) {\r
- queryParams.add(key, value);\r
+ if (queryParams != null) {\r
+ queryParams.add(key, value);\r
+ }\r
}\r
\r
public List<String> getQueryParam(String key) {\r
- return queryParams.get(key);\r
+ if (queryParams != null) {\r
+ return queryParams.get(key);\r
+ } else {\r
+ return new ArrayList<String>();\r
+ }\r
}\r
\r
public MultivaluedMap<String, String> getQueryParams() {\r
DocumentWrapper<List<T>> wrapDoc =
new DocumentWrapperImpl<List<T>>(rl);
handler.handle(Action.CREATE, wrapDoc);
- emf = getEntityManagerFactory();
+ emf = JpaStorageUtils.getEntityManagerFactory();
em = emf.createEntityManager();
em.getTransaction().begin();
for (T r : rl) {
throw new DocumentException(e);
} finally {
if (em != null) {
- releaseEntityManagerFactory(emf);
+ JpaStorageUtils.releaseEntityManagerFactory(emf);
}
}
}
if ((null != where) && (where.length() > 0)) {
queryStrBldr.append(" AND " + where);
}
- emf = getEntityManagerFactory();
+ emf = JpaStorageUtils.getEntityManagerFactory();
em = emf.createEntityManager();
String queryStr = queryStrBldr.toString(); //for debugging
if (logger.isDebugEnabled()) {
throw new DocumentException(e);
} finally {
if (emf != null) {
- releaseEntityManagerFactory(emf);
+ JpaStorageUtils.releaseEntityManagerFactory(emf);
}
}
}
logger.debug("delete: using objectId=" + objectId);
}
deleteStr.append(" WHERE " + objectId + " = :objectId");
- emf = getEntityManagerFactory();
+ emf = JpaStorageUtils.getEntityManagerFactory();
em = emf.createEntityManager();
if (logger.isDebugEnabled()) {
logger.debug("delete: jql=" + deleteStr.toString());
throw new DocumentException(e);
} finally {
if (emf != null) {
- releaseEntityManagerFactory(emf);
+ JpaStorageUtils.releaseEntityManagerFactory(emf);
}
}
}
/** The logger. */
private final Logger logger = LoggerFactory.getLogger(JpaStorageClientImpl.class);
- /** The Constant CS_PERSISTENCE_UNIT. */
- public final static String CS_PERSISTENCE_UNIT = "org.collectionspace.services";
private Class entityClazz;
/**
DocumentWrapper<Object> wrapDoc = new DocumentWrapperImpl<Object>(entity);
handler.handle(Action.CREATE, wrapDoc);
setValue(entity, "setCreatedAtItem", Date.class, new Date());
- emf = getEntityManagerFactory();
+ emf = JpaStorageUtils.getEntityManagerFactory();
em = emf.createEntityManager();
em.getTransaction().begin();
em.persist(entity);
throw new DocumentException(e);
} finally {
if (em != null) {
- releaseEntityManagerFactory(emf);
+ JpaStorageUtils.releaseEntityManagerFactory(emf);
}
}
EntityManager em = null;
try {
handler.prepare(Action.GET);
- 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)) {
- queryStrBldr.append(" AND " + where);
- }
- emf = getEntityManagerFactory();
- em = emf.createEntityManager();
- String queryStr = queryStrBldr.toString(); //for debugging
- Query q = em.createQuery(queryStr);
- q.setParameter("csid", id);
- //TODO: add tenant id
-
- //TODO: get page
- if ((docFilter.getOffset() > 0) || (docFilter.getPageSize() > 0)) {
- } else {
- }
Object o = null;
-
try {
- //require transaction for get?
- em.getTransaction().begin();
- o = q.getSingleResult();
- em.getTransaction().commit();
+ o = JpaStorageUtils.getEntity(getEntityName(ctx), id, docFilter);
} catch (NoResultException nre) {
if (em != null && em.getTransaction().isActive()) {
em.getTransaction().rollback();
throw new DocumentException(e);
} finally {
if (emf != null) {
- releaseEntityManagerFactory(emf);
+ JpaStorageUtils.releaseEntityManagerFactory(emf);
}
}
}
throw new IllegalArgumentException(
"JpaStorageClient.getFiltered: handler is missing");
}
+
DocumentFilter docFilter = handler.getDocumentFilter();
if (docFilter == null) {
docFilter = handler.createDocumentFilter();
queryStrBldr.append(" a");
List<DocumentFilter.ParamBinding> params = docFilter.buildWhereForSearch(queryStrBldr);
//TODO: add tenant id
- emf = getEntityManagerFactory();
+ emf = JpaStorageUtils.getEntityManagerFactory();
em = emf.createEntityManager();
String queryStr = queryStrBldr.toString(); //for debugging
Query q = em.createQuery(queryStr);
throw new DocumentException(e);
} finally {
if (emf != null) {
- releaseEntityManagerFactory(emf);
+ JpaStorageUtils.releaseEntityManagerFactory(emf);
}
}
}
setCsid(entity, id);
DocumentWrapper<Object> wrapDoc = new DocumentWrapperImpl<Object>(entity);
handler.handle(Action.UPDATE, wrapDoc);
- emf = getEntityManagerFactory();
+ emf = JpaStorageUtils.getEntityManagerFactory();
em = emf.createEntityManager();
em.getTransaction().begin();
Object entityFound = em.find(entity.getClass(), id);
throw new DocumentException(e);
} finally {
if (emf != null) {
- releaseEntityManagerFactory(emf);
+ JpaStorageUtils.releaseEntityManagerFactory(emf);
}
}
}
//TODO: add tenant id
- emf = getEntityManagerFactory();
+ emf = JpaStorageUtils.getEntityManagerFactory();
em = emf.createEntityManager();
em.getTransaction().begin();
throw new DocumentException(e);
} finally {
if (emf != null) {
- releaseEntityManagerFactory(emf);
+ JpaStorageUtils.releaseEntityManagerFactory(emf);
}
}
}
deleteStr.append(" WHERE csid = :csid");
//TODO: add tenant id
- emf = getEntityManagerFactory();
+ emf = JpaStorageUtils.getEntityManagerFactory();
em = emf.createEntityManager();
Query q = em.createQuery(deleteStr.toString());
q.setParameter("csid", id);
throw new DocumentException(e);
} finally {
if (emf != null) {
- releaseEntityManagerFactory(emf);
+ JpaStorageUtils.releaseEntityManagerFactory(emf);
}
}
}
- /**
- * Gets the entity manager factory.
- *
- * @return the entity manager factory
- */
- public EntityManagerFactory getEntityManagerFactory() {
- return getEntityManagerFactory(CS_PERSISTENCE_UNIT);
- }
-
- /**
- * Gets the entity manager factory.
- *
- * @param persistenceUnit the persistence unit
- *
- * @return the entity manager factory
- */
- public EntityManagerFactory getEntityManagerFactory(
- String persistenceUnit) {
- return Persistence.createEntityManagerFactory(persistenceUnit);
-
- }
-
- /**
- * Release entity manager factory.
- *
- * @param emf the emf
- */
- public void releaseEntityManagerFactory(EntityManagerFactory emf) {
- if (emf != null) {
- emf.close();
- }
- }
/**
* getValue gets invokes specified accessor method on given object. Assumption
--- /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 2010 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
+ */
+package org.collectionspace.services.common.storage.jpa;
+
+import java.util.List;
+import javax.persistence.EntityManager;
+import javax.persistence.EntityManagerFactory;
+import javax.persistence.Persistence;
+import javax.persistence.Query;
+import org.collectionspace.services.common.document.DocumentFilter;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Utilities for JpaStorage
+ * @author
+ */
+public class JpaStorageUtils {
+
+ final private static Logger logger = LoggerFactory.getLogger(JpaStorageUtils.class);
+ /** The Constant CS_PERSISTENCE_UNIT. */
+ public final static String CS_PERSISTENCE_UNIT = "org.collectionspace.services";
+
+ /**
+ * getEntity for given id and class
+ * @param id
+ * @param entityClazz
+ * @return null if entity is not found
+ * @throws Exception
+ */
+ public static Object getEntity(String id, Class entityClazz) {
+ EntityManagerFactory emf = null;
+ EntityManager em = null;
+ Object entityFound = null;
+ try {
+ emf = getEntityManagerFactory();
+ em = emf.createEntityManager();
+ entityFound = em.find(entityClazz, id);
+ if (entityFound == null) {
+ if (em != null && em.getTransaction().isActive()) {
+ em.getTransaction().rollback();
+ }
+ }
+ } finally {
+ if (em != null) {
+ releaseEntityManagerFactory(emf);
+ }
+ }
+ return entityFound;
+ }
+
+ /**
+ * getEntity using where clause from given docFilter
+ * @param entityName fully qualified entity name
+ * @param id
+ * @param docFilter
+ * @return
+ * @throws Exception
+ */
+ public static Object getEntity(String entityName, String id, DocumentFilter docFilter)
+ throws Exception {
+ EntityManagerFactory emf = null;
+ EntityManager em = null;
+ Object o = null;
+ try {
+ if (docFilter == null) {
+ docFilter = new DocumentFilter();
+ }
+ StringBuilder queryStrBldr = new StringBuilder("SELECT a FROM ");
+ queryStrBldr.append(entityName);
+ queryStrBldr.append(" a");
+ queryStrBldr.append(" WHERE csid = :csid");
+ //TODO: add tenant id
+ String where = docFilter.getWhereClause();
+ if ((null != where) && (where.length() > 0)) {
+ queryStrBldr.append(" AND " + where);
+ }
+
+ emf = getEntityManagerFactory();
+ em = emf.createEntityManager();
+ String queryStr = queryStrBldr.toString(); //for debugging
+ Query q = em.createQuery(queryStr);
+ q.setParameter("csid", id);
+ List<DocumentFilter.ParamBinding> params =
+ docFilter.buildWhereForSearch(queryStrBldr);
+ for (DocumentFilter.ParamBinding p : params) {
+ q.setParameter(p.getName(), p.getValue());
+ }
+
+ //require transaction for get?
+ em.getTransaction().begin();
+ o = q.getSingleResult();
+ em.getTransaction().commit();
+ } finally {
+ if (em != null) {
+ releaseEntityManagerFactory(emf);
+ }
+ }
+ return o;
+ }
+
+ /**
+ * Gets the entity manager factory.
+ *
+ * @return the entity manager factory
+ */
+ public static EntityManagerFactory getEntityManagerFactory() {
+ return getEntityManagerFactory(CS_PERSISTENCE_UNIT);
+ }
+
+ /**
+ * Gets the entity manager factory.
+ *
+ * @param persistenceUnit the persistence unit
+ *
+ * @return the entity manager factory
+ */
+ public static EntityManagerFactory getEntityManagerFactory(
+ String persistenceUnit) {
+ return Persistence.createEntityManagerFactory(persistenceUnit);
+
+ }
+
+ /**
+ * Release entity manager factory.
+ *
+ * @param emf the emf
+ */
+ public static void releaseEntityManagerFactory(EntityManagerFactory emf) {
+ if (emf != null) {
+ emf.close();
+ }
+
+ }
+}
+