View Javadoc
1   package com.github.mygreen.supercsv.builder.standard;
2   
3   import java.text.DateFormat;
4   import java.text.SimpleDateFormat;
5   import java.util.Calendar;
6   import java.util.Date;
7   import java.util.Locale;
8   import java.util.Map;
9   import java.util.Optional;
10  import java.util.TimeZone;
11  
12  import com.github.mygreen.supercsv.annotation.constraint.CsvDateTimeMax;
13  import com.github.mygreen.supercsv.annotation.constraint.CsvDateTimeMin;
14  import com.github.mygreen.supercsv.annotation.constraint.CsvDateTimeRange;
15  import com.github.mygreen.supercsv.annotation.format.CsvDateTimeFormat;
16  import com.github.mygreen.supercsv.builder.AbstractProcessorBuilder;
17  import com.github.mygreen.supercsv.builder.Configuration;
18  import com.github.mygreen.supercsv.builder.FieldAccessor;
19  import com.github.mygreen.supercsv.cellprocessor.constraint.DateTimeMaxFactory;
20  import com.github.mygreen.supercsv.cellprocessor.constraint.DateTimeMinFactory;
21  import com.github.mygreen.supercsv.cellprocessor.constraint.DateTimeRangeFactory;
22  import com.github.mygreen.supercsv.cellprocessor.format.AbstractTextFormatter;
23  import com.github.mygreen.supercsv.cellprocessor.format.DateFormatWrapper;
24  import com.github.mygreen.supercsv.cellprocessor.format.SimpleDateFormatBuilder;
25  import com.github.mygreen.supercsv.cellprocessor.format.TextFormatter;
26  import com.github.mygreen.supercsv.util.Utils;
27  
28  /**
29   * {@link Calendar}クラスに対するビルダ。
30   *
31   * @since 2.0
32   * @author T.TSUCHIE
33   *
34   */
35  public class CalendarProcessorBuilder extends AbstractProcessorBuilder<Calendar> {
36      
37      @Override
38      protected void init() {
39          super.init();
40          
41          // 制約のアノテーションの追加
42          registerForConstraint(CsvDateTimeRange.class, new DateTimeRangeFactory<>());
43          registerForConstraint(CsvDateTimeMin.class, new DateTimeMinFactory<>());
44          registerForConstraint(CsvDateTimeMax.class, new DateTimeMaxFactory<>());
45          
46      }
47      
48      @Override
49      protected TextFormatter<Calendar> getDefaultFormatter(final FieldAccessor field, final Configuration config) {
50          
51          final TextFormatter<Date> dateFormatter = getDateFormatter(field, config);
52          
53          return new AbstractTextFormatter<Calendar>() {
54              
55              @Override
56              public String print(final Calendar object) {
57                  
58                  return dateFormatter.print(object.getTime());
59              }
60              
61              @Override
62              public Calendar parse(final String text) {
63                  
64                  final Date date = dateFormatter.parse(text);
65                  final Calendar cal = createDefaultCalendar(field);
66                  cal.setTime(date);
67                  
68                  return cal;
69              }
70              
71              @Override
72              public Optional<String> getPattern() {
73                  return dateFormatter.getPattern();
74              }
75              
76              @Override
77              public void setValidationMessage(String validationMessage) {
78                  dateFormatter.setValidationMessage(validationMessage);
79              }
80              
81              @Override
82              public Map<String, Object> getMessageVariables() {
83                  return dateFormatter.getMessageVariables();
84              }
85              
86              @Override
87              public Optional<String> getValidationMessage() {
88                  return dateFormatter.getValidationMessage();
89              }
90          };
91          
92      }
93      
94      private DateFormatWrapper<Date> getDateFormatter(final FieldAccessor field, final Configuration config) {
95          
96          final String defaultPattern = "yyyy-MM-dd HH:mm:ss";
97          
98          final Optional<CsvDateTimeFormat> formatAnno = field.getAnnotation(CsvDateTimeFormat.class);
99          if(!formatAnno.isPresent()) {
100             return new DateFormatWrapper<>(new SimpleDateFormat(defaultPattern), Date.class);
101         }
102         
103         String pattern = formatAnno.get().pattern();
104         if(pattern.isEmpty()) {
105             pattern = defaultPattern;
106         }
107         
108         final boolean lenient = formatAnno.get().lenient();
109         final Locale locale = Utils.getLocale(formatAnno.get().locale());
110         final TimeZone timeZone = formatAnno.get().timezone().isEmpty() ? TimeZone.getDefault()
111                 : TimeZone.getTimeZone(formatAnno.get().timezone());
112         
113         final DateFormat formatter = SimpleDateFormatBuilder.create(pattern)
114                 .lenient(lenient)
115                 .locale(locale)
116                 .timeZone(timeZone)
117                 .build();
118         
119         final DateFormatWrapper<Date> wrapper = new DateFormatWrapper<>(formatter, Date.class); 
120         wrapper.setValidationMessage(formatAnno.get().message());
121         
122         return wrapper;
123         
124     }
125     
126     private Calendar createDefaultCalendar(final FieldAccessor field) {
127         
128         final Optional<CsvDateTimeFormat> formatAnno = field.getAnnotation(CsvDateTimeFormat.class);
129         if(!formatAnno.isPresent()) {
130             return Calendar.getInstance();
131         }
132         
133         final Locale locale = Utils.getLocale(formatAnno.get().locale());
134         final TimeZone timeZone = formatAnno.get().timezone().isEmpty() ? TimeZone.getDefault()
135                 : TimeZone.getTimeZone(formatAnno.get().timezone());
136         
137         return Calendar.getInstance(timeZone, locale);
138     }
139     
140 }