1 package fr.ifremer.quadrige3.core.dao.technical;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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
43
44 public class DateVersions {
45
46
47
48
49 protected DateVersions() {
50
51 }
52
53 private static final Log log = LogFactory.getLog(DateVersions.class);
54
55
56
57
58
59
60
61
62
63 public static Date convertVersion2Date(Version version) {
64 Assert.notNull(version);
65 return convertVersion2Date(version.toString(), null);
66 }
67
68
69
70
71
72
73
74
75
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
87 int year = Integer.parseInt(versionParts[0]);
88 result.set(Calendar.YEAR, year);
89
90
91 int month = Integer.parseInt(versionParts[1]);
92 result.set(Calendar.MONTH, month - 1);
93
94
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
112 int seconds = Integer.parseInt(time.substring(time.length() - 2));
113 result.set(Calendar.SECOND, seconds);
114
115
116 int minutes = Integer.parseInt(time.substring(time.length() - 4, time.length() - 2));
117 result.set(Calendar.MINUTE, minutes);
118
119
120 int hours = Integer.parseInt(time.substring(0, time.length() - 4));
121 result.set(Calendar.HOUR_OF_DAY, hours);
122 }
123 }
124
125
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
140
141
142
143
144
145
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
161
162
163
164
165
166
167 public static Date safeConvertVersion2Date(String version) {
168 return convertVersion2Date(version, null);
169 }
170
171
172
173
174
175
176
177 public static Version convertDate2Version(Date date) {
178 return convertDate2Version(date, null);
179 }
180
181
182
183
184
185
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 }