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