Subversion Repositories SmartDukaan

Rev

Rev 30613 | Rev 30648 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
23612 amit.gupta 1
package com.spice.profitmandi.common.services;
2
 
30296 amit.gupta 3
import com.fasterxml.jackson.databind.JavaType;
4
import com.fasterxml.jackson.databind.ObjectMapper;
5
import com.fasterxml.jackson.databind.ObjectReader;
6
import com.spice.profitmandi.common.enumuration.ReporticoProject;
7
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
8
import com.spice.profitmandi.common.model.ReporticoResponseModel;
9
import com.spice.profitmandi.common.web.client.RestClient;
23612 amit.gupta 10
import org.apache.http.HttpResponse;
11
import org.apache.http.conn.HttpHostConnectException;
29581 manish 12
import org.apache.logging.log4j.LogManager;
13
import org.apache.logging.log4j.Logger;
23651 amit.gupta 14
import org.springframework.beans.factory.annotation.Autowired;
26375 amit.gupta 15
import org.springframework.beans.factory.annotation.Value;
24694 amit.gupta 16
import org.springframework.core.io.ByteArrayResource;
17
import org.springframework.core.io.InputStreamSource;
18
import org.springframework.http.HttpEntity;
19
import org.springframework.http.HttpHeaders;
20
import org.springframework.http.HttpMethod;
21
import org.springframework.http.ResponseEntity;
23612 amit.gupta 22
import org.springframework.stereotype.Component;
24694 amit.gupta 23
import org.springframework.web.client.RestTemplate;
24698 amit.gupta 24
import org.springframework.web.util.UriComponentsBuilder;
23612 amit.gupta 25
 
30296 amit.gupta 26
import java.util.*;
23612 amit.gupta 27
 
