5ebf34bc5f4372c7f6a2d79751b4f2f4be524b92
[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.model.runtime;
21
22 import java.text.DateFormat;
23 import java.text.ParseException;
24 import java.text.SimpleDateFormat;
25 import java.util.*;
26 import javax.servlet.http.*;
27
28 import org.openecomp.portalsdk.analytics.model.base.*;
29 import org.openecomp.portalsdk.analytics.system.Globals;
30 import org.openecomp.portalsdk.analytics.util.*;
31 import org.openecomp.portalsdk.analytics.xmlobj.*;
32
33 public class ReportParamDateValueParser {
34
35         /*public static final SimpleDateFormat[] dateFormats;
36
37         static {
38                 dateFormats = new SimpleDateFormat[5];
39                 (dateFormats[0] = new SimpleDateFormat(AppConstants.JAVA_DATE_FORMAT_MONTHYYYY))
40                                 .setLenient(true);
41                 (dateFormats[1] = new SimpleDateFormat(AppConstants.JAVA_DATE_FORMAT_MONTHDDYYYY))
42                                 .setLenient(true);
43                 (dateFormats[2] = new SimpleDateFormat(AppConstants.JAVA_DATE_FORMAT_MMDDYYYY))
44                                 .setLenient(true);
45                 (dateFormats[3] = new SimpleDateFormat(AppConstants.JAVA_DATE_FORMAT_MMYYYY))
46                                 .setLenient(true);
47                 (dateFormats[4] = new SimpleDateFormat(AppConstants.JAVA_DATE_FORMAT_DDMONYYYY))
48                                 .setLenient(true);
49         }*/
50
51         public static boolean isDateHrParam(String param) {
52                 SimpleDateFormat[] dateFormats = new SimpleDateFormat[2];
53                 (dateFormats[0] = new SimpleDateFormat(AppConstants.JAVA_DATE_FORMAT_MMDDYYYY_HR))
54                 .setLenient(true);
55                 (dateFormats[1] = new SimpleDateFormat(AppConstants.JAVA_DATE_FORMAT_DDMONYYYY_HR))
56                 .setLenient(true);
57                 for (int i = 0; i < dateFormats.length; i++) {
58                         try {
59                                 if (dateFormats[i].parse(param) != null) {
60                                         return true;
61                                 }
62                         } catch (ParseException pe) {
63                                 // do nothing, continue to check param against other dates
64                         }
65                         catch (NumberFormatException pe) {
66                         // do nothing, continue to check param against other dates
67                         }
68                 }
69                 return false;
70         }
71         public static boolean isDateParam(String param) {
72                 SimpleDateFormat[] dateFormats = new SimpleDateFormat[5];
73                 (dateFormats[0] = new SimpleDateFormat(AppConstants.JAVA_DATE_FORMAT_MONTHYYYY))
74                 .setLenient(true);
75                 (dateFormats[1] = new SimpleDateFormat(AppConstants.JAVA_DATE_FORMAT_MONTHDDYYYY))
76                 .setLenient(true);
77                 (dateFormats[2] = new SimpleDateFormat(AppConstants.JAVA_DATE_FORMAT_MMDDYYYY))
78                 .setLenient(true);
79                 (dateFormats[3] = new SimpleDateFormat(AppConstants.JAVA_DATE_FORMAT_MMYYYY))
80                 .setLenient(true);
81                 (dateFormats[4] = new SimpleDateFormat(AppConstants.JAVA_DATE_FORMAT_DDMONYYYY))
82                 .setLenient(true);
83                 
84                 for (int i = 0; i < dateFormats.length; i++) {
85                         try {
86                                 if (dateFormats[i].parse(param) != null) {
87                                         return true;
88                                 }
89                         } catch (ParseException pe) {
90                                 // do nothing, continue to check param against other dates
91                         }
92                         catch (NumberFormatException pe) {
93                         // do nothing, continue to check param against other dates
94                         }
95                 }
96                 return false;
97         }
98
99         public static String formatDateParamValue(String param) {
100                 return ReportParamDateValueParser.formatDateParamValue(param, null);
101         }
102
103         public static String formatDateHrParamValue(String param) {
104                 return ReportParamDateValueParser.formatDateHrParamValue(param, null);
105         }
106         
107         public static String formatDateHrParamValue(String param, String dateHrFormatPattern) {
108                 String formattedDate = null;
109                 Date parsedDate = null;
110
111                 dateHrFormatPattern = (dateHrFormatPattern != null) ? dateHrFormatPattern
112                                 : "HH";
113
114                 SimpleDateFormat[] dateFormats = new SimpleDateFormat[2];
115                 (dateFormats[0] = new SimpleDateFormat(AppConstants.JAVA_DATE_FORMAT_MMDDYYYY_HR))
116                 .setLenient(true);
117                 (dateFormats[1] = new SimpleDateFormat(AppConstants.JAVA_DATE_FORMAT_DDMONYYYY_HR))
118                 .setLenient(true);
119
120                 for (int i = 0; i < dateFormats.length; i++) {
121                         try {
122                                 if (dateFormats[i].parse(param) != null) {
123                                         SimpleDateFormat newDateFormat = new SimpleDateFormat(dateHrFormatPattern);
124                                         parsedDate = dateFormats[i].parse(param);
125                                         formattedDate = newDateFormat.format(parsedDate);
126
127
128                                         return formattedDate;
129                                 }
130                         } catch (ParseException pe) {
131                                 // do nothing, continue to check param against other dates and
132                                 // format accordingly
133                         }
134                         catch (NumberFormatException pe) {
135                                 // do nothing, continue to check param against other dates
136                         }
137                 }
138                 return param;
139         }
140         
141
142         public static String formatDateParamValue(String param, String dateFormatPattern) {
143                 String formattedDate = null;
144                 Date parsedDate = null;
145
146                 dateFormatPattern = (dateFormatPattern != null) ? dateFormatPattern
147                                 : AppConstants.JAVA_DATE_FORMAT_MMDDYYYY;
148
149                 SimpleDateFormat[] dateFormats = new SimpleDateFormat[5];
150                 (dateFormats[0] = new SimpleDateFormat(AppConstants.JAVA_DATE_FORMAT_MONTHYYYY))
151                 .setLenient(true);
152                 (dateFormats[1] = new SimpleDateFormat(AppConstants.JAVA_DATE_FORMAT_MONTHDDYYYY))
153                 .setLenient(true);
154                 (dateFormats[2] = new SimpleDateFormat(AppConstants.JAVA_DATE_FORMAT_MMDDYYYY))
155                 .setLenient(true);
156                 (dateFormats[3] = new SimpleDateFormat(AppConstants.JAVA_DATE_FORMAT_MMYYYY))
157                 .setLenient(true);
158                 (dateFormats[4] = new SimpleDateFormat(AppConstants.JAVA_DATE_FORMAT_DDMONYYYY))
159                 .setLenient(true);
160
161                 for (int i = 0; i < dateFormats.length; i++) {
162                         try {
163                                 if (dateFormats[i].parse(param) != null) {
164                                         SimpleDateFormat newDateFormat = new SimpleDateFormat(dateFormatPattern);
165                                         parsedDate = dateFormats[i].parse(param);
166                                         formattedDate = newDateFormat.format(parsedDate);
167
168                                         if (Globals.getMonthFormatUseLastDay()
169                                                         && (dateFormats[i].toPattern().equals(
170                                                                         AppConstants.JAVA_DATE_FORMAT_MMYYYY) || dateFormats[i]
171                                                                         .toPattern().equals(
172                                                                                         AppConstants.JAVA_DATE_FORMAT_MONTHYYYY))) {
173
174                                                 GregorianCalendar gc = new GregorianCalendar();
175                                                 gc.setTime(parsedDate);
176                                                 int day = gc.getActualMaximum(GregorianCalendar.DAY_OF_MONTH);
177                                                 formattedDate = Utils.replaceInString(formattedDate, "/01/", "/"
178                                                                 + String.valueOf(day) + "/");
179
180                                         }
181
182                                         return formattedDate;
183                                 }
184                         } catch (ParseException pe) {
185                                 // do nothing, continue to check param against other dates and
186                                 // format accordingly
187                         }
188                         catch (NumberFormatException pe) {
189                                 // do nothing, continue to check param against other dates
190                         }
191                 }
192                 return param;
193         }
194
195 } // ReportParamValues
196