2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6 * ===================================================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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.
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
26 * https://creativecommons.org/licenses/by/4.0/
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.
34 * ============LICENSE_END============================================
38 package org.onap.portalsdk.analytics.model.runtime;
40 import java.util.Enumeration;
41 import java.util.Hashtable;
42 import java.util.Iterator;
44 import javax.servlet.http.HttpServletRequest;
46 import org.onap.portalsdk.analytics.error.RaptorException;
47 import org.onap.portalsdk.analytics.error.ReportSQLException;
48 import org.onap.portalsdk.analytics.system.AppUtils;
49 import org.onap.portalsdk.analytics.system.ConnectionUtils;
50 import org.onap.portalsdk.analytics.system.Globals;
51 import org.onap.portalsdk.analytics.util.AppConstants;
52 import org.onap.portalsdk.analytics.util.DataSet;
53 import org.onap.portalsdk.analytics.util.Utils;
54 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
55 import org.onap.portalsdk.core.util.SecurityCodecUtil;
56 import org.owasp.esapi.ESAPI;
58 public class ReportParamValuesForPDFExcel extends Hashtable {
60 private static final EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(ReportParamValuesForPDFExcel.class);
62 private Hashtable paramIsMultiValue = null;
63 private Hashtable paramIsTextAreaValue = null;
64 private ReportFormFields rff = null;
66 private Hashtable multiValueBaseSQL = null;
68 public ReportParamValuesForPDFExcel() {
70 paramIsMultiValue = new Hashtable();
71 paramIsTextAreaValue = new Hashtable();
72 multiValueBaseSQL = new Hashtable();
73 } // ReportParamValues
75 public ReportParamValuesForPDFExcel(ReportFormFields rff, String reportDefType) {
78 for (Iterator iter = rff.iterator(); iter.hasNext();) {
79 FormField ff = (FormField) iter.next();
81 put(ff.getFieldName(), nvl(ff.getDefaultValue()));
83 boolean isMultiValue = ff.getFieldType().equals(FormField.FFT_CHECK_BOX)
84 || ff.getFieldType().equals(FormField.FFT_LIST_MULTI);
85 boolean isTextAreaValue = ff.getFieldType().equals(FormField.FFT_TEXTAREA) && reportDefType
86 .equals(AppConstants.RD_SQL_BASED);
87 paramIsMultiValue.put(ff.getFieldName(), new Boolean(isMultiValue));
88 paramIsTextAreaValue.put(ff.getFieldName(), new Boolean(isTextAreaValue));
89 if (isMultiValue && ff.getBaseSQL() != null)
90 multiValueBaseSQL.put(ff.getFieldName(), ff.getBaseSQL());
92 } // ReportParamValues
94 public boolean isParameterMultiValue(String fieldName) {
95 Boolean b = (Boolean) paramIsMultiValue.get(fieldName);
96 return (b != null) ? b.booleanValue() : false;
97 } // isParameterMultiValue
99 public boolean isParameterTextAreaValue(String fieldName) {
100 Boolean b = (Boolean) paramIsTextAreaValue.get(fieldName);
101 return (b != null) ? b.booleanValue() : false;
102 } // isParameterMultiValue
104 public boolean setParamValues(HttpServletRequest request, boolean refresh) {
105 long currentTime = System.currentTimeMillis();
106 boolean paramUpdated = false;
114 String dbInfo = null;
115 ReportRuntime rr = (ReportRuntime) request.getSession().getAttribute(AppConstants.SI_REPORT_RUNTIME);
116 if(rr!=null && rr.getReportType().equals(AppConstants.RT_DASHBOARD)) {
117 rr = (ReportRuntime) request.getSession().getAttribute("FirstDashReport");
119 rff = rr.getReportFormFields();
120 } else if (rr == null) {
121 rr = (ReportRuntime) request.getSession().getAttribute("FirstDashReport");
123 rff = rr.getReportFormFields();
125 rff = rr.getReportFormFields();
129 dbInfo = rr.getDBInfo();
130 if (Utils.isNull(dbInfo)) {
131 dbInfo = (String) request.getSession().getAttribute("remoteDB");
133 if(!Utils.isNull(dbInfo)){
134 for (Iterator iter = rff.iterator(); iter.hasNext();) {
135 name=""; //just added
136 ff = (FormField) iter.next();
137 if(!ff.getFieldType().equals(FormField.FFT_BLANK)) {
138 sql = ff.getBaseSQLForPDFExcel();
139 if(sql!=null && sql.trim().length()>0)
140 sql = Utils.replaceInString(sql, "[LOGGED_USERID]", AppUtils.getUserID(request));
141 if(ff.getFieldType().equals(FormField.FFT_COMBO_BOX) || ff.getFieldType().equals(FormField.FFT_LIST_BOX) || ff.getFieldType().equals(FormField.FFT_TEXT_W_POPUP) || ff.getFieldType().equals(FormField.FFT_HIDDEN)) {
142 for (Enumeration enum1 = rr.getParamKeys(); enum1.hasMoreElements();) {
143 name = (String) enum1.nextElement();
144 value = rr.getParamValue(name);
145 value = getParamValueForSQL(name, value);
146 if(name.startsWith("ff")) {
147 for (Iterator iter1 = rff.iterator(); iter1.hasNext();) {
148 FormField ff1 = (FormField) iter1.next();
150 if(sql!=null && sql.trim().length()>0){
151 if(name.equals(ff.getFieldName())){
152 sql = Utils.replaceInString(sql, "[VALUE]", ESAPI.encoder().encodeForSQL( SecurityCodecUtil.getCodec(),value));
154 if(name.equals(ff1.getFieldName())){
155 sql = Utils.replaceInString(sql, "["+ff1.getFieldDisplayName()+"]", ESAPI.encoder().encodeForSQL( SecurityCodecUtil.getCodec(),value));
162 } else if (ff.getFieldType().equals(ff.FFT_LIST_MULTI)||ff.getFieldType().equals(ff.FFT_CHECK_BOX)) {
163 for (Enumeration enum1 = rr.getParamKeys(); enum1.hasMoreElements();) {
164 name = (String) enum1.nextElement();
165 value = rr.getParamValue(name);
166 value = getParamValueForSQL(name, value);
167 if(name.startsWith("ff")) {
168 for (Iterator iter1 = rff.iterator(); iter1.hasNext();) {
169 FormField ff1 = (FormField) iter1.next();
171 if(sql!=null && sql.trim().length()>0){
172 if(name.equals(ff.getFieldName())){
173 sql = Utils.replaceInString(sql, "[VALUE]", ESAPI.encoder().encodeForSQL( SecurityCodecUtil.getCodec(),value));
175 if(name.equals(ff1.getFieldName())){
176 sql = Utils.replaceInString(sql, "["+ff1.getFieldDisplayName()+"]", ESAPI.encoder().encodeForSQL( SecurityCodecUtil.getCodec(),value));
184 if(nvl(ff.getFieldDefaultSQL()).length()<=0)
187 if(sql!=null && sql.trim().length()>0){
190 name = ff.getFieldName();
191 value = rr.getParamValue(name);
192 String paramValue = ESAPI.encoder().encodeForSQL( SecurityCodecUtil.getCodec(), getParamValueForSQL(name, value));
193 if(name!=null && name.equals(ff.getFieldName()))
194 sql = Utils.replaceInString(sql, "[VALUE]", paramValue);
195 if(paramValue == null) {
196 if(sql.lastIndexOf("where id = ''")>0)
197 sql = sql.substring(0, sql.lastIndexOf("where id = ''"));
199 FormField ff2 = null;
200 for (Iterator iter1 = rff.iterator(); iter1.hasNext();) {
201 ff2 = (FormField)iter1.next();
202 sql = Utils.replaceInString(sql, "[" + ff2.getFieldDisplayName() +"]", ESAPI.encoder().encodeForSQL( SecurityCodecUtil.getCodec(),getParamValue(ff2.getFieldName())));
205 String[] reqParameters = Globals.getRequestParams().split(",");
206 String[] sessionParameters = Globals.getSessionParams().split(",");
207 String[] scheduleSessionParameters = Globals.getSessionParamsForScheduling().split(",");
208 javax.servlet.http.HttpSession session = request.getSession();
209 if(session != null ) {
210 for (int i = 0; i < sessionParameters.length; i++) {
211 sql = Utils.replaceInString(sql, "[" + sessionParameters[i].toUpperCase()+"]", (String)session.getAttribute(sessionParameters[i]) );
214 if(request != null ) {
215 for (int i = 0; i < scheduleSessionParameters.length; i++) {
216 sql = Utils.replaceInString(sql, "[" + scheduleSessionParameters[i].toUpperCase()+"]", ESAPI.encoder().encodeForSQL( SecurityCodecUtil.getCodec(), request.getParameter(scheduleSessionParameters[i]) ));
218 for (int i = 0; i < reqParameters.length; i++) {
219 if(!reqParameters[i].startsWith("ff")) {
220 if (request.getParameter(reqParameters[i])!=null) {
221 sql = Utils.replaceInString(sql, "[" + reqParameters[i]+"]", request.getParameter(reqParameters[i]) );
222 sql = Utils.replaceInString(sql, "[" + reqParameters[i].toUpperCase()+"]", ESAPI.encoder().encodeForSQL( SecurityCodecUtil.getCodec(), request.getParameter(reqParameters[i]) ));
225 sql = Utils.replaceInString(sql, "[" + reqParameters[i]+"]", request.getParameter(reqParameters[i].toUpperCase()) );
226 sql = Utils.replaceInString(sql, "[" + reqParameters[i].toUpperCase()+"]", ESAPI.encoder().encodeForSQL( SecurityCodecUtil.getCodec(), request.getParameter(reqParameters[i].toUpperCase()) ));
230 sql = Utils.replaceInString(sql, "[" + reqParameters[i].toUpperCase()+"]", ESAPI.encoder().encodeForSQL( SecurityCodecUtil.getCodec(), request.getParameter(reqParameters[i]) ));
235 ds = ConnectionUtils.getDataSet(sql, dbInfo);
236 } catch (ReportSQLException ex) {
237 logger.debug(EELFLoggerDelegate.debugLogger, ("sql not complete" + sql));
239 if(ff.getFieldType().equals(FormField.FFT_LIST_MULTI) || ff.getFieldType().equals(FormField.FFT_CHECK_BOX)) {
240 StringBuffer multiValue = new StringBuffer("");
242 for(int i = 0; i < ds.getRowCount(); i++) {
243 multiValue.append(ds.getString(i,1));
244 if(i<ds.getRowCount()-1)
245 multiValue.append("|");
248 put(ff.getFieldName(), nvl(multiValue.toString()));
250 if(ff.getValidationType().equals(FormField.VT_TIMESTAMP_HR) || ff.getValidationType().equals(FormField.VT_TIMESTAMP_MIN) || ff.getValidationType().equals(FormField.VT_TIMESTAMP_SEC) ) {
251 value1 = nvl(rr.getParamValue(ff.getFieldName())) + " "+addZero(Utils.oracleSafe(nvl(rr
252 .getParamValue(ff.getFieldName()+"_Hr"))));
253 if(ff.getValidationType().equals(FormField.VT_TIMESTAMP_MIN) || ff.getValidationType().equals(FormField.VT_TIMESTAMP_SEC) ) {
254 value1 = value1 + (nvl(rr
255 .getParamValue(ff.getFieldName()+"_Min")).length() > 0 ? ":" + addZero(Utils.oracleSafe(nvl(rr
256 .getParamValue(ff.getFieldName()+"_Min")))) : "");
258 if(ff.getValidationType().equals(FormField.VT_TIMESTAMP_SEC) ) {
259 value1 = value1 + (nvl(rr
260 .getParamValue(ff.getFieldName()+"_Sec")).length() > 0 ? ":"+ addZero(Utils.oracleSafe(nvl(rr
261 .getParamValue(ff.getFieldName()+"_Sec")))) : "");
263 put(ff.getFieldName(), nvl(value1));
266 if(ds!=null && ds.getRowCount()>0)
267 put(ff.getFieldName(), nvl(ds.getString(0,1)));
268 else put(ff.getFieldName(), nvl(value));
273 } catch (ReportSQLException ex) {
274 logger.debug(EELFLoggerDelegate.debugLogger, ("sql not complete" + sql));
276 catch (Exception ex) {}
278 //Added for TimeStamp validation
281 if(!ff.getFieldType().equals(FormField.FFT_BLANK)) {
282 //Added for TimeStamp validation
283 if(ff.getValidationType().equals(FormField.VT_TIMESTAMP_HR) || ff.getValidationType().equals(FormField.VT_TIMESTAMP_MIN) || ff.getValidationType().equals(FormField.VT_TIMESTAMP_SEC) ) {
284 value1 = nvl(rr.getParamValue(ff.getFieldName())) + " "+addZero(Utils.oracleSafe(nvl(rr
285 .getParamValue(ff.getFieldName()+"_Hr"))));
286 if(ff.getValidationType().equals(FormField.VT_TIMESTAMP_MIN) || ff.getValidationType().equals(FormField.VT_TIMESTAMP_SEC) ) {
287 value1 = value1 + (nvl(rr
288 .getParamValue(ff.getFieldName()+"_Min")).length() > 0 ? ":" + addZero(Utils.oracleSafe(nvl(rr
289 .getParamValue(ff.getFieldName()+"_Min")))) : "");
291 if(ff.getValidationType().equals(FormField.VT_TIMESTAMP_SEC) ) {
292 value1 = value1 + (nvl(rr
293 .getParamValue(ff.getFieldName()+"_Sec")).length() > 0 ? ":"+ addZero(Utils.oracleSafe(nvl(rr
294 .getParamValue(ff.getFieldName()+"_Sec")))) : "");
297 value1 = nvl(rr.getParamValue(ff.getFieldName()));
298 if(value1.length()<=0)
299 value1 = nvl(ff.getDefaultValue());
300 put(ff.getFieldName(), nvl(value1));
311 logger.debug(EELFLoggerDelegate.debugLogger, ("[DEBUG MESSAGE FROM RAPTOR] ------->Time Taken for Adding/Clearing Param Values for FormField Info Bar " + (System.currentTimeMillis() - currentTime)));
315 public String getParamValue(String key) {
317 return (String) get(key);
322 public String getParamValueForSQL(String key, String value) {
323 value = Utils.oracleSafe(value);
324 if (isParameterMultiValue(key))
325 value = "('" + Utils.replaceInString(value, "|", "','") + "')";
329 public String getParamDisplayValue(String key) {
330 String value = getParamValue(key);
331 if (isParameterMultiValue(key))
332 value = "(" + Utils.replaceInString(value, "|", ",") + ")";
336 public String getParamBaseSQL(String key) {
337 return (String) multiValueBaseSQL.get(key);
340 /** ************************************************************************************************* */
342 private String nvl(String s) {
343 return (s == null) ? "" : s;
346 private String nvl(String s, String sDefault) {
347 return nvl(s).equals("") ? sDefault : s;
350 private boolean isNull(String a) {
351 if ((a == null) || (a.length() == 0) || a.equalsIgnoreCase("null"))
357 private void clearValues() {
359 String defaultValue = "";
360 String defaultSQL = "";
361 String defaultQuery = "";
362 DataSet dsDefault = null;
364 for (Enumeration enKeys = keys(); enKeys.hasMoreElements();) {
365 String key = (String) enKeys.nextElement();
366 for(rff.resetNext(); rff.hasNext(); ) {
368 if(ff.getFieldName().equals(key)) {
370 defaultValue = ff.getDefaultValue();
371 defaultSQL = ff.getFieldDefaultSQL();
372 if(nvl(defaultValue).length()>0) {
373 put(key,ff.getDefaultValue());
374 } else if(nvl(defaultSQL).length() > 0) {
375 if(!(isParameterMultiValue(key) || isParameterTextAreaValue(key))) {
376 defaultQuery = "SELECT id, name FROM (SELECT rownum r, id, name FROM (" + defaultSQL
380 dsDefault = ConnectionUtils.getDataSet(defaultQuery, ff.getDbInfo());
381 if(dsDefault!=null && dsDefault.getRowCount()>0) {
382 for (int i = 0; i < dsDefault.getRowCount(); i++) {
383 put(key, dsDefault.getString(i, 1));
386 } catch (RaptorException ex) {}
398 public void printValues() {
399 for (Enumeration enKeys = keys(); enKeys.hasMoreElements();) {
400 String key = (String) enKeys.nextElement();
401 String value = (String) get(key);
402 logger.debug(EELFLoggerDelegate.debugLogger, ("ReportParamValuesForPDFEXCEL " + key + " "+ value));
405 public String addZero(String num) {
408 numInt = Integer.parseInt(num);
409 }catch(NumberFormatException ex){
414 else return ""+numInt;
417 } // ReportParamValues