c1ca9932f4d42ea1da6560a9ccdaa069d607cf3e
[portal/sdk.git] /
1 /*
2  * ============LICENSE_START==========================================
3  * ONAP Portal SDK
4  * ===================================================================
5  * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6  * ===================================================================
7  *
8  * Unless otherwise specified, all software contained herein is licensed
9  * under the Apache License, Version 2.0 (the "License");
10  * you may not use this software except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *             http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  *
21  * Unless otherwise specified, all documentation contained herein is licensed
22  * under the Creative Commons License, Attribution 4.0 Intl. (the "License");
23  * you may not use this documentation except in compliance with the License.
24  * You may obtain a copy of the License at
25  *
26  *             https://creativecommons.org/licenses/by/4.0/
27  *
28  * Unless required by applicable law or agreed to in writing, documentation
29  * distributed under the License is distributed on an "AS IS" BASIS,
30  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31  * See the License for the specific language governing permissions and
32  * limitations under the License.
33  *
34  * ============LICENSE_END============================================
35  *
36  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
37  */
38 package org.onap.portalsdk.analytics.system.fusion.adapter;
39
40
41 import java.io.Serializable;
42 import java.text.DateFormat;
43 import java.text.SimpleDateFormat;
44 import java.util.Date;
45 import java.util.List;
46 import java.util.TimeZone;
47
48 import org.onap.portalsdk.core.domain.FusionObject;
49 import org.onap.portalsdk.core.domain.User;
50 import org.onap.portalsdk.core.service.DataAccessService;
51 import org.onap.portalsdk.core.util.SystemProperties;
52 import org.onap.portalsdk.core.web.support.AppUtils;
53
54
55 public class DateUtils implements Serializable, FusionObject{
56         
57         public static final String US_PACIFIC = "US/Pacific";
58         public static final String US_MOUNTAIN = "US/Mountain";
59         public static final String US_CENTRAL = "US/Central";
60         public static final String US_EASTERN = "US/Eastern";
61         public static final String US_HAWAII = "US/Hawaii";
62         public static final String US_ALASKA = "US/Alaska";
63
64         //Arizona State has Mountain Time with no Daylight Savings
65         public static final String US_ARIZONA = "America/Phoenix";
66         
67         private static final String DB_DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
68         private static final String GET_CURRENT_DATE = "getCurrentDate";
69         
70         private static DataAccessService dataAccessService;
71         
72         public static DataAccessService getDataAccessService() {
73                 return dataAccessService;
74         }
75
76         public void setDataAccessService(DataAccessService dataAccessService) {
77                 this.dataAccessService = dataAccessService;
78         }
79
80         /**
81          * Parses a date value with given pattern, 
82          * to return a Date Object
83          * 
84          * @param dateValue
85          * @param inPattern
86          * @return Date Object
87          * @throws Exception
88          * 
89          */
90         public static Date parseDate(String dateValue,String inPattern) throws Exception{       
91                 return parseDate(dateValue,inPattern,null);
92         }
93         
94         /**
95          * Parses a date value with the given pattern for the specific TimeZone, 
96          * to return a Date Object
97          *
98          * @param dateValue
99          * @param inPattern
100          * @param currentTimeZone
101          * @return Date Object
102          * @throws Exception
103          * 
104          */
105         public static Date parseDate(String dateValue,String inPattern,
106                         String currentTimeZone) throws Exception{       
107                 DateFormat df = new SimpleDateFormat(inPattern);
108                 if(currentTimeZone !=null && !(currentTimeZone.trim().equals(""))){
109                         df.setTimeZone(TimeZone.getTimeZone(currentTimeZone));
110                 }
111                 Date date = df.parse(dateValue);
112                 return date;
113         }
114         
115         /**
116          * Parses a date value with the given pattern for the specific User(in User TimeZone), 
117          * to return a Date Object
118          * 
119          * @param dateValue
120          * @param inPattern
121          * @param userId
122          * @return Date Object
123          * @throws Exception
124          * 
125          */
126         public static Date parseUserDate(String dateValue, String inPattern,    Long userId) throws Exception{  
127                 User user = (User)getDataAccessService().getDomainObject(User.class, userId, null);
128
129                 String userTimeZone = null;
130                 Long     timezoneId = user.getTimeZoneId();
131                 
132                 if (timezoneId != null) {
133                         userTimeZone = AppUtils.getLookupValueByLabel(timezoneId.toString(), "fn_lu_timezone", "timezone_id", "timezone_value");
134                 }
135
136                 return parseDate(dateValue,inPattern,userTimeZone);
137         }
138         
139         /**
140          * Formats a given date object to the desired pattern
141          * 
142          * @param date
143          * @param outPattern
144          * @return Formatted date value
145          * @throws Exception
146          */
147         public static String formatDate(Date date,String outPattern)throws Exception{
148                 return formatDate(date,outPattern,null);
149         }
150         
151         /**
152          * Formats a date value with the given pattern into a date value with the desired pattern
153          * 
154          * @param dateValue
155          * @param inPattern
156          * @param outPattern
157          * @return Formatted date value
158          * @throws Exception
159          * 
160          */
161         public static String formatDate(String dateValue,String inPattern,
162                         String outPattern) throws Exception{
163                 return formatDate(dateValue,inPattern,null,outPattern,null);
164         }
165         
166         /**
167          * Formats a given date object to the desired pattern for the TimeZone provided
168          * @param date
169          * @param outPattern
170          * @param requiredTimeZone
171          * @return Formatted date value
172          * @throws Exception
173          */
174         public static String formatDate(Date date,String outPattern,
175                         String requiredTimeZone) throws Exception{              
176                 DateFormat df = new SimpleDateFormat(outPattern);
177                 if(requiredTimeZone != null && !requiredTimeZone.trim().equals("")){
178                         df.setTimeZone(TimeZone.getTimeZone(requiredTimeZone));
179                 }
180                 return df.format(date);
181         }
182         
183         /**
184          * Formats a date value with the given pattern
185          * into a date value with the desired pattern for the TimeZone provided
186          * 
187          * @param dateValue
188          * @param inPattern
189          * @param outPattern
190          * @param requiredTimeZone
191          * @return Formatted date value
192          * @throws Exception
193          * 
194          */
195         public static String formatDate(String dateValue,String inPattern,
196                         String outPattern,String requiredTimeZone) throws Exception{
197                 return formatDate(dateValue,inPattern,null,outPattern,requiredTimeZone);
198         }
199         
200         /**
201          * Formats a date value with the given pattern for a specific TimeZone, 
202          * into a date value with the desired pattern for the TimeZone provided
203          *
204          * @param dateValue
205          * @param inPattern
206          * @param currentTimeZone
207          * @param outPattern
208          * @param requiredTimeZone
209          * @return Formatted date value
210          * @throws Exception
211          * 
212          */
213         public static String formatDate(String dateValue,String inPattern,String currentTimeZone,
214                         String outPattern,String requiredTimeZone) throws Exception{
215                 Date date = parseDate(dateValue,inPattern,currentTimeZone);             
216                 return formatDate(date,outPattern,requiredTimeZone);
217         }
218         
219         /**
220          * Formats a date value with the given pattern, for a specific User(in User TimeZone), 
221          * into a date value with the desired pattern for the TimeZone provided
222          * 
223          * @param dateValue
224          * @param inPattern
225          * @param userId
226          * @param outPattern
227          * @param requiredTimeZone
228          * @return Formatted date value
229          * @throws Exception
230          * 
231          */
232         public static String formatUserDate(String dateValue,String inPattern, Long userId,String outPattern,String requiredTimeZone) throws Exception{
233                 User user = (User)getDataAccessService().getDomainObject(User.class, userId, null);
234
235                 String userTimeZone = null;
236                 Long     timezoneId = user.getTimeZoneId();
237                 
238                 if (timezoneId != null) {
239                         userTimeZone = AppUtils.getLookupValueByLabel(timezoneId.toString(), "fn_lu_timezone", "timezone_id", "timezone_value");
240                 }
241                 
242                 return formatDate(dateValue,inPattern,userTimeZone,outPattern,requiredTimeZone);
243         }
244         
245         /**
246          * Formats a date value with a given pattern for a specific User(User TimeZone), 
247          * into a date value with the desired pattern for Database TimeZone 
248          * 
249          * @param dateValue
250          * @param inPattern
251          * @param userId
252          * @param outPattern
253          * @return Formatted date value
254          * @throws Exception
255          * 
256          */
257         public static String formatUserDateForDBTimeZone(String dateValue,String inPattern, Long userId,String outPattern) throws Exception{
258                 User user = (User)getDataAccessService().getDomainObject(User.class, userId, null);
259
260                 String userTimeZone = null;
261                 Long     timezoneId = user.getTimeZoneId();
262                 
263                 /*if (timezoneId != null) {
264                         userTimeZone = AppUtils.getLookupValueByLabel(timezoneId.toString(), "fn_lu_timezone", "timezone_id", "timezone_value");
265                 }*/
266
267                 String dbTimeZone = SystemProperties.getProperty(SystemProperties.DATABASE_TIME_ZONE);
268
269                 return formatDate(dateValue,inPattern,userTimeZone,outPattern,dbTimeZone);
270         }
271         
272         /**
273          * Get the current database Date/Time
274          * @return Date object
275          */
276         public static Date getCurrentDBDate()throws Exception{
277                 String dbTimeZone = SystemProperties.getProperty(SystemProperties.DATABASE_TIME_ZONE);
278                 List results = (List)getDataAccessService().executeNamedQuery(GET_CURRENT_DATE, null, null);
279                 return parseDate(((Object[])results.get(0))[0]+" "+((Object[])results.get(0))[1],DB_DATE_FORMAT,dbTimeZone);
280         }
281         
282         /**
283          * Get the current date value formatted for the User's TimeZone in the desired pattern
284          * 
285          * @param outPattern
286          * @param userId
287          * @return Date value
288          * @throws Exception
289          */
290         public static String getCurrentDBDateForUser(String outPattern,Long userId)throws Exception{
291                 User user = (User)getDataAccessService().getDomainObject(User.class, userId, null);
292
293                 String userTimeZone = null;
294                 Long     timezoneId = user.getTimeZoneId();
295                 
296                 /*if (timezoneId != null) {
297                         userTimeZone = AppUtils.getLookupValueByLabel(timezoneId.toString(), "fn_lu_timezone", "timezone_id", "timezone_value");
298                 }*/
299
300                 Date dbDate = getCurrentDBDate();
301
302                 return formatDate(dbDate,outPattern,userTimeZone);
303         }
304         
305 }