Skip to content

Commit

Permalink
Merged pull request #3 from m33integrated/master.
Browse files Browse the repository at this point in the history
Allow ordering to use associations not already referenced in the restrictions.
  • Loading branch information
jcoleman committed Apr 29, 2011
2 parents e81785e + f24c847 commit 53c2e33
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 35 deletions.
40 changes: 38 additions & 2 deletions src/groovy/com/radiadesign/relationalscope/RelationalScope.groovy
Expand Up @@ -309,7 +309,7 @@ class RelationalScope {
}

orderBy.each { orderProperty ->
criteria.addOrder( new Order(orderProperty.property,
criteria.addOrder( new Order(RelationalScope.propertyFor(options, orderProperty.property),
orderProperty.direction == 'asc') )
}
}
Expand Down Expand Up @@ -346,7 +346,43 @@ class RelationalScope {
aliasMap[associationPath] = alias
}
}


static propertyFor(options, propertyKey) {
def alias
def associationPath

// Determine if an association...
def lastDotIndex = propertyKey.lastIndexOf('.')
if (lastDotIndex != -1) {
if (lastDotIndex == 0 || lastDotIndex == propertyKey.size() - 1) {
throw new RuntimeException("Selected property string cannot start or end with a dot, was: '${propertyKey}'")
}

associationPath = propertyKey[0..lastDotIndex-1]
propertyKey = propertyKey[lastDotIndex+1..-1]
}

if (associationPath) {
def associations = associationPath.tokenize('.')
def path = associations[0]
RelationalScope.createAssociationAliasIfNecessary(options, path)

if (associations.size() > 1) {
associations[1..-1].each { association ->
path += ".${association}"
RelationalScope.createAssociationAliasIfNecessary(options, path)
}
}

def discriminator = RelationalScope.aliasDiscriminatorFor(options)
def discriminatedAliases = options.associationAliases[discriminator]
assert discriminatedAliases : "An association was used for which no alias has been created"
alias = discriminatedAliases[associationPath]
assert alias : "An association was used for which no alias has been created"
}
return "${alias ?: options.currentRootAlias}.${propertyKey}"
}

Criterion toCriterion(options) {
def currentAssociationPath = fullAssociationPath(options.associationPath)

Expand Down
Expand Up @@ -12,39 +12,7 @@ class AbstractSelection {
}

static propertyFor(options, propertyKey) {
def alias
def associationPath

// Determine if an association...
def lastDotIndex = propertyKey.lastIndexOf('.')
if (lastDotIndex != -1) {
if (lastDotIndex == 0 || lastDotIndex == propertyKey.size() - 1) {
throw new RuntimeException("Selected property string cannot start or end with a dot, was: '${propertyKey}'")
}

associationPath = propertyKey[0..lastDotIndex-1]
propertyKey = propertyKey[lastDotIndex+1..-1]
}

if (associationPath) {
def associations = associationPath.tokenize('.')
def path = associations[0]
RelationalScope.createAssociationAliasIfNecessary(options, path)

if (associations.size() > 1) {
associations[1..-1].each { association ->
path += ".${association}"
RelationalScope.createAssociationAliasIfNecessary(options, path)
}
}

def discriminator = RelationalScope.aliasDiscriminatorFor(options)
def discriminatedAliases = options.associationAliases[discriminator]
assert discriminatedAliases : "An association was used for which no alias has been created"
alias = discriminatedAliases[associationPath]
assert alias : "An association was used for which no alias has been created"
}
return "${alias ?: options.currentRootAlias}.${propertyKey}"
RelationalScope.propertyFor(options, propertyKey)
}

Projection toProjection(options) {
Expand Down

0 comments on commit 53c2e33

Please sign in to comment.