/* * ============LICENSE_START========================================== * ONAP Portal SDK * =================================================================== * Copyright © 2017 AT&T Intellectual Property. All rights reserved. * =================================================================== * * Unless otherwise specified, all software contained herein is licensed * under the Apache License, Version 2.0 (the "License"); * you may not use this software except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * * Unless otherwise specified, all documentation contained herein is licensed * under the Creative Commons License, Attribution 4.0 Intl. (the "License"); * you may not use this documentation except in compliance with the License. * You may obtain a copy of the License at * * https://creativecommons.org/licenses/by/4.0/ * * Unless required by applicable law or agreed to in writing, documentation * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * * ============LICENSE_END============================================ * * */ package org.onap.portalsdk.analytics.model.runtime; import java.io.Serializable; import java.util.Arrays; import java.util.Enumeration; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; import org.onap.portalsdk.analytics.error.RaptorException; import org.onap.portalsdk.analytics.model.ReportHandler; import org.onap.portalsdk.analytics.system.AppUtils; import org.onap.portalsdk.analytics.util.AppConstants; import org.onap.portalsdk.analytics.view.ReportData; import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate; import org.onap.portalsdk.core.web.support.UserUtils; public class ChartWebRuntime implements Serializable { private static final EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(ChartWebRuntime.class); // CONSTANTS FOR QUERY public final String QRY_COUNT_REPORT = ""; public final String QRY_DATA_REPORT = ""; public ReportRuntime reportRuntime; public ReportData reportData; // Used to pass user information private final transient Map params = new HashMap<>(); // from chart generator retrieves list of charts to render public transient List chartList; public transient List infoList; private String totalSql; public List getRolesCommaSeperated(HttpServletRequest request) { Map roles = UserUtils.getRoles(request); List roleList = null; if (roles != null) { roleList = Arrays.asList(roles.keySet().toArray()); } return roleList; } public String getUserId(HttpServletRequest request) { return AppUtils.getUserID(request); } public String generateChart(HttpServletRequest request) { return generateChart(request, true); } public String generateChart(HttpServletRequest request, boolean showData) { System.currentTimeMillis(); request.getSession(); String action = nvl(request.getParameter(AppConstants.RI_ACTION), request.getParameter("action")); boolean genReportData = (!"chart.json".equals(action) || "chart.data.json".equals(action)); final Long user_id = Long.valueOf(UserUtils.getUserId(request)); try { if(AppUtils.isAdminUser(request) || AppUtils.isSuperUser(request)) { logger.debug(EELFLoggerDelegate.debugLogger, "Request by Admin or Super User"); } } catch (RaptorException ex) { logger.error(EELFLoggerDelegate.errorLogger, "RaptorException occurred in generateChart",ex); } List roleList = getRolesCommaSeperated(request); params.put("user_id", user_id); params.put("role_list", roleList); String reportID = AppUtils.getRequestValue(request, AppConstants.RI_REPORT_ID); ReportHandler rh = new ReportHandler(); ReportRuntime rr = null; try { if (reportID != null) rr = rh.loadReportRuntime(request, reportID, true, 1); if (rr.getReportType().equals(AppConstants.RT_HIVE)) { String sql = rr.getReportSQL(); rr.setWholeSQL(sql); } else { if (genReportData) reportData = rr.loadReportData(0, user_id.toString(), 10000, request, false /* download */); } } catch (RaptorException ex) { logger.error(EELFLoggerDelegate.errorLogger, "RaptorException occurred in generateChart",ex); } setReportRuntime(rr); setReportData(reportData); reportRuntime = getReportRuntime(); reportData = getReportData(); HashMap chartOptionsMap = new HashMap<>(); String rotateLabelsStr = ""; rotateLabelsStr = AppUtils.nvl(reportRuntime.getLegendLabelAngle()); if (rotateLabelsStr.equalsIgnoreCase("standard")) { rotateLabelsStr = "0"; } else if (rotateLabelsStr.equalsIgnoreCase("up45")) { rotateLabelsStr = "45"; } else if (rotateLabelsStr.equalsIgnoreCase("down45")) { rotateLabelsStr = "-45"; } else if (rotateLabelsStr.equalsIgnoreCase("up90")) { rotateLabelsStr = "90"; } else if (rotateLabelsStr.equalsIgnoreCase("down90")) { rotateLabelsStr = "-90"; } else rotateLabelsStr = "0"; String width = (AppUtils.getRequestNvlValue(request, "width").length() > 0 ? AppUtils.getRequestNvlValue(request, "width") : (AppUtils.nvl(reportRuntime.getChartWidth()).length() > 0 ? reportRuntime.getChartWidth() : "700")); String height = (AppUtils.getRequestNvlValue(request, "height").length() > 0 ? AppUtils.getRequestNvlValue(request, "height") : (AppUtils.nvl(reportRuntime.getChartHeight()).length() > 0 ? reportRuntime.getChartHeight() : "300")); String animationStr = (AppUtils.getRequestNvlValue(request, "animation").length() > 0 ? AppUtils.getRequestNvlValue(request, "animation") : Boolean.toString(reportRuntime.isAnimateAnimatedChart())); String rotateLabels = (AppUtils.getRequestNvlValue(request, "rotateLabels").length() > 0 ? AppUtils.getRequestNvlValue(request, "rotateLabels") : (rotateLabelsStr.length() > 0 ? rotateLabelsStr : "0")); String staggerLabelsStr = (AppUtils.getRequestNvlValue(request, "staggerLabels").length() > 0 ? AppUtils.getRequestNvlValue(request, "staggerLabels") : "false"); String showMaxMinStr = (AppUtils.getRequestNvlValue(request, "showMaxMin").length() > 0 ? AppUtils.getRequestNvlValue(request, "showMaxMin") : "false"); String showControlsStr = (AppUtils.getRequestNvlValue(request, "showControls").length() > 0 ? AppUtils.getRequestNvlValue(request, "showControls") : Boolean.toString(reportRuntime.displayBarControls())); String showLegendStr = (AppUtils.getRequestNvlValue(request, "showLegend").length() > 0 ? AppUtils.getRequestNvlValue(request, "showLegend") : Boolean.toString(!Boolean.valueOf(reportRuntime.hideChartLegend()))); String topMarginStr = AppUtils.getRequestNvlValue(request, "topMargin"); String topMargin = (AppUtils.nvl(topMarginStr).length() <= 0) ? (reportRuntime.getTopMargin() != null ? reportRuntime.getTopMargin().toString() : "30") : topMarginStr; String bottomMarginStr = AppUtils.getRequestNvlValue(request, "bottomMargin"); String bottomMargin = (AppUtils.nvl(bottomMarginStr).length() <= 0) ? (reportRuntime.getBottomMargin() != null ? reportRuntime.getBottomMargin().toString() : "50") : bottomMarginStr; String leftMarginStr = AppUtils.getRequestNvlValue(request, "leftMargin"); String leftMargin = (AppUtils.nvl(leftMarginStr).length() <= 0) ? (reportRuntime.getLeftMargin() != null ? reportRuntime.getLeftMargin().toString() : "100") : leftMarginStr; String rightMarginStr = AppUtils.getRequestNvlValue(request, "rightMargin"); String rightMargin = (AppUtils.nvl(rightMarginStr).length() <= 0) ? (reportRuntime.getRightMargin() != null ? reportRuntime.getRightMargin().toString() : "160") : rightMarginStr; String showTitleStr = (AppUtils.getRequestNvlValue(request, "showTitle").length() > 0 ? AppUtils.getRequestNvlValue(request, "showTitle") : Boolean.toString(reportRuntime.displayChartTitle())); String subType = AppUtils.getRequestNvlValue(request, "subType").length() > 0 ? AppUtils.getRequestNvlValue(request, "subType") : (AppUtils.nvl(reportRuntime.getTimeSeriesRender()).equals("area") ? reportRuntime.getTimeSeriesRender() : ""); String stackedStr = AppUtils.getRequestNvlValue(request, "stacked").length() > 0 ? AppUtils.getRequestNvlValue(request, "stacked") : Boolean.toString(reportRuntime.isChartStacked()); String horizontalBar = AppUtils.getRequestNvlValue(request, "horizontalBar").length() > 0 ? AppUtils.getRequestNvlValue(request, "horizontalBar") : Boolean.toString(reportRuntime.isHorizontalOrientation()); String barRealTimeAxis = AppUtils.getRequestNvlValue(request, "barRealTimeAxis"); String barReduceXAxisLabels = AppUtils.getRequestNvlValue(request, "barReduceXAxisLabels").length() > 0 ? AppUtils.getRequestNvlValue(request, "barReduceXAxisLabels") : Boolean.toString(reportRuntime.isLessXaxisTickers()); String timeAxis = AppUtils.getRequestNvlValue(request, "timeAxis").length() > 0 ? AppUtils.getRequestNvlValue(request, "timeAxis") : Boolean.toString(reportRuntime.isTimeAxis()); String logScale = AppUtils.getRequestNvlValue(request, "logScale").length() > 0 ? AppUtils.getRequestNvlValue(request, "logScale") : Boolean.toString(reportRuntime.isLogScale()); String precision = AppUtils.getRequestNvlValue(request, "precision").length() > 0 ? AppUtils.getRequestNvlValue(request, "precision") : "2"; // Add all options to Map chartOptionsMap.put("width", width); chartOptionsMap.put("height", height); chartOptionsMap.put("animation", animationStr); chartOptionsMap.put("rotateLabels", rotateLabels); chartOptionsMap.put("staggerLabels", staggerLabelsStr); chartOptionsMap.put("showMaxMin", showMaxMinStr); chartOptionsMap.put("showControls", showControlsStr); chartOptionsMap.put("showLegend", showLegendStr); chartOptionsMap.put("topMargin", topMargin); chartOptionsMap.put("bottomMargin", bottomMargin); chartOptionsMap.put("leftMargin", leftMargin); chartOptionsMap.put("rightMargin", rightMargin); chartOptionsMap.put("showTitle", showTitleStr); chartOptionsMap.put("subType", subType); chartOptionsMap.put("stacked", stackedStr); chartOptionsMap.put("horizontalBar", horizontalBar); chartOptionsMap.put("timeAxis", timeAxis); chartOptionsMap.put("barRealTimeAxis", barRealTimeAxis); chartOptionsMap.put("barReduceXAxisLabels", barReduceXAxisLabels); chartOptionsMap.put("logScale", logScale); chartOptionsMap.put("precision", precision); if (reportRuntime != null) { StringBuilder title = new StringBuilder(""); title.append(reportRuntime.getReportName()); } if (!("chart.json".equals(action) || "chart.data.json".equals(action))) { reportRuntime.getChartType(); return drawD3Charts(chartOptionsMap, request); } else{ reportRuntime.getChartType(); return returnChartJSON(chartOptionsMap, request, showData); } } public String nvl(String s) { return (s == null) ? "" : s; } /** * @return the reportRuntime */ public ReportRuntime getReportRuntime() { return reportRuntime; } /** * @param reportRuntime the reportRuntime to set */ public void setReportRuntime(ReportRuntime reportRuntime) { this.reportRuntime = reportRuntime; } /** * @return the reportData */ public ReportData getReportData() { return reportData; } /** * @param reportData the reportData to set */ public void setReportData(ReportData reportData) { this.reportData = reportData; } public boolean isNull(String a) { if ((a == null) || (a.length() == 0) || a.equalsIgnoreCase("null")) return true; else return false; } protected String nvl(String s, String sDefault) { return nvl(s).equals("") ? sDefault : s; } protected static String nvls(String s) { return (s == null) ? "" : s; } protected static String nvls(String s, String sDefault) { return nvls(s).equals("") ? sDefault : s; } protected boolean getFlagInBoolean(String s) { return nvl(s).toUpperCase().startsWith("Y") || nvl(s).equalsIgnoreCase("true"); } /** * @return the chartList */ public List getChartList() { return chartList; } /** * @param chartList the chartList to set */ public void setChartList(List chartList) { this.chartList = chartList; } /** * @return the infoList */ public List getInfoList() { return infoList; } /** * @param infoList the infoList to set */ public void setInfoList(List infoList) { this.infoList = infoList; } private void clearReportRuntimeBackup(HttpSession session, HttpServletRequest request) { session.removeAttribute(AppConstants.DRILLDOWN_REPORTS_LIST); request.removeAttribute(AppConstants.DRILLDOWN_INDEX); session.removeAttribute(AppConstants.DRILLDOWN_INDEX); request.removeAttribute(AppConstants.FORM_DRILLDOWN_INDEX); session.removeAttribute(AppConstants.FORM_DRILLDOWN_INDEX); Enumeration enum1 = session.getAttributeNames(); String attributeName = ""; while (enum1.hasMoreElements()) { attributeName = enum1.nextElement(); if (attributeName.startsWith("parent_")) { session.removeAttribute(attributeName); } } session.removeAttribute(AppConstants.DRILLDOWN_REPORTS_LIST); session.removeAttribute(AppConstants.SI_BACKUP_FOR_REP_ID); session.removeAttribute(AppConstants.SI_COLUMN_LOOKUP); session.removeAttribute(AppConstants.SI_DASHBOARD_REP_ID); session.removeAttribute(AppConstants.SI_DASHBOARD_REPORTRUNTIME_MAP); session.removeAttribute(AppConstants.SI_DASHBOARD_REPORTRUNTIME); session.removeAttribute(AppConstants.SI_DASHBOARD_REPORTDATA_MAP); session.removeAttribute(AppConstants.SI_DASHBOARD_CHARTDATA_MAP); session.removeAttribute(AppConstants.SI_DASHBOARD_DISPLAYTYPE_MAP); session.removeAttribute(AppConstants.SI_DATA_SIZE_FOR_TEXTFIELD_POPUP); session.removeAttribute(AppConstants.SI_MAP); session.removeAttribute(AppConstants.SI_MAP_OBJECT); session.removeAttribute(AppConstants.SI_REPORT_DEFINITION); session.removeAttribute(AppConstants.SI_REPORT_RUNTIME); session.removeAttribute(AppConstants.SI_REPORT_RUN_BACKUP); session.removeAttribute(AppConstants.SI_REPORT_SCHEDULE); session.removeAttribute(AppConstants.RI_REPORT_DATA); session.removeAttribute(AppConstants.RI_CHART_DATA); session.removeAttribute(AppConstants.SI_FORMFIELD_INFO); session.removeAttribute(AppConstants.SI_FORMFIELD_DOWNLOAD_INFO); } // clearReportRuntimeBackup public String getTotalSql() { return totalSql; } public void setTotalSql(String totalSql) { this.totalSql = totalSql; } public String drawD3Charts(Map chartOptionsMap, HttpServletRequest request) { ChartD3Helper chartHelper = new ChartD3Helper(reportRuntime); chartHelper.setChartType(reportRuntime.getChartType()); return chartHelper.createVisualization(reportRuntime, chartOptionsMap, request); } public String returnChartJSON(Map chartOptionsMap, HttpServletRequest request, boolean showData) { ChartJSONHelper chartJSONHelper = new ChartJSONHelper(reportRuntime); chartJSONHelper.setChartType(reportRuntime.getChartType()); try { return chartJSONHelper.generateJSON(reportRuntime, chartOptionsMap, request, showData); } catch (RaptorException ex) { logger.error(EELFLoggerDelegate.errorLogger, "RaptorException occurred in returnChartJSON",ex); } return ""; } }