Subversion Repositories SmartDukaan

Rev

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