summaryrefslogtreecommitdiffstats
path: root/ecomp-sdk/sdk-analytics/src/main/java/org/openecomp/portalsdk/analytics/system/fusion/web/RaptorControllerAsync.java
blob: bd3fdce3b33b9116bc959a0bcf64084de1cb218f (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
/*-
 * ================================================================================
 * 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.system.fusion.web;


import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

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

import org.openecomp.portalsdk.analytics.controller.Action;
import org.openecomp.portalsdk.analytics.controller.ErrorHandler;
import org.openecomp.portalsdk.analytics.error.RaptorException;
import org.openecomp.portalsdk.analytics.error.RaptorRuntimeException;
import org.openecomp.portalsdk.analytics.model.ReportHandler;
import org.openecomp.portalsdk.analytics.model.pdf.PdfReportHandler;
import org.openecomp.portalsdk.analytics.model.runtime.CategoryAxisJSON;
import org.openecomp.portalsdk.analytics.model.runtime.ChartJSON;
import org.openecomp.portalsdk.analytics.model.runtime.RangeAxisJSON;
import org.openecomp.portalsdk.analytics.model.runtime.ReportRuntime;
import org.openecomp.portalsdk.analytics.system.AppUtils;
import org.openecomp.portalsdk.analytics.system.Globals;
import org.openecomp.portalsdk.analytics.util.AppConstants;
import org.openecomp.portalsdk.analytics.view.ReportData;
import org.openecomp.portalsdk.analytics.xmlobj.DataColumnType;
import org.openecomp.portalsdk.core.controller.UnRestrictedBaseController;
import org.openecomp.portalsdk.core.domain.User;
import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
import org.openecomp.portalsdk.core.web.support.UserUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping("/")
public class RaptorControllerAsync extends UnRestrictedBaseController{ 
	String viewName;

	@RequestMapping(value = {"/raptor.htm" }, method = RequestMethod.GET)
	public void RaptorSearch(HttpServletRequest request, HttpServletResponse response) throws IOException {
		
       /* List   items  = null;
        int    reportId = ServletRequestUtils.getIntParameter(request, "report_id", 0);
        //String task   = ServletRequestUtils.getStringParameter(request, "task", TASK_GET);

        HashMap  additionalParams = new HashMap();
        additionalParams.put(Parameters.PARAM_HTTP_REQUEST, request);

        return new ModelAndView(getViewName(), "model", null);
        
		//return new ModelAndView(getViewName(), "model", null);
		//System.out.println("Fill with proper code");
		//return null;*/
		viewName = "";
		String actionKey = nvl(request.getParameter(AppConstants.RI_ACTION), request.getParameter("action"));
		actionKey = nvl(actionKey, "report.run");
		
		HttpSession session = request.getSession();
		User user = UserUtils.getUserSession(request);

		if(actionKey.equals("report.download.excel2007.session") || actionKey.equals("report.download.csv.session") || actionKey.equals("report.download.excel.session") || actionKey.equals("report.download.pdf.session")   ) {
			if(session!=null && user!=null){
				ServletContext servletContext = request.getSession().getServletContext();
				if( !Globals.isSystemInitialized()) {
					 Globals.initializeSystem(servletContext);
				}
				ReportRuntime rr = null;
				ReportData    rd = null;
				String parent = "";
				int parentFlag = 0;
				if(!nvl(request.getParameter("parent"), "").equals("N")) parent = nvl(request.getParameter("parent"), "");
				if(parent.startsWith("parent_")) parentFlag = 1;
				if(parentFlag == 1) {
					rr = (ReportRuntime) request.getSession().getAttribute(parent+"_rr");
					rd = (ReportData) request.getSession().getAttribute(parent+"_rd");
				}
       
				boolean isEmbedded = false;
				Object temp = request.getSession().getAttribute("isEmbedded");
				if(temp!=null){
					isEmbedded = (boolean)temp;
				}
				if(isEmbedded){
					String reportID = AppUtils.getRequestValue(request, AppConstants.RI_REPORT_ID);
					if(rr==null) rr = (ReportRuntime) ((HashMap)request.getSession().getAttribute(AppConstants.EMBEDDED_REPORTRUNTIME_MAP)).get(reportID);
					if(rd==null) rd = (ReportData)    ((HashMap)request.getSession().getAttribute(AppConstants.EMBEDDED_REPORTDATA_MAP)).get(reportID);
				} else {
					if(rr==null) rr = (ReportRuntime) request.getSession().getAttribute(AppConstants.SI_REPORT_RUNTIME);
					if(rd==null) rd = (ReportData)    request.getSession().getAttribute(AppConstants.RI_REPORT_DATA);
				}
				String user_id = AppUtils.getUserID(request);
				try {
					OutputStreamWriter out = new OutputStreamWriter(response.getOutputStream());
						
					if(actionKey.equals("report.download.pdf.session")) {
						new PdfReportHandler().createPdfFileContent(request,response, 3);
					} else if (actionKey.equals("report.download.csv.session")) {
						(new ReportHandler()).createCSVFileContent(out, rd, rr, request, response);
					} else if (actionKey.equals("report.download.excel.session")) {
						new ReportHandler().createExcelFileContent(out, rd, rr, request, response, user_id, 3); //3 whole
					} else {
						
						new ReportHandler().createExcel2007FileContent(out, rd, rr, request, response, user_id, 3); //3 whole
					}
				} catch(Exception e) {
						e.printStackTrace();
						//Log.write("Fatal error [report_download_xlsx.jsp]: "+e.getMessage());
				}
			} else {
				response.sendRedirect("login.htm");
			}
		} else {
			if(session!=null && user!=null){
				Action action = null;
				ServletContext servletContext = request.getSession().getServletContext();
				if( !Globals.isSystemInitialized()) {
					 Globals.initializeSystem(servletContext);
				}
				try {
					action = Globals.getRaptorActionMapping().getAction(actionKey);
					if (action == null)
						throw new RaptorRuntimeException("Action not found");
				} catch (RaptorException e) {
					logger.debug(EELFLoggerDelegate.debugLogger, ("[Controller.processRequest]Invalid raptor action [" + actionKey
 							+ "]. RaptorException: " + e.getMessage()));
	
					viewName =  (new ErrorHandler()).processFatalError(request, new RaptorRuntimeException(
							"[Controller.processRequest]Invalid raptor action [" + actionKey
									+ "]. Exception: " + e.getMessage()));
				}

				try {
					Class[] paramTypes = new Class[2];
					paramTypes[0] = Class.forName("javax.servlet.http.HttpServletRequest");
					paramTypes[1] = Class.forName("java.lang.String");
		
		            Class handlerClass = Class.forName(action.getControllerClass());
					Object handler = handlerClass.newInstance();
					Method handlerMethod = handlerClass.getMethod(action.getControllerMethod(),
							paramTypes);
		
					Object[] paramValues = new Object[2];
					paramValues[0] = request;
					paramValues[1] = action.getJspName();;
		
					viewName = (String) handlerMethod.invoke(handler, paramValues);
					//ObjectMapper mapper = new ObjectMapper();
					if(!actionKey.equals("chart.run"))
						response.setContentType("application/json");
					else
						response.setContentType("text/html");
			        PrintWriter out = response.getWriter();
			        out.write(viewName);
					
				} catch (ClassNotFoundException e) {
					logger.debug(EELFLoggerDelegate.debugLogger, ("[Controller.processRequest]Invalid raptor action [" + actionKey
							+ "]. ClassNotFoundException: " + e.getMessage()));
					viewName = (new ErrorHandler()).processFatalError(request, new RaptorRuntimeException(
							"[Controller.processRequest] Unable to instantiate and invoke action handler. Exception: "
									+ e.getMessage()));
				} catch (IllegalAccessException e) {
					logger.debug(EELFLoggerDelegate.debugLogger, ("[Controller.processRequest]Invalid raptor action [" + actionKey
							+ "]. IllegalAccessException: " + e.getMessage()));
					viewName =  (new ErrorHandler()).processFatalError(request, new RaptorRuntimeException(
							"[Controller.processRequest] Unable to instantiate and invoke action handler. Exception: "
									+ e.getMessage()));
				}catch (InstantiationException e) {
					logger.debug(EELFLoggerDelegate.debugLogger, ("[Controller.processRequest]Invalid raptor action [" + actionKey
							+ "]. InstantiationException: " + e.getMessage()));
					viewName =   (new ErrorHandler()).processFatalError(request, new RaptorRuntimeException(
							"[Controller.processRequest] Unable to instantiate and invoke action handler. Exception: "
									+ e.getMessage()));
				}catch (NoSuchMethodException e) {
					logger.debug(EELFLoggerDelegate.debugLogger, ("[Controller.processRequest]Invalid raptor action [" + actionKey
							+ "]. NoSuchMethodException: " + e.getMessage()));
					viewName =   (new ErrorHandler()).processFatalError(request, new RaptorRuntimeException(
							"[Controller.processRequest] Unable to instantiate and invoke action handler. Exception: "
									+ e.getMessage()));
				}catch (InvocationTargetException e) {
					e.printStackTrace();
					logger.debug(EELFLoggerDelegate.debugLogger, ("[Controller.processRequest]Invalid raptor action [" + actionKey
							+ "]. InvocationTargetException: " + e.getMessage()));
					viewName =   (new ErrorHandler()).processFatalError(request, new RaptorRuntimeException(
							"[Controller.processRequest] Unable to instantiate and invoke action handler. Exception: "
									+ e.getMessage()));
				}
				//return new ModelAndView(getViewName(), "model", null);
			} else {
				PrintWriter out = response.getWriter();
				out.write("session has timed out for user");
			}

		}
    }

	@RequestMapping(value = "save_chart", method = RequestMethod.POST)
	public void reportChartReceive(@RequestBody ChartJSON chartJSON, HttpServletRequest request, HttpServletResponse response) throws IOException {
	 ReportRuntime reportRuntime;
	 System.out.println("*****Hit RaptorChart******");
	 reportRuntime = (ReportRuntime) request.getSession().getAttribute(AppConstants.SI_REPORT_RUNTIME); //changing session to request
	 String reportID = request.getParameter("c_master");
	 if (reportRuntime == null && AppUtils.nvl(reportID).length() > 0) {
	  try {
	   ReportHandler rh = new ReportHandler();
	   reportRuntime = rh.loadReportRuntime(request, reportID);
	  } catch (RaptorException ex) {
	   ex.printStackTrace();

	  }
	 }

	 if (reportRuntime != null) {
	  String chartType = chartJSON.getChartType();
	  reportRuntime.setChartType(chartJSON.getChartType());
	  reportRuntime.setChartAnimate(chartJSON.isAnimation());
	  reportRuntime.setChartWidth(chartJSON.getWidth());
	  reportRuntime.setChartHeight(chartJSON.getHeight());
	  reportRuntime.setShowChartTitle(chartJSON.isShowTitle());
	  

	  String domainAxis = null;
	  domainAxis = chartJSON.getDomainAxis();

	  List < DataColumnType > reportCols = reportRuntime.getAllColumns();

	  for (Iterator < DataColumnType > iter = reportCols.iterator(); iter
	   .hasNext();) {
	   DataColumnType dct = (DataColumnType) iter.next();
	   if (dct.getColId().equals(domainAxis)) {
	    dct.setColOnChart(AppConstants.GC_LEGEND);
	   } else {
	    dct.setColOnChart(null);
	   }
	  }

	  CategoryAxisJSON categoryAxisJSON = chartJSON.getCategoryAxisJSON();
	  String categoryAxis = null;
	  
	  categoryAxis = (categoryAxisJSON!=null?categoryAxisJSON.getValue():"");

	  reportCols = reportRuntime.getAllColumns();

	  for (Iterator < DataColumnType > iter = reportCols.iterator(); iter
	   .hasNext();) {
	   DataColumnType dct = (DataColumnType) iter.next();
	   if (dct.getColId().equals(categoryAxis)) {
	    dct.setChartSeries(true);
	   } else {
	    dct.setChartSeries(false);
	   }
	  }


	  ArrayList < RangeAxisJSON > rangeAxisList = chartJSON.getRangeAxisList();
	  int r = 0;
	  for (int i = 0; i < rangeAxisList.size(); i++) {
	   RangeAxisJSON rangeAxisJSON = rangeAxisList.get(i);
	   String rangeAxis = rangeAxisJSON.getRangeAxis();
	   String rangeYAxis = AppUtils.nvl(rangeAxisJSON.getRangeYAxis());
	   String rangeChartGroup = AppUtils.nvl(rangeAxisJSON.getRangeChartGroup());
	   String rangeColor = AppUtils.nvl(rangeAxisJSON.getRangeColor());
	   String rangeLineType = AppUtils.nvl(rangeAxisJSON.getRangeLineType());

	   rangefor:
	    for (Iterator < DataColumnType > iterator = reportCols.iterator(); iterator.hasNext();) {
	     DataColumnType dct = (DataColumnType) iterator.next();
	     if (dct.getColId().equals(rangeAxis)) {
	      dct.setChartSeq(++r);
	      dct.setColOnChart("0");
	      dct.setYAxis(rangeYAxis); //+"|"+dct.getColId());
	      dct.setChartGroup(rangeChartGroup); //+"|"+dct.getColId());
	      dct.setChartColor(rangeColor);
	      dct.setChartLineType(rangeLineType);

	      if (chartType.equals(AppConstants.GT_ANNOTATION_CHART) || chartType.equals(AppConstants.GT_FLEX_TIME_CHARTS)) {
	       if (rangeAxisJSON.isShowAsArea()) {
	        dct.setIsRangeAxisFilled(true);
	       } else {
	        dct.setIsRangeAxisFilled(false);
	       }
	      }
	      break rangefor;
	     }
	    }

	  }


	  reportRuntime.setChartLeftAxisLabel(chartJSON.getPrimaryAxisLabel());
	  reportRuntime.setChartRightAxisLabel(chartJSON.getSecondaryAxisLabel());

	  reportRuntime.setRangeAxisLowerLimit(chartJSON.getMinRange());
	  reportRuntime.setRangeAxisUpperLimit(chartJSON.getMaxRange());



	  if (chartType.equals(AppConstants.GT_ANNOTATION_CHART) || chartType.equals(AppConstants.GT_FLEX_TIME_CHARTS)) {
	   if (chartJSON.getFlexTimeSeriesChartOptions() != null) {
	    reportRuntime.setZoomIn(chartJSON.getFlexTimeSeriesChartOptions().getZoomIn());
	    reportRuntime.setTimeAxisType(chartJSON.getFlexTimeSeriesChartOptions().getTimeAxisType());
	   }

	  }

	  if (chartType.equals(AppConstants.GT_TIME_SERIES)) {
	   if (chartJSON.getTimeSeriesChartOptions() != null) {
	    reportRuntime.setTimeSeriesRender(chartJSON.getTimeSeriesChartOptions().getLineChartRenderer());
	    reportRuntime.setShowXAxisLabel(chartJSON.getTimeSeriesChartOptions().isShowXAxisLabel());
	    reportRuntime.setAddXAxisTickers(chartJSON.getTimeSeriesChartOptions().isAddXAxisTicker());
	    reportRuntime.setTimeAxis(chartJSON.getTimeSeriesChartOptions().isNonTimeAxis());
	    reportRuntime.setMultiSeries(chartJSON.getTimeSeriesChartOptions().isMultiSeries());
	   }

	  }

	  if (chartType.equals(AppConstants.GT_BAR_3D)) {
	   if (chartJSON.getBarChartOptions() != null) {
	    reportRuntime.setChartOrientation(chartJSON.getBarChartOptions().isVerticalOrientation() ? "vertical" : "horizontal");
	    reportRuntime.setChartStacked(chartJSON.getBarChartOptions().isStackedChart());
	    reportRuntime.setBarControls(chartJSON.getBarChartOptions().isDisplayBarControls());
	    reportRuntime.setXAxisDateType(chartJSON.getBarChartOptions().isxAxisDateType());
	    reportRuntime.setLessXaxisTickers(chartJSON.getBarChartOptions().isMinimizeXAxisTickers());
	    reportRuntime.setTimeAxis(chartJSON.getBarChartOptions().isTimeAxis());
	    reportRuntime.setLogScale(chartJSON.getBarChartOptions().isyAxisLogScale());
	   }
	  }


	  reportRuntime.setLegendLabelAngle(chartJSON.getCommonChartOptions().getLegendLabelAngle());
	  reportRuntime.setChartLegendDisplay(chartJSON.getCommonChartOptions().isHideLegend() ? "Y" : "N");
	  reportRuntime.setAnimateAnimatedChart(chartJSON.getCommonChartOptions().isAnimateAnimatedChart());

	  reportRuntime.setTopMargin(chartJSON.getCommonChartOptions().getTopMargin());
	  reportRuntime.setBottomMargin(chartJSON.getCommonChartOptions().getBottomMargin());
	  reportRuntime.setLeftMargin(chartJSON.getCommonChartOptions().getLeftMargin());
	  reportRuntime.setRightMargin(chartJSON.getCommonChartOptions().getRightMargin());



	  for (Iterator < DataColumnType > iterator = reportCols.iterator(); iterator.hasNext();) {
	   DataColumnType dct = (DataColumnType) iterator.next();
	   if (!(AppUtils.nvl(dct.getColOnChart()).equals(AppConstants.GC_LEGEND) || (dct.getChartSeq()!=null && dct.getChartSeq()>0) || dct.isChartSeries()) ) {
	    dct.setChartSeq(-1);
	    dct.setChartColor(null);
	    dct.setColOnChart(null);
	    dct.setCreateInNewChart(false);
	    dct.setChartGroup(null);
	    dct.setYAxis(null);
	   }
	  }


	  try {
	   reportRuntime.persistLinearReport(request);
	  } catch (Exception ex) {
	   ex.printStackTrace();
	   logger.error("While SAVING CHART", ex);
	  }
	 }

	}

	public String getViewName() {
		return viewName;
	}
	public void setViewName(String viewName) {
		this.viewName = viewName;
	}	
	
	public String nvl(String s) {
		return (s == null) ? "" : s;
	}
	
	public String nvl(String s, String sDefault) {
		return nvl(s).equals("") ? sDefault : s;
	}
	
	EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(RaptorControllerAsync.class);



}