2 * ================================================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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 * ================================================================================
20 package org.openecomp.portalsdk.analytics.system.fusion.adapter;
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;
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;
37 public class DateUtils implements Serializable, FusionObject{
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";
46 //Arizona State has Mountain Time with no Daylight Savings
47 public static final String US_ARIZONA = "America/Phoenix";
49 private static final String DB_DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
50 private static final String GET_CURRENT_DATE = "getCurrentDate";
52 private static DataAccessService dataAccessService;
54 public static DataAccessService getDataAccessService() {
55 return dataAccessService;
58 public void setDataAccessService(DataAccessService dataAccessService) {
59 this.dataAccessService = dataAccessService;
63 * Parses a date value with given pattern,
64 * to return a Date Object
72 public static Date parseDate(String dateValue,String inPattern) throws Exception{
73 return parseDate(dateValue,inPattern,null);
77 * Parses a date value with the given pattern for the specific TimeZone,
78 * to return a Date Object
82 * @param currentTimeZone
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));
93 Date date = df.parse(dateValue);
98 * Parses a date value with the given pattern for the specific User(in User TimeZone),
99 * to return a Date Object
104 * @return Date Object
108 public static Date parseUserDate(String dateValue, String inPattern, Long userId) throws Exception{
109 User user = (User)getDataAccessService().getDomainObject(User.class, userId, null);
111 String userTimeZone = null;
112 Long timezoneId = user.getTimeZoneId();
114 if (timezoneId != null) {
115 userTimeZone = AppUtils.getLookupValueByLabel(timezoneId.toString(), "fn_lu_timezone", "timezone_id", "timezone_value");
118 return parseDate(dateValue,inPattern,userTimeZone);
122 * Formats a given date object to the desired pattern
126 * @return Formatted date value
129 public static String formatDate(Date date,String outPattern)throws Exception{
130 return formatDate(date,outPattern,null);
134 * Formats a date value with the given pattern into a date value with the desired pattern
139 * @return Formatted date value
143 public static String formatDate(String dateValue,String inPattern,
144 String outPattern) throws Exception{
145 return formatDate(dateValue,inPattern,null,outPattern,null);
149 * Formats a given date object to the desired pattern for the TimeZone provided
152 * @param requiredTimeZone
153 * @return Formatted date value
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));
162 return df.format(date);
166 * Formats a date value with the given pattern
167 * into a date value with the desired pattern for the TimeZone provided
172 * @param requiredTimeZone
173 * @return Formatted date value
177 public static String formatDate(String dateValue,String inPattern,
178 String outPattern,String requiredTimeZone) throws Exception{
179 return formatDate(dateValue,inPattern,null,outPattern,requiredTimeZone);
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
188 * @param currentTimeZone
190 * @param requiredTimeZone
191 * @return Formatted date value
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);
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
209 * @param requiredTimeZone
210 * @return Formatted date value
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);
217 String userTimeZone = null;
218 Long timezoneId = user.getTimeZoneId();
220 if (timezoneId != null) {
221 userTimeZone = AppUtils.getLookupValueByLabel(timezoneId.toString(), "fn_lu_timezone", "timezone_id", "timezone_value");
224 return formatDate(dateValue,inPattern,userTimeZone,outPattern,requiredTimeZone);
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
235 * @return Formatted date value
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);
242 String userTimeZone = null;
243 Long timezoneId = user.getTimeZoneId();
245 /*if (timezoneId != null) {
246 userTimeZone = AppUtils.getLookupValueByLabel(timezoneId.toString(), "fn_lu_timezone", "timezone_id", "timezone_value");
249 String dbTimeZone = SystemProperties.getProperty(SystemProperties.DATABASE_TIME_ZONE);
251 return formatDate(dateValue,inPattern,userTimeZone,outPattern,dbTimeZone);
255 * Get the current database Date/Time
256 * @return Date object
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);
265 * Get the current date value formatted for the User's TimeZone in the desired pattern
272 public static String getCurrentDBDateForUser(String outPattern,Long userId)throws Exception{
273 User user = (User)getDataAccessService().getDomainObject(User.class, userId, null);
275 String userTimeZone = null;
276 Long timezoneId = user.getTimeZoneId();
278 /*if (timezoneId != null) {
279 userTimeZone = AppUtils.getLookupValueByLabel(timezoneId.toString(), "fn_lu_timezone", "timezone_id", "timezone_value");
282 Date dbDate = getCurrentDBDate();
284 return formatDate(dbDate,outPattern,userTimeZone);