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