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