2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6 * ===================================================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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.
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
26 * https://creativecommons.org/licenses/by/4.0/
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.
34 * ============LICENSE_END============================================
36 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
38 package org.onap.portalsdk.analytics.model.runtime;
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;
51 import javax.servlet.http.HttpServletRequest;
52 import javax.servlet.http.HttpSession;
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;
62 public class ChartWebRuntime implements Serializable {
65 // Not used - planned to use if Hibernate used as data access layer
66 private String runningDataQuery = "";
67 private String runningCountQuery = "";
69 public final String QRY_COUNT_REPORT = "";
70 public final String QRY_DATA_REPORT = "";
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;
78 public ReportRuntime reportRuntime;
79 public ReportData reportData;
81 //Used to pass user information
82 private final Map<String, Object> params = new HashMap<String, Object>();
84 //from chart generator retrieves list of charts to render
85 public ArrayList chartList;
86 public ArrayList infoList;
88 private String totalSql;
92 private String drilldown_index = "0";
94 public List getRolesCommaSeperated(HttpServletRequest request) {
95 Map roles = UserUtils.getRoles(request);
97 // StringBuffer roleBuf = new StringBuffer("");
100 roleList = Arrays.asList(roles.keySet().toArray());
107 public String getUserId(HttpServletRequest request) {
108 return AppUtils.getUserID(request);
111 public String generateChart(HttpServletRequest request) {
112 return generateChart(request, true);
116 public String generateChart(HttpServletRequest request, boolean showData) {
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"));
126 final Long user_id = new Long((long) UserUtils.getUserId(request));
129 boolean adminUser = false;
131 adminUser = AppUtils.isAdminUser(request) || AppUtils.isSuperUser(request);
132 } catch (RaptorException ex) {
133 ex.printStackTrace();
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");
141 //String action = request.getParameter(AppConstants.RI_ACTION);
142 String reportID = AppUtils.getRequestValue(request, AppConstants.RI_REPORT_ID);
144 ReportHandler rh = new ReportHandler();
145 ReportRuntime rr = null;
148 rr = rh.loadReportRuntime(request, reportID, true, 1);
149 if(rr.getReportType().equals(AppConstants.RT_HIVE)) {
150 String sql = rr.getReportSQL();
153 //reportData = rr.loadHiveLinearReportData(rr.getWholeSQL(),user_id.toString(), 10000,request);
156 reportData = rr.loadReportData(0, user_id.toString(), 10000,request, false /*download*/);
158 } catch (RaptorException ex) {
159 ex.printStackTrace();
161 setReportRuntime(rr);
162 setReportData( reportData);
164 reportRuntime = getReportRuntime();
165 reportData = getReportData();
168 HashMap<String, String> chartOptionsMap = new HashMap<String, String>();
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";
183 rotateLabelsStr = "0";
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());
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";
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);
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);
241 chartOptionsMap.put("logScale", logScale);
242 chartOptionsMap.put("precision", precision);
246 if(reportRuntime!=null) {
247 StringBuffer title = new StringBuffer("");
248 title.append(reportRuntime.getReportName());
251 if(! (action.equals("chart.json") || action.equals("chart.data.json"))) {
255 String chartType = reportRuntime.getChartType();
256 return drawD3Charts(chartOptionsMap, request);
258 } else /*if (action.equals("chart.json"))*/ {
259 String chartType = reportRuntime.getChartType();
260 return returnChartJSON(chartOptionsMap, request, showData);
265 return ("Internal Error Occurred.");
271 public String nvl(String s) {
272 return (s == null) ? "" : s;
276 * @return the reportRuntime
278 public ReportRuntime getReportRuntime() {
279 return reportRuntime;
283 * @param reportRuntime the reportRuntime to set
285 public void setReportRuntime(ReportRuntime reportRuntime) {
286 this.reportRuntime = reportRuntime;
290 * @return the reportData
292 public ReportData getReportData() {
297 * @param reportData the reportData to set
299 public void setReportData(ReportData reportData) {
300 this.reportData = reportData;
303 public boolean isNull(String a) {
304 if ((a == null) || (a.length() == 0) || a.equalsIgnoreCase("null"))
311 protected String nvl(String s, String sDefault) {
312 return nvl(s).equals("") ? sDefault : s;
315 protected static String nvls(String s) {
316 return (s == null) ? "" : s;
319 protected static String nvls(String s, String sDefault) {
320 return nvls(s).equals("") ? sDefault : s;
323 protected boolean getFlagInBoolean(String s) {
324 return nvl(s).toUpperCase().startsWith("Y") || nvl(s).toLowerCase().equals("true");
329 * @return the chartList
331 public ArrayList getChartList() {
336 * @param chartList the chartList to set
338 public void setChartList(ArrayList chartList) {
339 this.chartList = chartList;
343 * @return the infoList
345 public ArrayList getInfoList() {
350 * @param infoList the infoList to set
352 public void setInfoList(ArrayList infoList) {
353 this.infoList = infoList;
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);
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
395 public String getTotalSql() {
399 public void setTotalSql(String totalSql) {
400 this.totalSql = totalSql;
405 /* public void drawD3Charts(HashMap<String,String> chartOptionsMap) {
406 drawD3Charts(chartOptionsMap);
411 public String drawD3Charts(HashMap<String,String> chartOptionsMap, HttpServletRequest request) {
413 ChartD3Helper chartHelper = new ChartD3Helper(reportRuntime);
414 chartHelper.setChartType(reportRuntime.getChartType());
416 return chartHelper.createVisualization(reportRuntime, chartOptionsMap, request);
417 } catch(RaptorException ex) {
418 ex.printStackTrace();
424 public String returnChartJSON(HashMap<String,String> chartOptionsMap, HttpServletRequest request, boolean showData) {
426 ChartJSONHelper chartJSONHelper = new ChartJSONHelper(reportRuntime);
427 chartJSONHelper.setChartType(reportRuntime.getChartType());
429 return chartJSONHelper.generateJSON(reportRuntime, chartOptionsMap, request, showData);
430 } catch(RaptorException ex) {
431 ex.printStackTrace();