1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.springframework.batch.core.converter;
17
18 import java.text.DateFormat;
19 import java.text.DecimalFormat;
20 import java.text.NumberFormat;
21 import java.text.ParseException;
22 import java.text.SimpleDateFormat;
23 import java.util.Date;
24 import java.util.Iterator;
25 import java.util.Locale;
26 import java.util.Map;
27 import java.util.Map.Entry;
28 import java.util.Properties;
29
30 import org.springframework.batch.core.JobParameter;
31 import org.springframework.batch.core.JobParameter.ParameterType;
32 import org.springframework.batch.core.JobParameters;
33 import org.springframework.batch.core.JobParametersBuilder;
34 import org.springframework.util.StringUtils;
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59 public class DefaultJobParametersConverter implements JobParametersConverter {
60
61 public static final String DATE_TYPE = "(date)";
62
63 public static final String STRING_TYPE = "(string)";
64
65 public static final String LONG_TYPE = "(long)";
66
67 private static final String DOUBLE_TYPE = "(double)";
68
69 private static NumberFormat DEFAULT_NUMBER_FORMAT = NumberFormat.getInstance(Locale.US);
70
71 private DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
72
73 private NumberFormat numberFormat = DEFAULT_NUMBER_FORMAT;
74
75 private final NumberFormat longNumberFormat = new DecimalFormat("#");
76
77
78
79
80
81
82
83
84
85
86 @Override
87 public JobParameters getJobParameters(Properties props) {
88
89 if (props == null || props.isEmpty()) {
90 return new JobParameters();
91 }
92
93 JobParametersBuilder propertiesBuilder = new JobParametersBuilder();
94
95 for (Iterator<Entry<Object, Object>> it = props.entrySet().iterator(); it.hasNext();) {
96 Entry<Object, Object> entry = it.next();
97 String key = (String) entry.getKey();
98 String value = (String) entry.getValue();
99 if (key.endsWith(DATE_TYPE)) {
100 Date date;
101 try {
102 date = dateFormat.parse(value);
103 }
104 catch (ParseException ex) {
105 String suffix = (dateFormat instanceof SimpleDateFormat) ? ", use "
106 + ((SimpleDateFormat) dateFormat).toPattern() : "";
107 throw new IllegalArgumentException("Date format is invalid: [" + value + "]" + suffix);
108 }
109 propertiesBuilder.addDate(StringUtils.replace(key, DATE_TYPE, ""), date);
110 }
111 else if (key.endsWith(LONG_TYPE)) {
112 Long result;
113 try {
114 result = (Long) parseNumber(value);
115 }
116 catch (ClassCastException ex) {
117 throw new IllegalArgumentException("Number format is invalid for long value: [" + value
118 + "], use a format with no decimal places");
119 }
120 propertiesBuilder.addLong(StringUtils.replace(key, LONG_TYPE, ""), result);
121 }
122 else if (key.endsWith(DOUBLE_TYPE)) {
123 Double result = parseNumber(value).doubleValue();
124 propertiesBuilder.addDouble(StringUtils.replace(key, DOUBLE_TYPE, ""), result);
125 }
126 else if (StringUtils.endsWithIgnoreCase(key, STRING_TYPE)) {
127 propertiesBuilder.addString(StringUtils.replace(key, STRING_TYPE, ""), value);
128 }
129 else {
130 propertiesBuilder.addString(key, value);
131 }
132 }
133
134 return propertiesBuilder.toJobParameters();
135 }
136
137
138
139
140 private Number parseNumber(String value) {
141 try {
142 return numberFormat.parse(value);
143 }
144 catch (ParseException ex) {
145 String suffix = (numberFormat instanceof DecimalFormat) ? ", use "
146 + ((DecimalFormat) numberFormat).toPattern() : "";
147 throw new IllegalArgumentException("Number format is invalid: [" + value + "], use " + suffix);
148 }
149 }
150
151
152
153
154
155
156
157 @Override
158 public Properties getProperties(JobParameters params) {
159
160 if (params == null || params.isEmpty()) {
161 return new Properties();
162 }
163
164 Map<String, JobParameter> parameters = params.getParameters();
165 Properties result = new Properties();
166 for (Entry<String, JobParameter> entry : parameters.entrySet()) {
167
168 String key = entry.getKey();
169 JobParameter jobParameter = entry.getValue();
170 Object value = jobParameter.getValue();
171 if (value != null) {
172 if (jobParameter.getType() == ParameterType.DATE) {
173 result.setProperty(key + DATE_TYPE, dateFormat.format(value));
174 }
175 else if (jobParameter.getType() == ParameterType.LONG) {
176 result.setProperty(key + LONG_TYPE, longNumberFormat.format(value));
177 }
178 else if (jobParameter.getType() == ParameterType.DOUBLE) {
179 result.setProperty(key + DOUBLE_TYPE, decimalFormat((Double)value));
180 }
181 else {
182 result.setProperty(key, "" + value);
183 }
184 }
185 }
186 return result;
187 }
188
189
190
191
192
193 private String decimalFormat(double value) {
194 if (numberFormat != DEFAULT_NUMBER_FORMAT) {
195 return numberFormat.format(value);
196 }
197 return Double.toString(value);
198 }
199
200
201
202
203
204
205 public void setDateFormat(DateFormat dateFormat) {
206 this.dateFormat = dateFormat;
207 }
208
209
210
211
212
213
214
215 public void setNumberFormat(NumberFormat numberFormat) {
216 this.numberFormat = numberFormat;
217 }
218 }