Skip to content

Instantly share code, notes, and snippets.

@walshe
Last active August 29, 2015 14:03
Show Gist options
  • Save walshe/f2aaff41d2f547ae8776 to your computer and use it in GitHub Desktop.
Save walshe/f2aaff41d2f547ae8776 to your computer and use it in GitHub Desktop.
SpecTests.groovy
import com.cassidian.ip.data.*
import com.cassidian.ip.data.repository.specification.*
import com.cassidian.ip.data.repository.specification.util.JoinBuilder
import groovy.util.logging.Slf4j
import org.springframework.data.jpa.domain.Specification
import javax.persistence.criteria.CriteriaBuilder
import javax.persistence.criteria.CriteriaQuery
import javax.persistence.criteria.Join
import javax.persistence.criteria.JoinType
import javax.persistence.criteria.Predicate
import javax.persistence.criteria.Root
import javax.persistence.criteria.Subquery
import org.springframework.data.domain.Page
import org.springframework.data.domain.PageRequest
import org.springframework.data.domain.Pageable
import org.springframework.data.domain.Sort
import org.springframework.data.jpa.domain.Specification
import org.springframework.data.jpa.domain.Specifications
import org.apache.log4j.Level
import org.apache.log4j.Logger
Logger.getLogger('org.hibernate.SQL').level = Level.toLevel('debug')
/**
* gets all affected orgs of an event i.e. union of all its activationScenario.affectedGeographicArea's
*/
class AffectedGeographicalAreaOfEventSpec extends JoinBuilder implements Specification {
long eventId
AffectedGeographicalAreaOfEventSpec(long eventId) {
this.eventId = eventId
}
@Override
Predicate toPredicate(Root<Organization> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
Subquery<Event> eventQuery = query.subquery(Event)
Root<Event> eventRoot = eventQuery.from(Event)
eventQuery.select(
eventRoot.join("activationScenarios")
).where(
cb.equal(eventRoot.get("id"), eventId)
)
Subquery<ActivationScenario> activationScenariosQuery = query.subquery(ActivationScenario)
Root<ActivationScenario> activationScenariosRoot = activationScenariosQuery.from(ActivationScenario)
activationScenariosQuery.select(
activationScenariosRoot.join("affectedGeographicArea")
).where(
cb.in(activationScenariosRoot, eventQuery)
)
cb.in(root, activationScenariosQuery)
}
}
class UserJurisdictionIntersectsAffectedGeographicalAreaOfEventSpec extends JoinBuilder implements Specification<Organization> {
long userOrganizationId
long eventId
UserJurisdictionIntersectsAffectedGeographicalAreaOfEventSpec(long userOrganizationId, long eventId) {
this.userOrganizationId = userOrganizationId
this.eventId = eventId
}
Predicate toPredicate(Root root, CriteriaQuery query, CriteriaBuilder cb) {
Subquery<Organization> orgQuery = query.subquery(Organization)
Root<Organization> orgRoot = orgQuery.from(Organization)
orgQuery.select(
root.get("id")
).where(new AffectedGeographicalAreaOfEventSpec(eventId).toPredicate(root, query, cb))
Subquery<Organization> q = query.subquery(Organization)
Root<Organization> r = q.from(Organization)
q.select(
r.get("id")
).where(cb.or(
new OrganizationAndDescendants(userOrganizationId).toPredicate(root, query, cb),
new SharedOrganizationsAndDescendants(userOrganizationId).toPredicate(root, query, cb)
))
return cb.and(
cb.in(root.get("id"), orgQuery),
cb.in(root.get("id"), q)
)
}
}
def time = System.currentTimeMillis()
//21621 (admin) is slow, 21624 (EST) is fast enough
def usersJurisdiction = ctx.organizationRepository.findAll(Specifications.where(new OrganizationsByUserJurisdiction(21624L))).collect{it.id}.toSet()
def geoAre = ctx.organizationRepository.findAll(Specifications.where(new AffectedGeographicalAreaOfEventSpec( 59660L))).collect{it.id}.toSet()
//ctx.organizationRepository.count(Specifications.where(new OrganizationsByUserJurisdiction(21621L))); //takes LONG
//ctx.organizationRepository.count(Specifications.where(new OrganizationsByUserJurisdiction(21624L))); //faster
println "jur: ${usersJurisdiction.size()} }"
println "geoa: ${geoAre.size()} }"
println "interset : ${usersJurisdiction.intersect(geoAre).size()}"
//ctx.organizationRepository.count(Specifications.where(new UserJurisdictionIntersectsAffectedGeographicalAreaOfEventSpec( 21621L,59660L)))
//print ctx.organizationRepository.count(Specifications.where(new UserJurisdictionIntersectsAffectedGeographicalAreaOfEventSpec( 21624L,59660L)))
time = System.currentTimeMillis() - time
println "took ${time}"
//ctx.organizationRepository.count(Specifications.where(new AffectedGeographicalAreaOfEventSpec( 59660L)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment