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;
29 import org.collectionspace.services.common.context.ServiceContext;
30 import org.collectionspace.services.common.security.SecurityUtils;
31 import org.collectionspace.services.common.storage.jpa.JpaDocumentFilter;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
36 * PermissionJpaFilter is to build where clause for role queries
39 public class PermissionJpaFilter extends JpaDocumentFilter {
42 private final Logger logger = LoggerFactory.getLogger(PermissionJpaFilter.class);
45 * Instantiates a new permission jpa filter.
49 public PermissionJpaFilter(ServiceContext ctx) {
54 * Append where clause.
56 * @param csAdmin the cs admin
57 * @param paramList the param list
58 * @param queryStrBldr the query str bldr
59 * @param fieldName the field name
60 * @param queryParam the query param
61 * @return the string builder
63 private StringBuilder appendWhereClause(boolean whereOpNeeded,
64 StringBuilder queryStrBldr,
66 String queryParamName,
67 List<ParamBinding> paramList,
68 String queryParamValue) {
69 String op = whereOpNeeded ? " WHERE " : " AND ";
70 if (queryParamValue != null && !queryParamValue.isEmpty()) {
71 queryStrBldr.append(op);
72 queryStrBldr.append("UPPER(a." + fieldName + ")");
73 queryStrBldr.append(" LIKE");
74 queryStrBldr.append(" :" + queryParamName);
75 paramList.add(new ParamBinding(queryParamName, "%"
76 + queryParamValue.toUpperCase() + "%"));
83 * @see org.collectionspace.services.common.document.DocumentFilter#buildWhereForSearch(java.lang.StringBuilder)
86 public List<ParamBinding> buildWhereForSearch(StringBuilder queryStrBldr) {
88 List<ParamBinding> paramList = new ArrayList<ParamBinding>();
89 boolean whereOpNeeded = true;
91 boolean csAdmin = SecurityUtils.isCSpaceAdmin();
93 queryStrBldr.append(addTenant(false /* add WHERE or AND */,
95 whereOpNeeded = false;
98 // get the resource query param
99 String resName = null;
100 List<String> resNames = getQueryParam(PermissionStorageConstants.Q_RESOURCE_NAME);
101 if (resNames != null && resNames.size() > 0) {
102 // grab just the first instance
103 resName = resNames.get(0);
104 appendWhereClause(whereOpNeeded,
106 PermissionStorageConstants.RESOURCE_NAME,
107 PermissionStorageConstants.Q_RESOURCE_NAME,
110 whereOpNeeded = false;
114 // get the actiongroup query param
115 String actionGroup = null;
116 List<String> actionGroups = getQueryParam(PermissionStorageConstants.Q_ACTION_GROUP);
117 if (actionGroups != null && actionGroups.size() > 0) {
118 actionGroup = actionGroups.get(0);
119 appendWhereClause(whereOpNeeded,
121 PermissionStorageConstants.ACTION_GROUP,
122 PermissionStorageConstants.Q_ACTION_GROUP,
125 whereOpNeeded = false;
128 // if (null != resName && !resName.isEmpty()) {
130 // queryStrBldr.append(" AND");
132 // queryStrBldr.append(" WHERE");
134 // queryStrBldr.append(" UPPER(a." + PermissionStorageConstants.RESOURCE_NAME + ")");
135 // queryStrBldr.append(" LIKE");
136 // queryStrBldr.append(" :" + PermissionStorageConstants.Q_RESOURCE_NAME);
137 // paramList.add(new ParamBinding(PermissionStorageConstants.Q_RESOURCE_NAME, "%"
138 // + resName.toUpperCase() + "%"));
141 if (logger.isDebugEnabled()) {
142 String query = queryStrBldr.toString();
143 logger.debug("query=" + query);
150 * @see org.collectionspace.services.common.document.DocumentFilter#buildWhere(java.lang.StringBuilder)
153 public List<ParamBinding> buildWhere(StringBuilder queryStrBldr) {
154 return new ArrayList<ParamBinding>();