73380025fc8b86eef61d57d26c21fe02eef6baa1
[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  * 
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                 dateFormats[0].setLenient(true);
71                 dateFormats[1] = new SimpleDateFormat(AppConstants.JAVA_DATE_FORMAT_DDMONYYYY_HR);
72                 dateFormats[1].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 | NumberFormatException pe) {
79                                 // do nothing, continue to check param against other dates
80                         }
81                 }
82                 return false;
83         }
84         public static boolean isDateParam(String param) {
85                 SimpleDateFormat[] dateFormats = new SimpleDateFormat[5];
86                 dateFormats[0] = new SimpleDateFormat(AppConstants.JAVA_DATE_FORMAT_MONTHYYYY);
87                 dateFormats[0].setLenient(true);
88                 dateFormats[1] = new SimpleDateFormat(AppConstants.JAVA_DATE_FORMAT_MONTHDDYYYY);
89                 dateFormats[1].setLenient(true);
90                 dateFormats[2] = new SimpleDateFormat(AppConstants.JAVA_DATE_FORMAT_MMDDYYYY);
91                 dateFormats[2].setLenient(true);
92                 dateFormats[3] = new SimpleDateFormat(AppConstants.JAVA_DATE_FORMAT_MMYYYY);
93                 dateFormats[3].setLenient(true);
94                 dateFormats[4] = new SimpleDateFormat(AppConstants.JAVA_DATE_FORMAT_DDMONYYYY);
95                 dateFormats[4].setLenient(true);
96                 
97                 for (int i = 0; i < dateFormats.length; i++) {
98                         try {
99                                 if (dateFormats[i].parse(param) != null) {
100                                         return true;
101                                 }
102                         } catch (ParseException | NumberFormatException pe) {
103                                 // do nothing, continue to check param against other dates
104                         }
105                 }
106                 return false;
107         }
108
109         public static String formatDateParamValue(String param) {
110                 return ReportParamDateValueParser.formatDateParamValue(param, null);
111         }
112
113         public static String formatDateHrParamValue(String param) {
114                 return ReportParamDateValueParser.formatDateHrParamValue(param, null);
115         }
116         
117         public static String formatDateHrParamValue(String param, String dateHrFormatPattern) {
118                 String formattedDate = null;
119                 Date parsedDate = null;
120
121                 dateHrFormatPattern = (dateHrFormatPattern != null) ? dateHrFormatPattern
122                                 : "HH";
123
124                 SimpleDateFormat[] dateFormats = new SimpleDateFormat[2];
125                 dateFormats[0] = new SimpleDateFormat(AppConstants.JAVA_DATE_FORMAT_MMDDYYYY_HR);
126                 dateFormats[0].setLenient(true);
127                 dateFormats[1] = new SimpleDateFormat(AppConstants.JAVA_DATE_FORMAT_DDMONYYYY_HR);
128                 dateFormats[1].setLenient(true);
129
130                 for (int i = 0; i < dateFormats.length; i++) {
131                         try {
132                                 if (dateFormats[i].parse(param) != null) {
133                                         SimpleDateFormat newDateFormat = new SimpleDateFormat(dateHrFormatPattern);
134                                         parsedDate = dateFormats[i].parse(param);
135                                         formattedDate = newDateFormat.format(parsedDate);
136
137
138                                         return formattedDate;
139                                 }
140                         } catch (ParseException | NumberFormatException pe) {
141                                 // do nothing, continue to check param against other dates and
142                                 // format accordingly
143                         }
144                 }
145                 return param;
146         }
147         
148
149         public static String formatDateParamValue(String param, String dateFormatPattern) {
150                 String formattedDate = null;
151                 Date parsedDate = null;
152
153                 dateFormatPattern = (dateFormatPattern != null) ? dateFormatPattern
154                                 : AppConstants.JAVA_DATE_FORMAT_MMDDYYYY;
155
156                 SimpleDateFormat[] dateFormats = new SimpleDateFormat[5];
157                 dateFormats[0] = new SimpleDateFormat(AppConstants.JAVA_DATE_FORMAT_MONTHYYYY);
158                 dateFormats[0].setLenient(true);
159                 dateFormats[1] = new SimpleDateFormat(AppConstants.JAVA_DATE_FORMAT_MONTHDDYYYY);
160                 dateFormats[1].setLenient(true);
161                 dateFormats[2] = new SimpleDateFormat(AppConstants.JAVA_DATE_FORMAT_MMDDYYYY);
162                 dateFormats[2].setLenient(true);
163                 dateFormats[3] = new SimpleDateFormat(AppConstants.JAVA_DATE_FORMAT_MMYYYY);
164                 dateFormats[3].setLenient(true);
165                 dateFormats[4] = new SimpleDateFormat(AppConstants.JAVA_DATE_FORMAT_DDMONYYYY);
166                 dateFormats[4].setLenient(true);
167
168                 for (int i = 0; i < dateFormats.length; i++) {
169                         try {
170                                 if (dateFormats[i].parse(param) == null) {
171                                         continue;
172                                 }
173
174                                 SimpleDateFormat newDateFormat = new SimpleDateFormat(dateFormatPattern);
175                                 parsedDate = dateFormats[i].parse(param);
176                                 formattedDate = newDateFormat.format(parsedDate);
177
178                                 if (Globals.getMonthFormatUseLastDay()
179                                                 && (dateFormats[i].toPattern().equals(
180                                                                 AppConstants.JAVA_DATE_FORMAT_MMYYYY) || dateFormats[i]
181                                                                 .toPattern().equals(
182                                                                                 AppConstants.JAVA_DATE_FORMAT_MONTHYYYY))) {
183
184                                         GregorianCalendar gc = new GregorianCalendar();
185                                         gc.setTime(parsedDate);
186                                         int day = gc.getActualMaximum(GregorianCalendar.DAY_OF_MONTH);
187                                         formattedDate = Utils.replaceInString(formattedDate, "/01/", "/"
188                                                         + day + "/");
189
190                                 }
191
192                                 return formattedDate;
193
194                         } catch (ParseException | NumberFormatException pe) {
195                                 // do nothing, continue to check param against other dates and
196                                 // format accordingly
197                         }
198                 }
199                 return param;
200         }
201
202 } // ReportParamValues
203