summaryrefslogtreecommitdiffstats
path: root/ecomp-sdk/sdk-analytics/src/main/java/org/openecomp/portalsdk/analytics/model/runtime/ChartWebRuntime.java
blob: 3fb244020d3ebefd708f48b8c66b63ebf95af733 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
/*-
 * ================================================================================
 * eCOMP Portal SDK
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ================================================================================
 */
package org.openecomp.portalsdk.analytics.model.runtime;




import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.openecomp.portalsdk.analytics.error.RaptorException;
import org.openecomp.portalsdk.analytics.model.ReportHandler;
import org.openecomp.portalsdk.analytics.system.AppUtils;
import org.openecomp.portalsdk.analytics.util.AppConstants;
import org.openecomp.portalsdk.analytics.view.ReportData;
import org.openecomp.portalsdk.core.web.support.UserUtils;


public class ChartWebRuntime implements Serializable {
			

	    // Not used - planned to use if Hibernate used as data access layer
		private String runningDataQuery = "";
		private String runningCountQuery = "";
		//CONSTANTS FOR QUERY
		public final String QRY_COUNT_REPORT = ""; 
		public final String QRY_DATA_REPORT = ""; 
		
		// Not used planning to use when filter is used
		private StringBuffer whereClause = new StringBuffer("");
		// request used to grab request parameters
		private HttpServletRequest request;
	    
		
	    public ReportRuntime reportRuntime;
	    public ReportData reportData;
		
		//Used to pass user information
	    private final Map<String, Object> params = new HashMap<String, Object>();
	    
		//from chart generator retrieves list of charts to render
		public ArrayList chartList;
		public ArrayList infoList;
		
		private String totalSql;
		
		
		//
		private String drilldown_index = "0";
		
		public List getRolesCommaSeperated(HttpServletRequest request) {
			HashMap roles = UserUtils.getRoles(request);
			List roleList =  null;
			StringBuffer roleBuf = new StringBuffer("");
			int count = 0;
			if( roles != null ) {
				roleList = Arrays.asList(roles.keySet().toArray());
			}
			
			return roleList;
		}

	    
		public String getUserId(HttpServletRequest request) {
			return AppUtils.getUserID(request);
		}	

