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.model.runtime.ReportRuntime;
57 import org.onap.portalsdk.analytics.system.AppUtils;
58 import org.onap.portalsdk.analytics.util.AppConstants;
59 import org.onap.portalsdk.analytics.view.ReportData;
60 import org.onap.portalsdk.core.web.support.UserUtils;
61 import org.springframework.web.context.request.RequestContextHolder;
62 import org.springframework.web.context.request.ServletRequestAttributes;
65 public class ChartWebRuntime implements Serializable {
68 // Not used - planned to use if Hibernate used as data access layer
69 private String runningDataQuery = "";
70 private String runningCountQuery = "";
72 public final String QRY_COUNT_REPORT = "";
73 public final String QRY_DATA_REPORT = "";
75 // Not used planning to use when filter is used
76 private StringBuffer whereClause = new StringBuffer("");
77 // request used to grab request parameters
78 private HttpServletRequest request;
81 public ReportRuntime reportRuntime;
82 public ReportData reportData;
84 //Used to pass user information
85 private final Map<String, Object> params = new HashMap<String, Object>();
87 //from chart generator retrieves list of charts to render
88 public ArrayList chartList;
89 public ArrayList infoList;
91 private String totalSql;
95 private String drilldown_index = "0";
97 public List getRolesCommaSeperated(HttpServletRequest request) {
98 HashMap roles = UserUtils.getRoles(request);
100 StringBuffer roleBuf = new StringBuffer("");
102 if( roles != null ) {
103 roleList = Arrays.asList(roles.keySet().toArray());
110 public String getUserId(HttpServletRequest request) {
111 return AppUtils.getUserID(request);
114 public String generateChart(HttpServletRequest request) {
115 return generateChart(request, true);
119 public String generateChart(HttpServletRequest request, boolean showData) {
121 //processRecursive(this, this);
122 long currentTime = System.currentTimeMillis();
123 HttpSession session = request.getSession();
124 String action = nvl(request.getParameter(AppConstants.RI_ACTION), request.getParameter("action"));
125 boolean genReportData = (!action.equals("chart.json") || action.equals("chart.data.json"));
129 final Long user_id = new Long((long) UserUtils.getUserId(request));
132 boolean adminUser = false;
134 adminUser = AppUtils.isAdminUser(request) || AppUtils.isSuperUser(request);
135 } catch (RaptorException ex) {
136 ex.printStackTrace();
138 List roleList = getRolesCommaSeperated(request);
139 //final Map<String, Object> params = new HashMap<String, Object>();
140 params.put("user_id", user_id);
141 params.put("role_list", roleList);
142 //params.put("public_yn", "Y");
144 //String action = request.getParameter(AppConstants.RI_ACTION);
145 String reportID = AppUtils.getRequestValue(request, AppConstants.RI_REPORT_ID);
147 ReportHandler rh = new ReportHandler();
148 ReportRuntime rr = null;
151 rr = rh.loadReportRuntime(request, reportID, true, 1);
152 if(rr.getReportType().equals(AppConstants.RT_HIVE)) {
153 String sql = rr.getReportSQL();
156 //reportData = rr.loadHiveLinearReportData(rr.getWholeSQL(),user_id.toString(), 10000,request);
159 reportData = rr.loadReportData(0, user_id.toString(), 10000,request, false /*download*/);
161 } catch (RaptorException ex) {
162 ex.printStackTrace();
164 setReportRuntime(rr);
165 setReportData( reportData);
167 reportRuntime = getReportRuntime();
168 reportData = getReportData();
171 HashMap<String, String> chartOptionsMap = new HashMap<String, String>();
173 String rotateLabelsStr = "";
174 rotateLabelsStr = AppUtils.nvl(reportRuntime.getLegendLabelAngle());
175 if(rotateLabelsStr.toLowerCase().equals("standard")) {
176 rotateLabelsStr = "0";
177 } else if (rotateLabelsStr.toLowerCase().equals("up45")) {
178 rotateLabelsStr = "45";
179 } else if (rotateLabelsStr.toLowerCase().equals("down45")) {
180 rotateLabelsStr = "-45";
181 } else if (rotateLabelsStr.toLowerCase().equals("up90")) {
182 rotateLabelsStr = "90";
183 } else if (rotateLabelsStr.toLowerCase().equals("down90")) {
184 rotateLabelsStr = "-90";
186 rotateLabelsStr = "0";
188 String width = (AppUtils.getRequestNvlValue(request, "width").length()>0?AppUtils.getRequestNvlValue(request, "width"):(AppUtils.nvl(reportRuntime.getChartWidth()).length()>0?reportRuntime.getChartWidth():"700"));
189 String height = (AppUtils.getRequestNvlValue(request, "height").length()>0?AppUtils.getRequestNvlValue(request, "height"):(AppUtils.nvl(reportRuntime.getChartHeight()).length()>0?reportRuntime.getChartHeight():"300"));
190 String animationStr = (AppUtils.getRequestNvlValue(request, "animation").length()>0?AppUtils.getRequestNvlValue(request, "animation"):new Boolean(reportRuntime.isAnimateAnimatedChart()).toString());
192 String rotateLabels = (AppUtils.getRequestNvlValue(request, "rotateLabels").length()>0?AppUtils.getRequestNvlValue(request, "rotateLabels"):(rotateLabelsStr.length()>0?rotateLabelsStr:"0"));
193 String staggerLabelsStr = (AppUtils.getRequestNvlValue(request, "staggerLabels").length()>0?AppUtils.getRequestNvlValue(request, "staggerLabels"):"false");
194 String showMaxMinStr = (AppUtils.getRequestNvlValue(request, "showMaxMin").length()>0?AppUtils.getRequestNvlValue(request, "showMaxMin"):"false");
195 String showControlsStr = (AppUtils.getRequestNvlValue(request, "showControls").length()>0?AppUtils.getRequestNvlValue(request, "showControls"):new Boolean(reportRuntime.displayBarControls()).toString());
196 String showLegendStr = (AppUtils.getRequestNvlValue(request, "showLegend").length()>0?AppUtils.getRequestNvlValue(request, "showLegend"):new Boolean(!new Boolean(reportRuntime.hideChartLegend())).toString());
197 String topMarginStr = AppUtils.getRequestNvlValue(request, "topMargin");
198 String topMargin = (AppUtils.nvl(topMarginStr).length()<=0)?(reportRuntime.getTopMargin()!=null?reportRuntime.getTopMargin().toString():"30"):topMarginStr;
199 String bottomMarginStr = AppUtils.getRequestNvlValue(request, "bottomMargin");
200 String bottomMargin = (AppUtils.nvl(bottomMarginStr).length()<=0)?(reportRuntime.getBottomMargin()!=null?reportRuntime.getBottomMargin().toString():"50"):bottomMarginStr;
201 String leftMarginStr = AppUtils.getRequestNvlValue(request, "leftMargin");
202 String leftMargin = (AppUtils.nvl(leftMarginStr).length()<=0)?(reportRuntime.getLeftMargin()!=null?reportRuntime.getLeftMargin().toString():"100"):leftMarginStr;
203 String rightMarginStr = AppUtils.getRequestNvlValue(request, "rightMargin");
204 String rightMargin = (AppUtils.nvl(rightMarginStr).length()<=0)?(reportRuntime.getRightMargin()!=null?reportRuntime.getRightMargin().toString():"160"):rightMarginStr;
205 String showTitleStr = (AppUtils.getRequestNvlValue(request, "showTitle").length()>0?AppUtils.getRequestNvlValue(request, "showTitle"):new Boolean(reportRuntime.displayChartTitle()).toString());
206 String subType = AppUtils.getRequestNvlValue(request, "subType").length()>0?AppUtils.getRequestNvlValue(request, "subType"):(AppUtils.nvl(reportRuntime.getTimeSeriesRender()).equals("area")?reportRuntime.getTimeSeriesRender():"");
207 String stackedStr = AppUtils.getRequestNvlValue(request, "stacked").length()>0?AppUtils.getRequestNvlValue(request, "stacked"):new Boolean(reportRuntime.isChartStacked()).toString();
208 String horizontalBar = AppUtils.getRequestNvlValue(request, "horizontalBar").length()>0?AppUtils.getRequestNvlValue(request, "horizontalBar"):new Boolean(reportRuntime.isHorizontalOrientation()).toString();
209 String barRealTimeAxis = AppUtils.getRequestNvlValue(request, "barRealTimeAxis");
210 String barReduceXAxisLabels = AppUtils.getRequestNvlValue(request, "barReduceXAxisLabels").length()>0?AppUtils.getRequestNvlValue(request, "barReduceXAxisLabels"):new Boolean(reportRuntime.isLessXaxisTickers()).toString();;
211 String timeAxis = AppUtils.getRequestNvlValue(request, "timeAxis").length()>0?AppUtils.getRequestNvlValue(request, "timeAxis"):new Boolean(reportRuntime.isTimeAxis()).toString();
212 String logScale = AppUtils.getRequestNvlValue(request, "logScale").length()>0?AppUtils.getRequestNvlValue(request, "logScale"):new Boolean(reportRuntime.isLogScale()).toString();
213 String precision = AppUtils.getRequestNvlValue(request, "precision").length()>0?AppUtils.getRequestNvlValue(request, "precision"):"2";
215 /* boolean animation = AppUtils.getRequestFlag(request, "animation");
216 boolean staggerLabels = AppUtils.getRequestFlag(request, "staggerLabels");
217 boolean showMaxMin = (showMaxMinStr.length()<=0)?false:Boolean.parseBoolean(showMaxMinStr);
218 boolean showControls = (showControlsStr.length()<=0)?true:Boolean.parseBoolean(showControlsStr);
219 boolean showLegend = (showLegendStr.length()<=0)?true:Boolean.parseBoolean(showLegendStr);
220 boolean showTitle = (showTitleStr.length()<=0)?true:Boolean.parseBoolean(showTitleStr);
221 boolean stacked = (stackedStr.length()<=0)?true:Boolean.parseBoolean(stackedStr);
223 // Add all options to Map
224 chartOptionsMap.put("width", width);
225 chartOptionsMap.put("height", height);
226 chartOptionsMap.put("animation", animationStr);
227 chartOptionsMap.put("rotateLabels", rotateLabels);
228 chartOptionsMap.put("staggerLabels", staggerLabelsStr);
229 chartOptionsMap.put("showMaxMin", showMaxMinStr);
230 chartOptionsMap.put("showControls", showControlsStr);
231 chartOptionsMap.put("showLegend", showLegendStr);
232 chartOptionsMap.put("topMargin", topMargin);
233 chartOptionsMap.put("bottomMargin", bottomMargin);
234 chartOptionsMap.put("leftMargin", leftMargin);
235 chartOptionsMap.put("rightMargin", rightMargin);
236 chartOptionsMap.put("showTitle", showTitleStr);
237 chartOptionsMap.put("subType", subType);
238 chartOptionsMap.put("stacked", stackedStr);
239 chartOptionsMap.put("horizontalBar", horizontalBar);
240 chartOptionsMap.put("timeAxis", timeAxis);
241 chartOptionsMap.put("barRealTimeAxis", barRealTimeAxis);
242 chartOptionsMap.put("barReduceXAxisLabels", barReduceXAxisLabels);
244 chartOptionsMap.put("logScale", logScale);
245 chartOptionsMap.put("precision", precision);
249 if(reportRuntime!=null) {
250 StringBuffer title = new StringBuffer("");
251 title.append(reportRuntime.getReportName());
254 if(! (action.equals("chart.json") || action.equals("chart.data.json"))) {
258 String chartType = reportRuntime.getChartType();
259 return drawD3Charts(chartOptionsMap, request);
261 } else /*if (action.equals("chart.json"))*/ {
262 String chartType = reportRuntime.getChartType();
263 return returnChartJSON(chartOptionsMap, request, showData);
268 return ("Internal Error Occurred.");
274 public String nvl(String s) {
275 return (s == null) ? "" : s;
279 * @return the reportRuntime
281 public ReportRuntime getReportRuntime() {
282 return reportRuntime;
286 * @param reportRuntime the reportRuntime to set
288 public void setReportRuntime(ReportRuntime reportRuntime) {
289 this.reportRuntime = reportRuntime;
293 * @return the reportData
295 public ReportData getReportData() {
300 * @param reportData the reportData to set
302 public void setReportData(ReportData reportData) {
303 this.reportData = reportData;
306 public boolean isNull(String a) {
307 if ((a == null) || (a.length() == 0) || a.equalsIgnoreCase("null"))
314 protected String nvl(String s, String sDefault) {
315 return nvl(s).equals("") ? sDefault : s;
318 protected static String nvls(String s) {
319 return (s == null) ? "" : s;
322 protected static String nvls(String s, String sDefault) {
323 return nvls(s).equals("") ? sDefault : s;
326 protected boolean getFlagInBoolean(String s) {
327 return nvl(s).toUpperCase().startsWith("Y") || nvl(s).toLowerCase().equals("true");
332 * @return the chartList
334 public ArrayList getChartList() {
339 * @param chartList the chartList to set
341 public void setChartList(ArrayList chartList) {
342 this.chartList = chartList;
346 * @return the infoList
348 public ArrayList getInfoList() {
353 * @param infoList the infoList to set
355 public void setInfoList(ArrayList infoList) {
356 this.infoList = infoList;
361 private void clearReportRuntimeBackup(HttpSession session, HttpServletRequest request) {
362 session.removeAttribute(AppConstants.DRILLDOWN_REPORTS_LIST);
363 request.removeAttribute(AppConstants.DRILLDOWN_INDEX);
364 session.removeAttribute(AppConstants.DRILLDOWN_INDEX);
365 request.removeAttribute(AppConstants.FORM_DRILLDOWN_INDEX);
366 session.removeAttribute(AppConstants.FORM_DRILLDOWN_INDEX);
367 Enumeration<String> enum1 = session.getAttributeNames();
368 String attributeName = "";
369 while(enum1.hasMoreElements()) {
370 attributeName = enum1.nextElement();
371 if(attributeName.startsWith("parent_")) {
372 session.removeAttribute(attributeName);
375 session.removeAttribute(AppConstants.DRILLDOWN_REPORTS_LIST);
376 session.removeAttribute(AppConstants.SI_BACKUP_FOR_REP_ID);
377 session.removeAttribute(AppConstants.SI_COLUMN_LOOKUP);
378 session.removeAttribute(AppConstants.SI_DASHBOARD_REP_ID);
379 session.removeAttribute(AppConstants.SI_DASHBOARD_REPORTRUNTIME_MAP);
380 session.removeAttribute(AppConstants.SI_DASHBOARD_REPORTRUNTIME);
381 session.removeAttribute(AppConstants.SI_DASHBOARD_REPORTDATA_MAP);
382 session.removeAttribute(AppConstants.SI_DASHBOARD_CHARTDATA_MAP);
383 session.removeAttribute(AppConstants.SI_DASHBOARD_DISPLAYTYPE_MAP);
384 session.removeAttribute(AppConstants.SI_DATA_SIZE_FOR_TEXTFIELD_POPUP);
385 session.removeAttribute(AppConstants.SI_MAP);
386 session.removeAttribute(AppConstants.SI_MAP_OBJECT);
387 session.removeAttribute(AppConstants.SI_REPORT_DEFINITION);
388 session.removeAttribute(AppConstants.SI_REPORT_RUNTIME);
389 session.removeAttribute(AppConstants.SI_REPORT_RUN_BACKUP);
390 session.removeAttribute(AppConstants.SI_REPORT_SCHEDULE);
391 session.removeAttribute(AppConstants.RI_REPORT_DATA);
392 session.removeAttribute(AppConstants.RI_CHART_DATA);
393 session.removeAttribute(AppConstants.SI_FORMFIELD_INFO);
394 session.removeAttribute(AppConstants.SI_FORMFIELD_DOWNLOAD_INFO);
395 } // clearReportRuntimeBackup
398 public String getTotalSql() {
402 public void setTotalSql(String totalSql) {
403 this.totalSql = totalSql;
408 /* public void drawD3Charts(HashMap<String,String> chartOptionsMap) {
409 drawD3Charts(chartOptionsMap);
414 public String drawD3Charts(HashMap<String,String> chartOptionsMap, HttpServletRequest request) {
416 ChartD3Helper chartHelper = new ChartD3Helper(reportRuntime);
417 chartHelper.setChartType(reportRuntime.getChartType());
419 return chartHelper.createVisualization(reportRuntime, chartOptionsMap, request);
420 } catch(RaptorException ex) {
421 ex.printStackTrace();
427 public String returnChartJSON(HashMap<String,String> chartOptionsMap, HttpServletRequest request, boolean showData) {
429 ChartJSONHelper chartJSONHelper = new ChartJSONHelper(reportRuntime);
430 chartJSONHelper.setChartType(reportRuntime.getChartType());
432 return chartJSONHelper.generateJSON(reportRuntime, chartOptionsMap, request, showData);
433 } catch(RaptorException ex) {
434 ex.printStackTrace();