View Javadoc
1   package fr.ifremer.quadrige3.core.dao.technical;
2   
3   /*-
4    * #%L
5    * Quadrige3 Core :: Quadrige3 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.Splitter;
29  import fr.ifremer.quadrige3.core.exception.QuadrigeTechnicalException;
30  import org.apache.commons.lang3.StringUtils;
31  import org.apache.commons.logging.Log;
32  import org.apache.commons.logging.LogFactory;
33  import org.nuiton.version.Version;
34  import org.nuiton.version.Versions;
35  
36  import java.text.SimpleDateFormat;
37  import java.util.Calendar;
38  import java.util.Date;
39  import java.util.TimeZone;
40  
41  /**
42   * <p>DateVersions class.</p>
43   */
44  public class DateVersions {
45  
46      /**
47       * <p>Constructor for DateVersions.</p>
48       */
49      protected DateVersions() {
50          // Helper class
51      }
52  
53      private static final Log log = LogFactory.getLog(DateVersions.class);
54  
55      /**
56       * convert a version to a date
57       *
58       * ex: version = 2014.9.22.115930 will return 2014-09-22 11:59:30
59       *
60       * @param version a {@link org.nuiton.version.Version} object.
61       * @return a {@link java.util.Date} object.
62       */
63      public static Date convertVersion2Date(Version version) {
64          Assert.notNull(version);
65          return convertVersion2Date(version.toString(), null);
66      }
67  
68      /**
69       * convert a version to a date
70       *
71       * ex: version = 2014.9.22.115930 will return 2014-09-22 11:59:30
72       *
73       * @param version a {@link String} object.
74       * @param timezone a {@link TimeZone} object.
75       * @return a {@link Date} object.
76       */
77      public static Date convertVersion2Date(String version, TimeZone timezone) {
78          Assert.notNull(version);
79  
80          String[] versionParts = Splitter.on('.').splitToList(version).toArray(new String[4]);
81  
82          Calendar result = Calendar.getInstance(timezone != null ? timezone : TimeZone.getDefault());
83  
84          boolean parseError = false;
85          try {
86              // year 
87              int year = Integer.parseInt(versionParts[0]);
88              result.set(Calendar.YEAR, year);
89  
90              // month 
91              int month = Integer.parseInt(versionParts[1]);
92              result.set(Calendar.MONTH, month - 1);
93  
94              // day 
95              int day = Integer.parseInt(versionParts[2]);
96              result.set(Calendar.DAY_OF_MONTH, day);
97  
98              if (StringUtils.isBlank(versionParts[3])) {
99                  result.set(Calendar.SECOND, 0);
100                 result.set(Calendar.MINUTE, 0);
101                 result.set(Calendar.HOUR_OF_DAY, 0);
102             }
103             else {
104                 String time = versionParts[3];
105                 if (time.length() > 6) {
106                     parseError = true;
107                 }
108                 else {
109                     time = StringUtils.leftPad(time, 6, '0');
110 
111                     // seconds
112                     int seconds = Integer.parseInt(time.substring(time.length() - 2));
113                     result.set(Calendar.SECOND, seconds);
114 
115                     // minutes
116                     int minutes = Integer.parseInt(time.substring(time.length() - 4, time.length() - 2));
117                     result.set(Calendar.MINUTE, minutes);
118 
119                     // hours
120                     int hours = Integer.parseInt(time.substring(0, time.length() - 4));
121                     result.set(Calendar.HOUR_OF_DAY, hours);
122                 }
123             }
124 
125             // no milliseconds
126             result.set(Calendar.MILLISECOND, 0);
127 
128         } catch (Exception ex) {
129             parseError = true;
130         }
131         if (parseError) {
132             throw new QuadrigeTechnicalException(String.format("the database version '%s' can't be converted to a synchronization date", version));
133         }
134 
135         return result.getTime();
136     }
137 
138     /**
139      * convert a version to a date, with no parse error
140      *
141      * ex: version = 2014.9.22.115930 will return 2014-09-22 11:59:30
142      *
143      * @param version a {@link String} object.
144      * @param timezone a {@link TimeZone} object.
145      * @return the date, or null if parse error
146      */
147     public static Date safeConvertVersion2Date(String version, TimeZone timezone) {
148         if (StringUtils.isBlank(version)) {
149             return null;
150         }
151         try {
152             return convertVersion2Date(version, timezone);
153         } catch (QuadrigeTechnicalException e) {
154             log.error(e.getMessage(), e);
155         }
156         return null;
157     }
158 
159     /**
160      * convert a version to a date, with no parse error
161      *
162      * ex: version = 2014.9.22.115930 will return 2014-09-22 11:59:30
163      *
164      * @param version a {@link String} object.
165      * @return the date, or null if parse error
166      */
167     public static Date safeConvertVersion2Date(String version) {
168         return convertVersion2Date(version, null);
169     }
170 
171     /**
172      * Convert a date into Version, using format 'yyyy.MM.dd.HHmmss'
173      *
174      * @param date a not null date
175      * @return a {@link org.nuiton.version.Version} object.
176      */
177     public static Version convertDate2Version(Date date) {
178         return convertDate2Version(date, null);
179     }
180 
181     /**
182      * Convert a date into Version, using format 'yyyy.MM.dd.HHmmss'
183      *
184      * @param date a not null date
185      * @return a {@link Version} object.
186      */
187     public static Version convertDate2Version(Date date, TimeZone timezone) {
188         Assert.notNull(date);
189 
190         SimpleDateFormat formatter = new SimpleDateFormat("yyyy.MM.dd.HHmmss");
191         if (timezone != null) formatter.setTimeZone(timezone);
192         String dateStr = formatter.format(date);
193         return Versions.valueOf(dateStr);
194     }
195 }