Skip to content

Commit

Permalink
Convert parameter values from any type into nearly any type
Browse files Browse the repository at this point in the history
  • Loading branch information
jlolling committed Nov 10, 2018
1 parent 2aee860 commit 4c1b508
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 65 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>de.jlo.talendcomp</groupId>
<artifactId>jlo-talendcomp-jasperreportexec</artifactId>
<version>4.0</version>
<version>4.1</version>
<organization>
<name>Jan Lolling</name>
<url>http://jan-lolling.de</url>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -961,12 +961,12 @@ public void setAndConvertParameterCollectionValues(
*/
public void setAndConvertParameterValue(
String parameterName,
Object valueAsString,
Object value,
String dataType,
String pattern)
throws Exception {
parameterMap.put(parameterName,
Util.convertToDatatype(String.valueOf(valueAsString), dataType, pattern));
Util.convertToDatatype(value, dataType, pattern));
}

public Object getParameterValue(String parameterName) {
Expand Down
180 changes: 122 additions & 58 deletions src/main/java/de/jlo/talendcomp/jasperreportexec/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,127 +42,191 @@ public static java.util.List<Object> convertToList(String valuesSeparated, Strin
return resultList;
}

public static Object convertToDatatype(String value, String dataType, String pattern) throws Exception {
if (value != null && value.isEmpty() == false) {
if ("String".equalsIgnoreCase(dataType)) {
public static Object convertToDatatype(Object value, String targetDataType, String pattern) throws Exception {
if (value != null) {
if ("String".equalsIgnoreCase(targetDataType)) {
return value;
} else if ("BigDecimal".equalsIgnoreCase(dataType)) {
} else if ("BigDecimal".equalsIgnoreCase(targetDataType)) {
return convertToBigDecimal(value);
} else if ("Boolean".equalsIgnoreCase(dataType)) {
} else if ("Boolean".equalsIgnoreCase(targetDataType)) {
return convertToBoolean(value);
} else if ("Date".equalsIgnoreCase(dataType)) {
} else if ("Date".equalsIgnoreCase(targetDataType)) {
return convertToDate(value, pattern);
} else if ("Double".equalsIgnoreCase(dataType)) {
} else if ("Double".equalsIgnoreCase(targetDataType)) {
return convertToDouble(value);
} else if ("Float".equalsIgnoreCase(dataType)) {
} else if ("Float".equalsIgnoreCase(targetDataType)) {
return convertToFloat(value);
} else if ("Int".equalsIgnoreCase(dataType) || "Integer".equalsIgnoreCase(dataType)) {
} else if ("Int".equalsIgnoreCase(targetDataType) || "Integer".equalsIgnoreCase(targetDataType)) {
return convertToInteger(value);
} else if ("Long".equalsIgnoreCase(dataType)) {
} else if ("Long".equalsIgnoreCase(targetDataType)) {
return convertToLong(value);
} else if ("Short".equalsIgnoreCase(dataType)) {
} else if ("Short".equalsIgnoreCase(targetDataType)) {
return convertToShort(value);
} else if ("Timestamp".equalsIgnoreCase(dataType)) {
} else if ("Timestamp".equalsIgnoreCase(targetDataType)) {
return convertToTimestamp(value, pattern);
} else {
throw new Exception("Unsupported dataType:" + dataType);
throw new Exception("Unsupported dataType:" + targetDataType);
}
} else {
return null;
}
}

public static String convertToString(Object value, String pattern) {
String s = null;
if (value instanceof String) {
return (String) value;
} else if (value instanceof Number) {
return NumberFormat.getInstance().format(value);
} else if (value instanceof Date) {
return new SimpleDateFormat(pattern).format((Date) value);
} else if (value != null) {
return String.valueOf(value);
}
return s;
}

/**
* concerts the string format into a Date
* @param dateString
* @param pattern
* @return the resulting Date
*/
public static Date convertToDate(String dateString, String pattern) throws Exception {
if (dateString == null || dateString.isEmpty()) {
public static Date convertToDate(Object value, String pattern) throws Exception {
if (value == null) {
return null;
}
if (pattern == null || pattern.isEmpty()) {
throw new Exception("convertToDate failed: pattern cannot be null or empty");
}
try {
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
return sdf.parse(dateString);
} catch (Throwable t) {
throw new Exception("Failed to convert string to date:" + t.getMessage(), t);
} else if (value instanceof String) {
if (pattern == null || pattern.isEmpty()) {
throw new Exception("convertToDate failed: pattern cannot be null or empty");
}
try {
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
return sdf.parse((String) value);
} catch (Throwable t) {
throw new Exception("Failed to convert string to date:" + t.getMessage(), t);
}
} else if (value instanceof Date) {
return (Date) value;
} else {
throw new Exception("Type: " + value.getClass().getName() + " cannot be converted to Date");
}
}

public static Timestamp convertToTimestamp(String dateString, String pattern) throws Exception {
Date date = convertToDate(dateString, pattern);
public static Timestamp convertToTimestamp(Object value, String pattern) throws Exception {
Date date = convertToDate(value, pattern);
if (date != null) {
return new Timestamp(date.getTime());
} else {
return null;
}
}

public static Boolean convertToBoolean(String value) throws Exception {
if (value == null || value.isEmpty()) {
public static Boolean convertToBoolean(Object value) throws Exception {
if (value == null) {
return null;
} else {
if ("true".equals(value)) {
return true;
} else if ("false".equals(value)) {
return false;
if (value instanceof Boolean) {
return (Boolean) value;
} else if (value instanceof String) {
if ("true".equals(value)) {
return true;
} else if ("false".equals(value)) {
return false;
} else {
throw new Exception("Value:" + value + " is not a boolean value!");
}
} else {
throw new Exception("Value:" + value + " is not a boolean value!");
}
}
}

public static Double convertToDouble(String value) throws Exception {
if (value == null || value.isEmpty()) {
public static Double convertToDouble(Object value) throws Exception {
if (value == null) {
return null;
} else if (value instanceof Number) {
return ((Number) value).doubleValue();
} else if (value instanceof String) {
NumberFormat nf = NumberFormat.getInstance(Locale.ENGLISH);
return nf.parse((String) value).doubleValue();
} else {
throw new Exception("Type: " + value.getClass().getName() + " cannot be converted to Double");
}
NumberFormat nf = NumberFormat.getInstance(Locale.ENGLISH);
return nf.parse(value).doubleValue();
}

public static Integer convertToInteger(String value) throws Exception {
if (value == null || value.isEmpty()) {
public static Float convertToFloat(Object value) throws Exception {
if (value == null) {
return null;
} else if (value instanceof Number) {
return ((Number) value).floatValue();
} else if (value instanceof String) {
NumberFormat nf = NumberFormat.getInstance(Locale.ENGLISH);
return nf.parse((String) value).floatValue();
} else {
throw new Exception("Type: " + value.getClass().getName() + " cannot be converted to Float");
}
NumberFormat nf = NumberFormat.getInstance(Locale.ENGLISH);
return nf.parse(value).intValue();
}

public static Float convertToFloat(String value) throws Exception {
if (value == null || value.isEmpty()) {
public static Integer convertToInteger(Object value) throws Exception {
if (value == null) {
return null;
} else if (value instanceof Number) {
return ((Number) value).intValue();
} else if (value instanceof String) {
NumberFormat nf = NumberFormat.getInstance(Locale.ENGLISH);
return nf.parse((String) value).intValue();
} else {
throw new Exception("Type: " + value.getClass().getName() + " cannot be converted to Integer");
}
NumberFormat nf = NumberFormat.getInstance(Locale.ENGLISH);
return nf.parse(value).floatValue();
}

public static Short convertToShort(String value) throws Exception {
if (value == null || value.isEmpty()) {
public static Short convertToShort(Object value) throws Exception {
if (value == null) {
return null;
} else if (value instanceof Number) {
return ((Number) value).shortValue();
} else if (value instanceof String) {
NumberFormat nf = NumberFormat.getInstance(Locale.ENGLISH);
return nf.parse((String) value).shortValue();
} else {
throw new Exception("Type: " + value.getClass().getName() + " cannot be converted to Short");
}
NumberFormat nf = NumberFormat.getInstance(Locale.ENGLISH);
return nf.parse(value).shortValue();
}

public static Long convertToLong(String value) throws Exception {
if (value == null || value.isEmpty()) {
public static Long convertToLong(Object value) throws Exception {
if (value == null) {
return null;
} else if (value instanceof Long) {
return (Long) value;
} else if (value instanceof Number) {
return ((Number) value).longValue();
} else if (value instanceof String) {
NumberFormat nf = NumberFormat.getInstance(Locale.ENGLISH);
return nf.parse((String) value).longValue();
} else {
throw new Exception("Type: " + value.getClass().getName() + " cannot be converted to Long");
}
NumberFormat nf = NumberFormat.getInstance(Locale.ENGLISH);
return nf.parse(value).longValue();
}

public static BigDecimal convertToBigDecimal(String value) throws Exception {
if (value == null || value.isEmpty()) {
return null;
}
public static BigDecimal convertToBigDecimal(Object value) throws Exception {
try {
return new BigDecimal(value);
} catch (RuntimeException e) {
if (value == null) {
return null;
} else if (value instanceof BigDecimal) {
return (BigDecimal) value;
} else if (value instanceof Long) {
return new BigDecimal((Long) value);
} else if (value instanceof Integer) {
return new BigDecimal((Integer) value);
} else if (value instanceof String) {
if (((String) value).isEmpty()) {
return null;
}
return new BigDecimal((String) value);
} else {
throw new Exception("Type: " + value.getClass().getName() + " cannot be converted to BigDecimal");
}
} catch (Throwable e) {
throw new Exception("convertToBigDecimal:" + value + " failed:" + e.getMessage(), e);
}
}
Expand Down
Binary file not shown.
Binary file not shown.
8 changes: 4 additions & 4 deletions talend_component/tJasperReportExec/tJasperReportExec_java.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>

<COMPONENT>
<HEADER PLATEFORM="ALL" SERIAL="" VERSION="4.0" STATUS="PROD" COMPATIBILITY="ALL" AUTHOR="Jan Lolling" RELEASE_DATE="20181107" STARTABLE="true">
<HEADER PLATEFORM="ALL" SERIAL="" VERSION="4.1" STATUS="PROD" COMPATIBILITY="ALL" AUTHOR="Jan Lolling" RELEASE_DATE="20181110" STARTABLE="true">
<SIGNATURE/>
</HEADER>
<FAMILIES>
Expand Down Expand Up @@ -215,13 +215,13 @@
<PARAMETER NAME="DIE_ON_ERROR" FIELD="CHECK" NUM_ROW="130">
<DEFAULT>true</DEFAULT>
</PARAMETER>
<PARAMETER NAME="RELEASE_LABEL_20181107" FIELD="LABEL" COLOR="0;0;0" NUM_ROW="900">
<DEFAULT>Release: 4.0 build at: 20181107</DEFAULT>
<PARAMETER NAME="RELEASE_LABEL_20181110" FIELD="LABEL" COLOR="0;0;0" NUM_ROW="900">
<DEFAULT>Release: 4.1 build at: 20181110</DEFAULT>
</PARAMETER>
</ADVANCED_PARAMETERS>
<CODEGENERATION>
<IMPORTS>
<IMPORT NAME="jlo-talendcomp-jasperreportexec" MODULE="jlo-talendcomp-jasperreportexec-4.0.jar" REQUIRED="true"/>
<IMPORT NAME="jlo-talendcomp-jasperreportexec" MODULE="jlo-talendcomp-jasperreportexec-4.1.jar" REQUIRED="true"/>
<IMPORT NAME="jasperreports-fonts" MODULE="jasperreports-fonts-6.0.0.jar" REQUIRED="true"/>
<IMPORT NAME="commons-io" MODULE="commons-io-1.3.2.jar" REQUIRED="true"/>
<IMPORT NAME="commons-compress" MODULE="commons-compress-1.9.jar" REQUIRED="true"/>
Expand Down

0 comments on commit 4c1b508

Please sign in to comment.