Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixes #24 #25

Open
wants to merge 1 commit into
base: grails2
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ExportGrailsPlugin.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import org.apache.commons.logging.LogFactory

class ExportGrailsPlugin {
// the plugin version
def version = "1.7-SNAPSHOT"
def version = "1.6.1"
// the version or versions of Grails the plugin is designed for
def grailsVersion = "1.3 > *"
// the other plugins this plugin depends on
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
package de.andreasschmitt.export.exporter

import grails.util.Holders

import javax.annotation.PostConstruct

/**
* @author Andreas Schmitt
*
*/
abstract class AbstractExporter implements Exporter {

Closure defaultFormatter

@PostConstruct
void postInit() {
defaultFormatter = Holders.getFlatConfig().get("export.defaultFormatter")?: ExportConstant.formatByType
}

List exportFields = []
Map labels = [:]
Expand All @@ -27,12 +38,14 @@ abstract class AbstractExporter implements Exporter {

return field
}
protected Object formatValue(Object domain, Object object, String field){
if(formatters?.containsKey(field)){

protected Object formatValue(Object domain, Object object, String field) {
if (formatters?.containsKey(field)) {
return formatters[field].call(domain, object)
}

if (defaultFormatter) {
return defaultFormatter.call(domain, object, field)
}
return object
}

Expand Down
53 changes: 53 additions & 0 deletions src/groovy/de/andreasschmitt/export/exporter/ExportConstant.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package de.andreasschmitt.export.exporter

import groovy.transform.CompileStatic
import org.apache.commons.lang.time.FastDateFormat
import org.springframework.stereotype.Service

import java.text.DecimalFormat
import java.text.NumberFormat

/**
* @author Andreas Schmitt
* @author sgirotti
*
*/
@CompileStatic
@Service
class ExportConstant {
public static final String TIME_FORMAT_PATTERN = "HH:mm:ss"
public static final String DATE_FORMAT_PATTERN = "dd/MM/yyyy"
public static final String DATE_TIME_FORMAT_PATTERN = DATE_FORMAT_PATTERN + " " + TIME_FORMAT_PATTERN
public static final FastDateFormat df = FastDateFormat.getInstance(DATE_FORMAT_PATTERN)
public static final FastDateFormat tf = FastDateFormat.getInstance(TIME_FORMAT_PATTERN)
public static final FastDateFormat dtf = FastDateFormat.getInstance(DATE_TIME_FORMAT_PATTERN)
public static final String DECIMAL_FORMAT_PATTERN = '#,##0.00000'
public static final DecimalFormat decf = new DecimalFormat(DECIMAL_FORMAT_PATTERN)
public static final NumberFormat intf = NumberFormat.getIntegerInstance()

static {
intf.setGroupingUsed(false)
}

public static final Closure formatByType = { Object domain, Object object, String field ->
switch (object) {
case Date:
return ExportConstant.df.format(object)
break
case BigDecimal:
BigDecimal bd = (BigDecimal) object
int bdScale = bd.scale()
if(bdScale>0){
return ExportConstant.decf.format(object)
}
return ExportConstant.intf.format(object)
break
case Number:
return ExportConstant.intf.format(object)
break
default:
return object
break
}
}
}