		public String generateChart(HttpServletRequest request) {
			return generateChart(request, true);
		}
		
		
		public String generateChart(HttpServletRequest request, boolean showData) {
			 //wire variables
			//processRecursive(this, this);
			long currentTime = System.currentTimeMillis();
	        HttpSession session = request.getSession();
			String action = nvl(request.getParameter(AppConstants.RI_ACTION), request.getParameter("action"));
			boolean genReportData = (!action.equals("chart.json") || action.equals("chart.data.json")); 


	        
	        final Long user_id = new Long((long) UserUtils.getUserId(request));

	        
	        boolean adminUser = false;
	        try {
	        adminUser = AppUtils.isAdminUser(request) || AppUtils.isSuperUser(request);
	        } catch (RaptorException ex) {
	        	ex.printStackTrace();
	        }
	        List roleList = getRolesCommaSeperated(request);
	        //final Map<String, Object> params = new HashMap<String, Object>();
	        params.put("user_id", user_id);
	        params.put("role_list", roleList);
	        //params.put("public_yn", "Y");
	                
			//String action = request.getParameter(AppConstants.RI_ACTION);
			String reportID = AppUtils.getRequestValue(request, AppConstants.RI_REPORT_ID);
			
			ReportHandler rh = new ReportHandler();
			ReportRuntime rr = null;
			try {
			 if(reportID !=null)	
				 rr = rh.loadReportRuntime(request, reportID, true, 1);		
			 	 if(rr.getReportType().equals(AppConstants.RT_HIVE)) {
					String sql = rr.getReportSQL();
					rr.setWholeSQL(sql);
					//if(genReportData)
			 		//reportData = rr.loadHiveLinearReportData(rr.getWholeSQL(),user_id.toString(), 10000,request);
			 	 } else {
						//if(genReportData)
			 		 //reportData 		= rr.loadReportData(0, user_id.toString(), 10000,request, false /*download*/);
			 	 }
			} catch (RaptorException ex) {
				ex.printStackTrace();
			}
	        setReportRuntime(rr);
	        setReportData( reportData);

	        reportRuntime = getReportRuntime();
	        reportData = getReportData();

	        
			HashMap<String, String> chartOptionsMap = new HashMap<String, String>();
			
			String rotateLabelsStr = "";
			rotateLabelsStr = AppUtils.nvl(reportRuntime.getLegendLabelAngle());
			if(rotateLabelsStr.toLowerCase().equals("standard")) {
				rotateLabelsStr = "0";
			} else if (rotateLabelsStr.toLowerCase().equals("up45")) {
				rotateLabelsStr = "45";
			} else if (rotateLabelsStr.toLowerCase().equals("down45")) {
				rotateLabelsStr = "-45";
			} else if (rotateLabelsStr.toLowerCase().equals("up90")) {
				rotateLabelsStr = "90";
			} else if (rotateLabelsStr.toLowerCase().equals("down90")) {
				rotateLabelsStr = "-90";
			} else
				rotateLabelsStr = "0";
			
			String width 							= (AppUtils.getRequestNvlValue(request, "width").length()>0?AppUtils.getRequestNvlValue(request, "width"):(AppUtils.nvl(reportRuntime.getChartWidth()).length()>0?reportRuntime.getChartWidth():"700"));
			String height 							= (AppUtils.getRequestNvlValue(request, "height").length()>0?AppUtils.getRequestNvlValue(request, "height"):(AppUtils.nvl(reportRuntime.getChartHeight()).length()>0?reportRuntime.getChartHeight():"300"));
			String animationStr 					= (AppUtils.getRequestNvlValue(request, "animation").length()>0?AppUtils.getRequestNvlValue(request, "animation"):new Boolean(reportRuntime.isAnimateAnimatedChart()).toString());
			
			String rotateLabels 					= (AppUtils.getRequestNvlValue(request, "rotateLabels").length()>0?AppUtils.getRequestNvlValue(request, "rotateLabels"):(rotateLabelsStr.length()>0?rotateLabelsStr:"0"));
			String staggerLabelsStr 				= (AppUtils.getRequestNvlValue(request, "staggerLabels").length()>0?AppUtils.getRequestNvlValue(request, "staggerLabels"):"false");
			String showMaxMinStr 					= (AppUtils.getRequestNvlValue(request, "showMaxMin").length()>0?AppUtils.getRequestNvlValue(request, "showMaxMin"):"false");
			String showControlsStr 					= (AppUtils.getRequestNvlValue(request, "showControls").length()>0?AppUtils.getRequestNvlValue(request, "showControls"):new Boolean(reportRuntime.displayBarControls()).toString());
			String showLegendStr 					= (AppUtils.getRequestNvlValue(request, "showLegend").length()>0?AppUtils.getRequestNvlValue(request, "showLegend"):new Boolean(!new Boolean(reportRuntime.hideChartLegend())).toString()); 
			String topMarginStr 					= AppUtils.getRequestNvlValue(request, "topMargin");
			String topMargin 						= (AppUtils.nvl(topMarginStr).length()<=0)?(reportRuntime.getTopMargin()!=null?reportRuntime.getTopMargin().toString():"30"):topMarginStr;
			String bottomMarginStr 					= AppUtils.getRequestNvlValue(request, "bottomMargin");
			String bottomMargin 					= (AppUtils.nvl(bottomMarginStr).length()<=0)?(reportRuntime.getBottomMargin()!=null?reportRuntime.getBottomMargin().toString():"50"):bottomMarginStr;
			String leftMarginStr 					= AppUtils.getRequestNvlValue(request, "leftMargin");
			String leftMargin 						= (AppUtils.nvl(leftMarginStr).length()<=0)?(reportRuntime.getLeftMargin()!=null?reportRuntime.getLeftMargin().toString():"100"):leftMarginStr;
			String rightMarginStr 					= AppUtils.getRequestNvlValue(request, "rightMargin");
			String rightMargin 						= (AppUtils.nvl(rightMarginStr).length()<=0)?(reportRuntime.getRightMargin()!=null?reportRuntime.getRightMargin().toString():"160"):rightMarginStr;
			String showTitleStr 					= (AppUtils.getRequestNvlValue(request, "showTitle").length()>0?AppUtils.getRequestNvlValue(request, "showTitle"):new Boolean(reportRuntime.displayChartTitle()).toString()); 
			String subType 							= AppUtils.getRequestNvlValue(request, "subType").length()>0?AppUtils.getRequestNvlValue(request, "subType"):(AppUtils.nvl(reportRuntime.getTimeSeriesRender()).equals("area")?reportRuntime.getTimeSeriesRender():"");
			String stackedStr 						= AppUtils.getRequestNvlValue(request, "stacked").length()>0?AppUtils.getRequestNvlValue(request, "stacked"):new Boolean(reportRuntime.isChartStacked()).toString();
			String horizontalBar 					= AppUtils.getRequestNvlValue(request, "horizontalBar").length()>0?AppUtils.getRequestNvlValue(request, "horizontalBar"):new Boolean(reportRuntime.isHorizontalOrientation()).toString();
			String barRealTimeAxis					= AppUtils.getRequestNvlValue(request, "barRealTimeAxis");
			String barReduceXAxisLabels				= AppUtils.getRequestNvlValue(request, "barReduceXAxisLabels").length()>0?AppUtils.getRequestNvlValue(request, "barReduceXAxisLabels"):new Boolean(reportRuntime.isLessXaxisTickers()).toString();;
			String timeAxis							= AppUtils.getRequestNvlValue(request, "timeAxis").length()>0?AppUtils.getRequestNvlValue(request, "timeAxis"):new Boolean(reportRuntime.isTimeAxis()).toString();
			String logScale 						= AppUtils.getRequestNvlValue(request, "logScale").length()>0?AppUtils.getRequestNvlValue(request, "logScale"):new Boolean(reportRuntime.isLogScale()).toString();
			String precision 						= AppUtils.getRequestNvlValue(request, "precision").length()>0?AppUtils.getRequestNvlValue(request, "precision"):"2";

	/*		boolean animation = AppUtils.getRequestFlag(request, "animation");
			boolean staggerLabels = AppUtils.getRequestFlag(request, "staggerLabels");
			boolean showMaxMin = (showMaxMinStr.length()<=0)?false:Boolean.parseBoolean(showMaxMinStr);
			boolean showControls = (showControlsStr.length()<=0)?true:Boolean.parseBoolean(showControlsStr);
			boolean showLegend = (showLegendStr.length()<=0)?true:Boolean.parseBoolean(showLegendStr);
			boolean showTitle = (showTitleStr.length()<=0)?true:Boolean.parseBoolean(showTitleStr);
			boolean stacked = (stackedStr.length()<=0)?true:Boolean.parseBoolean(stackedStr);
	*/		
			// Add all options to Map
			chartOptionsMap.put("width", width);
			chartOptionsMap.put("height", height);
			chartOptionsMap.put("animation", animationStr);
			chartOptionsMap.put("rotateLabels", rotateLabels);
			chartOptionsMap.put("staggerLabels", staggerLabelsStr);
			chartOptionsMap.put("showMaxMin", showMaxMinStr);
			chartOptionsMap.put("showControls", showControlsStr);
			chartOptionsMap.put("showLegend", showLegendStr);
			chartOptionsMap.put("topMargin", topMargin);
			chartOptionsMap.put("bottomMargin", bottomMargin);
			chartOptionsMap.put("leftMargin", leftMargin);
			chartOptionsMap.put("rightMargin", rightMargin);
			chartOptionsMap.put("showTitle", showTitleStr);
			chartOptionsMap.put("subType", subType);
			chartOptionsMap.put("stacked", stackedStr);
			chartOptionsMap.put("horizontalBar", horizontalBar);
			chartOptionsMap.put("timeAxis", timeAxis);
			chartOptionsMap.put("barRealTimeAxis", barRealTimeAxis);
			chartOptionsMap.put("barReduceXAxisLabels", barReduceXAxisLabels);
			
			chartOptionsMap.put("logScale", logScale);
			chartOptionsMap.put("precision", precision);
			

	        
	        if(reportRuntime!=null) {
	        	StringBuffer title = new StringBuffer("");
	        	title.append(reportRuntime.getReportName());
	        }
	        
	        if(! (action.equals("chart.json") || action.equals("chart.data.json"))) {
	        	
				
	            //Chart
				String chartType = reportRuntime.getChartType();
				return drawD3Charts(chartOptionsMap, request);
				//drawD3Charts();
	        } else /*if (action.equals("chart.json"))*/ {
				String chartType = reportRuntime.getChartType();
				return returnChartJSON(chartOptionsMap, request, showData);

			        
	        } /*else {
	        	
	        	return ("Internal Error Occurred.");
	        }*/
	        
		}
		
		
		public String nvl(String s) {
			return (s == null) ? "" : s;
		}

