GLS ShipIT
GLS ShipIT - REST services
 All Classes Namespaces Files Functions Variables Enumerator Pages
DataTypeAdapter.java
Go to the documentation of this file.
1 package eu.glsgroup.fpcs.soap.util;
2 
3 import eu.glsgroup.fpcs.dto.error.FPCSServiceException;
4 import eu.glsgroup.fpcs.dto.util.DateFormatter;
5 
6 import java.math.BigDecimal;
7 import java.text.ParseException;
8 import java.text.SimpleDateFormat;
9 import java.util.Date;
10 
11 public class DataTypeAdapter {
12 
13  private static final String DATETIME_FORMAT_STRING = "yyyy-MM-dd'T'HH:mm:ssXXX";
14 
15  public static BigDecimal unmarshalBigDecimal(String value) {
16 
17  return assumeValidBigDecimal(value, "");
18  }
19 
20  private static BigDecimal assumeValidBigDecimal(String value, String fieldName) {
21 
22  try {
23  return new BigDecimal(value);
24  }
25  catch (NumberFormatException e) {
26  throw new SOAPRequestValidationException("Cannot interpret " + value + " as a valid decimal value.", e);
27  }
28  }
29 
30  public static String marshalBigDecimal(BigDecimal value) {
31 
32  return value.toPlainString();
33  }
34 
35  public static Date unmarshalDate(String value) {
36 
37  if (empty(value)) {
38  return null;
39  }
40  try {
41  /*
42  * we trim the date fields to max length of 10 so we can maintain the behaviour of previous lenient date formatter,
43  * for example in "FindParcels - OK - Fields MaxLen for test TS_STBE_REST_Tracking.F-111.FPCSSTBETRP0023"
44  */
45  return DateFormatter.getInstance().parseDate(trimDate(value));
46  }
47  catch (FPCSServiceException e) {
48 
49  throw new SOAPRequestValidationException("Cannot interpret " + value + " as a valid date.", e);
50  }
51  }
52 
53  private static boolean empty(String value) {
54 
55  return value == null || value.isEmpty();
56  }
57 
58  public static String trimDate(String value) {
59 
60  String trimmed = value.trim();
61  return trimmed.length() < 11? trimmed : trimmed.substring(0, 10);
62  }
63 
64  public static String marshalDate(Date value) {
65 
66  if (value == null) {
67  return null;
68  }
69  return DateFormatter.getInstance().formatDate(value);
70  }
71 
72  public static Date unmarshalDateTime(String value) {
73 
74  if (empty(value)) {
75  return null;
76  }
77  try {
78  //TODO use DateFormatter instead
79  return new SimpleDateFormat(DATETIME_FORMAT_STRING).parse(value);
80  }
81  catch (ParseException e) {
83  "Cannot interpret " + value + " as a valid date. Please submit in format " + DATETIME_FORMAT_STRING);
84  }
85  }
86 
87  public static String marshalDateTime(Date value) {
88 
89  if (value == null) {
90  return null;
91  }
92  //TODO use DateFormatter instead
93  return new SimpleDateFormat(DATETIME_FORMAT_STRING).format(value);
94  }
95 
96 }