Skip to content

Instantly share code, notes, and snippets.

@jneira
Created November 17, 2017 10:21
Show Gist options
  • Save jneira/b0254528a2a6ead8d75ad22721e0008a to your computer and use it in GitHub Desktop.
Save jneira/b0254528a2a6ead8d75ad22721e0008a to your computer and use it in GitHub Desktop.
class UIState {
def context
// From params
Boolean filtrar=false
Boolean disabled=false
Boolean withNew=true
Boolean withDelete=true
Boolean embedded=false
Boolean returnEmbedded=false
Boolean withPagination=true
ModoAcceso modoAcceso
String lang
// Derivados
Boolean filtrarOriginal=false
Boolean decorated=true
def getRequest() {
context.request
}
def getSession() {
context.session
}
def getParams() {
context.params
}
def getActionName() {
context.actionName
}
def getControllerName() {
context.controllerName
}
void setState() {
setFromBooleanParams()
setReturnEmbedded()
setPagination()
setFiltrado()
setDecoratedFromUrl()
setLang()
setModoAcceso()
setDisabled()
}
def setFromBooleanParams() {
["filtrar","filtrarOriginal","disabled","withNew","withDelete","embedded","returnEmbedded",
"withPagination"].each {
def defVal=this[it]
this[it]=toBoolean(params[it],defVal)
}
}
void setReturnEmbedded() {
returnEmbedded=returnEmbedded && !embedded
}
void setPagination() {
withPagination=getParamOr("withPagination",!embedded) && actionName != "relations"
}
void setFiltrado() {
if (esIndex) {
filtrar=params.filtrar=true
if (params["enviar"] && params["filtrarOriginal"]==null)
filtrarOriginal=true
}
}
void setLang() {
lang = params["lang"]
if (!lang)
lang=request.locale.language?:"es"
}
void setDecoratedFromUrl(req=context.request) {
def url=req.forwardURI
if (url.contains("/ND/"))
decorated=false
}
def getModoAccesoParam() {
ModoAcceso.porCodigoONombre(params.modoAcceso)
}
ModoAcceso getModoAcceso() {
if (modoAcceso==null)
setModoAcceso()
modoAcceso
}
def getModosAcceso(session=context.session) {
session["modosAcceso"]
}
void setModoAcceso(session=context.session,request=context.request) {
def modos=modosAcceso
modoAcceso=modos[controllerName]
modoAcceso=ModoAcceso.masRestrictivo([modoAcceso,modoAccesoParam])
if (!esModoAccesoModificacion) {
withNew=false
withDelete=false
}
}
void setDisabled() {
disabled=disabled || (!filtrar && esModoAccesoConsulta)
}
Boolean toBoolean(String val,Boolean defValue=false) {
if (val==null)
defValue
else
val.toBoolean()
}
Boolean getParamOr(String prop,Boolean defValue=false) {
if (params[prop]==null)
defValue
else
this[prop]
}
Boolean getIfNull(String prop,Boolean val) {
if (val==null)
this[prop]
else
val
}
def getCodigoModoAcceso() {
modoAcceso?.codigo
}
def getEsModoAccesoConsulta() {
ModoAcceso.esConsultaIfNull(modoAcceso)
}
def getEsModoAccesoModificacion() {
ModoAcceso.esModificacion(modoAcceso)
}
def getEsIndex() {
actionName=="index"
}
def getEsDetalle() {
actionName in ["show","create","update"]
}
def getIsNew() {
(actionName == 'show' && params.id == 'new') || actionName == 'create'
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment