Skip to content

Commit

Permalink
Adding fix for github #37 #37
Browse files Browse the repository at this point in the history
  • Loading branch information
christian.oestreich committed Jul 24, 2014
1 parent 07aac16 commit e6176e0
Showing 1 changed file with 65 additions and 66 deletions.
131 changes: 65 additions & 66 deletions src/groovy/org/grails/plugin/filterpane/FilterPaneUtils.groovy
Original file line number Diff line number Diff line change
@@ -1,38 +1,33 @@
package org.grails.plugin.filterpane

import java.text.SimpleDateFormat

import org.apache.commons.logging.Log
import org.apache.commons.logging.LogFactory
import org.codehaus.groovy.grails.commons.GrailsDomainClass
import org.joda.time.DateTime
import org.joda.time.Instant
import org.joda.time.LocalDate
import org.joda.time.LocalDateTime
import org.joda.time.LocalTime
import org.joda.time.*
import org.joda.time.base.AbstractInstant
import org.joda.time.base.AbstractPartial

/**
* @author skrenek
*/
import java.text.SimpleDateFormat

class FilterPaneUtils {

private static String df = 'EEE MMM dd HH:mm:ss zzz yyyy'
private static String[] df = ["yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", 'EEE MMM dd HH:mm:ss zzz yyyy']
private static final Log log = LogFactory.getLog(this)

static Date parseDateFromDatePickerParams(paramProperty, params) {
try {
if(params[paramProperty] instanceof Date) {
return params[paramProperty]
if (params[paramProperty] instanceof Date) {
return (Date) params[paramProperty]
}

if(params[paramProperty] instanceof String) {
try {
return new SimpleDateFormat(df).parse(params[paramProperty]?.toString())
} catch(Exception ex) {
/* Do nothing. */
log.debug("Parse exception for ${params[paramProperty]}: ${ex.message}")
if (params[paramProperty] instanceof String) {
for (int i = 0; i < df.length; i++) {
try {
return new SimpleDateFormat(df[i]).parse(params[paramProperty]?.toString())
} catch (Exception ex) {
/* Do nothing. */
log.debug("Parse exception for ${paramProperty} : ${params[paramProperty]}: ${ex.message} with Format ${df[i]}")
}
}
}

Expand All @@ -48,49 +43,49 @@ class FilterPaneUtils {

String format = ''
String value = ''
if(year != null) {
if (year != null) {
format = "yyyy"
value = year
}
if(month != null) {
if (month != null) {
format += 'MM'
value += zeroPad(month)
}
if(day != null) {
if (day != null) {
format += 'dd'
value += zeroPad(day)
}
if(hour != null) {
if (hour != null) {
format += 'HH'
value += zeroPad(hour)
} else if(paramProperty.endsWith('To')) {
} else if (paramProperty.endsWith('To')) {
format += 'HH'
value += '23'
}

if(minute != null) {
if (minute != null) {
format += 'mm'
value += zeroPad(minute)
} else if(paramProperty.endsWith('To')) {
} else if (paramProperty.endsWith('To')) {
format += 'mm'
value += '59'
}

if(second != null){
if (second != null) {
format += 'ss'
value += zeroPad(second)
} else if(paramProperty.endsWith('To')) {
} else if (paramProperty.endsWith('To')) {
format += 'ss.SS'
value += '59.999'
}

if(value == '' || !paramExists) { // Don't even bother parsing. Just return null if blank.
if (value == '' || !paramExists) { // Don't even bother parsing. Just return null if blank.
return null
}

log.debug("Parsing ${value} with format ${format}")
return Date.parse(format, value)// new java.text.SimpleDateFormat(format).parse(value)
} catch(Exception ex) {
} catch (Exception ex) {
log.error("${ex.getClass().simpleName} parsing date for property ${paramProperty}: ${ex.message}")
return null
}
Expand All @@ -108,7 +103,7 @@ class FilterPaneUtils {
log.debug("Parse exception for ${params[paramProperty]}: ${ex.message}")
}

def dateTimeRepresent
def dateTimeRepresent = null

def year = params["${paramProperty}_year"]
def month = params["${paramProperty}_month"]
Expand All @@ -128,7 +123,8 @@ class FilterPaneUtils {
dateTimeRepresent = setDate(dateTimeRepresent, 'withMinuteOfHour', minute)
dateTimeRepresent = setDate(dateTimeRepresent, 'withSecondOfMinute', second)
} else if (clazz == Instant) {
dateTimeRepresent = new LocalDateTime().withMillisOfSecond(0) // current local date time - easier implementation with LocalDateTime
dateTimeRepresent = new LocalDateTime().withMillisOfSecond(0)
// current local date time - easier implementation with LocalDateTime
dateTimeRepresent = setDate(dateTimeRepresent, 'withYear', year)
dateTimeRepresent = setDate(dateTimeRepresent, 'withMonthOfYear', month)
dateTimeRepresent = setDate(dateTimeRepresent, 'withDayOfMonth', day)
Expand Down Expand Up @@ -160,27 +156,27 @@ class FilterPaneUtils {
return dateTimeRepresent
}
return null
} catch(Exception ex) {
} catch (Exception ex) {
log.error("Cannot parse date for property $paramProperty", ex)
return null
}
}

static private setDate(dateTimeRepresent, method, val){
static private setDate(dateTimeRepresent, method, val) {
def newDate = dateTimeRepresent
if(val != null){
if (val != null) {
try {
newDate = dateTimeRepresent."$method"(Integer.parseInt(val))
} catch(Exception ex){
} catch (Exception ex) {
log.error "Value for $method call appears to invalid Integer. Value is: $val", ex
}
}
newDate
}

static Date getBeginningOfDay(aDate) {
Date beginningOfDay
if(aDate && Date.isAssignableFrom(aDate.class)) {
Date beginningOfDay = null
if (aDate && Date.isAssignableFrom(aDate.class)) {
Date date = (Date) aDate
Calendar calendar = Calendar.instance.with {
time = date
Expand All @@ -196,8 +192,8 @@ class FilterPaneUtils {
}

static Date getEndOfDay(aDate) {
Date endOfDay
if(aDate && Date.isAssignableFrom(aDate.class)) {
Date endOfDay = null
if (aDate && Date.isAssignableFrom(aDate.class)) {
Date date = (Date) aDate
Calendar calendar = Calendar.instance.with {
time = date
Expand All @@ -214,11 +210,11 @@ class FilterPaneUtils {

private static zeroPad(val) {
try {
if(val != null) {
if (val != null) {
int i = val as int
return (i < 10) ? "0${i}" : val
}
} catch(Exception ex) {
} catch (Exception ex) {
log.error ex
return val
}
Expand All @@ -227,7 +223,7 @@ class FilterPaneUtils {
static extractFilterParams(params) {
def ret = [:]
params.each { entry ->
if(entry?.key?.startsWith("filter.") || entry?.key?.equals("filterProperties") || entry?.key?.equals("filterBean")) {
if (entry?.key?.startsWith("filter.") || entry?.key?.equals("filterProperties") || entry?.key?.equals("filterBean")) {
ret[entry.key] = entry.value
}
}
Expand All @@ -237,9 +233,9 @@ class FilterPaneUtils {
static extractFilterParams(params, boolean datesToStruct) {
def ret = [:]
params.each { entry ->
if(entry.key.startsWith("filter.") || entry.key.equals("filterProperties") || entry.key.equals("filterBean")) {
if (entry.key.startsWith("filter.") || entry.key.equals("filterProperties") || entry.key.equals("filterBean")) {
def val = entry.value
if(datesToStruct && val instanceof Date) {
if (datesToStruct && val instanceof Date) {
val = 'struct'
}
ret[entry.key] = val
Expand All @@ -251,7 +247,7 @@ class FilterPaneUtils {
static boolean isFilterApplied(params) {
boolean isApplied = false
params.each { key, value ->
if(key.startsWith('filter.op.') && value != null && !''.equals(value)) {
if (key.startsWith('filter.op.') && value != null && !''.equals(value)) {
isApplied = true
return
}
Expand All @@ -261,22 +257,22 @@ class FilterPaneUtils {

static resolveDomainClass(grailsApplication, bean) {
String beanName
def result
def result = null

log.debug("resolveDomainClass: bean is ${bean?.class}")
if(bean instanceof GrailsDomainClass) {
if (bean instanceof GrailsDomainClass) {
return bean
}

if(bean instanceof Class) {
if (bean instanceof Class) {
beanName = bean.name
} else if(bean instanceof String) {
} else if (bean instanceof String) {
beanName = bean
}

if(beanName) {
if (beanName) {
result = grailsApplication.getDomainClass(beanName)
if(!result) {
if (!result) {
result = grailsApplication.domainClasses.find { it.clazz.simpleName == beanName }
}
}
Expand All @@ -285,12 +281,13 @@ class FilterPaneUtils {

static resolveDomainProperty(domainClass, property) {

if("id".equals(property) || "identifier".equals(property)) {
if ("id".equals(property) || "identifier".equals(property)) {
return domainClass.identifier
}

if("class".equals(property)) {
return [name: "class", type: Class, domainClass: domainClass, naturalName: "Class"] // fake GrailsDomainClassProperty object
if ("class".equals(property)) {
return [name: "class", type: Class, domainClass: domainClass, naturalName: "Class"]
// fake GrailsDomainClassProperty object
}

def thisDomainProp = domainClass.persistentProperties.find {
Expand All @@ -317,33 +314,35 @@ class FilterPaneUtils {
static resolveSubDomainsProperties(domainClass) {
def subClassPersistentProps = []
domainClass.subClasses.each { subDomain ->
def newProps = subDomain.persistentProperties.findAll { !subClassPersistentProps.contains(it) && !domainClass.persistentProperties.contains(it) }
def newProps = subDomain.persistentProperties.findAll {
!subClassPersistentProps.contains(it) && !domainClass.persistentProperties.contains(it)
}
subClassPersistentProps.addAll(newProps)
}
return subClassPersistentProps
}

static getOperatorMapKey(opType) {
def type = 'text'
if(opType.getSimpleName().equalsIgnoreCase("boolean")) {
if (opType.getSimpleName().equalsIgnoreCase("boolean")) {
type = 'boolean'
} else if( opType == Byte || opType == byte || opType == Integer || opType == int || opType == Long || opType == long
} else if (opType == Byte || opType == byte || opType == Integer || opType == int || opType == Long || opType == long
|| opType == Double || opType == double || opType == Float || opType == float
|| opType == Short || opType == short || opType == BigDecimal || opType == BigInteger) {
type = 'numeric'
} else if(Date.isAssignableFrom(opType) || AbstractInstant.isAssignableFrom(opType) || AbstractPartial.isAssignableFrom(opType)) {
} else if (Date.isAssignableFrom(opType) || AbstractInstant.isAssignableFrom(opType) || AbstractPartial.isAssignableFrom(opType)) {
type = 'date'
} else if(opType.isEnum()) {
} else if (opType.isEnum()) {
type = 'enum'
} else if(opType.simpleName.equalsIgnoreCase("currency")) {
} else if (opType.simpleName.equalsIgnoreCase("currency")) {
type = 'currency'
} else if(opType == Class)
} else if (opType == Class)
type = 'class'
type
}

static isDateType(clazz) {
// java.util.Date, Joda Time
return Date.isAssignableFrom(clazz) || AbstractPartial.isAssignableFrom(clazz) || AbstractInstant.isAssignableFrom(clazz)
}
static isDateType(clazz) {
// java.util.Date, Joda Time
return Date.isAssignableFrom(clazz) || AbstractPartial.isAssignableFrom(clazz) || AbstractInstant.isAssignableFrom(clazz)
}
}

0 comments on commit e6176e0

Please sign in to comment.