d5911cbb22bc9613f1c8f53540d0c11dab59a7b2
[portal/sdk.git] /
1 /*
2  * ============LICENSE_START==========================================
3  * ONAP Portal SDK
4  * ===================================================================
5  * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6  * ===================================================================
7  *
8  * Unless otherwise specified, all software contained herein is licensed
9  * under the Apache License, Version 2.0 (the "License");
10  * you may not use this software except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *             http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  *
21  * Unless otherwise specified, all documentation contained herein is licensed
22  * under the Creative Commons License, Attribution 4.0 Intl. (the "License");
23  * you may not use this documentation except in compliance with the License.
24  * You may obtain a copy of the License at
25  *
26  *             https://creativecommons.org/licenses/by/4.0/
27  *
28  * Unless required by applicable law or agreed to in writing, documentation
29  * distributed under the License is distributed on an "AS IS" BASIS,
30  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31  * See the License for the specific language governing permissions and
32  * limitations under the License.
33  *
34  * ============LICENSE_END============================================
35  *
36  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
37  */
38 package org.onap.portalsdk.analytics.model.runtime;
39
40 import java.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
56 public class ReportParamValuesForPDFExcel extends Hashtable {
57         
58         private static final EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(ReportParamValuesForPDFExcel.class);
59
60         private Hashtable paramIsMultiValue = null;
61         private Hashtable paramIsTextAreaValue = null;
62         private ReportFormFields rff = null;
63
64         private Hashtable multiValueBaseSQL = null;
65
66         public ReportParamValuesForPDFExcel() {
67                 super();
68                 paramIsMultiValue = new Hashtable();
69                 paramIsTextAreaValue = new Hashtable();
70                 multiValueBaseSQL = new Hashtable();
71         } // ReportParamValues
72
73         public ReportParamValuesForPDFExcel(ReportFormFields rff, String reportDefType) {
74                 this();
75                 this.rff = rff;
76                 for (Iterator iter = rff.iterator(); iter.hasNext();) {
77                         FormField ff = (FormField) iter.next();
78                         
79                         put(ff.getFieldName(), nvl(ff.getDefaultValue()));
80
81                         boolean isMultiValue = ff.getFieldType().equals(FormField.FFT_CHECK_BOX)
82                                         || ff.getFieldType().equals(FormField.FFT_LIST_MULTI);
83                         boolean isTextAreaValue = ff.getFieldType().equals(FormField.FFT_TEXTAREA) && reportDefType
84                                                         .equals(AppConstants.RD_SQL_BASED);
85                         paramIsMultiValue.put(ff.getFieldName(), new Boolean(isMultiValue));
86                         paramIsTextAreaValue.put(ff.getFieldName(), new Boolean(isTextAreaValue));
87                         if (isMultiValue && ff.getBaseSQL() != null)
88                                 multiValueBaseSQL.put(ff.getFieldName(), ff.getBaseSQL());
89                 } // for
90         } // ReportParamValues
91
92         /*
93          * public ReportParamValues(ReportFormFields rff, HttpServletRequest
94          * request) { this(rff);
95          * 
96          * setParamValues(request); } // ReportParamValues
97          */
98         public boolean isParameterMultiValue(String fieldName) {
99                 Boolean b = (Boolean) paramIsMultiValue.get(fieldName);
100                 return (b != null) ? b.booleanValue() : false;
101         } // isParameterMultiValue
102         
103         public boolean isParameterTextAreaValue(String fieldName) {
104                 Boolean b = (Boolean) paramIsTextAreaValue.get(fieldName);
105                 return (b != null) ? b.booleanValue() : false;
106         } // isParameterMultiValue      
107
108         public boolean setParamValues(HttpServletRequest request, boolean refresh) {
109                 //debugLogger.debug("ReportParamValues for PDF Excel setParamValues called " + refresh);
110            long currentTime = System.currentTimeMillis();
111                 boolean paramUpdated = false;
112        if(refresh) clearValues();
113                String name = null;
114                String value = null; 
115                String value1 = "";
116                String sql = "";
117                FormField ff = null;
118                          String dbInfo = null;
119                          ReportRuntime rr = (ReportRuntime) request.getSession().getAttribute(AppConstants.SI_REPORT_RUNTIME);
120                          if(rr!=null && rr.getReportType().equals(AppConstants.RT_DASHBOARD)) {
121                                 rr = (ReportRuntime) request.getSession().getAttribute("FirstDashReport");
122                                 if(rr!=null)
123                                         rff = rr.getReportFormFields();
124                          } else if (rr == null) {
125                                         rr = (ReportRuntime) request.getSession().getAttribute("FirstDashReport");
126                                         if(rr!=null)
127                                                 rff = rr.getReportFormFields();
128                          } else {
129                                  rff = rr.getReportFormFields();
130                          }
131                          
132                          if(rr!=null) { 
133                                 dbInfo = rr.getDBInfo();
134                                 if (Utils.isNull(dbInfo)) {
135                                         dbInfo = (String) request.getSession().getAttribute("remoteDB");
136                                 }       
137                                 if(!Utils.isNull(dbInfo)){             
138                         for (Iterator iter = rff.iterator(); iter.hasNext();) {
139                                     name=""; //just added
140                                         ff = (FormField) iter.next();
141                                         //debugLogger.debug("ff.getFieldName " + ff.getFieldName() + " " + ff.getFieldDisplayName() + " " + ff.getFieldType()+ " " +ff.getBaseSQLForPDFExcel()+ " "+ rr.getParamValue(ff.getFieldName()));
142                                         if(!ff.getFieldType().equals(FormField.FFT_BLANK)) {
143                                         sql = ff.getBaseSQLForPDFExcel();
144                                         if(sql!=null && sql.trim().length()>0)
145                                                 sql = Utils.replaceInString(sql, "[LOGGED_USERID]", AppUtils.getUserID(request));
146                                         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)) {
147                                        for (Enumeration enum1 = rr.getParamKeys(); enum1.hasMoreElements();) {
148                                                name = (String) enum1.nextElement();
149                                                    value = rr.getParamValue(name);
150                                                    value = getParamValueForSQL(name, value);
151                                                    if(name.startsWith("ff")) {
152                                                                 for (Iterator iter1 = rff.iterator(); iter1.hasNext();) {
153                                                                         FormField ff1 = (FormField) iter1.next();
154                                                                         
155                                                                         if(sql!=null && sql.trim().length()>0){
156                                                                                  if(name.equals(ff.getFieldName())){
157                                                                                         sql = Utils.replaceInString(sql, "[VALUE]", value);
158                                                                                  }
159                                                if(name.equals(ff1.getFieldName())){
160                                                    sql = Utils.replaceInString(sql, "["+ff1.getFieldDisplayName()+"]", value);
161         
162                                                } else continue;
163                                                                         }
164                                                                 }
165                                            }
166                                        }
167                                         } else if (ff.getFieldType().equals(ff.FFT_LIST_MULTI)||ff.getFieldType().equals(ff.FFT_CHECK_BOX)) {
168                                                for (Enumeration enum1 = rr.getParamKeys(); enum1.hasMoreElements();) {
169                                                        name = (String) enum1.nextElement();
170                                                            value = rr.getParamValue(name);
171                                                            value = getParamValueForSQL(name, value);
172                                                            if(name.startsWith("ff")) {
173                                                                                 for (Iterator iter1 = rff.iterator(); iter1.hasNext();) {
174                                                                                         FormField ff1 = (FormField) iter1.next();
175                                                                                         
176                                                                                         if(sql!=null && sql.trim().length()>0){
177                                                                                                  if(name.equals(ff.getFieldName())){
178                                                                                                         sql = Utils.replaceInString(sql, "[VALUE]", value);
179                                                                                                  }
180                                                                if(name.equals(ff1.getFieldName())){
181                                                                    sql = Utils.replaceInString(sql, "["+ff1.getFieldDisplayName()+"]", value);
182                         
183                                                                } else continue;
184                                                                                         }
185                                                                                 }
186                                                    }
187                                                }
188                                                 } else {
189                                                         if(nvl(ff.getFieldDefaultSQL()).length()<=0)
190                                                                 sql = "";
191                                                 }
192                                          if(sql!=null && sql.trim().length()>0){
193                                                  name = "";
194                                                  if(name.length()<=0) name = ff.getFieldName();
195                                                  value = rr.getParamValue(name);
196                                                  //debugLogger.debug("Name "+ name+ " value:" + value);
197                                                  String paramValue = getParamValueForSQL(name, value);
198                                                  //debugLogger.debug("PDFEXCEL " + name+ " " + ff.getFieldName()+ " " +  value + " " + sql +" "+ paramValue);
199                                                  if(name!=null && name.equals(ff.getFieldName()))
200                                                         sql = Utils.replaceInString(sql, "[VALUE]", paramValue);
201                                                   if(paramValue == null) {
202                                                           if(sql.lastIndexOf("where id = ''")>0) 
203                                                                   sql = sql.substring(0, sql.lastIndexOf("where id = ''"));
204                                                   }
205                                                         //debugLogger.debug("SQL Modified " + sql);
206                                                         FormField ff2 = null;
207                                                         for (Iterator iter1 = rff.iterator(); iter1.hasNext();) {
208                                                                 ff2 = (FormField)iter1.next();
209                                                                 sql = Utils.replaceInString(sql, "[" + ff2.getFieldDisplayName() +"]", getParamValue(ff2.getFieldName()));
210                                                         }
211                                                         //debugLogger.debug("SQL Modified after replacing formfield" + sql);
212                                                         try {
213                                                         String[] reqParameters = Globals.getRequestParams().split(",");
214                                                         String[] sessionParameters = Globals.getSessionParams().split(",");
215                                                         String[] scheduleSessionParameters = Globals.getSessionParamsForScheduling().split(",");
216                                                         javax.servlet.http.HttpSession session = request.getSession();
217                                 //debugLogger.debug("B4 Session " + sql);                                                               
218                                                     if(session != null ) {
219                                                         for (int i = 0; i < sessionParameters.length; i++) {
220                                                               sql = Utils.replaceInString(sql, "[" + sessionParameters[i].toUpperCase()+"]", (String)session.getAttribute(sessionParameters[i]) );
221                                                         }
222                                                      }                                                                  
223                                 //debugLogger.debug("B4 request " + sql);                                                               
224                                                         if(request != null ) {
225                                                         for (int i = 0; i < scheduleSessionParameters.length; i++) {
226                                                                 sql = Utils.replaceInString(sql, "[" + scheduleSessionParameters[i].toUpperCase()+"]", request.getParameter(scheduleSessionParameters[i]) );
227                                                         }
228                                                         for (int i = 0; i < reqParameters.length; i++) {
229                                                             if(!reqParameters[i].startsWith("ff")) {
230                                                                 if (request.getParameter(reqParameters[i])!=null) {
231                                                                         sql = Utils.replaceInString(sql, "[" + reqParameters[i]+"]", request.getParameter(reqParameters[i]) );
232                                                                         sql = Utils.replaceInString(sql, "[" + reqParameters[i].toUpperCase()+"]", request.getParameter(reqParameters[i]) );
233                                                                 }
234                                                                 else {
235                                                                         sql = Utils.replaceInString(sql, "[" + reqParameters[i]+"]", request.getParameter(reqParameters[i].toUpperCase()) );
236                                                                         sql = Utils.replaceInString(sql, "[" + reqParameters[i].toUpperCase()+"]", request.getParameter(reqParameters[i].toUpperCase()) );
237                                                                 }
238                                                             }
239                                                             else
240                                                               sql = Utils.replaceInString(sql, "[" + reqParameters[i].toUpperCase()+"]", request.getParameter(reqParameters[i]) );   
241                                                         }
242                                                      }
243                                 //debugLogger.debug("After request " + sql);                                                                    
244                                                      DataSet ds = null;
245                                                      try {
246                                                          ds = ConnectionUtils.getDataSet(sql, dbInfo);
247                                                      } catch (ReportSQLException ex) {
248                                                          logger.debug(EELFLoggerDelegate.debugLogger, ("sql not complete" + sql));
249                                                          }
250                                                           if(ff.getFieldType().equals(FormField.FFT_LIST_MULTI) || ff.getFieldType().equals(FormField.FFT_CHECK_BOX)) {
251                                                          StringBuffer multiValue = new StringBuffer("");
252                                                          if(ds!=null) {
253                                                          for(int i = 0; i < ds.getRowCount(); i++) {
254                                                                  //if(i==0) multiValue.append("(");
255                                                                  multiValue.append(ds.getString(i,1));
256                                                                  if(i<ds.getRowCount()-1)
257                                                                   multiValue.append("|");
258                                                                  //else multiValue.append(")");
259                                                                  
260                                                          }
261                                                          }
262                                                           put(ff.getFieldName(), nvl(multiValue.toString()));
263                                                           } else {
264                                                                         if(ff.getValidationType().equals(FormField.VT_TIMESTAMP_HR) || ff.getValidationType().equals(FormField.VT_TIMESTAMP_MIN) || ff.getValidationType().equals(FormField.VT_TIMESTAMP_SEC) ) {
265                                                                                 value1 = nvl(rr.getParamValue(ff.getFieldName())) + " "+addZero(Utils.oracleSafe(nvl(rr
266                                                                                 .getParamValue(ff.getFieldName()+"_Hr"))));
267                                                                                 if(ff.getValidationType().equals(FormField.VT_TIMESTAMP_MIN) || ff.getValidationType().equals(FormField.VT_TIMESTAMP_SEC) ) {
268                                                                                         value1 = value1 + (nvl(rr
269                                                                                                         .getParamValue(ff.getFieldName()+"_Min")).length() > 0 ? ":" + addZero(Utils.oracleSafe(nvl(rr
270                                                                                                 .getParamValue(ff.getFieldName()+"_Min")))) : ""); 
271                                                                                 }
272                                                                                 if(ff.getValidationType().equals(FormField.VT_TIMESTAMP_SEC) ) {
273                                                                                         value1 =  value1 + (nvl(rr
274                                                                                                         .getParamValue(ff.getFieldName()+"_Sec")).length() > 0 ? ":"+ addZero(Utils.oracleSafe(nvl(rr
275                                                                                                                 .getParamValue(ff.getFieldName()+"_Sec")))) : "");
276                                                                                 }
277                                                                                 //debugLogger.debug("77777777777777 " + value1);
278                                                                                 put(ff.getFieldName(), nvl(value1));
279                                                                         } else { 
280                                                                   
281                                                                   if(ds!=null && ds.getRowCount()>0) put(ff.getFieldName(), nvl(ds.getString(0,1)));
282                                                                   else put(ff.getFieldName(), nvl(value));
283                                                                         }
284                                                           }
285                                                          
286                                                           paramUpdated = true; 
287                                                         } catch (ReportSQLException ex) {
288                                                                 logger.debug(EELFLoggerDelegate.debugLogger, ("sql not complete" + sql));
289                                                         }
290                                                         catch (Exception ex) {}
291                                                         
292                                                         //debugLogger.debug("66666666666666666 " + ff.getValidationType());
293
294                                                         //Added for TimeStamp validation
295                                                         
296                                          } else {
297                                                  if(!ff.getFieldType().equals(FormField.FFT_BLANK)) {
298                                                                 //Added for TimeStamp validation
299                                                          //debugLogger.debug("666666666666 " + ff.getValidationType());
300                                                                 if(ff.getValidationType().equals(FormField.VT_TIMESTAMP_HR) || ff.getValidationType().equals(FormField.VT_TIMESTAMP_MIN) || ff.getValidationType().equals(FormField.VT_TIMESTAMP_SEC) ) {
301                                                                         value1 = nvl(rr.getParamValue(ff.getFieldName())) + " "+addZero(Utils.oracleSafe(nvl(rr
302                                                                         .getParamValue(ff.getFieldName()+"_Hr"))));
303                                                                         if(ff.getValidationType().equals(FormField.VT_TIMESTAMP_MIN) || ff.getValidationType().equals(FormField.VT_TIMESTAMP_SEC) ) {
304                                                                                 value1 = value1 + (nvl(rr
305                                                                                         .getParamValue(ff.getFieldName()+"_Min")).length() > 0 ? ":" + addZero(Utils.oracleSafe(nvl(rr
306                                                                                         .getParamValue(ff.getFieldName()+"_Min")))) : ""); 
307                                                                         }
308                                                                         if(ff.getValidationType().equals(FormField.VT_TIMESTAMP_SEC) ) {
309                                                                                 value1 =  value1 + (nvl(rr
310                                                                                         .getParamValue(ff.getFieldName()+"_Sec")).length() > 0 ? ":"+ addZero(Utils.oracleSafe(nvl(rr
311                                                                                                         .getParamValue(ff.getFieldName()+"_Sec")))) : "");
312                                                                         }
313                                                                         //debugLogger.debug("77777777777777 " + value1);
314                                                                 } else 
315                                                                      value1 = nvl(rr.getParamValue(ff.getFieldName()));
316                                                                      if(value1.length()<=0) value1 = nvl(ff.getDefaultValue());
317                                                                          put(ff.getFieldName(), nvl(value1));
318
319                                                  }
320                                                  paramUpdated = true;
321                                          }
322
323                                         } // BLANK       
324                                                 } // for
325                                 } // dbInfo
326                          } // !=null
327                          
328                         //printValues();                 
329         logger.debug(EELFLoggerDelegate.debugLogger, ("[DEBUG MESSAGE FROM RAPTOR] ------->Time Taken for Adding/Clearing Param Values for FormField Info Bar " + (System.currentTimeMillis() - currentTime)));
330         return paramUpdated;
331         } // setParamValues
332
333         public String getParamValue(String key) {
334                 if (key!=null)          
335                         return (String) get(key);
336                 else 
337                         return "NULL";
338         } // getParamValue
339
340         public String getParamValueForSQL(String key, String value) {
341                 value = Utils.oracleSafe(value);
342                 if (isParameterMultiValue(key))
343                         value = "('" + Utils.replaceInString(value, "|", "','") + "')";
344                 return value;
345         } // getParamValue
346         
347         public String getParamDisplayValue(String key) {
348                 //debugLogger.debug("Key is " + key +" Value is " + getParamValue(key));
349                 String value = getParamValue(key);
350                 if (isParameterMultiValue(key))
351                         value = "(" + Utils.replaceInString(value, "|", ",") + ")";
352                 return value;
353         } // getParamValue
354
355         public String getParamBaseSQL(String key) {
356                 return (String) multiValueBaseSQL.get(key);
357         } // getParamBaseSQL
358
359         /** ************************************************************************************************* */
360
361         private String nvl(String s) {
362                 return (s == null) ? "" : s;
363         }
364
365         private String nvl(String s, String sDefault) {
366                 return nvl(s).equals("") ? sDefault : s;
367         }
368     
369     private boolean isNull(String a) {
370         if ((a == null) || (a.length() == 0) || a.equalsIgnoreCase("null"))
371             return true;
372         else
373             return false;
374     }
375     
376     private void clearValues() {
377         FormField ff = null;
378         String defaultValue = "";
379         String defaultSQL = "";
380         String defaultQuery = "";
381         DataSet dsDefault = null; 
382         if (rff!= null) {
383                         for (Enumeration enKeys = keys(); enKeys.hasMoreElements();) {
384                             String key = (String) enKeys.nextElement();
385                                 for(rff.resetNext(); rff.hasNext(); ) {
386                                         ff = rff.getNext();
387                                         if(ff.getFieldName().equals(key)) {
388                                                 // Add default Value
389                                                 defaultValue = ff.getDefaultValue();
390                                                 defaultSQL = ff.getFieldDefaultSQL();
391                                                 if(nvl(defaultValue).length()>0) {
392                                                         put(key,ff.getDefaultValue());
393                                                 } else if(nvl(defaultSQL).length() > 0) {
394                                                         //defaultSQL = Utils.replaceInString(defaultSQL, "[LOGGED_USERID]", userId);
395                                                         if(!(isParameterMultiValue(key) || isParameterTextAreaValue(key))) {
396                                                     defaultQuery = "SELECT id, name FROM (SELECT rownum r, id, name FROM (" + defaultSQL
397                                                     + ") x "
398                                                     + ") xx ";
399                                                     try {
400                                                             dsDefault = ConnectionUtils.getDataSet(defaultQuery, ff.getDbInfo());
401                                                             if(dsDefault!=null && dsDefault.getRowCount()>0) {
402                                                                 for (int i = 0; i < dsDefault.getRowCount(); i++) {
403                                                                         put(key, dsDefault.getString(i, 1));
404                                                                 }
405                                                             }
406                                                     } catch (RaptorException ex) {}
407                                                         } else put(key, "");
408
409                                                 } else put(key,"");
410                                                 break;
411                                         }
412                                 }
413                         }
414                 }
415
416     }
417     
418     public void printValues() {
419         for (Enumeration enKeys = keys(); enKeys.hasMoreElements();) {
420             String key = (String) enKeys.nextElement();
421             String value = (String) get(key);
422             logger.debug(EELFLoggerDelegate.debugLogger, ("ReportParamValuesForPDFEXCEL " + key + "  "+ value));
423         }
424     }    
425     public String addZero(String num) {
426         int numInt = 0;
427         try {
428                 numInt = Integer.parseInt(num);
429         }catch(NumberFormatException ex){
430                 numInt = 0;
431         }
432         if(numInt < 10) return "0"+numInt;
433         else return ""+numInt;
434     }    
435
436 } // ReportParamValues
437