View Javadoc
1   package fr.ifremer.quadrige2.core.dao.technical;
2   
3   /*-
4    * #%L
5    * Quadrige2 Core :: Quadrige2 Core Shared
6    * $Id:$
7    * $HeadURL:$
8    * %%
9    * Copyright (C) 2017 Ifremer
10   * %%
11   * This program is free software: you can redistribute it and/or modify
12   * it under the terms of the GNU Affero General Public License as published by
13   * the Free Software Foundation, either version 3 of the License, or
14   * (at your option) any later version.
15   * 
16   * This program is distributed in the hope that it will be useful,
17   * but WITHOUT ANY WARRANTY; without even the implied warranty of
18   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19   * GNU General Public License for more details.
20   * 
21   * You should have received a copy of the GNU Affero General Public License
22   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23   * #L%
24   */
25  
26  
27  
28  import com.google.common.base.Preconditions;
29  import com.google.common.base.Splitter;
30  import fr.ifremer.quadrige2.core.exception.Quadrige2TechnicalException;
31  import org.apache.commons.lang3.StringUtils;
32  import org.apache.commons.logging.Log;
33  import org.apache.commons.logging.LogFactory;
34  import org.nuiton.version.Version;
35  import org.nuiton.version.Versions;
36  
37  import java.text.SimpleDateFormat;
38  import java.util.Calendar;
39  import java.util.Date;
40  import java.util.TimeZone;
41  
42  /**
43   * <p>DateVersions class.</p>
44   */
45  public class DateVersions {
46  
47      /**
48       * <p>Constructor for DateVersions.</p>
49       */
50      protected DateVersions() {
51          // Helper class
52      }
53  
54      private static final Log log = LogFactory.getLog(DateVersions.class);
55  
56      /**
57       * convert a version to a date
58       *
59       * ex: version = 2014.9.22.115930 will return 2014-09-22 11:59:30
60       *
61       * @param version a {@link org.nuiton.version.Version} object.
62       * @return a {@link java.util.Date} object.
63       */
64      public static Date convertVersion2Date(Version version) {
65          Preconditions.checkNotNull(version);
66          return convertVersion2Date(version.toString(), null);
67      }
68  
69      /**
70       * convert a version to a date
71       *
72       * ex: version = 2014.9.22.115930 will return 2014-09-22 11:59:30
73       *
74       * @param version a {@link String} object.
75       * @param timezone a {@link TimeZone} object.
76       * @return a {@link Date} object.
77       */
78      public static Date convertVersion2Date(String version, TimeZone timezone) {
79          Preconditions.checkNotNull(version);
80  
81          String[] versionParts = Splitter.on('.').splitToList(version).toArray(new String[4]);
82  
83          Calendar result = Calendar.getInstance(timezone != null ? timezone : TimeZone.getDefault());
84  
85          boolean parseError = false;
86          try {
87              // year 
88              int year = Integer.parseInt(versionParts[0]);
89              result.set(Calendar.YEAR, year);
90  
91              // month 
92              int month = Integer.parseInt(versionParts[1]);
93              result.set(Calendar.MONTH, month - 1);
94  
95              // day 
96              int day = Integer.parseInt(versionParts[2]);
97              result.set(Calendar.DAY_OF_MONTH, day);
98  
99              if (StringUtils.isBlank(versionParts[3])) {
100                 result.set(Calendar.SECOND, 0);
101                 result.set(Calendar.MINUTE, 0);
102                 result.set(Calendar.HOUR_OF_DAY, 0);
103             }
104             else {
105                 String time = versionParts[3];
106                 if (time.length() > 6) {
107                     parseError = true;
108                 }
109                 else {
110                     time = StringUtils.leftPad(time, 6, '0');
111 
112                     // seconds
113                     int seconds = Integer.parseInt(time.substring(time.length() - 2));
114                     result.set(Calendar.SECOND, seconds);
115 
116                     // minutes
117                     int minutes = Integer.parseInt(time.substring(time.length() - 4, time.length() - 2));
118                     result.set(Calendar.MINUTE, minutes);
119 
120                     // hours
121                     int hours = Integer.parseInt(time.substring(0, time.length() - 4));
122                     result.set(Calendar.HOUR_OF_DAY, hours);
123                 }
124             }
125 
126             // no milliseconds
127             result.set(Calendar.MILLISECOND, 0);
128 
129         } catch (Exception ex) {
130             parseError = true;
131         }
132         if (parseError) {
133             throw new Quadrige2TechnicalException(String.format("the database version '%s' can't be converted to a synchronization date", version));
134         }
135 
136         return result.getTime();
137     }
138 
139     /**
140      * convert a version to a date, with no parse error
141      *
142      * ex: version = 2014.9.22.115930 will return 2014-09-22 11:59:30
143      *
144      * @param version a {@link String} object.
145      * @param timezone a {@link TimeZone} object.
146      * @return the date, or null if parse error
147      */
148     public static Date safeConvertVersion2Date(String version, TimeZone timezone) {
149         if (StringUtils.isBlank(version)) {
150             return null;
151         }
152         try {
153             return convertVersion2Date(version, timezone);
154         } catch (Quadrige2TechnicalException e) {
155             log.error(e.getMessage(), e);
156         }
157         return null;
158     }
159 
160     /**
161      * convert a version to a date, with no parse error
162      *
163      * ex: version = 2014.9.22.115930 will return 2014-09-22 11:59:30
164      *
165      * @param version a {@link String} object.
166      * @return the date, or null if parse error
167      */
168     public static Date safeConvertVersion2Date(String version) {
169         return convertVersion2Date(version, null);
170     }
171 
172     /**
173      * Convert a date into Version, using format 'yyyy.MM.dd.HHmmss'
174      *
175      * @param date a not null date
176      * @return a {@link org.nuiton.version.Version} object.
177      */
178     public static Version convertDate2Version(Date date) {
179         return convertDate2Version(date, null);
180     }
181 
182     /**
183      * Convert a date into Version, using format 'yyyy.MM.dd.HHmmss'
184      *
185      * @param date a not null date
186      * @return a {@link Version} object.
187      */
188     public static Version convertDate2Version(Date date, TimeZone timezone) {
189         Preconditions.checkNotNull(date);
190 
191         SimpleDateFormat formatter = new SimpleDateFormat("yyyy.MM.dd.HHmmss");
192         if (timezone != null) formatter.setTimeZone(timezone);
193         String dateStr = formatter.format(date);
194         return Versions.valueOf(dateStr);
195     }
196 }