2 * This document is a part of the source code and related artifacts
3 * for CollectionSpace, an open source collections management system
4 * for museums and related institutions:
6 * http://www.collectionspace.org
7 * http://wiki.collectionspace.org
9 * Copyright 2009 University of California at Berkeley
11 * Licensed under the Educational Community License (ECL), Version 2.0.
12 * You may not use this file except in compliance with this License.
14 * You may obtain a copy of the ECL 2.0 License at
16 * https://source.collectionspace.org/collection-space/LICENSE.txt
18 * Unless required by applicable law or agreed to in writing, software
19 * distributed under the License is distributed on an "AS IS" BASIS,
20 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21 * See the License for the specific language governing permissions and
22 * limitations under the License.
25 package org.collectionspace.services.authorization.storage;
27 import java.util.ArrayList;
28 import java.util.List;
30 import org.collectionspace.services.common.context.ServiceContext;
31 import org.collectionspace.services.common.security.SecurityUtils;
32 import org.collectionspace.services.common.storage.jpa.JpaDocumentFilter;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
37 * PermissionJpaFilter is to build where clause for role queries
40 public class PermissionJpaFilter extends JpaDocumentFilter {
43 private final Logger logger = LoggerFactory.getLogger(PermissionJpaFilter.class);
46 * Instantiates a new permission jpa filter.
50 public PermissionJpaFilter(ServiceContext ctx) {
55 * Append where clause.
57 * @param csAdmin the cs admin
58 * @param paramList the param list
59 * @param queryStrBldr the query str bldr
60 * @param fieldName the field name
61 * @param queryParam the query param
62 * @return the string builder
64 private StringBuilder appendWhereClause(boolean whereOpNeeded,
65 StringBuilder queryStrBldr,
67 String queryParamName,
68 List<ParamBinding> paramList,
69 String queryParamValue) {
70 String op = whereOpNeeded ? " WHERE " : " AND ";
71 if (queryParamValue != null && !queryParamValue.isEmpty()) {
72 queryStrBldr.append(op);
73 queryStrBldr.append("UPPER(a." + fieldName + ")");
74 queryStrBldr.append(" LIKE");
75 queryStrBldr.append(" :" + queryParamName);
76 paramList.add(new ParamBinding(queryParamName, "%"
77 + queryParamValue.toUpperCase() + "%"));
84 * @see org.collectionspace.services.common.document.DocumentFilter#buildWhereForSearch(java.lang.StringBuilder)
87 public List<ParamBinding> buildWhereForSearch(StringBuilder queryStrBldr) {
89 List<ParamBinding> paramList = new ArrayList<ParamBinding>();
90 boolean whereOpNeeded = true;
92 boolean csAdmin = SecurityUtils.isCSpaceAdmin();
94 queryStrBldr.append(addTenant(false /* add WHERE or AND */,
96 whereOpNeeded = false;
99 // get the resource query param
100 String resName = null;
101 List<String> resNames = getQueryParam(PermissionStorageConstants.Q_RESOURCE_NAME);
102 if (resNames != null && resNames.size() > 0) {
103 // grab just the first instance
104 resName = resNames.get(0);
105 appendWhereClause(whereOpNeeded,
107 PermissionStorageConstants.RESOURCE_NAME,
108 PermissionStorageConstants.Q_RESOURCE_NAME,
111 whereOpNeeded = false;
115 // get the actiongroup query param
116 String actionGroup = null;
117 List<String> actionGroups = getQueryParam(PermissionStorageConstants.Q_ACTION_GROUP);
118 if (actionGroups != null && actionGroups.size() > 0) {
119 actionGroup = actionGroups.get(0);
120 appendWhereClause(whereOpNeeded,
122 PermissionStorageConstants.ACTION_GROUP,
123 PermissionStorageConstants.Q_ACTION_GROUP,
126 whereOpNeeded = false;
129 // if (null != resName && !resName.isEmpty()) {
131 // queryStrBldr.append(" AND");
133 // queryStrBldr.append(" WHERE");
135 // queryStrBldr.append(" UPPER(a." + PermissionStorageConstants.RESOURCE_NAME + ")");
136 // queryStrBldr.append(" LIKE");
137 // queryStrBldr.append(" :" + PermissionStorageConstants.Q_RESOURCE_NAME);
138 // paramList.add(new ParamBinding(PermissionStorageConstants.Q_RESOURCE_NAME, "%"
139 // + resName.toUpperCase() + "%"));
142 if (logger.isDebugEnabled()) {
143 String query = queryStrBldr.toString();
144 logger.debug("query=" + query);
151 * @see org.collectionspace.services.common.document.DocumentFilter#buildWhere(java.lang.StringBuilder)
154 public List<ParamBinding> buildWhere(StringBuilder queryStrBldr) {
155 return new ArrayList<ParamBinding>();