		/**
		 * @return the reportRuntime
		 */
		public ReportRuntime getReportRuntime() {
			return reportRuntime;
		}

		/**
		 * @param reportRuntime the reportRuntime to set
		 */
		public void setReportRuntime(ReportRuntime reportRuntime) {
			this.reportRuntime = reportRuntime;
		} 

		/**
		 * @return the reportData
		 */
		public ReportData getReportData() {
			return reportData;
		}

		/**
		 * @param reportData the reportData to set
		 */
		public void setReportData(ReportData reportData) {
			this.reportData = reportData;
		}	

		public boolean isNull(String a) {
			if ((a == null) || (a.length() == 0) || a.equalsIgnoreCase("null"))
				return true;
			else
				return false;
		}
		

		protected String nvl(String s, String sDefault) {
			return nvl(s).equals("") ? sDefault : s;
		}

		protected static String nvls(String s) {
			return (s == null) ? "" : s;
		}

		protected static String nvls(String s, String sDefault) {
			return nvls(s).equals("") ? sDefault : s;
		}
		
		protected boolean getFlagInBoolean(String s) {
			return nvl(s).toUpperCase().startsWith("Y") || nvl(s).toLowerCase().equals("true");
		}
		
	    
		/**
		 * @return the chartList
		 */
		public ArrayList getChartList() {
			return chartList;
		}

