Skip to content
Closed
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,10 @@ private FormatFactory() {
* @throws IllegalArgumentException if not suitable formatter is found
*/
@SuppressWarnings("unchecked")
private static Format<?> doGetFormat(Class<?> clazz, String pattern, String locale, int precision, boolean impliedDecimalSeparator) throws Exception {
private static Format<?> doGetFormat(Class<?> clazz, String pattern, String locale,
String timezone, int precision, boolean impliedDecimalSeparator)
throws Exception
{
if (clazz == byte.class || clazz == Byte.class) {
return ObjectHelper.isNotEmpty(pattern)
? new BytePatternFormat(pattern, getLocale(locale))
Expand Down Expand Up @@ -97,7 +100,7 @@ private static Format<?> doGetFormat(Class<?> clazz, String pattern, String loca
} else if (clazz == String.class) {
return new StringFormat();
} else if (clazz == Date.class) {
return new DatePatternFormat(pattern, getLocale(locale));
return new DatePatternFormat(pattern, timezone, getLocale(locale));
} else if (clazz == char.class || clazz == Character.class) {
return new CharacterFormat();
} else if (clazz.isEnum()) {
Expand All @@ -117,9 +120,10 @@ private static Format<?> doGetFormat(Class<?> clazz, String pattern, String loca
*/
public static Format<?> getFormat(Class<?> clazz, String locale, DataField data) throws Exception {
String pattern = data.pattern();
String timezone = data.timezone();
int precision = data.precision();

return doGetFormat(clazz, pattern, locale, precision, data.impliedDecimalSeparator());
return doGetFormat(clazz, pattern, locale, timezone, precision, data.impliedDecimalSeparator());
}

/**
Expand All @@ -132,9 +136,10 @@ public static Format<?> getFormat(Class<?> clazz, String locale, DataField data)
*/
public static Format<?> getFormat(Class<?> clazz, String locale, KeyValuePairField data) throws Exception {
String pattern = data.pattern();
String timezone = data.timezone();
int precision = data.precision();

return doGetFormat(clazz, pattern, locale, precision, data.impliedDecimalSeparator());
return doGetFormat(clazz, pattern, locale, timezone, precision, data.impliedDecimalSeparator());
}

private static Locale getLocale(String locale) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@
*/
String pattern() default "";

/**
* @return String timezone ID
*/
String timezone() default "";

/**
* Length of the data block if the record is set to a fixed length
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@
*/
String pattern() default "";

/**
* @return String timezone ID
*/
String timezone() default "";

/**
* Position of the field in the message generated
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;

import org.apache.camel.dataformat.bindy.PatternFormat;
import org.apache.camel.util.ObjectHelper;
Expand All @@ -28,6 +29,7 @@ public class DatePatternFormat implements PatternFormat<Date> {

private String pattern;
private Locale locale;
private TimeZone timezone;

public DatePatternFormat() {
}
Expand All @@ -37,6 +39,14 @@ public DatePatternFormat(String pattern, Locale locale) {
this.locale = locale;
}

public DatePatternFormat(String pattern, String timezone, Locale locale) {
this.pattern = pattern;
this.locale = locale;
if (!timezone.isEmpty()) {
this.timezone = TimeZone.getTimeZone(timezone);
}
}

public String format(Date object) throws Exception {
ObjectHelper.notNull(this.pattern, "pattern");
return this.getDateFormat().format(object);
Expand Down Expand Up @@ -69,11 +79,16 @@ public Date parse(String string) throws Exception {
}

protected java.text.DateFormat getDateFormat() {
SimpleDateFormat result;
if (locale != null) {
return new SimpleDateFormat(pattern, locale);
result = new SimpleDateFormat(pattern, locale);
} else {
return new SimpleDateFormat(pattern);
result = new SimpleDateFormat(pattern);
}
if (timezone != null) {
result.setTimeZone(timezone);
}
return result;
}

public String getPattern() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.math.BigDecimal;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.TimeZone;

import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.dataformat.bindy.model.simple.oneclass.Order;
Expand All @@ -34,7 +35,7 @@ public class BindyCsvClassTypeAsStringTest extends CamelTestSupport {

@Test
public void testMarshallMessage() throws Exception {
String expected = "1,B2,Keira,Knightley,ISIN,XX23456789,BUY,Share,400.25,EUR,14-01-2009\r\n";
String expected = "1,B2,Keira,Knightley,ISIN,XX23456789,BUY,Share,400.25,EUR,14-01-2009,11-02-2010 23:21:59\r\n";

getMockEndpoint("mock:in").expectedBodiesReceived(expected);

Expand All @@ -45,7 +46,7 @@ public void testMarshallMessage() throws Exception {

@Test
public void testUnmarshallMessage() throws Exception {
String data = "1,B2,Keira,Knightley,ISIN,XX23456789,BUY,Share,400.25,EUR,14-01-2009\r\n";
String data = "1,B2,Keira,Knightley,ISIN,XX23456789,BUY,Share,400.25,EUR,14-01-2009,03-02-2010 23:21:59\r\n";

getMockEndpoint("mock:out").expectedMessageCount(1);
getMockEndpoint("mock:out").message(0).body().isInstanceOf(Order.class);
Expand All @@ -65,6 +66,13 @@ public void testUnmarshallMessage() throws Exception {
assertEquals("XX23456789", order.getInstrumentNumber());
assertEquals("Share", order.getInstrumentType());
assertEquals("EUR", order.getCurrency());

Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
// 4 hour shift
// 03-02-2010 23:21:59 by GMT+4
calendar.set(2010, 1, 3, 19, 21, 59);
calendar.set(Calendar.MILLISECOND, 0);
assertEquals(calendar.getTime(), order.getOrderDateTime());
}

public Order generateOrder() {
Expand All @@ -84,6 +92,13 @@ public Order generateOrder() {
calendar.set(2009, 0, 14);
order.setOrderDate(calendar.getTime());

calendar = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
// 4 hour shift
// 11-02-2010 23:21:59 by GMT+4
calendar.set(2010, 1, 11, 19, 21, 59);
calendar.set(Calendar.MILLISECOND, 0);
order.setOrderDateTime(calendar.getTime());

return order;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.math.BigDecimal;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.TimeZone;

import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.dataformat.bindy.model.simple.oneclass.Order;
Expand All @@ -34,7 +35,7 @@ public class BindyCsvClassTypeTest extends CamelTestSupport {

@Test
public void testMarshallMessage() throws Exception {
String expected = "1,B2,Keira,Knightley,ISIN,XX23456789,BUY,Share,400.25,EUR,14-01-2009\r\n";
String expected = "1,B2,Keira,Knightley,ISIN,XX23456789,BUY,Share,400.25,EUR,14-01-2009,17-02-2010 23:21:59\r\n";

getMockEndpoint("mock:in").expectedBodiesReceived(expected);

Expand All @@ -48,7 +49,7 @@ public void testUnmarshallMessage() throws Exception {
getMockEndpoint("mock:out").expectedMessageCount(1);
getMockEndpoint("mock:out").message(0).body().isInstanceOf(Order.class);

String data = "1,B2,Keira,Knightley,ISIN,XX23456789,BUY,Share,400.25,EUR,14-01-2009\r\n";
String data = "1,B2,Keira,Knightley,ISIN,XX23456789,BUY,Share,400.25,EUR,14-01-2009,16-02-2010 23:21:59\r\n";
template.sendBody("direct:out", data);

assertMockEndpointsSatisfied();
Expand All @@ -64,6 +65,13 @@ public void testUnmarshallMessage() throws Exception {
assertEquals("XX23456789", order.getInstrumentNumber());
assertEquals("Share", order.getInstrumentType());
assertEquals("EUR", order.getCurrency());

Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
// 4 hour shift
// 16-02-2010 23:21:59 by GMT+4
calendar.set(2010, 1, 16, 19, 21, 59);
calendar.set(Calendar.MILLISECOND, 0);
assertEquals(calendar.getTime(), order.getOrderDateTime());
}

public Order generateOrder() {
Expand All @@ -83,6 +91,12 @@ public Order generateOrder() {
calendar.set(2009, 0, 14);
order.setOrderDate(calendar.getTime());

calendar = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
// 4 hour shift
// 17-02-2010 23:21:59 by GMT+4
calendar.set(2010, 1, 17, 19, 21, 59);
order.setOrderDateTime(calendar.getTime());

return order;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.math.BigDecimal;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.TimeZone;

import org.apache.camel.EndpointInject;
import org.apache.camel.LoggingLevel;
Expand Down Expand Up @@ -52,7 +53,7 @@ public class BindyPojoSimpleCsvMarshallTest extends AbstractJUnit4SpringContextT
@DirtiesContext
public void testMarshallMessage() throws Exception {

expected = "1,B2,Keira,Knightley,ISIN,XX23456789,BUY,Share,400.25,EUR,14-01-2009\r\n";
expected = "1,B2,Keira,Knightley,ISIN,XX23456789,BUY,Share,400.25,EUR,14-01-2009,17-02-2010 23:27:59\r\n";

result.expectedBodiesReceived(expected);

Expand All @@ -79,6 +80,13 @@ public Object generateModel() {
calendar.set(2009, 0, 14);
order.setOrderDate(calendar.getTime());

calendar = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
// 4 hour shift
// 16-02-2010 23:21:59 by GMT+4
calendar.set(2010, 1, 17, 19, 27, 59);
calendar.set(Calendar.MILLISECOND, 0);
order.setOrderDateTime(calendar.getTime());

return order;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,7 @@
package org.apache.camel.dataformat.bindy.csv;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;

import org.apache.camel.EndpointInject;
import org.apache.camel.Produce;
Expand All @@ -40,7 +35,7 @@
public class BindySimpleCsvMarshallDslTest extends AbstractJUnit4SpringContextTests {

private List<Map<String, Object>> models = new ArrayList<Map<String, Object>>();
private String result = "1,B2,Keira,Knightley,ISIN,XX23456789,BUY,Share,450.45,EUR,14-01-2009\r\n";
private String result = "1,B2,Keira,Knightley,ISIN,XX23456789,BUY,Share,450.45,EUR,14-01-2009,17-05-2010 23:21:59\r\n";

@Produce(uri = "direct:start")
private ProducerTemplate template;
Expand Down Expand Up @@ -76,6 +71,12 @@ public List<Map<String, Object>> generateModel() {
calendar.set(2009, 0, 14);
order.setOrderDate(calendar.getTime());

calendar = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
// 4 hour shift
// 17-05-2010 23:21:59 by GMT+4
calendar.set(2010, 4, 17, 19, 21, 59);
order.setOrderDateTime(calendar.getTime());

modelObjects.put(order.getClass().getName(), order);

models.add(modelObjects);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,7 @@
package org.apache.camel.dataformat.bindy.csv;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;

import org.apache.camel.EndpointInject;
import org.apache.camel.LoggingLevel;
Expand Down Expand Up @@ -57,7 +52,7 @@ public class BindySimpleCsvMarshallTest extends AbstractJUnit4SpringContextTests
@DirtiesContext
public void testMarshallMessage() throws Exception {

expected = "1,B2,Keira,Knightley,ISIN,XX23456789,BUY,Share,400.25,EUR,14-01-2009\r\n";
expected = "1,B2,Keira,Knightley,ISIN,XX23456789,BUY,Share,400.25,EUR,14-01-2009,17-02-2011 23:21:59\r\n";

result.expectedBodiesReceived(expected);

Expand Down Expand Up @@ -85,6 +80,12 @@ public List<Map<String, Object>> generateModel() {
calendar.set(2009, 0, 14);
order.setOrderDate(calendar.getTime());

calendar = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
// 4 hour shift
// 17-02-2011 23:21:59 by GMT+4
calendar.set(2011, 1, 17, 19, 21, 59);
order.setOrderDateTime(calendar.getTime());

modelObjects.put(order.getClass().getName(), order);

models.add(modelObjects);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
public class BindySimpleCsvNullMarshallTest extends AbstractJUnit4SpringContextTests {

private List<Map<String, Object>> models = new ArrayList<Map<String, Object>>();
private String result = "1,B2,Keira,Knightley,ISIN,XX23456789,BUY,,450.45,EUR,14-01-2009\r\n";
private String result = "1,B2,Keira,Knightley,ISIN,XX23456789,BUY,,450.45,EUR,14-01-2009,\r\n";

@Produce(uri = "direct:start")
private ProducerTemplate template;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,11 @@ public void init() throws Exception {
@Test
public void testOneGroupMessage() throws Exception {

String message = "8=FIX 4.19=2034=135=049=INVMGR56=BRKR" + "1=BE.CHM.00111=CHM0001-0158=this is a camel - bindy test" + "22=448=BE000124567854=1" + "10=220";
String message = "8=FIX 4.19=2034=135=049=INVMGR56=BRKR"
+ "1=BE.CHM.00111=CHM0001-0158=this is a camel - bindy test"
+ "22=448=BE000124567854=1"
+ "10=220"
+ "777=22-06-2013 12:21:11";

List<String> data = Arrays.asList(message.split("\\u0001"));

Expand All @@ -84,8 +88,10 @@ public void testOneGroupMessage() throws Exception {
@Test
public void testSeveralGroupMessage() throws Exception {

String message = "8=FIX 4.19=2034=135=049=INVMGR56=BRKR" + "1=BE.CHM.00111=CHM0001-0158=this is a camel - bindy test" + "22=448=BE000124567854=1"
+ "22=548=BE000987654354=2" + "22=648=BE000999999954=3" + "10=220";
String message = "8=FIX 4.19=2034=135=049=INVMGR56=BRKR"
+ "1=BE.CHM.00111=CHM0001-0158=this is a camel - bindy test" + "22=448=BE000124567854=1"
+ "22=548=BE000987654354=2" + "22=648=BE000999999954=3" + "10=220"
+ "777=22-06-2013 12:21:11";

List<String> data = Arrays.asList(message.split("\\u0001"));

Expand All @@ -100,7 +106,10 @@ public void testSeveralGroupMessage() throws Exception {
@Test
public void testNoGroupMessage() throws Exception {

String message = "8=FIX 4.19=2034=135=049=INVMGR56=BRKR" + "1=BE.CHM.00111=CHM0001-0158=this is a camel - bindy test" + "10=220";
String message = "8=FIX 4.19=2034=135=049=INVMGR56=BRKR"
+ "1=BE.CHM.00111=CHM0001-0158=this is a camel - bindy test"
+ "10=220"
+ "777=22-06-2013 12:21:11";

List<String> data = Arrays.asList(message.split("\\u0001"));

Expand Down
Loading