Permalink
Browse files

Merge pull request #31 from amondel2/master

GPSPRINGSECURITYUI-58/GPSPRINGSECURITYUI-23: Transform executeQuery to Criteria
  • Loading branch information...
2 parents 7ace934 + 0671fec commit af3845aa6d035082ea4d96ab26716303e277aed0 @graemerocher graemerocher committed Jul 28, 2015
@@ -83,31 +83,22 @@ class AclClassController extends AbstractS2UiController {
boolean useOffset = params.containsKey('offset')
setIfMissing 'max', 10, 100
setIfMissing 'offset', 0
-
- def hql = new StringBuilder('FROM ').append(lookupClassName()).append(' c WHERE 1=1 ')
- def queryParams = [:]
-
- for (name in ['className']) {
- if (params[name]) {
- hql.append " AND LOWER(c.$name) LIKE :$name"
- queryParams[name] = '%' + params[name].toLowerCase() + '%'
- }
- }
-
- int totalCount = lookupClass().executeQuery("SELECT COUNT(DISTINCT c) $hql", queryParams)[0]
-
+
Integer max = params.int('max')
Integer offset = params.int('offset')
-
- String orderBy = ''
- if (params.sort) {
- orderBy = " ORDER BY c.$params.sort ${params.order ?: 'ASC'}"
+
+ def cs = lookupClass().createCriteria()
+ def results = cs.list(max: max,offset: offset) {
+ firstResult: offset
+ maxResults: max
+ if (params['className']) {
+ ilike('className','%' + params['className'] + '%')
+ }
+ if (params.sort) {
+ order(params.sort,params.order ?: 'ASC')
+ }
}
-
- def results = lookupClass().executeQuery(
- "SELECT DISTINCT c $hql $orderBy",
- queryParams, [max: max, offset: offset])
- def model = [results: results, totalCount: totalCount, searched: true]
+ def model = [results: results, totalCount: results.totalCount, searched: true]
// add query params to model for paging
for (name in ['className']) {
@@ -127,14 +118,16 @@ class AclClassController extends AbstractS2UiController {
if (params.term?.length() > 2) {
String className = params.term
setIfMissing 'max', 10, 100
-
- def results = lookupClass().executeQuery(
- "SELECT DISTINCT c.className " +
- "FROM ${lookupClassName()} c " +
- "WHERE LOWER(c.className) LIKE :name " +
- "ORDER BY c.className ",
- [name: "%${className.toLowerCase()}%"],
- [max: params.max])
+ def cs = lookupClass().createCriteria()
+ def results = cs.list(max: params.int('max')) {
+ maxResults: params.int('max')
+ ilike('className','%' + className + '%')
+ order('className','DESC')
+ projections{
+ distinct('className')
+ }
+ }
+
for (result in results) {
jsonData << [value: result]
}
@@ -92,56 +92,62 @@ class AclEntryController extends AbstractS2UiController {
setIfMissing 'max', 10, 100
setIfMissing 'offset', 0
- def hql = new StringBuilder('FROM ').append(lookupClassName()).append(' e WHERE 1=1 ')
- def queryParams = [:]
+ Integer max = params.int('max')
+ Integer offset = params.int('offset')
- for (name in ['aceOrder', 'mask']) {
- if (params[name]) {
- hql.append " AND e.${name}=:$name"
- queryParams[name] = params.int(name)
- }
- }
+ def cs = lookupClass().createCriteria()
- for (name in ['sid', 'aclObjectIdentity']) {
- if (params[name] && params[name] != 'null') {
- hql.append " AND e.${name}.id=:$name"
- queryParams[name] = params.long(name)
+ def results = cs.list(max: max, offset: offset) {
+ firstResult: offset
+ maxResults: max
+ for (name in ['aceOrder', 'mask']) {
+ if (params[name]) {
+ eq(name,params.int(name))
+ }
}
- }
-
- for (name in ['granting', 'auditSuccess', 'auditFailure']) {
- Integer value = params.int(name)
- if (value) {
- hql.append " AND e.$name=:$name"
- queryParams[name] = value == 1
+ for (name in ['sid', 'aclObjectIdentity']) {
+ if (params[name] && params[name] != 'null') {
+ eq(name,params.long(name))
+ }
+ }
+ for (name in ['granting', 'auditSuccess', 'auditFailure']) {
+ Integer value = params.int(name)
+ if (value) {
+ eq(name,value == 1)
+ }
}
- }
-
- if (params.aclClass) {
- // special case for external search
- hql.append " AND e.aclObjectIdentity.aclClass.id=:aclClass"
- queryParams.aclClass = params.aclClass.toLong()
- }
-
- int totalCount = lookupClass().executeQuery("SELECT COUNT(DISTINCT e) $hql", queryParams)[0]
-
- Integer max = params.int('max')
- Integer offset = params.int('offset')
- String orderBy = ''
- if (params.sort) {
- orderBy = " ORDER BY e.$params.sort ${params.order ?: 'ASC'}"
+ if (params.aclClass) {
+ /*
+ * special case for external search - original query was
+ * hql.append " AND e.aclObjectIdentity.aclClass.id=:aclClass"
+ *
+ * Looking up current version of the plugin ( 2.0-RC2)
+ * AclEntry has a AclObjectIdentity aclObjectIdentity
+ * AclObjectIdentity is created by extending AbstractAclObjectIdentity
+ * AbstractAclObjectIdentity contains AclClass aclClass
+ *
+ * AbstractAclObjectIdentity is located in the grails-spring-security-acl at
+ * /src/groovy/grails/plugin/springsecurity/acl/AbstractAclObjectIdentity.groovy
+ */
+
+ aclObjectIdentity{
+ aclClass{
+ eq('id',params.long('aclClass'))
+ }
+ }
+ }
+ if (params.sort) {
+ order(params.sort,params.order ?: 'ASC')
+ }
}
-
- def results = lookupClass().executeQuery(
- "SELECT DISTINCT e $hql $orderBy",
- queryParams, [max: max, offset: offset])
- def model = [results: results, totalCount: totalCount, searched: true,
- sids: lookupAclSidClass().list(), permissionFactory: aclPermissionFactory]
+
+ def model = [results: results, totalCount: results.totalCount, searched: true,
+ sids: lookupAclSidClass().list(), permissionFactory: aclPermissionFactory]
// add query params to model for paging
for (name in ['granting', 'auditSuccess', 'auditFailure', 'sid',
- 'aclObjectIdentity', 'aceOrder', 'mask']) {
- model[name] = params[name]
+ 'aclObjectIdentity', 'aceOrder', 'mask']) {
+ model[name] = params[name]
}
render view: 'search', model: model
@@ -97,50 +97,40 @@ class AclObjectIdentityController extends AbstractS2UiController {
setIfMissing 'max', 10, 100
setIfMissing 'offset', 0
- def hql = new StringBuilder('FROM ').append(lookupClassName()).append(' oid WHERE 1=1 ')
- def queryParams = [:]
+ Integer max = params.int('max')
+ Integer offset = params.int('offset')
- if (params.objectId) {
- hql.append " AND oid.objectId=:objectId"
- queryParams.objectId = params.long('objectId')
- }
+ def cs = lookupClass().createCriteria()
- for (name in ['ownerSid', 'parent', 'aclClass']) {
- if (params[name] && params[name] != 'null') {
- long id = params.long(name)
- if (name == 'ownerSid') name = 'owner'
- hql.append " AND oid.${name}.id=:$name"
- queryParams[name] = id
+ def results = cs.list(max: max, offset: offset) {
+ firstResult: offset
+ maxResults: max
+ if (params.objectId) {
+ eq('objectId',params.long('objectId'))
}
- }
-
- for (name in ['entriesInheriting']) {
- Integer value = params.int(name)
- if (value) {
- hql.append " AND oid.$name=:$name"
- queryParams[name] = value == 1
+ for (name in ['ownerSid', 'parent', 'aclClass']) {
+ if (params[name] && params[name] != 'null') {
+ long id = params.long(name)
+ if (name == 'ownerSid') name = 'owner'
+ eq(name,id)
+ }
+ }
+ for (name in ['entriesInheriting']) {
+ Integer value = params.int(name)
+ if (value) {
+ eq(name,value)
+ }
+ }
+ if (params.sort) {
+ order(params.sort,params.order ?: 'ASC')
}
}
-
- int totalCount = lookupClass().executeQuery("SELECT COUNT(DISTINCT oid) $hql", queryParams)[0]
-
- Integer max = params.int('max')
- Integer offset = params.int('offset')
-
- String orderBy = ''
- if (params.sort) {
- orderBy = " ORDER BY oid.$params.sort ${params.order ?: 'ASC'}"
- }
-
- def results = lookupClass().executeQuery(
- "SELECT DISTINCT oid $hql $orderBy",
- queryParams, [max: max, offset: offset])
- def model = [results: results, totalCount: totalCount, searched: true,
- classes: lookupAclClassClass().list(), sids: lookupAclSidClass().list()]
+ def model = [results: results, totalCount: results.totalCount, searched: true,
+ classes: lookupAclClassClass().list(), sids: lookupAclSidClass().list()]
// add query params to model for paging
for (name in ['aclClass', 'objectId', 'entriesInheriting', 'ownerSid', 'parent']) {
- model[name] = params[name]
+ model[name] = params[name]
}
render view: 'search', model: model
@@ -85,39 +85,27 @@ class AclSidController extends AbstractS2UiController {
boolean useOffset = params.containsKey('offset')
setIfMissing 'max', 10, 100
setIfMissing 'offset', 0
-
- def hql = new StringBuilder('FROM ').append(lookupClassName()).append(' s WHERE 1=1 ')
- def queryParams = [:]
-
- for (name in ['sid']) {
- if (params[name]) {
- hql.append " AND LOWER(s.$name) LIKE :$name"
- queryParams[name] = params[name].toLowerCase() + '%'
+
+ Integer max = params.int('max')
+ Integer offset = params.int('offset')
+
+ def cs = lookupClass().createCriteria()
+
+ def results = cs.list(max: max, offset: offset) {
+ firstResult: offset
+ maxResults: max
+ if(params['sid']) {
+ ilike('sid','%' + params['sid'] + '%')
}
- }
-
- for (name in ['principal']) {
- Integer value = params.int(name)
+ Integer value = params.int('principal')
if (value) {
- hql.append " AND s.$name=:$name"
- queryParams[name] = value == 1
+ eq('principal',value == 1)
+ }
+ if (params.sort) {
+ order(params.sort,params.order ?: 'ASC')
}
}
-
- int totalCount = lookupClass().executeQuery("SELECT COUNT(DISTINCT s) $hql", queryParams)[0]
-
- Integer max = params.int('max')
- Integer offset = params.int('offset')
-
- String orderBy = ''
- if (params.sort) {
- orderBy = " ORDER BY s.$params.sort ${params.order ?: 'ASC'}"
- }
-
- def results = lookupClass().executeQuery(
- "SELECT DISTINCT s $hql $orderBy",
- queryParams, [max: max, offset: offset])
- def model = [results: results, totalCount: totalCount, searched: true]
+ def model = [results: results, totalCount: results.totalCount, searched: true]
// add query params to model for paging
for (name in ['sid', 'principal']) {
@@ -138,13 +126,16 @@ class AclSidController extends AbstractS2UiController {
String sid = params.term
setIfMissing 'max', 10, 100
- def results = lookupClass().executeQuery(
- "SELECT DISTINCT s.sid " +
- "FROM ${lookupClassName()} s " +
- "WHERE LOWER(s.sid) LIKE :name " +
- "ORDER BY s.sid ",
- [name: "%${sid.toLowerCase()}%"],
- [max: params.max])
+ def cs = lookupClass().createCriteria()
+
+ def results = cs.list(max: params.int('max')) {
+ maxResults: params.int('max')
+ ilike('sid','%' + sid + '%')
+ order('sid','DESC')
+ projections{
+ distinct('sid')
+ }
+ }
for (result in results) {
jsonData << [value: result]
}
Oops, something went wrong.

0 comments on commit af3845a

Please sign in to comment.