d10cd1c4a268d046d0aa3a6b1b280283b3a6619b
[portal/sdk.git] /
1 /*
2  * ============LICENSE_START==========================================
3  * ONAP Portal SDK
4  * ===================================================================
5  * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6  * ===================================================================
7  *
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
12  *
13  *             http://www.apache.org/licenses/LICENSE-2.0
14  *
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.
20  *
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
25  *
26  *             https://creativecommons.org/licenses/by/4.0/
27  *
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.
33  *
34  * ============LICENSE_END============================================
35  *
36  * 
37  */
38 package org.onap.portalsdk.analytics.model.runtime;
39
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;
45 import java.util.Map;
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;
55
56
57 public class ChartWebRuntime implements Serializable {
58
59     private static final EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(ChartWebRuntime.class);
60
61     // CONSTANTS FOR QUERY
62     public final String QRY_COUNT_REPORT = "";
63     public final String QRY_DATA_REPORT = "";
64
65     public ReportRuntime reportRuntime;
66     public ReportData reportData;
67
68     // Used to pass user information
69     private final transient Map<String, Object> params = new HashMap<>();
70
71     // from chart generator retrieves list of charts to render
72     public transient List chartList;
73     public transient List infoList;
74
75     private String totalSql;
76
77
78     public List getRolesCommaSeperated(HttpServletRequest request) {
79         Map roles = UserUtils.getRoles(request);
80         List roleList = null;
81         if (roles != null) {
82             roleList = Arrays.asList(roles.keySet().toArray());
83         }
84
85         return roleList;
86     }
87
88
89     public String getUserId(HttpServletRequest request) {
90         return AppUtils.getUserID(request);
91     }
92
93     public String generateChart(HttpServletRequest request) {
94         return generateChart(request, true);
95     }
96
97
98     public String generateChart(HttpServletRequest request, boolean showData) {
99         System.currentTimeMillis();
100         request.getSession();
101         String action =
102                 nvl(request.getParameter(AppConstants.RI_ACTION), request.getParameter("action"));
103         boolean genReportData = (!"chart.json".equals(action) || "chart.data.json".equals(action));
104
105
106
107         final Long user_id = Long.valueOf(UserUtils.getUserId(request));
108
109
110         try {
111             if(AppUtils.isAdminUser(request) || AppUtils.isSuperUser(request)) {
112                 logger.debug(EELFLoggerDelegate.debugLogger, "Request by Admin or Super User");
113             }
114         } catch (RaptorException ex) {
115             logger.error(EELFLoggerDelegate.errorLogger, "RaptorException occurred in generateChart",ex);
116         }
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");
122
123         // String action = request.getParameter(AppConstants.RI_ACTION);
124         String reportID = AppUtils.getRequestValue(request, AppConstants.RI_REPORT_ID);
125
126         ReportHandler rh = new ReportHandler();
127         ReportRuntime rr = null;
128         try {
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();
133                 rr.setWholeSQL(sql);
134                 // if(genReportData)
135                 // reportData = rr.loadHiveLinearReportData(rr.getWholeSQL(),user_id.toString(),
136                 // 10000,request);
137             } else {
138                 if (genReportData)
139                     reportData = rr.loadReportData(0, user_id.toString(), 10000, request,
140                             false /* download */);
141             }
142         } catch (RaptorException ex) {
143             logger.error(EELFLoggerDelegate.errorLogger, "RaptorException occurred in generateChart",ex);
144         }
145         setReportRuntime(rr);
146         setReportData(reportData);
147
148         reportRuntime = getReportRuntime();
149         reportData = getReportData();
150
151
152         HashMap<String, String> chartOptionsMap = new HashMap<>();
153
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";
166         } else
167             rotateLabelsStr = "0";
168
169         String width = (AppUtils.getRequestNvlValue(request, "width").length() > 0
170                 ? AppUtils.getRequestNvlValue(request, "width")
171                 : (AppUtils.nvl(reportRuntime.getChartWidth()).length() > 0
172                         ? reportRuntime.getChartWidth()
173                         : "700"));
174         String height = (AppUtils.getRequestNvlValue(request, "height").length() > 0
175                 ? AppUtils.getRequestNvlValue(request, "height")
176                 : (AppUtils.nvl(reportRuntime.getChartHeight()).length() > 0
177                         ? reportRuntime.getChartHeight()
178                         : "300"));
179         String animationStr = (AppUtils.getRequestNvlValue(request, "animation").length() > 0
180                 ? AppUtils.getRequestNvlValue(request, "animation")
181                 : Boolean.toString(reportRuntime.isAnimateAnimatedChart()));
182
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")
189                         : "false");
190         String showMaxMinStr = (AppUtils.getRequestNvlValue(request, "showMaxMin").length() > 0
191                 ? AppUtils.getRequestNvlValue(request, "showMaxMin")
192                 : "false");
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()
202                         : "30")
203                 : topMarginStr;
204         String bottomMarginStr = AppUtils.getRequestNvlValue(request, "bottomMargin");
205         String bottomMargin = (AppUtils.nvl(bottomMarginStr).length() <= 0)
206                 ? (reportRuntime.getBottomMargin() != null
207                         ? reportRuntime.getBottomMargin().toString()
208                         : "50")
209                 : bottomMarginStr;
210         String leftMarginStr = AppUtils.getRequestNvlValue(request, "leftMargin");
211         String leftMargin = (AppUtils.nvl(leftMarginStr).length() <= 0)
212                 ? (reportRuntime.getLeftMargin() != null ? reportRuntime.getLeftMargin().toString()
213                         : "100")
214                 : leftMarginStr;
215         String rightMarginStr = AppUtils.getRequestNvlValue(request, "rightMargin");
216         String rightMargin = (AppUtils.nvl(rightMarginStr).length() <= 0)
217                 ? (reportRuntime.getRightMargin() != null
218                         ? reportRuntime.getRightMargin().toString()
219                         : "160")
220                 : rightMarginStr;
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()
228                         : "");
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")
248                 : "2";
249
250         /*
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);
259          */
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);
280
281         chartOptionsMap.put("logScale", logScale);
282         chartOptionsMap.put("precision", precision);
283
284
285
286         if (reportRuntime != null) {
287             StringBuilder title = new StringBuilder("");
288             title.append(reportRuntime.getReportName());
289         }
290
291         if (!("chart.json".equals(action) || "chart.data.json".equals(action))) {
292
293
294             reportRuntime.getChartType();
295             return drawD3Charts(chartOptionsMap, request);
296             // drawD3Charts();
297         } else /* if (action.equals("chart.json")) */ {
298             reportRuntime.getChartType();
299             return returnChartJSON(chartOptionsMap, request, showData);
300
301
302         } /*
303            * else {
304            *
305            * return ("Internal Error Occurred."); }
306            */
307
308     }
309
310
311     public String nvl(String s) {
312         return (s == null) ? "" : s;
313     }
314
315     /**
316      * @return the reportRuntime
317      */
318     public ReportRuntime getReportRuntime() {
319         return reportRuntime;
320     }
321
322     /**
323      * @param reportRuntime the reportRuntime to set
324      */
325     public void setReportRuntime(ReportRuntime reportRuntime) {
326         this.reportRuntime = reportRuntime;
327     }
328
329     /**
330      * @return the reportData
331      */
332     public ReportData getReportData() {
333         return reportData;
334     }
335
336     /**
337      * @param reportData the reportData to set
338      */
339     public void setReportData(ReportData reportData) {
340         this.reportData = reportData;
341     }
342
343     public boolean isNull(String a) {
344         if ((a == null) || (a.length() == 0) || a.equalsIgnoreCase("null"))
345             return true;
346         else
347             return false;
348     }
349
350
351     protected String nvl(String s, String sDefault) {
352         return nvl(s).equals("") ? sDefault : s;
353     }
354
355     protected static String nvls(String s) {
356         return (s == null) ? "" : s;
357     }
358
359     protected static String nvls(String s, String sDefault) {
360         return nvls(s).equals("") ? sDefault : s;
361     }
362
363     protected boolean getFlagInBoolean(String s) {
364         return nvl(s).toUpperCase().startsWith("Y") || nvl(s).equalsIgnoreCase("true");
365     }
366
367
368     /**
369      * @return the chartList
370      */
371     public List getChartList() {
372         return chartList;
373     }
374
375     /**
376      * @param chartList the chartList to set
377      */
378     public void setChartList(List chartList) {
379         this.chartList = chartList;
380     }
381
382     /**
383      * @return the infoList
384      */
385     public List getInfoList() {
386         return infoList;
387     }
388
389     /**
390      * @param infoList the infoList to set
391      */
392     public void setInfoList(List infoList) {
393         this.infoList = infoList;
394     }
395
396
397
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);
410             }
411         }
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
433
434
435     public String getTotalSql() {
436         return totalSql;
437     }
438
439     public void setTotalSql(String totalSql) {
440         this.totalSql = totalSql;
441     }
442
443
444
445     /*
446      * public void drawD3Charts(HashMap<String,String> chartOptionsMap) {
447      * drawD3Charts(chartOptionsMap);
448      *
449      * }
450      */
451
452     public String drawD3Charts(Map<String, String> chartOptionsMap, HttpServletRequest request) {
453
454         ChartD3Helper chartHelper = new ChartD3Helper(reportRuntime);
455         chartHelper.setChartType(reportRuntime.getChartType());
456
457         return chartHelper.createVisualization(reportRuntime, chartOptionsMap, request);
458     }
459
460     public String returnChartJSON(Map<String, String> chartOptionsMap,
461             HttpServletRequest request, boolean showData) {
462
463         ChartJSONHelper chartJSONHelper = new ChartJSONHelper(reportRuntime);
464         chartJSONHelper.setChartType(reportRuntime.getChartType());
465         try {
466             return chartJSONHelper.generateJSON(reportRuntime, chartOptionsMap, request, showData);
467         } catch (RaptorException ex) {
468             logger.error(EELFLoggerDelegate.errorLogger, "RaptorException occurred in returnChartJSON",ex);
469         }
470         return "";
471
472     }
473
474 }
475