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