b046dabd630efee0cc0c5dc14f6ed385e5e73f63
[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
41
42
43 import java.io.Serializable;
44 import java.util.ArrayList;
45 import java.util.Arrays;
46 import java.util.Enumeration;
47 import java.util.HashMap;
48 import java.util.List;
49 import java.util.Map;
50
51 import javax.servlet.http.HttpServletRequest;
52 import javax.servlet.http.HttpSession;
53
54 import org.onap.portalsdk.analytics.error.RaptorException;
55 import org.onap.portalsdk.analytics.model.ReportHandler;
56 import org.onap.portalsdk.analytics.system.AppUtils;
57 import org.onap.portalsdk.analytics.util.AppConstants;
58 import org.onap.portalsdk.analytics.view.ReportData;
59 import org.onap.portalsdk.core.web.support.UserUtils;
60
61
62 public class ChartWebRuntime implements Serializable {
63                         
64
65             // Not used - planned to use if Hibernate used as data access layer
66                 private String runningDataQuery = "";
67                 private String runningCountQuery = "";
68                 //CONSTANTS FOR QUERY
69                 public final String QRY_COUNT_REPORT = ""; 
70                 public final String QRY_DATA_REPORT = ""; 
71                 
72                 // Not used planning to use when filter is used
73                 private StringBuffer whereClause = new StringBuffer("");
74                 // request used to grab request parameters
75                 private HttpServletRequest request;
76             
77                 
78             public ReportRuntime reportRuntime;
79             public ReportData reportData;
80                 
81                 //Used to pass user information
82             private final Map<String, Object> params = new HashMap<String, Object>();
83             
84                 //from chart generator retrieves list of charts to render
85                 public ArrayList chartList;
86                 public ArrayList infoList;
87                 
88                 private String totalSql;
89                 
90                 
91                 //
92                 private String drilldown_index = "0";
93                 
94                 public List getRolesCommaSeperated(HttpServletRequest request) {
95                         Map roles = UserUtils.getRoles(request);
96                         List roleList =  null;
97 //                      StringBuffer roleBuf = new StringBuffer("");
98                         int count = 0;
99                         if( roles != null ) {
100                                 roleList = Arrays.asList(roles.keySet().toArray());
101                         }
102                         
103                         return roleList;
104                 }
105
106             
107                 public String getUserId(HttpServletRequest request) {
108                         return AppUtils.getUserID(request);
109                 }       
110
111                 public String generateChart(HttpServletRequest request) {
112                         return generateChart(request, true);
113                 }
114                 
115                 
116                 public String generateChart(HttpServletRequest request, boolean showData) {
117                          //wire variables
118                         //processRecursive(this, this);
119                         long currentTime = System.currentTimeMillis();
120                 HttpSession session = request.getSession();
121                         String action = nvl(request.getParameter(AppConstants.RI_ACTION), request.getParameter("action"));
122                         boolean genReportData = (!action.equals("chart.json") || action.equals("chart.data.json")); 
123
124
125                 
126                 final Long user_id = new Long((long) UserUtils.getUserId(request));
127
128                 
129                 boolean adminUser = false;
130                 try {
131                 adminUser = AppUtils.isAdminUser(request) || AppUtils.isSuperUser(request);
132                 } catch (RaptorException ex) {
133                         ex.printStackTrace();
134                 }
135                 List roleList = getRolesCommaSeperated(request);
136                 //final Map<String, Object> params = new HashMap<String, Object>();
137                 params.put("user_id", user_id);
138                 params.put("role_list", roleList);
139                 //params.put("public_yn", "Y");
140                         
141                         //String action = request.getParameter(AppConstants.RI_ACTION);
142                         String reportID = AppUtils.getRequestValue(request, AppConstants.RI_REPORT_ID);
143                         
144                         ReportHandler rh = new ReportHandler();
145                         ReportRuntime rr = null;
146                         try {
147                          if(reportID !=null)    
148                                  rr = rh.loadReportRuntime(request, reportID, true, 1);         
149                                  if(rr.getReportType().equals(AppConstants.RT_HIVE)) {
150                                         String sql = rr.getReportSQL();
151                                         rr.setWholeSQL(sql);
152                                         //if(genReportData)
153                                         //reportData = rr.loadHiveLinearReportData(rr.getWholeSQL(),user_id.toString(), 10000,request);
154                                  } else {
155                                         if(genReportData)
156                                          reportData             = rr.loadReportData(0, user_id.toString(), 10000,request, false /*download*/);
157                                  }
158                         } catch (RaptorException ex) {
159                                 ex.printStackTrace();
160                         }
161                 setReportRuntime(rr);
162                 setReportData( reportData);
163
164                 reportRuntime = getReportRuntime();
165                 reportData = getReportData();
166
167                 
168                         HashMap<String, String> chartOptionsMap = new HashMap<String, String>();
169                         
170                         String rotateLabelsStr = "";
171                         rotateLabelsStr = AppUtils.nvl(reportRuntime.getLegendLabelAngle());
172                         if(rotateLabelsStr.toLowerCase().equals("standard")) {
173                                 rotateLabelsStr = "0";
174                         } else if (rotateLabelsStr.toLowerCase().equals("up45")) {
175                                 rotateLabelsStr = "45";
176                         } else if (rotateLabelsStr.toLowerCase().equals("down45")) {
177                                 rotateLabelsStr = "-45";
178                         } else if (rotateLabelsStr.toLowerCase().equals("up90")) {
179                                 rotateLabelsStr = "90";
180                         } else if (rotateLabelsStr.toLowerCase().equals("down90")) {
181                                 rotateLabelsStr = "-90";
182                         } else
183                                 rotateLabelsStr = "0";
184                         
185                         String width                                                    = (AppUtils.getRequestNvlValue(request, "width").length()>0?AppUtils.getRequestNvlValue(request, "width"):(AppUtils.nvl(reportRuntime.getChartWidth()).length()>0?reportRuntime.getChartWidth():"700"));
186                         String height                                                   = (AppUtils.getRequestNvlValue(request, "height").length()>0?AppUtils.getRequestNvlValue(request, "height"):(AppUtils.nvl(reportRuntime.getChartHeight()).length()>0?reportRuntime.getChartHeight():"300"));
187                         String animationStr                                     = (AppUtils.getRequestNvlValue(request, "animation").length()>0?AppUtils.getRequestNvlValue(request, "animation"):new Boolean(reportRuntime.isAnimateAnimatedChart()).toString());
188                         
189                         String rotateLabels                                     = (AppUtils.getRequestNvlValue(request, "rotateLabels").length()>0?AppUtils.getRequestNvlValue(request, "rotateLabels"):(rotateLabelsStr.length()>0?rotateLabelsStr:"0"));
190                         String staggerLabelsStr                                 = (AppUtils.getRequestNvlValue(request, "staggerLabels").length()>0?AppUtils.getRequestNvlValue(request, "staggerLabels"):"false");
191                         String showMaxMinStr                                    = (AppUtils.getRequestNvlValue(request, "showMaxMin").length()>0?AppUtils.getRequestNvlValue(request, "showMaxMin"):"false");
192                         String showControlsStr                                  = (AppUtils.getRequestNvlValue(request, "showControls").length()>0?AppUtils.getRequestNvlValue(request, "showControls"):new Boolean(reportRuntime.displayBarControls()).toString());
193                         String showLegendStr                                    = (AppUtils.getRequestNvlValue(request, "showLegend").length()>0?AppUtils.getRequestNvlValue(request, "showLegend"):new Boolean(!new Boolean(reportRuntime.hideChartLegend())).toString()); 
194                         String topMarginStr                                     = AppUtils.getRequestNvlValue(request, "topMargin");
195                         String topMargin                                                = (AppUtils.nvl(topMarginStr).length()<=0)?(reportRuntime.getTopMargin()!=null?reportRuntime.getTopMargin().toString():"30"):topMarginStr;
196                         String bottomMarginStr                                  = AppUtils.getRequestNvlValue(request, "bottomMargin");
197                         String bottomMargin                                     = (AppUtils.nvl(bottomMarginStr).length()<=0)?(reportRuntime.getBottomMargin()!=null?reportRuntime.getBottomMargin().toString():"50"):bottomMarginStr;
198                         String leftMarginStr                                    = AppUtils.getRequestNvlValue(request, "leftMargin");
199                         String leftMargin                                               = (AppUtils.nvl(leftMarginStr).length()<=0)?(reportRuntime.getLeftMargin()!=null?reportRuntime.getLeftMargin().toString():"100"):leftMarginStr;
200                         String rightMarginStr                                   = AppUtils.getRequestNvlValue(request, "rightMargin");
201                         String rightMargin                                              = (AppUtils.nvl(rightMarginStr).length()<=0)?(reportRuntime.getRightMargin()!=null?reportRuntime.getRightMargin().toString():"160"):rightMarginStr;
202                         String showTitleStr                                     = (AppUtils.getRequestNvlValue(request, "showTitle").length()>0?AppUtils.getRequestNvlValue(request, "showTitle"):new Boolean(reportRuntime.displayChartTitle()).toString()); 
203                         String subType                                                  = AppUtils.getRequestNvlValue(request, "subType").length()>0?AppUtils.getRequestNvlValue(request, "subType"):(AppUtils.nvl(reportRuntime.getTimeSeriesRender()).equals("area")?reportRuntime.getTimeSeriesRender():"");
204                         String stackedStr                                               = AppUtils.getRequestNvlValue(request, "stacked").length()>0?AppUtils.getRequestNvlValue(request, "stacked"):new Boolean(reportRuntime.isChartStacked()).toString();
205                         String horizontalBar                                    = AppUtils.getRequestNvlValue(request, "horizontalBar").length()>0?AppUtils.getRequestNvlValue(request, "horizontalBar"):new Boolean(reportRuntime.isHorizontalOrientation()).toString();
206                         String barRealTimeAxis                                  = AppUtils.getRequestNvlValue(request, "barRealTimeAxis");
207                         String barReduceXAxisLabels                             = AppUtils.getRequestNvlValue(request, "barReduceXAxisLabels").length()>0?AppUtils.getRequestNvlValue(request, "barReduceXAxisLabels"):new Boolean(reportRuntime.isLessXaxisTickers()).toString();;
208                         String timeAxis                                                 = AppUtils.getRequestNvlValue(request, "timeAxis").length()>0?AppUtils.getRequestNvlValue(request, "timeAxis"):new Boolean(reportRuntime.isTimeAxis()).toString();
209                         String logScale                                                 = AppUtils.getRequestNvlValue(request, "logScale").length()>0?AppUtils.getRequestNvlValue(request, "logScale"):new Boolean(reportRuntime.isLogScale()).toString();
210                         String precision                                                = AppUtils.getRequestNvlValue(request, "precision").length()>0?AppUtils.getRequestNvlValue(request, "precision"):"2";
211
212         /*              boolean animation = AppUtils.getRequestFlag(request, "animation");
213                         boolean staggerLabels = AppUtils.getRequestFlag(request, "staggerLabels");
214                         boolean showMaxMin = (showMaxMinStr.length()<=0)?false:Boolean.parseBoolean(showMaxMinStr);
215                         boolean showControls = (showControlsStr.length()<=0)?true:Boolean.parseBoolean(showControlsStr);
216                         boolean showLegend = (showLegendStr.length()<=0)?true:Boolean.parseBoolean(showLegendStr);
217                         boolean showTitle = (showTitleStr.length()<=0)?true:Boolean.parseBoolean(showTitleStr);
218                         boolean stacked = (stackedStr.length()<=0)?true:Boolean.parseBoolean(stackedStr);
219         */              
220                         // Add all options to Map
221                         chartOptionsMap.put("width", width);
222                         chartOptionsMap.put("height", height);
223                         chartOptionsMap.put("animation", animationStr);
224                         chartOptionsMap.put("rotateLabels", rotateLabels);
225                         chartOptionsMap.put("staggerLabels", staggerLabelsStr);
226                         chartOptionsMap.put("showMaxMin", showMaxMinStr);
227                         chartOptionsMap.put("showControls", showControlsStr);
228                         chartOptionsMap.put("showLegend", showLegendStr);
229                         chartOptionsMap.put("topMargin", topMargin);
230                         chartOptionsMap.put("bottomMargin", bottomMargin);
231                         chartOptionsMap.put("leftMargin", leftMargin);
232                         chartOptionsMap.put("rightMargin", rightMargin);
233                         chartOptionsMap.put("showTitle", showTitleStr);
234                         chartOptionsMap.put("subType", subType);
235                         chartOptionsMap.put("stacked", stackedStr);
236                         chartOptionsMap.put("horizontalBar", horizontalBar);
237                         chartOptionsMap.put("timeAxis", timeAxis);
238                         chartOptionsMap.put("barRealTimeAxis", barRealTimeAxis);
239                         chartOptionsMap.put("barReduceXAxisLabels", barReduceXAxisLabels);
240                         
241                         chartOptionsMap.put("logScale", logScale);
242                         chartOptionsMap.put("precision", precision);
243                         
244
245                 
246                 if(reportRuntime!=null) {
247                         StringBuffer title = new StringBuffer("");
248                         title.append(reportRuntime.getReportName());
249                 }
250                 
251                 if(! (action.equals("chart.json") || action.equals("chart.data.json"))) {
252                         
253                                 
254                     //Chart
255                                 String chartType = reportRuntime.getChartType();
256                                 return drawD3Charts(chartOptionsMap, request);
257                                 //drawD3Charts();
258                 } else /*if (action.equals("chart.json"))*/ {
259                                 String chartType = reportRuntime.getChartType();
260                                 return returnChartJSON(chartOptionsMap, request, showData);
261
262                                 
263                 } /*else {
264                         
265                         return ("Internal Error Occurred.");
266                 }*/
267                 
268                 }
269                 
270                 
271                 public String nvl(String s) {
272                         return (s == null) ? "" : s;
273                 }
274
275                 /**
276                  * @return the reportRuntime
277                  */
278                 public ReportRuntime getReportRuntime() {
279                         return reportRuntime;
280                 }
281
282                 /**
283                  * @param reportRuntime the reportRuntime to set
284                  */
285                 public void setReportRuntime(ReportRuntime reportRuntime) {
286                         this.reportRuntime = reportRuntime;
287                 } 
288
289                 /**
290                  * @return the reportData
291                  */
292                 public ReportData getReportData() {
293                         return reportData;
294                 }
295
296                 /**
297                  * @param reportData the reportData to set
298                  */
299                 public void setReportData(ReportData reportData) {
300                         this.reportData = reportData;
301                 }       
302
303                 public boolean isNull(String a) {
304                         if ((a == null) || (a.length() == 0) || a.equalsIgnoreCase("null"))
305                                 return true;
306                         else
307                                 return false;
308                 }
309                 
310
311                 protected String nvl(String s, String sDefault) {
312                         return nvl(s).equals("") ? sDefault : s;
313                 }
314
315                 protected static String nvls(String s) {
316                         return (s == null) ? "" : s;
317                 }
318
319                 protected static String nvls(String s, String sDefault) {
320                         return nvls(s).equals("") ? sDefault : s;
321                 }
322                 
323                 protected boolean getFlagInBoolean(String s) {
324                         return nvl(s).toUpperCase().startsWith("Y") || nvl(s).toLowerCase().equals("true");
325                 }
326                 
327             
328                 /**
329                  * @return the chartList
330                  */
331                 public ArrayList getChartList() {
332                         return chartList;
333                 }
334
335                 /**
336                  * @param chartList the chartList to set
337                  */
338                 public void setChartList(ArrayList chartList) {
339                         this.chartList = chartList;
340                 }
341
342                 /**
343                  * @return the infoList
344                  */
345                 public ArrayList getInfoList() {
346                         return infoList;
347                 }
348
349                 /**
350                  * @param infoList the infoList to set
351                  */
352                 public void setInfoList(ArrayList infoList) {
353                         this.infoList = infoList;
354                 }
355
356                 
357                 
358                 private void clearReportRuntimeBackup(HttpSession session, HttpServletRequest request) {
359                         session.removeAttribute(AppConstants.DRILLDOWN_REPORTS_LIST);
360                         request.removeAttribute(AppConstants.DRILLDOWN_INDEX);
361                         session.removeAttribute(AppConstants.DRILLDOWN_INDEX);
362                         request.removeAttribute(AppConstants.FORM_DRILLDOWN_INDEX);
363                         session.removeAttribute(AppConstants.FORM_DRILLDOWN_INDEX);
364                         Enumeration<String> enum1 = session.getAttributeNames();
365                         String attributeName = "";
366                         while(enum1.hasMoreElements()) {
367                                 attributeName = enum1.nextElement();
368                                 if(attributeName.startsWith("parent_")) {
369                                         session.removeAttribute(attributeName);
370                                 }
371                         }
372                 session.removeAttribute(AppConstants.DRILLDOWN_REPORTS_LIST);
373                         session.removeAttribute(AppConstants.SI_BACKUP_FOR_REP_ID);
374                         session.removeAttribute(AppConstants.SI_COLUMN_LOOKUP);
375                 session.removeAttribute(AppConstants.SI_DASHBOARD_REP_ID);
376                 session.removeAttribute(AppConstants.SI_DASHBOARD_REPORTRUNTIME_MAP);
377                 session.removeAttribute(AppConstants.SI_DASHBOARD_REPORTRUNTIME);
378                 session.removeAttribute(AppConstants.SI_DASHBOARD_REPORTDATA_MAP);
379                 session.removeAttribute(AppConstants.SI_DASHBOARD_CHARTDATA_MAP);
380                         session.removeAttribute(AppConstants.SI_DASHBOARD_DISPLAYTYPE_MAP);
381                 session.removeAttribute(AppConstants.SI_DATA_SIZE_FOR_TEXTFIELD_POPUP);
382                 session.removeAttribute(AppConstants.SI_MAP);
383                         session.removeAttribute(AppConstants.SI_MAP_OBJECT);
384                 session.removeAttribute(AppConstants.SI_REPORT_DEFINITION);                     
385                 session.removeAttribute(AppConstants.SI_REPORT_RUNTIME);                        
386                         session.removeAttribute(AppConstants.SI_REPORT_RUN_BACKUP);
387                 session.removeAttribute(AppConstants.SI_REPORT_SCHEDULE);
388                 session.removeAttribute(AppConstants.RI_REPORT_DATA);
389                 session.removeAttribute(AppConstants.RI_CHART_DATA);
390                 session.removeAttribute(AppConstants.SI_FORMFIELD_INFO);
391                 session.removeAttribute(AppConstants.SI_FORMFIELD_DOWNLOAD_INFO);
392                 } // clearReportRuntimeBackup
393
394
395                 public String getTotalSql() {
396                         return totalSql;
397                 }
398
399                 public void setTotalSql(String totalSql) {
400                         this.totalSql = totalSql;
401                 }
402                 
403                 
404          
405         /*    public void drawD3Charts(HashMap<String,String> chartOptionsMap) {
406                 drawD3Charts(chartOptionsMap);
407
408             }
409         */    
410             
411             public String drawD3Charts(HashMap<String,String> chartOptionsMap, HttpServletRequest request) {
412                         
413                 ChartD3Helper chartHelper = new ChartD3Helper(reportRuntime);
414                 chartHelper.setChartType(reportRuntime.getChartType());
415                         try {
416                                 return chartHelper.createVisualization(reportRuntime, chartOptionsMap, request);
417                         } catch(RaptorException ex) {
418                                 ex.printStackTrace();
419                         }
420                         return "";
421                
422             }
423                 
424             public String returnChartJSON(HashMap<String,String> chartOptionsMap, HttpServletRequest request, boolean showData) {
425                         
426                 ChartJSONHelper chartJSONHelper = new ChartJSONHelper(reportRuntime);
427                 chartJSONHelper.setChartType(reportRuntime.getChartType());
428                         try {
429                                 return chartJSONHelper.generateJSON(reportRuntime, chartOptionsMap, request, showData);
430                         } catch(RaptorException ex) {
431                                 ex.printStackTrace();
432                         }
433                         return "";
434                
435             }
436             
437         }       
438