		/**
		 * @param chartList the chartList to set
		 */
		public void setChartList(ArrayList chartList) {
			this.chartList = chartList;
		}

		/**
		 * @return the infoList
		 */
		public ArrayList getInfoList() {
			return infoList;
		}

		/**
		 * @param infoList the infoList to set
		 */
		public void setInfoList(ArrayList infoList) {
			this.infoList = infoList;
		}

		
		
		private void clearReportRuntimeBackup(HttpSession session, HttpServletRequest request) {
			session.removeAttribute(AppConstants.DRILLDOWN_REPORTS_LIST);
			request.removeAttribute(AppConstants.DRILLDOWN_INDEX);
			session.removeAttribute(AppConstants.DRILLDOWN_INDEX);
			request.removeAttribute(AppConstants.FORM_DRILLDOWN_INDEX);
			session.removeAttribute(AppConstants.FORM_DRILLDOWN_INDEX);
			Enumeration<String> enum1 = session.getAttributeNames();
			String attributeName = "";
			while(enum1.hasMoreElements()) {
				attributeName = enum1.nextElement();
				if(attributeName.startsWith("parent_")) {
					session.removeAttribute(attributeName);
				}
			}
	        session.removeAttribute(AppConstants.DRILLDOWN_REPORTS_LIST);
			session.removeAttribute(AppConstants.SI_BACKUP_FOR_REP_ID);
			session.removeAttribute(AppConstants.SI_COLUMN_LOOKUP);
	        session.removeAttribute(AppConstants.SI_DASHBOARD_REP_ID);
	        session.removeAttribute(AppConstants.SI_DASHBOARD_REPORTRUNTIME_MAP);
	        session.removeAttribute(AppConstants.SI_DASHBOARD_REPORTRUNTIME);
	        session.removeAttribute(AppConstants.SI_DASHBOARD_REPORTDATA_MAP);
	        session.removeAttribute(AppConstants.SI_DASHBOARD_CHARTDATA_MAP);
			session.removeAttribute(AppConstants.SI_DASHBOARD_DISPLAYTYPE_MAP);
	    	session.removeAttribute(AppConstants.SI_DATA_SIZE_FOR_TEXTFIELD_POPUP);
	    	session.removeAttribute(AppConstants.SI_MAP);
			session.removeAttribute(AppConstants.SI_MAP_OBJECT);
	        session.removeAttribute(AppConstants.SI_REPORT_DEFINITION);			
	        session.removeAttribute(AppConstants.SI_REPORT_RUNTIME);			
			session.removeAttribute(AppConstants.SI_REPORT_RUN_BACKUP);
	        session.removeAttribute(AppConstants.SI_REPORT_SCHEDULE);
	    	session.removeAttribute(AppConstants.RI_REPORT_DATA);
	    	session.removeAttribute(AppConstants.RI_CHART_DATA);
	    	session.removeAttribute(AppConstants.SI_FORMFIELD_INFO);
	    	session.removeAttribute(AppConstants.SI_FORMFIELD_DOWNLOAD_INFO);
		} // clearReportRuntimeBackup


		public String getTotalSql() {
			return totalSql;
		}

		public void setTotalSql(String totalSql) {
			this.totalSql = totalSql;
		}
		
		
	 
	/*    public void drawD3Charts(HashMap<String,String> chartOptionsMap) {
	        drawD3Charts(chartOptionsMap);

	    }
	*/    
	    
	    public String drawD3Charts(HashMap<String,String> chartOptionsMap, HttpServletRequest request) {
			
	        ChartD3Helper chartHelper = new ChartD3Helper(reportRuntime);
	        chartHelper.setChartType(reportRuntime.getChartType());
			try {
				return chartHelper.createVisualization(reportRuntime, chartOptionsMap, request);
			} catch(RaptorException ex) {
				ex.printStackTrace();
			}
			return "";
	       
	    }
		
	    public String returnChartJSON(HashMap<String,String> chartOptionsMap, HttpServletRequest request, boolean showData) {
			
	        ChartJSONHelper chartJSONHelper = new ChartJSONHelper(reportRuntime);
	        chartJSONHelper.setChartType(reportRuntime.getChartType());
			try {
				return chartJSONHelper.generateJSON(reportRuntime, chartOptionsMap, request, showData);
			} catch(RaptorException ex) {
				ex.printStackTrace();
			}
			return "";
	       
	    }
	    
	}