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;
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 StringBuilder whereClause = new StringBuilder("");
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<>();
84 //from chart generator retrieves list of charts to render
85 public List chartList;
88 private String totalSql;
92 private String drilldown_index = "0";
94 public List getRolesCommaSeperated(HttpServletRequest request) {
95 Map roles = UserUtils.getRoles(request);
98 roleList = Arrays.asList(roles.keySet().toArray());
105 public String getUserId(HttpServletRequest request) {
106 return AppUtils.getUserID(request);
109 public String generateChart(HttpServletRequest request) {
110 return generateChart(request, true);
114 public String generateChart(HttpServletRequest request, boolean showData) {
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));
124 final Long user_id = new Long((long) UserUtils.getUserId(request));
127 boolean adminUser = false;
129 adminUser = AppUtils.isAdminUser(request) || AppUtils.isSuperUser(request);
130 } catch (RaptorException ex) {
131 ex.printStackTrace();
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");
139 //String action = request.getParameter(AppConstants.RI_ACTION);
140 String reportID = AppUtils.getRequestValue(request, AppConstants.RI_REPORT_ID);
142 ReportHandler rh = new ReportHandler();
143 ReportRuntime rr = null;
146 rr = rh.loadReportRuntime(request, reportID, true, 1);
147 if(rr.getReportType().equals(AppConstants.RT_HIVE)) {
148 String sql = rr.getReportSQL();
151 //reportData = rr.loadHiveLinearReportData(rr.getWholeSQL(),user_id.toString(), 10000,request);
154 reportData = rr.loadReportData(0, user_id.toString(), 10000,request, false /*download*/);
156 } catch (RaptorException ex) {
157 ex.printStackTrace();
159 setReportRuntime(rr);
160 setReportData( reportData);
162 reportRuntime = getReportRuntime();
163 reportData = getReportData();
166 HashMap<String, String> chartOptionsMap = new HashMap<>();
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";
181 rotateLabelsStr = "0";
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());
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";
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);
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);
239 chartOptionsMap.put("logScale", logScale);
240 chartOptionsMap.put("precision", precision);
244 if(reportRuntime!=null) {
245 StringBuilder title = new StringBuilder("");
246 title.append(reportRuntime.getReportName());
249 if(! ("chart.json".equals(action) || "chart.data.json".equals(action))) {
253 String chartType = reportRuntime.getChartType();
254 return drawD3Charts(chartOptionsMap, request);
256 } else /*if (action.equals("chart.json"))*/ {
257 String chartType = reportRuntime.getChartType();
258 return returnChartJSON(chartOptionsMap, request, showData);
263 return ("Internal Error Occurred.");
269 public String nvl(String s) {
270 return (s == null) ? "" : s;
274 * @return the reportRuntime
276 public ReportRuntime getReportRuntime() {
277 return reportRuntime;
281 * @param reportRuntime the reportRuntime to set
283 public void setReportRuntime(ReportRuntime reportRuntime) {
284 this.reportRuntime = reportRuntime;
288 * @return the reportData
290 public ReportData getReportData() {
295 * @param reportData the reportData to set
297 public void setReportData(ReportData reportData) {
298 this.reportData = reportData;
301 public boolean isNull(String a) {
302 if ((a == null) || (a.length() == 0) || a.equalsIgnoreCase("null"))
309 protected String nvl(String s, String sDefault) {
310 return nvl(s).equals("") ? sDefault : s;
313 protected static String nvls(String s) {
314 return (s == null) ? "" : s;
317 protected static String nvls(String s, String sDefault) {
318 return nvls(s).equals("") ? sDefault : s;
321 protected boolean getFlagInBoolean(String s) {
322 return nvl(s).toUpperCase().startsWith("Y") || nvl(s).toLowerCase().equals("true");
327 * @return the chartList
329 public List getChartList() {
334 * @param chartList the chartList to set
336 public void setChartList(List chartList) {
337 this.chartList = chartList;
341 * @return the infoList
343 public List getInfoList() {
348 * @param infoList the infoList to set
350 public void setInfoList(List infoList) {
351 this.infoList = infoList;
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);
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
393 public String getTotalSql() {
397 public void setTotalSql(String totalSql) {
398 this.totalSql = totalSql;
403 /* public void drawD3Charts(HashMap<String,String> chartOptionsMap) {
404 drawD3Charts(chartOptionsMap);
409 public String drawD3Charts(Map<String,String> chartOptionsMap, HttpServletRequest request) {
411 ChartD3Helper chartHelper = new ChartD3Helper(reportRuntime);
412 chartHelper.setChartType(reportRuntime.getChartType());
414 return chartHelper.createVisualization(reportRuntime, chartOptionsMap, request);
417 public String returnChartJSON(HashMap<String,String> chartOptionsMap, HttpServletRequest request, boolean showData) {
419 ChartJSONHelper chartJSONHelper = new ChartJSONHelper(reportRuntime);
420 chartJSONHelper.setChartType(reportRuntime.getChartType());
422 return chartJSONHelper.generateJSON(reportRuntime, chartOptionsMap, request, showData);
423 } catch(RaptorException ex) {
424 ex.printStackTrace();