e023b8c030425c46de040af350ea8a2953b410ee
[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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
37  */
38 package org.onap.portalsdk.analytics.model.runtime;
39
40
41
42
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;
49 import java.util.Map;
50
51 import javax.servlet.http.HttpServletRequest;
52 import javax.servlet.http.HttpSession;
53
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;
63
64
65 public class ChartWebRuntime implements Serializable {
66                         
67
68             // Not used - planned to use if Hibernate used as data access layer
69                 private String runningDataQuery = "";
70                 private String runningCountQuery = "";
71                 //CONSTANTS FOR QUERY
72                 public final String QRY_COUNT_REPORT = ""; 
73                 public final String QRY_DATA_REPORT = ""; 
74                 
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;
79             
80                 
81             public ReportRuntime reportRuntime;
82             public ReportData reportData;
83                 
84                 //Used to pass user information
85             private final Map<String, Object> params = new HashMap<String, Object>();
86             
87                 //from chart generator retrieves list of charts to render
88                 public ArrayList chartList;
89                 public ArrayList infoList;
90                 
91                 private String totalSql;
92                 
93                 
94                 //
95                 private String drilldown_index = "0";
96                 
97                 public List getRolesCommaSeperated(HttpServletRequest request) {
98                         HashMap roles = UserUtils.getRoles(request);
99                         List roleList =  null;
100                         StringBuffer roleBuf = new StringBuffer("");
101                         int count = 0;
102                         if( roles != null ) {
103                                 roleList = Arrays.asList(roles.keySet().toArray());
104                         }
105                         
106                         return roleList;
107                 }
108
109             
110                 public String getUserId(HttpServletRequest request) {
111                         return AppUtils.getUserID(request);
112                 }       
113
114                 public String generateChart(HttpServletRequest request) {
115                         return generateChart(request, true);
116                 }
117                 
118                 
119                 public String generateChart(HttpServletRequest request, boolean showData) {
120                          //wire variables
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")); 
126
127
128                 
129                 final Long user_id = new Long((long) UserUtils.getUserId(request));
130
131                 
132                 boolean adminUser = false;
133                 try {
134                 adminUser = AppUtils.isAdminUser(request) || AppUtils.isSuperUser(request);
135                 } catch (RaptorException ex) {
136                         ex.printStackTrace();
137                 }
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");
143                         
144                         //String action = request.getParameter(AppConstants.RI_ACTION);
145                         String reportID = AppUtils.getRequestValue(request, AppConstants.RI_REPORT_ID);
146                         
147                         ReportHandler rh = new ReportHandler();
148                         ReportRuntime rr = null;
149                         try {
150                          if(reportID !=null)    
151                                  rr = rh.loadReportRuntime(request, reportID, true, 1);         
152                                  if(rr.getReportType().equals(AppConstants.RT_HIVE)) {
153                                         String sql = rr.getReportSQL();
154                                         rr.setWholeSQL(sql);
155                                         //if(genReportData)
156                                         //reportData = rr.loadHiveLinearReportData(rr.getWholeSQL(),user_id.toString(), 10000,request);
157                                  } else {
158                                         if(genReportData)
159                                          reportData             = rr.loadReportData(0, user_id.toString(), 10000,request, false /*download*/);
160                                  }
161                         } catch (RaptorException ex) {
162                                 ex.printStackTrace();
163                         }
164                 setReportRuntime(rr);
165                 setReportData( reportData);
166
167                 reportRuntime = getReportRuntime();
168                 reportData = getReportData();
169
170                 
171                         HashMap<String, String> chartOptionsMap = new HashMap<String, String>();
172                         
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";
185                         } else
186                                 rotateLabelsStr = "0";
187                         
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());
191                         
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";
214
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);
222         */              
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);
243                         
244                         chartOptionsMap.put("logScale", logScale);
245                         chartOptionsMap.put("precision", precision);
246                         
247
248                 
249                 if(reportRuntime!=null) {
250                         StringBuffer title = new StringBuffer("");
251                         title.append(reportRuntime.getReportName());
252                 }
253                 
254                 if(! (action.equals("chart.json") || action.equals("chart.data.json"))) {
255                         
256                                 
257                     //Chart
258                                 String chartType = reportRuntime.getChartType();
259                                 return drawD3Charts(chartOptionsMap, request);
260                                 //drawD3Charts();
261                 } else /*if (action.equals("chart.json"))*/ {
262                                 String chartType = reportRuntime.getChartType();
263                                 return returnChartJSON(chartOptionsMap, request, showData);
264
265                                 
266                 } /*else {
267                         
268                         return ("Internal Error Occurred.");
269                 }*/
270                 
271                 }
272                 
273                 
274                 public String nvl(String s) {
275                         return (s == null) ? "" : s;
276                 }
277
278                 /**
279                  * @return the reportRuntime
280                  */
281                 public ReportRuntime getReportRuntime() {
282                         return reportRuntime;
283                 }
284
285                 /**
286                  * @param reportRuntime the reportRuntime to set
287                  */
288                 public void setReportRuntime(ReportRuntime reportRuntime) {
289                         this.reportRuntime = reportRuntime;
290                 } 
291
292                 /**
293                  * @return the reportData
294                  */
295                 public ReportData getReportData() {
296                         return reportData;
297                 }
298
299                 /**
300                  * @param reportData the reportData to set
301                  */
302                 public void setReportData(ReportData reportData) {
303                         this.reportData = reportData;
304                 }       
305
306                 public boolean isNull(String a) {
307                         if ((a == null) || (a.length() == 0) || a.equalsIgnoreCase("null"))
308                                 return true;
309                         else
310                                 return false;
311                 }
312                 
313
314                 protected String nvl(String s, String sDefault) {
315                         return nvl(s).equals("") ? sDefault : s;
316                 }
317
318                 protected static String nvls(String s) {
319                         return (s == null) ? "" : s;
320                 }
321
322                 protected static String nvls(String s, String sDefault) {
323                         return nvls(s).equals("") ? sDefault : s;
324                 }
325                 
326                 protected boolean getFlagInBoolean(String s) {
327                         return nvl(s).toUpperCase().startsWith("Y") || nvl(s).toLowerCase().equals("true");
328                 }
329                 
330             
331                 /**
332                  * @return the chartList
333                  */
334                 public ArrayList getChartList() {
335                         return chartList;
336                 }
337
338                 /**
339                  * @param chartList the chartList to set
340                  */
341                 public void setChartList(ArrayList chartList) {
342                         this.chartList = chartList;
343                 }
344
345                 /**
346                  * @return the infoList
347                  */
348                 public ArrayList getInfoList() {
349                         return infoList;
350                 }
351
352                 /**
353                  * @param infoList the infoList to set
354                  */
355                 public void setInfoList(ArrayList infoList) {
356                         this.infoList = infoList;
357                 }
358
359                 
360                 
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);
373                                 }
374                         }
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
396
397
398                 public String getTotalSql() {
399                         return totalSql;
400                 }
401
402                 public void setTotalSql(String totalSql) {
403                         this.totalSql = totalSql;
404                 }
405                 
406                 
407          
408         /*    public void drawD3Charts(HashMap<String,String> chartOptionsMap) {
409                 drawD3Charts(chartOptionsMap);
410
411             }
412         */    
413             
414             public String drawD3Charts(HashMap<String,String> chartOptionsMap, HttpServletRequest request) {
415                         
416                 ChartD3Helper chartHelper = new ChartD3Helper(reportRuntime);
417                 chartHelper.setChartType(reportRuntime.getChartType());
418                         try {
419                                 return chartHelper.createVisualization(reportRuntime, chartOptionsMap, request);
420                         } catch(RaptorException ex) {
421                                 ex.printStackTrace();
422                         }
423                         return "";
424                
425             }
426                 
427             public String returnChartJSON(HashMap<String,String> chartOptionsMap, HttpServletRequest request, boolean showData) {
428                         
429                 ChartJSONHelper chartJSONHelper = new ChartJSONHelper(reportRuntime);
430                 chartJSONHelper.setChartType(reportRuntime.getChartType());
431                         try {
432                                 return chartJSONHelper.generateJSON(reportRuntime, chartOptionsMap, request, showData);
433                         } catch(RaptorException ex) {
434                                 ex.printStackTrace();
435                         }
436                         return "";
437                
438             }
439             
440         }       
441