2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6 * ===================================================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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.
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
26 * https://creativecommons.org/licenses/by/4.0/
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.
34 * ============LICENSE_END============================================
38 package org.onap.portalsdk.analytics.system.fusion.adapter;
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;
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;
60 public class DateUtils implements Serializable, FusionObject{
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";
69 //Arizona State has Mountain Time with no Daylight Savings
70 public static final String US_ARIZONA = "America/Phoenix";
72 private static final String DB_DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
73 private static final String GET_CURRENT_DATE = "getCurrentDate";
77 private static DataAccessService dataAccessService;
79 // public static DataAccessService getDataAccessService() {
80 // return dataAccessService;
83 // public void setDataAccessService(DataAccessService dataAccessService) {
84 // DateUtils.dataAccessService = dataAccessService;
88 public DateUtils(DataAccessService dataAccessService) {
89 DateUtils.dataAccessService = dataAccessService;
93 * Parses a date value with given pattern,
94 * to return a Date Object
102 public static Date parseDate(String dateValue,String inPattern) throws Exception{
103 return parseDate(dateValue,inPattern,null);
107 * Parses a date value with the given pattern for the specific TimeZone,
108 * to return a Date Object
112 * @param currentTimeZone
113 * @return Date Object
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));
123 Date date = df.parse(dateValue);
128 * Parses a date value with the given pattern for the specific User(in User TimeZone),
129 * to return a Date Object
134 * @return Date Object
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);
142 String userTimeZone = null;
143 Long timezoneId = user.getTimeZoneId();
145 if (timezoneId != null) {
146 userTimeZone = AppUtils.getLookupValueByLabel(timezoneId.toString(), "fn_lu_timezone", "timezone_id", "timezone_value");
149 return parseDate(dateValue,inPattern,userTimeZone);
153 * Formats a given date object to the desired pattern
157 * @return Formatted date value
160 public static String formatDate(Date date,String outPattern)throws Exception{
161 return formatDate(date,outPattern,null);
165 * Formats a date value with the given pattern into a date value with the desired pattern
170 * @return Formatted date value
174 public static String formatDate(String dateValue,String inPattern,
175 String outPattern) throws Exception{
176 return formatDate(dateValue,inPattern,null,outPattern,null);
180 * Formats a given date object to the desired pattern for the TimeZone provided
183 * @param requiredTimeZone
184 * @return Formatted date value
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));
193 return df.format(date);
197 * Formats a date value with the given pattern
198 * into a date value with the desired pattern for the TimeZone provided
203 * @param requiredTimeZone
204 * @return Formatted date value
208 public static String formatDate(String dateValue,String inPattern,
209 String outPattern,String requiredTimeZone) throws Exception{
210 return formatDate(dateValue,inPattern,null,outPattern,requiredTimeZone);
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
219 * @param currentTimeZone
221 * @param requiredTimeZone
222 * @return Formatted date value
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);
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
240 * @param requiredTimeZone
241 * @return Formatted date value
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();
251 if (timezoneId != null) {
252 userTimeZone = AppUtils.getLookupValueByLabel(timezoneId.toString(), "fn_lu_timezone", "timezone_id", "timezone_value");
255 return formatDate(dateValue,inPattern,userTimeZone,outPattern,requiredTimeZone);
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
266 * @return Formatted date value
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);
274 String userTimeZone = null;
275 Long timezoneId = user.getTimeZoneId();
277 /*if (timezoneId != null) {
278 userTimeZone = AppUtils.getLookupValueByLabel(timezoneId.toString(), "fn_lu_timezone", "timezone_id", "timezone_value");
281 String dbTimeZone = SystemProperties.getProperty(SystemProperties.DATABASE_TIME_ZONE);
283 return formatDate(dateValue,inPattern,userTimeZone,outPattern,dbTimeZone);
287 * Get the current database Date/Time
288 * @return Date object
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);
300 * Get the current date value formatted for the User's TimeZone in the desired pattern
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);
311 String userTimeZone = null;
312 Long timezoneId = user.getTimeZoneId();
314 /*if (timezoneId != null) {
315 userTimeZone = AppUtils.getLookupValueByLabel(timezoneId.toString(), "fn_lu_timezone", "timezone_id", "timezone_value");
318 Date dbDate = getCurrentDBDate();
320 return formatDate(dbDate,outPattern,userTimeZone);