Skip to content

Instantly share code, notes, and snippets.

@jneira jneira/DarkMagic.groovy
Last active Mar 22, 2016

Embed
What would you like to do?
abstract class Entidad {
Long idFiltro
static transients = ["idFiltro"]
/** Otros metodos */
def filter () {
filter([:])
}
def filter(params) {
filter (params,getDefaultFilter(params))
}
def filter(params,filter) {
withCriteria (params,filter)
}
def getDefaultFilter(params) {
return {}
}
def belongsTo(property) {
def byId=this[property]?.idFiltro?
{eq("${property}.id",this[property].idFiltro)}:{}
return byId >>> some(property)
}
def some(property) {
if (this[property]) {
def lista=this[property]?.filter()
return lista?
{'in' (property,lista)}:
{eq("id",0L)}
}
}
def like(property) {
if (this[property])
return {ilike (property,"%${this[property]}%")}
}
def between(property,params) {
if (params?.desde && params?.hasta)
return { between(property,desde,hasta)}
}
}
class CriteriaBuilder {
def eq(property,value) {println "${property} = ${value}"}
def 'in'(property,list) {println "${property} in ${list}"}
}
def criterio1= {eq("prop1",1)}
def criterio2= {'in'("prop2",[1,2,3])}
def composition=criterio1 >> criterio2
def c=new CriteriaBuilder()
c.with criterio1
// prop1 = 1
c.with criterio2
// prop2 in [1, 2, 3]
c.with composition
// groovy.lang.MissingMethodException: No signature of method: Script1.eq() is applicable
def c = Account.createCriteria()
def results = c {
between("balance", 500, 1000)
eq("branch", "London")
or {
like("holderFirstName", "Fred%")
like("holderFirstName", "Barney%")
}
maxResults(10)
order("holderLastName", "desc")
}
class DarkMagic {
static abracadabra() {
println "λ dark magic"
// monkey patching basic class + operator overload + closure internals
Closure.metaClass.rightShiftUnsigned= {other->
def self=delegate
return {ps->
self.delegate=delegate
other?.delegate=delegate
def tmp=self(ps)
other?other(tmp):tmp
}
}
/** fireworks: monkey patching null object to let
* ({it*2}>>>null)(2)
* (null>>>{it*2}) (2)
* ({it*2}>>>null>>>{it/3}>>>null>>>{it+1})(2) */
NullObject.metaClass.rightShiftUnsigned= {other ->
if (other instanceof Closure)
other
}
}
}
class Material extends Entidad{
Producto producto
Proveedor proveedor
String marcaComercial
String referenciaComercial
Date fechaAlta
def getDefaultFilter(params) {
super.getDefaultFilter(params) >>>
like("referenciaComercial")>>>
like("marcaComercial") >>>
belongsTo("proveedor") >>>
belongsTo("producto") >>>
between("fechaAlta",params)
}
}
def withProjections(params,filter) {
filter >>> withPagination(params) >>> withSort(params) >>>
withSelectedProperties(params)
}
def withPagination(params) {
if (params.offset || params.max) {
def offset=params.offset as Integer
def max=params.max as Integer
return {
if (offset)
firstResult offset
if (max)
maxResults max
}
}
}
def withSort(params) {
if (params?.sort) {
def props=(([]+params.sort)-null)?:['id']
def orders=(([]+params.order)-null)?:['asc']
return {
props.eachWithIndex {e,i->
if (entidad.hasProperty(e))
order (e,orders[i]?:'asc')}
}
}
}
def withSelectedProperties(params) {
if (params?.select) {
def props=([]+params.select).findAll {
entidad.hasProperty(it.split("\\.")?.getAt(0))}
return {
projections {
for (prop in props) {
property(prop)}}}}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.