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============================================
38 package org.onap.portalsdk.analytics.model.runtime;
40 import java.io.Serializable;
41 import java.util.Arrays;
42 import java.util.Enumeration;
43 import java.util.HashMap;
44 import java.util.List;
46 import javax.servlet.http.HttpServletRequest;
47 import javax.servlet.http.HttpSession;
48 import org.onap.portalsdk.analytics.error.RaptorException;
49 import org.onap.portalsdk.analytics.model.ReportHandler;
50 import org.onap.portalsdk.analytics.system.AppUtils;
51 import org.onap.portalsdk.analytics.util.AppConstants;
52 import org.onap.portalsdk.analytics.view.ReportData;
53 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
54 import org.onap.portalsdk.core.web.support.UserUtils;
57 public class ChartWebRuntime implements Serializable {
59 private static final EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(ChartWebRuntime.class);
61 // CONSTANTS FOR QUERY
62 public final String QRY_COUNT_REPORT = "";
63 public final String QRY_DATA_REPORT = "";
65 public ReportRuntime reportRuntime;
66 public ReportData reportData;
68 // Used to pass user information
69 private final transient Map<String, Object> params = new HashMap<>();
71 // from chart generator retrieves list of charts to render
72 public transient List chartList;
73 public transient List infoList;
75 private String totalSql;
78 public List getRolesCommaSeperated(HttpServletRequest request) {
79 Map roles = UserUtils.getRoles(request);
82 roleList = Arrays.asList(roles.keySet().toArray());
89 public String getUserId(HttpServletRequest request) {
90 return AppUtils.getUserID(request);
93 public String generateChart(HttpServletRequest request) {
94 return generateChart(request, true);
98 public String generateChart(HttpServletRequest request, boolean showData) {
99 System.currentTimeMillis();
100 request.getSession();
102 nvl(request.getParameter(AppConstants.RI_ACTION), request.getParameter("action"));
103 boolean genReportData = (!"chart.json".equals(action) || "chart.data.json".equals(action));
107 final Long user_id = Long.valueOf(UserUtils.getUserId(request));
111 if(AppUtils.isAdminUser(request) || AppUtils.isSuperUser(request)) {
112 logger.debug(EELFLoggerDelegate.debugLogger, "Request by Admin or Super User");
114 } catch (RaptorException ex) {
115 logger.error(EELFLoggerDelegate.errorLogger, "RaptorException occurred in generateChart",ex);
117 List roleList = getRolesCommaSeperated(request);
118 // final Map<String, Object> params = new HashMap<String, Object>();
119 params.put("user_id", user_id);
120 params.put("role_list", roleList);
121 // params.put("public_yn", "Y");
123 // String action = request.getParameter(AppConstants.RI_ACTION);
124 String reportID = AppUtils.getRequestValue(request, AppConstants.RI_REPORT_ID);
126 ReportHandler rh = new ReportHandler();
127 ReportRuntime rr = null;
129 if (reportID != null)
130 rr = rh.loadReportRuntime(request, reportID, true, 1);
131 if (rr.getReportType().equals(AppConstants.RT_HIVE)) {
132 String sql = rr.getReportSQL();
135 // reportData = rr.loadHiveLinearReportData(rr.getWholeSQL(),user_id.toString(),
139 reportData = rr.loadReportData(0, user_id.toString(), 10000, request,
140 false /* download */);
142 } catch (RaptorException ex) {
143 logger.error(EELFLoggerDelegate.errorLogger, "RaptorException occurred in generateChart",ex);
145 setReportRuntime(rr);
146 setReportData(reportData);
148 reportRuntime = getReportRuntime();
149 reportData = getReportData();
152 HashMap<String, String> chartOptionsMap = new HashMap<>();
154 String rotateLabelsStr = "";
155 rotateLabelsStr = AppUtils.nvl(reportRuntime.getLegendLabelAngle());
156 if (rotateLabelsStr.equalsIgnoreCase("standard")) {
157 rotateLabelsStr = "0";
158 } else if (rotateLabelsStr.equalsIgnoreCase("up45")) {
159 rotateLabelsStr = "45";
160 } else if (rotateLabelsStr.equalsIgnoreCase("down45")) {
161 rotateLabelsStr = "-45";
162 } else if (rotateLabelsStr.equalsIgnoreCase("up90")) {
163 rotateLabelsStr = "90";
164 } else if (rotateLabelsStr.equalsIgnoreCase("down90")) {
165 rotateLabelsStr = "-90";
167 rotateLabelsStr = "0";
169 String width = (AppUtils.getRequestNvlValue(request, "width").length() > 0
170 ? AppUtils.getRequestNvlValue(request, "width")
171 : (AppUtils.nvl(reportRuntime.getChartWidth()).length() > 0
172 ? reportRuntime.getChartWidth()
174 String height = (AppUtils.getRequestNvlValue(request, "height").length() > 0
175 ? AppUtils.getRequestNvlValue(request, "height")
176 : (AppUtils.nvl(reportRuntime.getChartHeight()).length() > 0
177 ? reportRuntime.getChartHeight()
179 String animationStr = (AppUtils.getRequestNvlValue(request, "animation").length() > 0
180 ? AppUtils.getRequestNvlValue(request, "animation")
181 : Boolean.toString(reportRuntime.isAnimateAnimatedChart()));
183 String rotateLabels = (AppUtils.getRequestNvlValue(request, "rotateLabels").length() > 0
184 ? AppUtils.getRequestNvlValue(request, "rotateLabels")
185 : (rotateLabelsStr.length() > 0 ? rotateLabelsStr : "0"));
186 String staggerLabelsStr =
187 (AppUtils.getRequestNvlValue(request, "staggerLabels").length() > 0
188 ? AppUtils.getRequestNvlValue(request, "staggerLabels")
190 String showMaxMinStr = (AppUtils.getRequestNvlValue(request, "showMaxMin").length() > 0
191 ? AppUtils.getRequestNvlValue(request, "showMaxMin")
193 String showControlsStr = (AppUtils.getRequestNvlValue(request, "showControls").length() > 0
194 ? AppUtils.getRequestNvlValue(request, "showControls")
195 : Boolean.toString(reportRuntime.displayBarControls()));
196 String showLegendStr = (AppUtils.getRequestNvlValue(request, "showLegend").length() > 0
197 ? AppUtils.getRequestNvlValue(request, "showLegend")
198 : Boolean.toString(!Boolean.valueOf(reportRuntime.hideChartLegend())));
199 String topMarginStr = AppUtils.getRequestNvlValue(request, "topMargin");
200 String topMargin = (AppUtils.nvl(topMarginStr).length() <= 0)
201 ? (reportRuntime.getTopMargin() != null ? reportRuntime.getTopMargin().toString()
204 String bottomMarginStr = AppUtils.getRequestNvlValue(request, "bottomMargin");
205 String bottomMargin = (AppUtils.nvl(bottomMarginStr).length() <= 0)
206 ? (reportRuntime.getBottomMargin() != null
207 ? reportRuntime.getBottomMargin().toString()
210 String leftMarginStr = AppUtils.getRequestNvlValue(request, "leftMargin");
211 String leftMargin = (AppUtils.nvl(leftMarginStr).length() <= 0)
212 ? (reportRuntime.getLeftMargin() != null ? reportRuntime.getLeftMargin().toString()
215 String rightMarginStr = AppUtils.getRequestNvlValue(request, "rightMargin");
216 String rightMargin = (AppUtils.nvl(rightMarginStr).length() <= 0)
217 ? (reportRuntime.getRightMargin() != null
218 ? reportRuntime.getRightMargin().toString()
221 String showTitleStr = (AppUtils.getRequestNvlValue(request, "showTitle").length() > 0
222 ? AppUtils.getRequestNvlValue(request, "showTitle")
223 : Boolean.toString(reportRuntime.displayChartTitle()));
224 String subType = AppUtils.getRequestNvlValue(request, "subType").length() > 0
225 ? AppUtils.getRequestNvlValue(request, "subType")
226 : (AppUtils.nvl(reportRuntime.getTimeSeriesRender()).equals("area")
227 ? reportRuntime.getTimeSeriesRender()
229 String stackedStr = AppUtils.getRequestNvlValue(request, "stacked").length() > 0
230 ? AppUtils.getRequestNvlValue(request, "stacked")
231 : Boolean.toString(reportRuntime.isChartStacked());
232 String horizontalBar = AppUtils.getRequestNvlValue(request, "horizontalBar").length() > 0
233 ? AppUtils.getRequestNvlValue(request, "horizontalBar")
234 : Boolean.toString(reportRuntime.isHorizontalOrientation());
235 String barRealTimeAxis = AppUtils.getRequestNvlValue(request, "barRealTimeAxis");
236 String barReduceXAxisLabels =
237 AppUtils.getRequestNvlValue(request, "barReduceXAxisLabels").length() > 0
238 ? AppUtils.getRequestNvlValue(request, "barReduceXAxisLabels")
239 : Boolean.toString(reportRuntime.isLessXaxisTickers());
240 String timeAxis = AppUtils.getRequestNvlValue(request, "timeAxis").length() > 0
241 ? AppUtils.getRequestNvlValue(request, "timeAxis")
242 : Boolean.toString(reportRuntime.isTimeAxis());
243 String logScale = AppUtils.getRequestNvlValue(request, "logScale").length() > 0
244 ? AppUtils.getRequestNvlValue(request, "logScale")
245 : Boolean.toString(reportRuntime.isLogScale());
246 String precision = AppUtils.getRequestNvlValue(request, "precision").length() > 0
247 ? AppUtils.getRequestNvlValue(request, "precision")
251 * boolean animation = AppUtils.getRequestFlag(request, "animation"); boolean staggerLabels
252 * = AppUtils.getRequestFlag(request, "staggerLabels"); boolean showMaxMin =
253 * (showMaxMinStr.length()<=0)?false:Boolean.parseBoolean(showMaxMinStr); boolean
254 * showControls = (showControlsStr.length()<=0)?true:Boolean.parseBoolean(showControlsStr);
255 * boolean showLegend =
256 * (showLegendStr.length()<=0)?true:Boolean.parseBoolean(showLegendStr); boolean showTitle =
257 * (showTitleStr.length()<=0)?true:Boolean.parseBoolean(showTitleStr); boolean stacked =
258 * (stackedStr.length()<=0)?true:Boolean.parseBoolean(stackedStr);
260 // Add all options to Map
261 chartOptionsMap.put("width", width);
262 chartOptionsMap.put("height", height);
263 chartOptionsMap.put("animation", animationStr);
264 chartOptionsMap.put("rotateLabels", rotateLabels);
265 chartOptionsMap.put("staggerLabels", staggerLabelsStr);
266 chartOptionsMap.put("showMaxMin", showMaxMinStr);
267 chartOptionsMap.put("showControls", showControlsStr);
268 chartOptionsMap.put("showLegend", showLegendStr);
269 chartOptionsMap.put("topMargin", topMargin);
270 chartOptionsMap.put("bottomMargin", bottomMargin);
271 chartOptionsMap.put("leftMargin", leftMargin);
272 chartOptionsMap.put("rightMargin", rightMargin);
273 chartOptionsMap.put("showTitle", showTitleStr);
274 chartOptionsMap.put("subType", subType);
275 chartOptionsMap.put("stacked", stackedStr);
276 chartOptionsMap.put("horizontalBar", horizontalBar);
277 chartOptionsMap.put("timeAxis", timeAxis);
278 chartOptionsMap.put("barRealTimeAxis", barRealTimeAxis);
279 chartOptionsMap.put("barReduceXAxisLabels", barReduceXAxisLabels);
281 chartOptionsMap.put("logScale", logScale);
282 chartOptionsMap.put("precision", precision);
286 if (reportRuntime != null) {
287 StringBuilder title = new StringBuilder("");
288 title.append(reportRuntime.getReportName());
291 if (!("chart.json".equals(action) || "chart.data.json".equals(action))) {
294 reportRuntime.getChartType();
295 return drawD3Charts(chartOptionsMap, request);
297 } else /* if (action.equals("chart.json")) */ {
298 reportRuntime.getChartType();
299 return returnChartJSON(chartOptionsMap, request, showData);
305 * return ("Internal Error Occurred."); }
311 public String nvl(String s) {
312 return (s == null) ? "" : s;
316 * @return the reportRuntime
318 public ReportRuntime getReportRuntime() {
319 return reportRuntime;
323 * @param reportRuntime the reportRuntime to set
325 public void setReportRuntime(ReportRuntime reportRuntime) {
326 this.reportRuntime = reportRuntime;
330 * @return the reportData
332 public ReportData getReportData() {
337 * @param reportData the reportData to set
339 public void setReportData(ReportData reportData) {
340 this.reportData = reportData;
343 public boolean isNull(String a) {
344 if ((a == null) || (a.length() == 0) || a.equalsIgnoreCase("null"))
351 protected String nvl(String s, String sDefault) {
352 return nvl(s).equals("") ? sDefault : s;
355 protected static String nvls(String s) {
356 return (s == null) ? "" : s;
359 protected static String nvls(String s, String sDefault) {
360 return nvls(s).equals("") ? sDefault : s;
363 protected boolean getFlagInBoolean(String s) {
364 return nvl(s).toUpperCase().startsWith("Y") || nvl(s).equalsIgnoreCase("true");
369 * @return the chartList
371 public List getChartList() {
376 * @param chartList the chartList to set
378 public void setChartList(List chartList) {
379 this.chartList = chartList;
383 * @return the infoList
385 public List getInfoList() {
390 * @param infoList the infoList to set
392 public void setInfoList(List infoList) {
393 this.infoList = infoList;
398 private void clearReportRuntimeBackup(HttpSession session, HttpServletRequest request) {
399 session.removeAttribute(AppConstants.DRILLDOWN_REPORTS_LIST);
400 request.removeAttribute(AppConstants.DRILLDOWN_INDEX);
401 session.removeAttribute(AppConstants.DRILLDOWN_INDEX);
402 request.removeAttribute(AppConstants.FORM_DRILLDOWN_INDEX);
403 session.removeAttribute(AppConstants.FORM_DRILLDOWN_INDEX);
404 Enumeration<String> enum1 = session.getAttributeNames();
405 String attributeName = "";
406 while (enum1.hasMoreElements()) {
407 attributeName = enum1.nextElement();
408 if (attributeName.startsWith("parent_")) {
409 session.removeAttribute(attributeName);
412 session.removeAttribute(AppConstants.DRILLDOWN_REPORTS_LIST);
413 session.removeAttribute(AppConstants.SI_BACKUP_FOR_REP_ID);
414 session.removeAttribute(AppConstants.SI_COLUMN_LOOKUP);
415 session.removeAttribute(AppConstants.SI_DASHBOARD_REP_ID);
416 session.removeAttribute(AppConstants.SI_DASHBOARD_REPORTRUNTIME_MAP);
417 session.removeAttribute(AppConstants.SI_DASHBOARD_REPORTRUNTIME);
418 session.removeAttribute(AppConstants.SI_DASHBOARD_REPORTDATA_MAP);
419 session.removeAttribute(AppConstants.SI_DASHBOARD_CHARTDATA_MAP);
420 session.removeAttribute(AppConstants.SI_DASHBOARD_DISPLAYTYPE_MAP);
421 session.removeAttribute(AppConstants.SI_DATA_SIZE_FOR_TEXTFIELD_POPUP);
422 session.removeAttribute(AppConstants.SI_MAP);
423 session.removeAttribute(AppConstants.SI_MAP_OBJECT);
424 session.removeAttribute(AppConstants.SI_REPORT_DEFINITION);
425 session.removeAttribute(AppConstants.SI_REPORT_RUNTIME);
426 session.removeAttribute(AppConstants.SI_REPORT_RUN_BACKUP);
427 session.removeAttribute(AppConstants.SI_REPORT_SCHEDULE);
428 session.removeAttribute(AppConstants.RI_REPORT_DATA);
429 session.removeAttribute(AppConstants.RI_CHART_DATA);
430 session.removeAttribute(AppConstants.SI_FORMFIELD_INFO);
431 session.removeAttribute(AppConstants.SI_FORMFIELD_DOWNLOAD_INFO);
432 } // clearReportRuntimeBackup
435 public String getTotalSql() {
439 public void setTotalSql(String totalSql) {
440 this.totalSql = totalSql;
446 * public void drawD3Charts(HashMap<String,String> chartOptionsMap) {
447 * drawD3Charts(chartOptionsMap);
452 public String drawD3Charts(Map<String, String> chartOptionsMap, HttpServletRequest request) {
454 ChartD3Helper chartHelper = new ChartD3Helper(reportRuntime);
455 chartHelper.setChartType(reportRuntime.getChartType());
457 return chartHelper.createVisualization(reportRuntime, chartOptionsMap, request);
460 public String returnChartJSON(Map<String, String> chartOptionsMap,
461 HttpServletRequest request, boolean showData) {
463 ChartJSONHelper chartJSONHelper = new ChartJSONHelper(reportRuntime);
464 chartJSONHelper.setChartType(reportRuntime.getChartType());
466 return chartJSONHelper.generateJSON(reportRuntime, chartOptionsMap, request, showData);
467 } catch (RaptorException ex) {
468 logger.error(EELFLoggerDelegate.errorLogger, "RaptorException occurred in returnChartJSON",ex);