b3e8e67878f43287c04f52815e59071c76229eb0
[portal/sdk.git] /
1 /*-
2  * ================================================================================
3  * ECOMP Portal SDK
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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  * ================================================================================
19  */
20 package org.openecomp.portalsdk.analytics.model.runtime;
21
22
23
24
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;
31 import java.util.Map;
32
33 import javax.servlet.http.HttpServletRequest;
34 import javax.servlet.http.HttpSession;
35
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;
45
46
47 public class ChartWebRuntime implements Serializable {
48                         
49
50             // Not used - planned to use if Hibernate used as data access layer
51                 private String runningDataQuery = "";
52                 private String runningCountQuery = "";
53                 //CONSTANTS FOR QUERY
54                 public final String QRY_COUNT_REPORT = ""; 
55                 public final String QRY_DATA_REPORT = ""; 
56                 
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;
61             
62                 
63             public ReportRuntime reportRuntime;
64             public ReportData reportData;
65                 
66                 //Used to pass user information
67             private final Map<String, Object> params = new HashMap<String, Object>();
68             
69                 //from chart generator retrieves list of charts to render
70                 public ArrayList chartList;
71                 public ArrayList infoList;
72                 
73                 private String totalSql;
74                 
75                 
76                 //
77                 private String drilldown_index = "0";
78                 
79                 public List getRolesCommaSeperated(HttpServletRequest request) {
80                         HashMap roles = UserUtils.getRoles(request);
81                         List roleList =  null;
82                         StringBuffer roleBuf = new StringBuffer("");
83                         int count = 0;
84                         if( roles != null ) {
85                                 roleList = Arrays.asList(roles.keySet().toArray());
86                         }
87                         
88                         return roleList;
89                 }
90
91             
92                 public String getUserId(HttpServletRequest request) {
93                         return AppUtils.getUserID(request);
94                 }       
95
96                 public String generateChart(HttpServletRequest request) {
97                         return generateChart(request, true);
98                 }
99                 
100                 
101                 public String generateChart(HttpServletRequest request, boolean showData) {
102                          //wire variables
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")); 
108
109
110                 
111                 final Long user_id = new Long((long) UserUtils.getUserId(request));
112
113                 
114                 boolean adminUser = false;
115                 try {
116                 adminUser = AppUtils.isAdminUser(request) || AppUtils.isSuperUser(request);
117                 } catch (RaptorException ex) {
118                         ex.printStackTrace();
119                 }
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");
125                         
126                         //String action = request.getParameter(AppConstants.RI_ACTION);
127                         String reportID = AppUtils.getRequestValue(request, AppConstants.RI_REPORT_ID);
128                         
129                         ReportHandler rh = new ReportHandler();
130                         ReportRuntime rr = null;
131                         try {
132                          if(reportID !=null)    
133                                  rr = rh.loadReportRuntime(request, reportID, true, 1);         
134                                  if(rr.getReportType().equals(AppConstants.RT_HIVE)) {
135                                         String sql = rr.getReportSQL();
136                                         rr.setWholeSQL(sql);
137                                         //if(genReportData)
138                                         //reportData = rr.loadHiveLinearReportData(rr.getWholeSQL(),user_id.toString(), 10000,request);
139                                  } else {
140                                         if(genReportData)
141                                          reportData             = rr.loadReportData(0, user_id.toString(), 10000,request, false /*download*/);
142                                  }
143                         } catch (RaptorException ex) {
144                                 ex.printStackTrace();
145                         }
146                 setReportRuntime(rr);
147                 setReportData( reportData);
148
149                 reportRuntime = getReportRuntime();
150                 reportData = getReportData();
151
152                 
153                         HashMap<String, String> chartOptionsMap = new HashMap<String, String>();
154                         
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";
167                         } else
168                                 rotateLabelsStr = "0";
169                         
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());
173                         
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";
196
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);
204         */              
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);
225                         
226                         chartOptionsMap.put("logScale", logScale);
227                         chartOptionsMap.put("precision", precision);
228                         
229
230                 
231                 if(reportRuntime!=null) {
232                         StringBuffer title = new StringBuffer("");
233                         title.append(reportRuntime.getReportName());
234                 }
235                 
236                 if(! (action.equals("chart.json") || action.equals("chart.data.json"))) {
237                         
238                                 
239                     //Chart
240                                 String chartType = reportRuntime.getChartType();
241                                 return drawD3Charts(chartOptionsMap, request);
242                                 //drawD3Charts();
243                 } else /*if (action.equals("chart.json"))*/ {
244                                 String chartType = reportRuntime.getChartType();
245                                 return returnChartJSON(chartOptionsMap, request, showData);
246
247                                 
248                 } /*else {
249                         
250                         return ("Internal Error Occurred.");
251                 }*/
252                 
253                 }
254                 
255                 
256                 public String nvl(String s) {
257                         return (s == null) ? "" : s;
258                 }
259
260                 /**
261                  * @return the reportRuntime
262                  */
263                 public ReportRuntime getReportRuntime() {
264                         return reportRuntime;
265                 }
266
267                 /**
268                  * @param reportRuntime the reportRuntime to set
269                  */
270                 public void setReportRuntime(ReportRuntime reportRuntime) {
271                         this.reportRuntime = reportRuntime;
272                 } 
273
274                 /**
275                  * @return the reportData
276                  */
277                 public ReportData getReportData() {
278                         return reportData;
279                 }
280
281                 /**
282                  * @param reportData the reportData to set
283                  */
284                 public void setReportData(ReportData reportData) {
285                         this.reportData = reportData;
286                 }       
287
288                 public boolean isNull(String a) {
289                         if ((a == null) || (a.length() == 0) || a.equalsIgnoreCase("null"))
290                                 return true;
291                         else
292                                 return false;
293                 }
294                 
295
296                 protected String nvl(String s, String sDefault) {
297                         return nvl(s).equals("") ? sDefault : s;
298                 }
299
300                 protected static String nvls(String s) {
301                         return (s == null) ? "" : s;
302                 }
303
304                 protected static String nvls(String s, String sDefault) {
305                         return nvls(s).equals("") ? sDefault : s;
306                 }
307                 
308                 protected boolean getFlagInBoolean(String s) {
309                         return nvl(s).toUpperCase().startsWith("Y") || nvl(s).toLowerCase().equals("true");
310                 }
311                 
312             
313                 /**
314                  * @return the chartList
315                  */
316                 public ArrayList getChartList() {
317                         return chartList;
318                 }
319
320                 /**
321                  * @param chartList the chartList to set
322                  */
323                 public void setChartList(ArrayList chartList) {
324                         this.chartList = chartList;
325                 }
326
327                 /**
328                  * @return the infoList
329                  */
330                 public ArrayList getInfoList() {
331                         return infoList;
332                 }
333
334                 /**
335                  * @param infoList the infoList to set
336                  */
337                 public void setInfoList(ArrayList infoList) {
338                         this.infoList = infoList;
339                 }
340
341                 
342                 
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);
355                                 }
356                         }
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
378
379
380                 public String getTotalSql() {
381                         return totalSql;
382                 }
383
384                 public void setTotalSql(String totalSql) {
385                         this.totalSql = totalSql;
386                 }
387                 
388                 
389          
390         /*    public void drawD3Charts(HashMap<String,String> chartOptionsMap) {
391                 drawD3Charts(chartOptionsMap);
392
393             }
394         */    
395             
396             public String drawD3Charts(HashMap<String,String> chartOptionsMap, HttpServletRequest request) {
397                         
398                 ChartD3Helper chartHelper = new ChartD3Helper(reportRuntime);
399                 chartHelper.setChartType(reportRuntime.getChartType());
400                         try {
401                                 return chartHelper.createVisualization(reportRuntime, chartOptionsMap, request);
402                         } catch(RaptorException ex) {
403                                 ex.printStackTrace();
404                         }
405                         return "";
406                
407             }
408                 
409             public String returnChartJSON(HashMap<String,String> chartOptionsMap, HttpServletRequest request, boolean showData) {
410                         
411                 ChartJSONHelper chartJSONHelper = new ChartJSONHelper(reportRuntime);
412                 chartJSONHelper.setChartType(reportRuntime.getChartType());
413                         try {
414                                 return chartJSONHelper.generateJSON(reportRuntime, chartOptionsMap, request, showData);
415                         } catch(RaptorException ex) {
416                                 ex.printStackTrace();
417                         }
418                         return "";
419                
420             }
421             
422         }       
423