1921d8578a5320683fc43db8c0aef47a9d2d4133
[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  * 
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.bouncycastle.asn1.dvcs.Data;
49 import org.onap.portalsdk.core.domain.FusionObject;
50 import org.onap.portalsdk.core.domain.User;
51 import org.onap.portalsdk.core.service.DataAccessService;
52 import org.onap.portalsdk.core.util.SystemProperties;
53 import org.onap.portalsdk.core.web.support.AppUtils;
54 import org.springframework.beans.factory.annotation.Autowired;
55 import org.springframework.context.ApplicationContext;
56 import org.springframework.stereotype.Component;
57 import org.springframework.stereotype.Service;
58
59 @Component
60 public class DateUtils implements Serializable, FusionObject{
61         
62         public static final String US_PACIFIC = "US/Pacific";
63         public static final String US_MOUNTAIN = "US/Mountain";
64         public static final String US_CENTRAL = "US/Central";
65         public static final String US_EASTERN = "US/Eastern";
66         public static final String US_HAWAII = "US/Hawaii";
67         public static final String US_ALASKA = "US/Alaska";
68
69         //Arizona State has Mountain Time with no Daylight Savings
70         public static final String US_ARIZONA = "America/Phoenix";
71         
72         private static final String DB_DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
73         private static final String GET_CURRENT_DATE = "getCurrentDate";
74         
75         
76         //@Autowired
77         private static DataAccessService dataAccessService;
78         
79 //      public static DataAccessService getDataAccessService() {
80 //              return dataAccessService;
81 //      }
82 //
83 //      public void setDataAccessService(DataAccessService dataAccessService) {
84 //              DateUtils.dataAccessService = dataAccessService;
85 //      }
86         
87         @Autowired
88         public DateUtils(DataAccessService dataAccessService) {
89                 DateUtils.dataAccessService =  dataAccessService; 
90         }
91
92         /**
93          * Parses a date value with given pattern, 
94          * to return a Date Object
95          * 
96          * @param dateValue
97          * @param inPattern
98          * @return Date Object
99          * @throws Exception
100          * 
101          */
102         public static Date parseDate(String dateValue,String inPattern) throws Exception{       
103                 return parseDate(dateValue,inPattern,null);
104         }
105         
106         /**
107          * Parses a date value with the given pattern for the specific TimeZone, 
108          * to return a Date Object
109          *
110          * @param dateValue
111          * @param inPattern
112          * @param currentTimeZone
113          * @return Date Object
114          * @throws Exception
115          * 
116          */
117         public static Date parseDate(String dateValue,String inPattern,
118                         String currentTimeZone) throws Exception{       
119                 DateFormat df = new SimpleDateFormat(inPattern);
120                 if(currentTimeZone !=null && !(currentTimeZone.trim().equals(""))){
121                         df.setTimeZone(TimeZone.getTimeZone(currentTimeZone));
122                 }
123                 Date date = df.parse(dateValue);
124                 return date;
125         }
126         
127         /**
128          * Parses a date value with the given pattern for the specific User(in User TimeZone), 
129          * to return a Date Object
130          * 
131          * @param dateValue
132          * @param inPattern
133          * @param userId
134          * @return Date Object
135          * @throws Exception
136          * 
137          */
138         public static Date parseUserDate(String dateValue, String inPattern,    Long userId) throws Exception{  
139 //              User user = (User)getDataAccessService().getDomainObject(User.class, userId, null);
140                 User user = (User)dataAccessService.getDomainObject(User.class, userId, null);
141
142                 String userTimeZone = null;
143                 Long     timezoneId = user.getTimeZoneId();
144                 
145                 if (timezoneId != null) {
146                         userTimeZone = AppUtils.getLookupValueByLabel(timezoneId.toString(), "fn_lu_timezone", "timezone_id", "timezone_value");
147                 }
148
149                 return parseDate(dateValue,inPattern,userTimeZone);
150         }
151         
152         /**
153          * Formats a given date object to the desired pattern
154          * 
155          * @param date
156          * @param outPattern
157          * @return Formatted date value
158          * @throws Exception
159          */
160         public static String formatDate(Date date,String outPattern)throws Exception{
161                 return formatDate(date,outPattern,null);
162         }
163         
164         /**
165          * Formats a date value with the given pattern into a date value with the desired pattern
166          * 
167          * @param dateValue
168          * @param inPattern
169          * @param outPattern
170          * @return Formatted date value
171          * @throws Exception
172          * 
173          */
174         public static String formatDate(String dateValue,String inPattern,
175                         String outPattern) throws Exception{
176                 return formatDate(dateValue,inPattern,null,outPattern,null);
177         }
178         
179         /**
180          * Formats a given date object to the desired pattern for the TimeZone provided
181          * @param date
182          * @param outPattern
183          * @param requiredTimeZone
184          * @return Formatted date value
185          * @throws Exception
186          */
187         public static String formatDate(Date date,String outPattern,
188                         String requiredTimeZone) throws Exception{              
189                 DateFormat df = new SimpleDateFormat(outPattern);
190                 if(requiredTimeZone != null && !requiredTimeZone.trim().equals("")){
191                         df.setTimeZone(TimeZone.getTimeZone(requiredTimeZone));
192                 }
193                 return df.format(date);
194         }
195         
196         /**
197          * Formats a date value with the given pattern
198          * into a date value with the desired pattern for the TimeZone provided
199          * 
200          * @param dateValue
201          * @param inPattern
202          * @param outPattern
203          * @param requiredTimeZone
204          * @return Formatted date value
205          * @throws Exception
206          * 
207          */
208         public static String formatDate(String dateValue,String inPattern,
209                         String outPattern,String requiredTimeZone) throws Exception{
210                 return formatDate(dateValue,inPattern,null,outPattern,requiredTimeZone);
211         }
212         
213         /**
214          * Formats a date value with the given pattern for a specific TimeZone, 
215          * into a date value with the desired pattern for the TimeZone provided
216          *
217          * @param dateValue
218          * @param inPattern
219          * @param currentTimeZone
220          * @param outPattern
221          * @param requiredTimeZone
222          * @return Formatted date value
223          * @throws Exception
224          * 
225          */
226         public static String formatDate(String dateValue,String inPattern,String currentTimeZone,
227                         String outPattern,String requiredTimeZone) throws Exception{
228                 Date date = parseDate(dateValue,inPattern,currentTimeZone);             
229                 return formatDate(date,outPattern,requiredTimeZone);
230         }
231         
232         /**
233          * Formats a date value with the given pattern, for a specific User(in User TimeZone), 
234          * into a date value with the desired pattern for the TimeZone provided
235          * 
236          * @param dateValue
237          * @param inPattern
238          * @param userId
239          * @param outPattern
240          * @param requiredTimeZone
241          * @return Formatted date value
242          * @throws Exception
243          * 
244          */
245         public static String formatUserDate(String dateValue,String inPattern, Long userId,String outPattern,String requiredTimeZone) throws Exception{
246                 //User user = (User)getDataAccessService().getDomainObject(User.class, userId, null);
247                 User user = (User)dataAccessService.getDomainObject(User.class, userId, null);
248                 String userTimeZone = null;
249                 Long     timezoneId = user.getTimeZoneId();
250                 
251                 if (timezoneId != null) {
252                         userTimeZone = AppUtils.getLookupValueByLabel(timezoneId.toString(), "fn_lu_timezone", "timezone_id", "timezone_value");
253                 }
254                 
255                 return formatDate(dateValue,inPattern,userTimeZone,outPattern,requiredTimeZone);
256         }
257         
258         /**
259          * Formats a date value with a given pattern for a specific User(User TimeZone), 
260          * into a date value with the desired pattern for Database TimeZone 
261          * 
262          * @param dateValue
263          * @param inPattern
264          * @param userId
265          * @param outPattern
266          * @return Formatted date value
267          * @throws Exception
268          * 
269          */
270         public static String formatUserDateForDBTimeZone(String dateValue,String inPattern, Long userId,String outPattern) throws Exception{
271 //              User user = (User)getDataAccessService().getDomainObject(User.class, userId, null);
272                 User user = (User)dataAccessService.getDomainObject(User.class, userId, null);
273
274                 String userTimeZone = null;
275                 Long     timezoneId = user.getTimeZoneId();
276                 
277                 /*if (timezoneId != null) {
278                         userTimeZone = AppUtils.getLookupValueByLabel(timezoneId.toString(), "fn_lu_timezone", "timezone_id", "timezone_value");
279                 }*/
280
281                 String dbTimeZone = SystemProperties.getProperty(SystemProperties.DATABASE_TIME_ZONE);
282
283                 return formatDate(dateValue,inPattern,userTimeZone,outPattern,dbTimeZone);
284         }
285         
286         /**
287          * Get the current database Date/Time
288          * @return Date object
289          */
290         public static Date getCurrentDBDate()throws Exception{
291                 String dbTimeZone = SystemProperties.getProperty(SystemProperties.DATABASE_TIME_ZONE);
292                 //List results = (List)getDataAccessService().executeNamedQuery(GET_CURRENT_DATE, null, null);
293                 List results = (List) dataAccessService.executeNamedQuery(GET_CURRENT_DATE, null, null);
294                 /*Object[]  currentDate =  (Object[]) results.get(0) ; 
295                 System.out.println(currentDate[0]);*/
296                 return parseDate((String)results.get(0),DB_DATE_FORMAT,dbTimeZone);
297         }
298         
299         /**
300          * Get the current date value formatted for the User's TimeZone in the desired pattern
301          * 
302          * @param outPattern
303          * @param userId
304          * @return Date value
305          * @throws Exception
306          */
307         public static String getCurrentDBDateForUser(String outPattern,Long userId)throws Exception{
308                 //User user = (User)getDataAccessService().getDomainObject(User.class, userId, null);
309                 User user = (User)dataAccessService.getDomainObject(User.class, userId, null);
310
311                 String userTimeZone = null;
312                 Long     timezoneId = user.getTimeZoneId();
313                 
314                 /*if (timezoneId != null) {
315                         userTimeZone = AppUtils.getLookupValueByLabel(timezoneId.toString(), "fn_lu_timezone", "timezone_id", "timezone_value");
316                 }*/
317
318                 Date dbDate = getCurrentDBDate();
319
320                 return formatDate(dbDate,outPattern,userTimeZone);
321         }
322         
323 }