View Javadoc
1   /*
2    * Licensed under the Apache License, Version 2.0 (the "License");
3    * you may not use this file except in compliance with the License.
4    * You may obtain a copy of the License at
5    *
6    * http://www.apache.org/licenses/LICENSE-2.0
7    *
8    * Unless required by applicable law or agreed to in writing, software
9    * distributed under the License is distributed on an "AS IS" BASIS,
10   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11   * See the License for the specific language governing permissions and
12   * limitations under the License.
13   */
14  
15  package org.basepom.mojo.propertyhelper;
16  
17  import static com.google.common.base.Preconditions.checkNotNull;
18  
19  import org.basepom.mojo.propertyhelper.beans.DateDefinition;
20  
21  import java.io.IOException;
22  import java.util.List;
23  import java.util.Optional;
24  
25  import com.google.common.collect.ImmutableList;
26  import com.google.common.collect.ImmutableList.Builder;
27  import org.joda.time.DateTime;
28  import org.joda.time.DateTimeZone;
29  import org.joda.time.format.DateTimeFormat;
30  import org.joda.time.format.DateTimeFormatter;
31  
32  public class DateField
33          implements PropertyElement {
34  
35      private final DateDefinition dateDefinition;
36      private final ValueProvider valueProvider;
37  
38      public DateField(final DateDefinition dateDefinition, final ValueProvider valueProvider) {
39          this.dateDefinition = dateDefinition;
40          this.valueProvider = valueProvider;
41      }
42  
43      public static List<DateField> createDates(final ValueCache valueCache, final DateDefinition... dateDefinitions)
44              throws IOException {
45          checkNotNull(valueCache, "valueCache is null");
46  
47          final Builder<DateField> result = ImmutableList.builder();
48  
49          for (DateDefinition dateDefinition : dateDefinitions) {
50              dateDefinition.check();
51              final ValueProvider dateValue = valueCache.getValueProvider(dateDefinition);
52              final DateField dateField = new DateField(dateDefinition, dateValue);
53              result.add(dateField);
54          }
55          return result.build();
56      }
57  
58      @Override
59      public String getPropertyName() {
60          return dateDefinition.getId();
61      }
62  
63      @Override
64      public Optional<String> getPropertyValue() {
65          final DateTimeZone timeZone = dateDefinition.getTimezone().isPresent()
66                  ? DateTimeZone.forID(dateDefinition.getTimezone().get())
67                  : DateTimeZone.getDefault();
68  
69          final Optional<String> format = dateDefinition.getFormat();
70          final DateTimeFormatter formatter;
71          formatter = format.map(DateTimeFormat::forPattern).orElse(null);
72  
73          DateTime date = valueProvider.getValue()
74                  .map(value -> getDateTime(value, formatter, timeZone))
75                  .orElse(null);
76  
77          if (date == null && dateDefinition.getValue().isPresent()) {
78              date = new DateTime(dateDefinition.getValue().get(), timeZone);
79          }
80  
81          if (date == null) {
82              date = new DateTime(timeZone);
83          }
84  
85          String result;
86          if (formatter != null) {
87              result = formatter.print(date);
88              valueProvider.setValue(result);
89          } else {
90              result = date.toString();
91              valueProvider.setValue(Long.toString(date.getMillis()));
92          }
93  
94          if (dateDefinition.getTransformers().isPresent()) {
95              result = TransformerRegistry.applyTransformers(dateDefinition.getTransformers().get(), result);
96          }
97          return Optional.ofNullable(result);
98      }
99  
100     private DateTime getDateTime(String value,
101             final DateTimeFormatter formatter,
102             final DateTimeZone timeZone) {
103         if (value == null) {
104             return null;
105         }
106 
107         if (formatter != null) {
108             return formatter.parseDateTime(value).withZone(timeZone);
109         }
110 
111         try {
112             return new DateTime(Long.parseLong(value), timeZone);
113         } catch (NumberFormatException nfe) {
114             return new DateTime(value, timeZone);
115         }
116     }
117 
118     @Override
119     public boolean isExport() {
120         return dateDefinition.isExport();
121     }
122 
123     @Override
124     public String toString() {
125         return getPropertyValue().orElse("");
126     }
127 }