483e9fe4f8690d89fb067e50103d0b25f1620f94
[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.util.Enumeration;
41 import java.util.Hashtable;
42 import java.util.Iterator;
43
44 import javax.servlet.http.HttpServletRequest;
45
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;
57
58 public class ReportParamValuesForPDFExcel extends Hashtable {
59         
60         private static final EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(ReportParamValuesForPDFExcel.class);
61
62         private Hashtable paramIsMultiValue = null;
63         private Hashtable paramIsTextAreaValue = null;
64         private ReportFormFields rff = null;
65
66         private Hashtable multiValueBaseSQL = null;
67
68         public ReportParamValuesForPDFExcel() {
69                 super();
70                 paramIsMultiValue = new Hashtable();
71                 paramIsTextAreaValue = new Hashtable();
72                 multiValueBaseSQL = new Hashtable();
73         } // ReportParamValues
74
75         public ReportParamValuesForPDFExcel(ReportFormFields rff, String reportDefType) {
76                 this();
77                 this.rff = rff;
78                 for (Iterator iter = rff.iterator(); iter.hasNext();) {
79                         FormField ff = (FormField) iter.next();
80                         
81                         put(ff.getFieldName(), nvl(ff.getDefaultValue()));
82
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());
91                 } // for
92         } // ReportParamValues
93
94         /*
95          * public ReportParamValues(ReportFormFields rff, HttpServletRequest
96          * request) { this(rff);
97          * 
98          * setParamValues(request); } // ReportParamValues
99          */
100         public boolean isParameterMultiValue(String fieldName) {
101                 Boolean b = (Boolean) paramIsMultiValue.get(fieldName);
102                 return (b != null) ? b.booleanValue() : false;
103         } // isParameterMultiValue
104         
105         public boolean isParameterTextAreaValue(String fieldName) {
106                 Boolean b = (Boolean) paramIsTextAreaValue.get(fieldName);
107                 return (b != null) ? b.booleanValue() : false;
108         } // isParameterMultiValue      
109
110         public boolean setParamValues(HttpServletRequest request, boolean refresh) {
111                 //debugLogger.debug("ReportParamValues for PDF Excel setParamValues called " + refresh);
112            long currentTime = System.currentTimeMillis();
113                 boolean paramUpdated = false;
114        if(refresh)
115            clearValues();
116                String name = null;
117                String value = null; 
118                String value1 = "";
119                String sql = "";
120                FormField ff = null;
121                          String dbInfo = null;
122                          ReportRuntime rr = (ReportRuntime) request.getSession().getAttribute(AppConstants.SI_REPORT_RUNTIME);
123                          if(rr!=null && rr.getReportType().equals(AppConstants.RT_DASHBOARD)) {
124                                 rr = (ReportRuntime) request.getSession().getAttribute("FirstDashReport");
125                                 if(rr!=null)
126                                         rff = rr.getReportFormFields();
127                          } else if (rr == null) {
128                                         rr = (ReportRuntime) request.getSession().getAttribute("FirstDashReport");
129                                         if(rr!=null)
130                                                 rff = rr.getReportFormFields();
131                          } else {
132                                  rff = rr.getReportFormFields();
133                          }
134                          
135                          if(rr!=null) { 
136                                 dbInfo = rr.getDBInfo();
137                                 if (Utils.isNull(dbInfo)) {
138                                         dbInfo = (String) request.getSession().getAttribute("remoteDB");
139                                 }       
140                                 if(!Utils.isNull(dbInfo)){             
141                         for (Iterator iter = rff.iterator(); iter.hasNext();) {
142                                     name=""; //just added
143                                         ff = (FormField) iter.next();
144                                         //debugLogger.debug("ff.getFieldName " + ff.getFieldName() + " " + ff.getFieldDisplayName() + " " + ff.getFieldType()+ " " +ff.getBaseSQLForPDFExcel()+ " "+ rr.getParamValue(ff.getFieldName()));
145                                         if(!ff.getFieldType().equals(FormField.FFT_BLANK)) {
146                                         sql = ff.getBaseSQLForPDFExcel();
147                                         if(sql!=null && sql.trim().length()>0)
148                                                 sql = Utils.replaceInString(sql, "[LOGGED_USERID]", AppUtils.getUserID(request));
149                                         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)) {
150                                        for (Enumeration enum1 = rr.getParamKeys(); enum1.hasMoreElements();) {
151                                                name = (String) enum1.nextElement();
152                                                    value = rr.getParamValue(name);
153                                                    value = getParamValueForSQL(name, value);
154                                                    if(name.startsWith("ff")) {
155                                                                 for (Iterator iter1 = rff.iterator(); iter1.hasNext();) {
156                                                                         FormField ff1 = (FormField) iter1.next();
157                                                                         
158                                                                         if(sql!=null && sql.trim().length()>0){
159                                                                                  if(name.equals(ff.getFieldName())){
160                                                                                         sql = Utils.replaceInString(sql, "[VALUE]", ESAPI.encoder().encodeForSQL( SecurityCodecUtil.getCodec(),value));
161                                                                                  }
162                                                if(name.equals(ff1.getFieldName())){
163                                                    sql = Utils.replaceInString(sql, "["+ff1.getFieldDisplayName()+"]", ESAPI.encoder().encodeForSQL( SecurityCodecUtil.getCodec(),value));
164         
165                                                } else continue;
166                                                                         }
167                                                                 }
168                                            }
169                                        }
170                                         } else if (ff.getFieldType().equals(ff.FFT_LIST_MULTI)||ff.getFieldType().equals(ff.FFT_CHECK_BOX)) {
171                                                for (Enumeration enum1 = rr.getParamKeys(); enum1.hasMoreElements();) {
172                                                        name = (String) enum1.nextElement();
173                                                            value = rr.getParamValue(name);
174                                                            value = getParamValueForSQL(name, value);
175                                                            if(name.startsWith("ff")) {
176                                                                                 for (Iterator iter1 = rff.iterator(); iter1.hasNext();) {
177                                                                                         FormField ff1 = (FormField) iter1.next();
178                                                                                         
179                                                                                         if(sql!=null && sql.trim().length()>0){
180                                                                                                  if(name.equals(ff.getFieldName())){
181                                                                                                         sql = Utils.replaceInString(sql, "[VALUE]", ESAPI.encoder().encodeForSQL( SecurityCodecUtil.getCodec(),value));
182                                                                                                  }
183                                                                if(name.equals(ff1.getFieldName())){
184                                                                    sql = Utils.replaceInString(sql, "["+ff1.getFieldDisplayName()+"]", ESAPI.encoder().encodeForSQL( SecurityCodecUtil.getCodec(),value));
185                         
186                                                                } else continue;
187                                                                                         }
188                                                                                 }
189                                                    }
190                                                }
191                                                 } else {
192                                                         if(nvl(ff.getFieldDefaultSQL()).length()<=0)
193                                                                 sql = "";
194                                                 }
195                                          if(sql!=null && sql.trim().length()>0){
196                                                  name = "";
197                                                  if(name.length()<=0)
198                                                          name = ff.getFieldName();
199                                                  value = rr.getParamValue(name);
200                                                  //debugLogger.debug("Name "+ name+ " value:" + value);
201                                                  String paramValue = ESAPI.encoder().encodeForSQL( SecurityCodecUtil.getCodec(), getParamValueForSQL(name, value));
202                                                  //debugLogger.debug("PDFEXCEL " + name+ " " + ff.getFieldName()+ " " +  value + " " + sql +" "+ paramValue);
203                                                  if(name!=null && name.equals(ff.getFieldName()))
204                                                         sql = Utils.replaceInString(sql, "[VALUE]", paramValue);
205                                                   if(paramValue == null) {
206                                                           if(sql.lastIndexOf("where id = ''")>0) 
207                                                                   sql = sql.substring(0, sql.lastIndexOf("where id = ''"));
208                                                   }
209                                                         //debugLogger.debug("SQL Modified " + sql);
210                                                         FormField ff2 = null;
211                                                         for (Iterator iter1 = rff.iterator(); iter1.hasNext();) {
212                                                                 ff2 = (FormField)iter1.next();
213                                                                 sql = Utils.replaceInString(sql, "[" + ff2.getFieldDisplayName() +"]", ESAPI.encoder().encodeForSQL( SecurityCodecUtil.getCodec(),getParamValue(ff2.getFieldName())));
214                                                         }
215                                                         //debugLogger.debug("SQL Modified after replacing formfield" + sql);
216                                                         try {
217                                                         String[] reqParameters = Globals.getRequestParams().split(",");
218                                                         String[] sessionParameters = Globals.getSessionParams().split(",");
219                                                         String[] scheduleSessionParameters = Globals.getSessionParamsForScheduling().split(",");
220                                                         javax.servlet.http.HttpSession session = request.getSession();
221                                 //debugLogger.debug("B4 Session " + sql);                                                               
222                                                     if(session != null ) {
223                                                         for (int i = 0; i < sessionParameters.length; i++) {
224                                                               sql = Utils.replaceInString(sql, "[" + sessionParameters[i].toUpperCase()+"]", (String)session.getAttribute(sessionParameters[i]) );
225                                                         }
226                                                      }                                                                  
227                                 //debugLogger.debug("B4 request " + sql);                                                               
228                                                         if(request != null ) {
229                                                         for (int i = 0; i < scheduleSessionParameters.length; i++) {
230                                                                 sql = Utils.replaceInString(sql, "[" + scheduleSessionParameters[i].toUpperCase()+"]", ESAPI.encoder().encodeForSQL( SecurityCodecUtil.getCodec(), request.getParameter(scheduleSessionParameters[i]) ));
231                                                         }
232                                                         for (int i = 0; i < reqParameters.length; i++) {
233                                                             if(!reqParameters[i].startsWith("ff")) {
234                                                                 if (request.getParameter(reqParameters[i])!=null) {
235                                                                         sql = Utils.replaceInString(sql, "[" + reqParameters[i]+"]", request.getParameter(reqParameters[i]) );
236                                                                         sql = Utils.replaceInString(sql, "[" + reqParameters[i].toUpperCase()+"]", ESAPI.encoder().encodeForSQL( SecurityCodecUtil.getCodec(), request.getParameter(reqParameters[i]) ));
237                                                                 }
238                                                                 else {
239                                                                         sql = Utils.replaceInString(sql, "[" + reqParameters[i]+"]", request.getParameter(reqParameters[i].toUpperCase()) );
240                                                                         sql = Utils.replaceInString(sql, "[" + reqParameters[i].toUpperCase()+"]", ESAPI.encoder().encodeForSQL( SecurityCodecUtil.getCodec(), request.getParameter(reqParameters[i].toUpperCase()) ));
241                                                                 }
242                                                             }
243                                                             else
244                                                               sql = Utils.replaceInString(sql, "[" + reqParameters[i].toUpperCase()+"]", ESAPI.encoder().encodeForSQL( SecurityCodecUtil.getCodec(), request.getParameter(reqParameters[i]) ));   
245                                                         }
246                                                      }
247                                 //debugLogger.debug("After request " + sql);                                                                    
248                                                      DataSet ds = null;
249                                                      try {
250                                                          ds = ConnectionUtils.getDataSet(sql, dbInfo);
251                                                      } catch (ReportSQLException ex) {
252                                                          logger.debug(EELFLoggerDelegate.debugLogger, ("sql not complete" + sql));
253                                                          }
254                                                           if(ff.getFieldType().equals(FormField.FFT_LIST_MULTI) || ff.getFieldType().equals(FormField.FFT_CHECK_BOX)) {
255                                                          StringBuffer multiValue = new StringBuffer("");
256                                                          if(ds!=null) {
257                                                          for(int i = 0; i < ds.getRowCount(); i++) {
258                                                                  //if(i==0) multiValue.append("(");
259                                                                  multiValue.append(ds.getString(i,1));
260                                                                  if(i<ds.getRowCount()-1)
261                                                                   multiValue.append("|");
262                                                                  //else multiValue.append(")");
263                                                                  
264                                                          }
265                                                          }
266                                                           put(ff.getFieldName(), nvl(multiValue.toString()));
267                                                           } else {
268                                                                         if(ff.getValidationType().equals(FormField.VT_TIMESTAMP_HR) || ff.getValidationType().equals(FormField.VT_TIMESTAMP_MIN) || ff.getValidationType().equals(FormField.VT_TIMESTAMP_SEC) ) {
269                                                                                 value1 = nvl(rr.getParamValue(ff.getFieldName())) + " "+addZero(Utils.oracleSafe(nvl(rr
270                                                                                 .getParamValue(ff.getFieldName()+"_Hr"))));
271                                                                                 if(ff.getValidationType().equals(FormField.VT_TIMESTAMP_MIN) || ff.getValidationType().equals(FormField.VT_TIMESTAMP_SEC) ) {
272                                                                                         value1 = value1 + (nvl(rr
273                                                                                                         .getParamValue(ff.getFieldName()+"_Min")).length() > 0 ? ":" + addZero(Utils.oracleSafe(nvl(rr
274                                                                                                 .getParamValue(ff.getFieldName()+"_Min")))) : ""); 
275                                                                                 }
276                                                                                 if(ff.getValidationType().equals(FormField.VT_TIMESTAMP_SEC) ) {
277                                                                                         value1 =  value1 + (nvl(rr
278                                                                                                         .getParamValue(ff.getFieldName()+"_Sec")).length() > 0 ? ":"+ addZero(Utils.oracleSafe(nvl(rr
279                                                                                                                 .getParamValue(ff.getFieldName()+"_Sec")))) : "");
280                                                                                 }
281                                                                                 //debugLogger.debug("77777777777777 " + value1);
282                                                                                 put(ff.getFieldName(), nvl(value1));
283                                                                         } else { 
284                                                                   
285                                                                   if(ds!=null && ds.getRowCount()>0)
286                                                                           put(ff.getFieldName(), nvl(ds.getString(0,1)));
287                                                                   else put(ff.getFieldName(), nvl(value));
288                                                                         }
289                                                           }
290                                                          
291                                                           paramUpdated = true; 
292                                                         } catch (ReportSQLException ex) {
293                                                                 logger.debug(EELFLoggerDelegate.debugLogger, ("sql not complete" + sql));
294                                                         }
295                                                         catch (Exception ex) {}
296                                                         
297                                                         //debugLogger.debug("66666666666666666 " + ff.getValidationType());
298
299                                                         //Added for TimeStamp validation
300                                                         
301                                          } else {
302                                                  if(!ff.getFieldType().equals(FormField.FFT_BLANK)) {
303                                                                 //Added for TimeStamp validation
304                                                          //debugLogger.debug("666666666666 " + ff.getValidationType());
305                                                                 if(ff.getValidationType().equals(FormField.VT_TIMESTAMP_HR) || ff.getValidationType().equals(FormField.VT_TIMESTAMP_MIN) || ff.getValidationType().equals(FormField.VT_TIMESTAMP_SEC) ) {
306                                                                         value1 = nvl(rr.getParamValue(ff.getFieldName())) + " "+addZero(Utils.oracleSafe(nvl(rr
307                                                                         .getParamValue(ff.getFieldName()+"_Hr"))));
308                                                                         if(ff.getValidationType().equals(FormField.VT_TIMESTAMP_MIN) || ff.getValidationType().equals(FormField.VT_TIMESTAMP_SEC) ) {
309                                                                                 value1 = value1 + (nvl(rr
310                                                                                         .getParamValue(ff.getFieldName()+"_Min")).length() > 0 ? ":" + addZero(Utils.oracleSafe(nvl(rr
311                                                                                         .getParamValue(ff.getFieldName()+"_Min")))) : ""); 
312                                                                         }
313                                                                         if(ff.getValidationType().equals(FormField.VT_TIMESTAMP_SEC) ) {
314                                                                                 value1 =  value1 + (nvl(rr
315                                                                                         .getParamValue(ff.getFieldName()+"_Sec")).length() > 0 ? ":"+ addZero(Utils.oracleSafe(nvl(rr
316                                                                                                         .getParamValue(ff.getFieldName()+"_Sec")))) : "");
317                                                                         }
318                                                                         //debugLogger.debug("77777777777777 " + value1);
319                                                                 } else 
320                                                                      value1 = nvl(rr.getParamValue(ff.getFieldName()));
321                                                                      if(value1.length()<=0)
322                                                                                  value1 = nvl(ff.getDefaultValue());
323                                                                          put(ff.getFieldName(), nvl(value1));
324
325                                                  }
326                                                  paramUpdated = true;
327                                          }
328
329                                         } // BLANK       
330                                                 } // for
331                                 } // dbInfo
332                          } // !=null
333                          
334                         //printValues();                 
335         logger.debug(EELFLoggerDelegate.debugLogger, ("[DEBUG MESSAGE FROM RAPTOR] ------->Time Taken for Adding/Clearing Param Values for FormField Info Bar " + (System.currentTimeMillis() - currentTime)));
336         return paramUpdated;
337         } // setParamValues
338
339         public String getParamValue(String key) {
340                 if (key!=null)          
341                         return (String) get(key);
342                 else 
343                         return "NULL";
344         } // getParamValue
345
346         public String getParamValueForSQL(String key, String value) {
347                 value = Utils.oracleSafe(value);
348                 if (isParameterMultiValue(key))
349                         value = "('" + Utils.replaceInString(value, "|", "','") + "')";
350                 return value;
351         } // getParamValue
352         
353         public String getParamDisplayValue(String key) {
354                 //debugLogger.debug("Key is " + key +" Value is " + getParamValue(key));
355                 String value = getParamValue(key);
356                 if (isParameterMultiValue(key))
357                         value = "(" + Utils.replaceInString(value, "|", ",") + ")";
358                 return value;
359         } // getParamValue
360
361         public String getParamBaseSQL(String key) {
362                 return (String) multiValueBaseSQL.get(key);
363         } // getParamBaseSQL
364
365         /** ************************************************************************************************* */
366
367         private String nvl(String s) {
368                 return (s == null) ? "" : s;
369         }
370
371         private String nvl(String s, String sDefault) {
372                 return nvl(s).equals("") ? sDefault : s;
373         }
374     
375     private boolean isNull(String a) {
376         if ((a == null) || (a.length() == 0) || a.equalsIgnoreCase("null"))
377             return true;
378         else
379             return false;
380     }
381     
382     private void clearValues() {
383         FormField ff = null;
384         String defaultValue = "";
385         String defaultSQL = "";
386         String defaultQuery = "";
387         DataSet dsDefault = null; 
388         if (rff!= null) {
389                         for (Enumeration enKeys = keys(); enKeys.hasMoreElements();) {
390                             String key = (String) enKeys.nextElement();
391                                 for(rff.resetNext(); rff.hasNext(); ) {
392                                         ff = rff.getNext();
393                                         if(ff.getFieldName().equals(key)) {
394                                                 // Add default Value
395                                                 defaultValue = ff.getDefaultValue();
396                                                 defaultSQL = ff.getFieldDefaultSQL();
397                                                 if(nvl(defaultValue).length()>0) {
398                                                         put(key,ff.getDefaultValue());
399                                                 } else if(nvl(defaultSQL).length() > 0) {
400                                                         //defaultSQL = Utils.replaceInString(defaultSQL, "[LOGGED_USERID]", userId);
401                                                         if(!(isParameterMultiValue(key) || isParameterTextAreaValue(key))) {
402                                                     defaultQuery = "SELECT id, name FROM (SELECT rownum r, id, name FROM (" + defaultSQL
403                                                     + ") x "
404                                                     + ") xx ";
405                                                     try {
406                                                             dsDefault = ConnectionUtils.getDataSet(defaultQuery, ff.getDbInfo());
407                                                             if(dsDefault!=null && dsDefault.getRowCount()>0) {
408                                                                 for (int i = 0; i < dsDefault.getRowCount(); i++) {
409                                                                         put(key, dsDefault.getString(i, 1));
410                                                                 }
411                                                             }
412                                                     } catch (RaptorException ex) {}
413                                                         } else put(key, "");
414
415                                                 } else put(key,"");
416                                                 break;
417                                         }
418                                 }
419                         }
420                 }
421
422     }
423     
424     public void printValues() {
425         for (Enumeration enKeys = keys(); enKeys.hasMoreElements();) {
426             String key = (String) enKeys.nextElement();
427             String value = (String) get(key);
428             logger.debug(EELFLoggerDelegate.debugLogger, ("ReportParamValuesForPDFEXCEL " + key + "  "+ value));
429         }
430     }    
431     public String addZero(String num) {
432         int numInt = 0;
433         try {
434                 numInt = Integer.parseInt(num);
435         }catch(NumberFormatException ex){
436                 numInt = 0;
437         }
438         if(numInt < 10)
439                         return "0"+numInt;
440         else return ""+numInt;
441     }    
442
443 } // ReportParamValues
444