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