2 * ================================================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ================================================================================
20 package org.openecomp.portalsdk.analytics.model.runtime;
25 import java.io.Serializable;
26 import java.util.ArrayList;
27 import java.util.Arrays;
28 import java.util.Enumeration;
29 import java.util.HashMap;
30 import java.util.List;
33 import javax.servlet.http.HttpServletRequest;
34 import javax.servlet.http.HttpSession;
36 import org.openecomp.portalsdk.analytics.error.RaptorException;
37 import org.openecomp.portalsdk.analytics.model.ReportHandler;
38 import org.openecomp.portalsdk.analytics.model.runtime.ReportRuntime;
39 import org.openecomp.portalsdk.analytics.system.AppUtils;
40 import org.openecomp.portalsdk.analytics.util.AppConstants;
41 import org.openecomp.portalsdk.analytics.view.ReportData;
42 import org.openecomp.portalsdk.core.web.support.UserUtils;
43 import org.springframework.web.context.request.RequestContextHolder;
44 import org.springframework.web.context.request.ServletRequestAttributes;
47 public class ChartWebRuntime implements Serializable {
50 // Not used - planned to use if Hibernate used as data access layer
51 private String runningDataQuery = "";
52 private String runningCountQuery = "";
54 public final String QRY_COUNT_REPORT = "";
55 public final String QRY_DATA_REPORT = "";
57 // Not used planning to use when filter is used
58 private StringBuffer whereClause = new StringBuffer("");
59 // request used to grab request parameters
60 private HttpServletRequest request;
63 public ReportRuntime reportRuntime;
64 public ReportData reportData;
66 //Used to pass user information
67 private final Map<String, Object> params = new HashMap<String, Object>();
69 //from chart generator retrieves list of charts to render
70 public ArrayList chartList;
71 public ArrayList infoList;
73 private String totalSql;
77 private String drilldown_index = "0";
79 public List getRolesCommaSeperated(HttpServletRequest request) {
80 HashMap roles = UserUtils.getRoles(request);
82 StringBuffer roleBuf = new StringBuffer("");
85 roleList = Arrays.asList(roles.keySet().toArray());
92 public String getUserId(HttpServletRequest request) {
93 return AppUtils.getUserID(request);
96 public String generateChart(HttpServletRequest request) {
97 return generateChart(request, true);
101 public String generateChart(HttpServletRequest request, boolean showData) {
103 //processRecursive(this, this);
104 long currentTime = System.currentTimeMillis();
105 HttpSession session = request.getSession();
106 String action = nvl(request.getParameter(AppConstants.RI_ACTION), request.getParameter("action"));
107 boolean genReportData = (!action.equals("chart.json") || action.equals("chart.data.json"));
111 final Long user_id = new Long((long) UserUtils.getUserId(request));
114 boolean adminUser = false;
116 adminUser = AppUtils.isAdminUser(request) || AppUtils.isSuperUser(request);
117 } catch (RaptorException ex) {
118 ex.printStackTrace();
120 List roleList = getRolesCommaSeperated(request);
121 //final Map<String, Object> params = new HashMap<String, Object>();
122 params.put("user_id", user_id);
123 params.put("role_list", roleList);
124 //params.put("public_yn", "Y");
126 //String action = request.getParameter(AppConstants.RI_ACTION);
127 String reportID = AppUtils.getRequestValue(request, AppConstants.RI_REPORT_ID);
129 ReportHandler rh = new ReportHandler();
130 ReportRuntime rr = null;
133 rr = rh.loadReportRuntime(request, reportID, true, 1);
134 if(rr.getReportType().equals(AppConstants.RT_HIVE)) {
135 String sql = rr.getReportSQL();
138 //reportData = rr.loadHiveLinearReportData(rr.getWholeSQL(),user_id.toString(), 10000,request);
141 reportData = rr.loadReportData(0, user_id.toString(), 10000,request, false /*download*/);
143 } catch (RaptorException ex) {
144 ex.printStackTrace();
146 setReportRuntime(rr);
147 setReportData( reportData);
149 reportRuntime = getReportRuntime();
150 reportData = getReportData();
153 HashMap<String, String> chartOptionsMap = new HashMap<String, String>();
155 String rotateLabelsStr = "";
156 rotateLabelsStr = AppUtils.nvl(reportRuntime.getLegendLabelAngle());
157 if(rotateLabelsStr.toLowerCase().equals("standard")) {
158 rotateLabelsStr = "0";
159 } else if (rotateLabelsStr.toLowerCase().equals("up45")) {
160 rotateLabelsStr = "45";
161 } else if (rotateLabelsStr.toLowerCase().equals("down45")) {
162 rotateLabelsStr = "-45";
163 } else if (rotateLabelsStr.toLowerCase().equals("up90")) {
164 rotateLabelsStr = "90";
165 } else if (rotateLabelsStr.toLowerCase().equals("down90")) {
166 rotateLabelsStr = "-90";
168 rotateLabelsStr = "0";
170 String width = (AppUtils.getRequestNvlValue(request, "width").length()>0?AppUtils.getRequestNvlValue(request, "width"):(AppUtils.nvl(reportRuntime.getChartWidth()).length()>0?reportRuntime.getChartWidth():"700"));
171 String height = (AppUtils.getRequestNvlValue(request, "height").length()>0?AppUtils.getRequestNvlValue(request, "height"):(AppUtils.nvl(reportRuntime.getChartHeight()).length()>0?reportRuntime.getChartHeight():"300"));
172 String animationStr = (AppUtils.getRequestNvlValue(request, "animation").length()>0?AppUtils.getRequestNvlValue(request, "animation"):new Boolean(reportRuntime.isAnimateAnimatedChart()).toString());
174 String rotateLabels = (AppUtils.getRequestNvlValue(request, "rotateLabels").length()>0?AppUtils.getRequestNvlValue(request, "rotateLabels"):(rotateLabelsStr.length()>0?rotateLabelsStr:"0"));
175 String staggerLabelsStr = (AppUtils.getRequestNvlValue(request, "staggerLabels").length()>0?AppUtils.getRequestNvlValue(request, "staggerLabels"):"false");
176 String showMaxMinStr = (AppUtils.getRequestNvlValue(request, "showMaxMin").length()>0?AppUtils.getRequestNvlValue(request, "showMaxMin"):"false");
177 String showControlsStr = (AppUtils.getRequestNvlValue(request, "showControls").length()>0?AppUtils.getRequestNvlValue(request, "showControls"):new Boolean(reportRuntime.displayBarControls()).toString());
178 String showLegendStr = (AppUtils.getRequestNvlValue(request, "showLegend").length()>0?AppUtils.getRequestNvlValue(request, "showLegend"):new Boolean(!new Boolean(reportRuntime.hideChartLegend())).toString());
179 String topMarginStr = AppUtils.getRequestNvlValue(request, "topMargin");
180 String topMargin = (AppUtils.nvl(topMarginStr).length()<=0)?(reportRuntime.getTopMargin()!=null?reportRuntime.getTopMargin().toString():"30"):topMarginStr;
181 String bottomMarginStr = AppUtils.getRequestNvlValue(request, "bottomMargin");
182 String bottomMargin = (AppUtils.nvl(bottomMarginStr).length()<=0)?(reportRuntime.getBottomMargin()!=null?reportRuntime.getBottomMargin().toString():"50"):bottomMarginStr;
183 String leftMarginStr = AppUtils.getRequestNvlValue(request, "leftMargin");
184 String leftMargin = (AppUtils.nvl(leftMarginStr).length()<=0)?(reportRuntime.getLeftMargin()!=null?reportRuntime.getLeftMargin().toString():"100"):leftMarginStr;
185 String rightMarginStr = AppUtils.getRequestNvlValue(request, "rightMargin");
186 String rightMargin = (AppUtils.nvl(rightMarginStr).length()<=0)?(reportRuntime.getRightMargin()!=null?reportRuntime.getRightMargin().toString():"160"):rightMarginStr;
187 String showTitleStr = (AppUtils.getRequestNvlValue(request, "showTitle").length()>0?AppUtils.getRequestNvlValue(request, "showTitle"):new Boolean(reportRuntime.displayChartTitle()).toString());
188 String subType = AppUtils.getRequestNvlValue(request, "subType").length()>0?AppUtils.getRequestNvlValue(request, "subType"):(AppUtils.nvl(reportRuntime.getTimeSeriesRender()).equals("area")?reportRuntime.getTimeSeriesRender():"");
189 String stackedStr = AppUtils.getRequestNvlValue(request, "stacked").length()>0?AppUtils.getRequestNvlValue(request, "stacked"):new Boolean(reportRuntime.isChartStacked()).toString();
190 String horizontalBar = AppUtils.getRequestNvlValue(request, "horizontalBar").length()>0?AppUtils.getRequestNvlValue(request, "horizontalBar"):new Boolean(reportRuntime.isHorizontalOrientation()).toString();
191 String barRealTimeAxis = AppUtils.getRequestNvlValue(request, "barRealTimeAxis");
192 String barReduceXAxisLabels = AppUtils.getRequestNvlValue(request, "barReduceXAxisLabels").length()>0?AppUtils.getRequestNvlValue(request, "barReduceXAxisLabels"):new Boolean(reportRuntime.isLessXaxisTickers()).toString();;
193 String timeAxis = AppUtils.getRequestNvlValue(request, "timeAxis").length()>0?AppUtils.getRequestNvlValue(request, "timeAxis"):new Boolean(reportRuntime.isTimeAxis()).toString();
194 String logScale = AppUtils.getRequestNvlValue(request, "logScale").length()>0?AppUtils.getRequestNvlValue(request, "logScale"):new Boolean(reportRuntime.isLogScale()).toString();
195 String precision = AppUtils.getRequestNvlValue(request, "precision").length()>0?AppUtils.getRequestNvlValue(request, "precision"):"2";
197 /* boolean animation = AppUtils.getRequestFlag(request, "animation");
198 boolean staggerLabels = AppUtils.getRequestFlag(request, "staggerLabels");
199 boolean showMaxMin = (showMaxMinStr.length()<=0)?false:Boolean.parseBoolean(showMaxMinStr);
200 boolean showControls = (showControlsStr.length()<=0)?true:Boolean.parseBoolean(showControlsStr);
201 boolean showLegend = (showLegendStr.length()<=0)?true:Boolean.parseBoolean(showLegendStr);
202 boolean showTitle = (showTitleStr.length()<=0)?true:Boolean.parseBoolean(showTitleStr);
203 boolean stacked = (stackedStr.length()<=0)?true:Boolean.parseBoolean(stackedStr);
205 // Add all options to Map
206 chartOptionsMap.put("width", width);
207 chartOptionsMap.put("height", height);
208 chartOptionsMap.put("animation", animationStr);
209 chartOptionsMap.put("rotateLabels", rotateLabels);
210 chartOptionsMap.put("staggerLabels", staggerLabelsStr);
211 chartOptionsMap.put("showMaxMin", showMaxMinStr);
212 chartOptionsMap.put("showControls", showControlsStr);
213 chartOptionsMap.put("showLegend", showLegendStr);
214 chartOptionsMap.put("topMargin", topMargin);
215 chartOptionsMap.put("bottomMargin", bottomMargin);
216 chartOptionsMap.put("leftMargin", leftMargin);
217 chartOptionsMap.put("rightMargin", rightMargin);
218 chartOptionsMap.put("showTitle", showTitleStr);
219 chartOptionsMap.put("subType", subType);
220 chartOptionsMap.put("stacked", stackedStr);
221 chartOptionsMap.put("horizontalBar", horizontalBar);
222 chartOptionsMap.put("timeAxis", timeAxis);
223 chartOptionsMap.put("barRealTimeAxis", barRealTimeAxis);
224 chartOptionsMap.put("barReduceXAxisLabels", barReduceXAxisLabels);
226 chartOptionsMap.put("logScale", logScale);
227 chartOptionsMap.put("precision", precision);
231 if(reportRuntime!=null) {
232 StringBuffer title = new StringBuffer("");
233 title.append(reportRuntime.getReportName());
236 if(! (action.equals("chart.json") || action.equals("chart.data.json"))) {
240 String chartType = reportRuntime.getChartType();
241 return drawD3Charts(chartOptionsMap, request);
243 } else /*if (action.equals("chart.json"))*/ {
244 String chartType = reportRuntime.getChartType();
245 return returnChartJSON(chartOptionsMap, request, showData);
250 return ("Internal Error Occurred.");
256 public String nvl(String s) {
257 return (s == null) ? "" : s;
261 * @return the reportRuntime
263 public ReportRuntime getReportRuntime() {
264 return reportRuntime;
268 * @param reportRuntime the reportRuntime to set
270 public void setReportRuntime(ReportRuntime reportRuntime) {
271 this.reportRuntime = reportRuntime;
275 * @return the reportData
277 public ReportData getReportData() {
282 * @param reportData the reportData to set
284 public void setReportData(ReportData reportData) {
285 this.reportData = reportData;
288 public boolean isNull(String a) {
289 if ((a == null) || (a.length() == 0) || a.equalsIgnoreCase("null"))
296 protected String nvl(String s, String sDefault) {
297 return nvl(s).equals("") ? sDefault : s;
300 protected static String nvls(String s) {
301 return (s == null) ? "" : s;
304 protected static String nvls(String s, String sDefault) {
305 return nvls(s).equals("") ? sDefault : s;
308 protected boolean getFlagInBoolean(String s) {
309 return nvl(s).toUpperCase().startsWith("Y") || nvl(s).toLowerCase().equals("true");
314 * @return the chartList
316 public ArrayList getChartList() {
321 * @param chartList the chartList to set
323 public void setChartList(ArrayList chartList) {
324 this.chartList = chartList;
328 * @return the infoList
330 public ArrayList getInfoList() {
335 * @param infoList the infoList to set
337 public void setInfoList(ArrayList infoList) {
338 this.infoList = infoList;
343 private void clearReportRuntimeBackup(HttpSession session, HttpServletRequest request) {
344 session.removeAttribute(AppConstants.DRILLDOWN_REPORTS_LIST);
345 request.removeAttribute(AppConstants.DRILLDOWN_INDEX);
346 session.removeAttribute(AppConstants.DRILLDOWN_INDEX);
347 request.removeAttribute(AppConstants.FORM_DRILLDOWN_INDEX);
348 session.removeAttribute(AppConstants.FORM_DRILLDOWN_INDEX);
349 Enumeration<String> enum1 = session.getAttributeNames();
350 String attributeName = "";
351 while(enum1.hasMoreElements()) {
352 attributeName = enum1.nextElement();
353 if(attributeName.startsWith("parent_")) {
354 session.removeAttribute(attributeName);
357 session.removeAttribute(AppConstants.DRILLDOWN_REPORTS_LIST);
358 session.removeAttribute(AppConstants.SI_BACKUP_FOR_REP_ID);
359 session.removeAttribute(AppConstants.SI_COLUMN_LOOKUP);
360 session.removeAttribute(AppConstants.SI_DASHBOARD_REP_ID);
361 session.removeAttribute(AppConstants.SI_DASHBOARD_REPORTRUNTIME_MAP);
362 session.removeAttribute(AppConstants.SI_DASHBOARD_REPORTRUNTIME);
363 session.removeAttribute(AppConstants.SI_DASHBOARD_REPORTDATA_MAP);
364 session.removeAttribute(AppConstants.SI_DASHBOARD_CHARTDATA_MAP);
365 session.removeAttribute(AppConstants.SI_DASHBOARD_DISPLAYTYPE_MAP);
366 session.removeAttribute(AppConstants.SI_DATA_SIZE_FOR_TEXTFIELD_POPUP);
367 session.removeAttribute(AppConstants.SI_MAP);
368 session.removeAttribute(AppConstants.SI_MAP_OBJECT);
369 session.removeAttribute(AppConstants.SI_REPORT_DEFINITION);
370 session.removeAttribute(AppConstants.SI_REPORT_RUNTIME);
371 session.removeAttribute(AppConstants.SI_REPORT_RUN_BACKUP);
372 session.removeAttribute(AppConstants.SI_REPORT_SCHEDULE);
373 session.removeAttribute(AppConstants.RI_REPORT_DATA);
374 session.removeAttribute(AppConstants.RI_CHART_DATA);
375 session.removeAttribute(AppConstants.SI_FORMFIELD_INFO);
376 session.removeAttribute(AppConstants.SI_FORMFIELD_DOWNLOAD_INFO);
377 } // clearReportRuntimeBackup
380 public String getTotalSql() {
384 public void setTotalSql(String totalSql) {
385 this.totalSql = totalSql;
390 /* public void drawD3Charts(HashMap<String,String> chartOptionsMap) {
391 drawD3Charts(chartOptionsMap);
396 public String drawD3Charts(HashMap<String,String> chartOptionsMap, HttpServletRequest request) {
398 ChartD3Helper chartHelper = new ChartD3Helper(reportRuntime);
399 chartHelper.setChartType(reportRuntime.getChartType());
401 return chartHelper.createVisualization(reportRuntime, chartOptionsMap, request);
402 } catch(RaptorException ex) {
403 ex.printStackTrace();
409 public String returnChartJSON(HashMap<String,String> chartOptionsMap, HttpServletRequest request, boolean showData) {
411 ChartJSONHelper chartJSONHelper = new ChartJSONHelper(reportRuntime);
412 chartJSONHelper.setChartType(reportRuntime.getChartType());
414 return chartJSONHelper.generateJSON(reportRuntime, chartOptionsMap, request, showData);
415 } catch(RaptorException ex) {
416 ex.printStackTrace();