28
@Component
29
public class ReporticoService {
27530 tejbeer 30
 
29581 manish 31
	private static final Logger LOGGER = LogManager.getLogger(ReporticoService.class);
32
 
27530 tejbeer 33
	// private String reporticoUrl="http://192.168.134.118/reports/run.php";
26379 amit.gupta 34
	@Value("${reportico.url}")
26378 amit.gupta 35
	private String reporticoUrl;
27530 tejbeer 36
 
23651 amit.gupta 37
	@Autowired
38
	RestClient restClient;
27530 tejbeer 39
 
24694 amit.gupta 40
	@Autowired
27530 tejbeer 41
	private ObjectMapper objectMapper;
42
 
43
	@Autowired
24694 amit.gupta 44
	RestTemplate restTemplate;
27530 tejbeer 45
 
46
	public static final Map<ReporticoProject, Map<String, String>> projectAuthMap = Collections
47
			.unmodifiableMap(new HashMap<ReporticoProject, Map<String, String>>() {
23612 amit.gupta 48
				{
49
					put(ReporticoProject.FOCO, new HashMap<String, String>() {
50
						{
51
							put("project", ReporticoProject.FOCO.getValue());
28511 amit.gupta 52
							put("project_password", "$ecreT@4321");
23612 amit.gupta 53
						}
54
					});
23637 amit.gupta 55
					put(ReporticoProject.FOCOR, new HashMap<String, String>() {
56
						{
57
							put("project", ReporticoProject.FOCOR.getValue());
58
							put("project_password", "focor");
59
						}
60
					});
23951 amit.gupta 61
					put(ReporticoProject.WAREHOUSENEW, new HashMap<String, String>() {
62
						{
63
							put("project", ReporticoProject.WAREHOUSENEW.getValue());
64
							put("project_password", "warehousenew");
65
						}
66
					});
23612 amit.gupta 67
				}
68
			});
27530 tejbeer 69
 
70
	public HttpResponse getReportFile(ReporticoProject projectName, String reportName)
26066 amit.gupta 71
			throws HttpHostConnectException, ProfitMandiBusinessException {
23612 amit.gupta 72
		Map<String, String> authMap = projectAuthMap.get(projectName);
73
		Map<String, String> params = new HashMap<>(authMap);
74
		params.put("xmlin", reportName);
75
		params.put("target_format", "CSV");
76
		params.put("execute_mode", "EXECUTE");
30296 amit.gupta 77
		return restClient.getResponse(reporticoUrl, params, null);
23612 amit.gupta 78
	}
27530 tejbeer 79
 
80
	public InputStreamSource getReportInputStreamSource(ReporticoProject projectName, String reportName)
81
			throws HttpHostConnectException, ProfitMandiBusinessException {
82
 
24694 amit.gupta 83
		Map<String, String> authMap = projectAuthMap.get(projectName);
27530 tejbeer 84
 
24694 amit.gupta 85
		Map<String, String> params = new HashMap<>(authMap);
86
		params.put("xmlin", reportName);
87
		params.put("target_format", "CSV");
88
		params.put("execute_mode", "EXECUTE");
24698 amit.gupta 89
		UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(reporticoUrl);
90
		Set<String> keys = params.keySet();
27530 tejbeer 91
 
92
		for (String key : keys) {
24698 amit.gupta 93
			builder.queryParam(key, params.get(key));
94
		}
24694 amit.gupta 95
		HttpEntity<String> requestEntity = new HttpEntity<String>("", new HttpHeaders());
27530 tejbeer 96
		ResponseEntity<byte[]> responseEntity = restTemplate.exchange(builder.build().encode().toUri(), HttpMethod.GET,
97
				requestEntity, byte[].class);
24694 amit.gupta 98
		return new ByteArrayResource(responseEntity.getBody());
27530 tejbeer 99
 
24694 amit.gupta 100
	}
25419 amit.gupta 101
 
27530 tejbeer 102
	public InputStreamSource getReportInputStreamSource(ReporticoProject projectName, String reportName,
103
			Map<String, String> params) throws HttpHostConnectException, ProfitMandiBusinessException {
104
 
25419 amit.gupta 105
		Map<String, String> authMap = projectAuthMap.get(projectName);
27530 tejbeer 106
 
25419 amit.gupta 107
		params.putAll(authMap);
108
		params.put("xmlin", reportName);
109
		params.put("target_format", "CSV");
110
		params.put("execute_mode", "EXECUTE");
111
		UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(reporticoUrl);
112
		Set<String> keys = params.keySet();
27530 tejbeer 113
 
114
		for (String key : keys) {
25419 amit.gupta 115
			builder.queryParam(key, params.get(key));
116
		}
117
		HttpEntity<String> requestEntity = new HttpEntity<String>("", new HttpHeaders());
27530 tejbeer 118
		ResponseEntity<byte[]> responseEntity = restTemplate.exchange(builder.build().encode().toUri(), HttpMethod.GET,
119
				requestEntity, byte[].class);
25419 amit.gupta 120
		return new ByteArrayResource(responseEntity.getBody());
27530 tejbeer 121
 
25419 amit.gupta 122
	}
27530 tejbeer 123
 
124
	public HttpResponse getReportFile(int fofoId, ReporticoProject projectName, String reportName)
125
			throws HttpHostConnectException, ProfitMandiBusinessException {
26066 amit.gupta 126
		Map<String, String> params = new HashMap<>();
127
		params.put("MANUAL_fofoId", String.valueOf(fofoId));
128
		return this.getReportFile(projectName, reportName, params);
129
	}
130
 
27530 tejbeer 131
	public HttpResponse getReportFile(ReporticoProject projectName, String reportName, Map<String, String> params)
132
			throws HttpHostConnectException, ProfitMandiBusinessException {
133
		if (params == null) {
134
			params = new HashMap<>();
26066 amit.gupta 135
		}
136
		params.putAll(projectAuthMap.get(projectName));
23612 amit.gupta 137
		params.put("xmlin", reportName);
138
		params.put("target_format", "CSV");
139
		params.put("execute_mode", "EXECUTE");
140
		return restClient.getResponse(reporticoUrl, params, new HashMap<>());
141
	}
27530 tejbeer 142
 
143
	public HttpResponse getJsonFile(ReporticoProject projectName, String reportName, Map<String, String> params)
144
			throws HttpHostConnectException, ProfitMandiBusinessException {
23945 amit.gupta 145
		Map<String, String> authMap = projectAuthMap.get(projectName);
146
		params.putAll(authMap);
147
		params.put("xmlin", reportName);
148
		params.put("target_format", "JSON");
149
		params.put("execute_mode", "EXECUTE");
30615 amit.gupta 150
		return restClient.getResponse(reporticoUrl, params, new HashMap<>());
23945 amit.gupta 151
	}
27530 tejbeer 152
 
153
	public <T> List<T> getReports(Class<T> className, ReporticoProject project, String reportName,
154
			Map<String, String> params) throws Exception {
155
		HttpResponse reportResponse = this.getJsonFile(project, reportName, params);
29581 manish 156
 
157
		LOGGER.info("reportResponse {}", reportResponse);
27530 tejbeer 158
		JavaType type = objectMapper.getTypeFactory().constructParametricType(ReporticoResponseModel.class, className);
159
		ObjectReader or = objectMapper.readerFor(type);
160
		ReporticoResponseModel<T> responseObj = or.readValue(reportResponse.getEntity().getContent());
29581 manish 161
 
162
		LOGGER.info("responseObj {}", responseObj);
163
 
27530 tejbeer 164
		return responseObj.getData();
165
	}
23612 amit.gupta 166
}