Subversion Repositories SmartDukaan

Rev

Rev 33733 | 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;
35623 amit 24
import org.springframework.util.LinkedMultiValueMap;
25
import org.springframework.util.MultiValueMap;
23612 amit.gupta 26
 
30296 amit.gupta 27
import java.util.*;
23612 amit.gupta 28
 
29
@Component
30
public class ReporticoService {
27530 tejbeer 31
 
29581 manish 32
	private static final Logger LOGGER = LogManager.getLogger(ReporticoService.class);
33
 
27530 tejbeer 34
	// private String reporticoUrl="http://192.168.134.118/reports/run.php";
26379 amit.gupta 35
	@Value("${reportico.url}")
26378 amit.gupta 36
	private String reporticoUrl;
27530 tejbeer 37
 
23651 amit.gupta 38
	@Autowired
39
	RestClient restClient;
27530 tejbeer 40
 
24694 amit.gupta 41
	@Autowired
27530 tejbeer 42
	private ObjectMapper objectMapper;
43
 
44
	@Autowired
24694 amit.gupta 45
	RestTemplate restTemplate;
27530 tejbeer 46
 
47
	public static final Map<ReporticoProject, Map<String, String>> projectAuthMap = Collections
48
			.unmodifiableMap(new HashMap<ReporticoProject, Map<String, String>>() {
23612 amit.gupta 49
				{
50
					put(ReporticoProject.FOCO, new HashMap<String, String>() {
51
						{
52
							put("project", ReporticoProject.FOCO.getValue());
33733 amit.gupta 53
							put("project_password", "Sm@rtR3portDukaan");
23612 amit.gupta 54
						}
55
					});
23637 amit.gupta 56
					put(ReporticoProject.FOCOR, new HashMap<String, String>() {
57
						{
58
							put("project", ReporticoProject.FOCOR.getValue());
59
							put("project_password", "focor");
60
						}
61
					});
23951 amit.gupta 62
					put(ReporticoProject.WAREHOUSENEW, new HashMap<String, String>() {
63
						{
64
							put("project", ReporticoProject.WAREHOUSENEW.getValue());
31631 amit.gupta 65
							put("project_password", "$Warehouse@123New");
23951 amit.gupta 66
						}
67
					});
23612 amit.gupta 68
				}
69
			});
27530 tejbeer 70
 
71
	public HttpResponse getReportFile(ReporticoProject projectName, String reportName)
26066 amit.gupta 72
			throws HttpHostConnectException, ProfitMandiBusinessException {
23612 amit.gupta 73
		Map<String, String> authMap = projectAuthMap.get(projectName);
74
		Map<String, String> params = new HashMap<>(authMap);
75
		params.put("xmlin", reportName);
76
		params.put("target_format", "CSV");
77
		params.put("execute_mode", "EXECUTE");
35623 amit 78
		return restClient.postResponse(reporticoUrl, params, null);
23612 amit.gupta 79
	}
27530 tejbeer 80
 
81
	public InputStreamSource getReportInputStreamSource(ReporticoProject projectName, String reportName)
82
			throws HttpHostConnectException, ProfitMandiBusinessException {
83
 
24694 amit.gupta 84
		Map<String, String> authMap = projectAuthMap.get(projectName);
27530 tejbeer 85
 
24694 amit.gupta 86
		Map<String, String> params = new HashMap<>(authMap);
87
		params.put("xmlin", reportName);
88
		params.put("target_format", "CSV");
89
		params.put("execute_mode", "EXECUTE");
27530 tejbeer 90
 
35623 amit 91
		MultiValueMap<String, String> formData = new LinkedMultiValueMap<>();
92
		for (Map.Entry<String, String> entry : params.entrySet()) {
93
			formData.add(entry.getKey(), entry.getValue());
24698 amit.gupta 94
		}
35623 amit 95
		HttpHeaders headers = new HttpHeaders();
96
		headers.setContentType(org.springframework.http.MediaType.APPLICATION_FORM_URLENCODED);
97
		HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<>(formData, headers);
98
		ResponseEntity<byte[]> responseEntity = restTemplate.exchange(reporticoUrl, HttpMethod.POST,
27530 tejbeer 99
				requestEntity, byte[].class);
24694 amit.gupta 100
		return new ByteArrayResource(responseEntity.getBody());
27530 tejbeer 101
 
24694 amit.gupta 102
	}
25419 amit.gupta 103
 
27530 tejbeer 104
	public InputStreamSource getReportInputStreamSource(ReporticoProject projectName, String reportName,
105
			Map<String, String> params) throws HttpHostConnectException, ProfitMandiBusinessException {
106
 
25419 amit.gupta 107
		Map<String, String> authMap = projectAuthMap.get(projectName);
27530 tejbeer 108
 
25419 amit.gupta 109
		params.putAll(authMap);
110
		params.put("xmlin", reportName);
111
		params.put("target_format", "CSV");
112
		params.put("execute_mode", "EXECUTE");
27530 tejbeer 113
 
35623 amit 114
		MultiValueMap<String, String> formData = new LinkedMultiValueMap<>();
115
		for (Map.Entry<String, String> entry : params.entrySet()) {
116
			formData.add(entry.getKey(), entry.getValue());
25419 amit.gupta 117
		}
35623 amit 118
		HttpHeaders headers = new HttpHeaders();
119
		headers.setContentType(org.springframework.http.MediaType.APPLICATION_FORM_URLENCODED);
120
		HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<>(formData, headers);
121
		ResponseEntity<byte[]> responseEntity = restTemplate.exchange(reporticoUrl, HttpMethod.POST,
27530 tejbeer 122
				requestEntity, byte[].class);
25419 amit.gupta 123
		return new ByteArrayResource(responseEntity.getBody());
27530 tejbeer 124
 
25419 amit.gupta 125
	}
27530 tejbeer 126
 
127
	public HttpResponse getReportFile(int fofoId, ReporticoProject projectName, String reportName)
128
			throws HttpHostConnectException, ProfitMandiBusinessException {
26066 amit.gupta 129
		Map<String, String> params = new HashMap<>();
130
		params.put("MANUAL_fofoId", String.valueOf(fofoId));
131
		return this.getReportFile(projectName, reportName, params);
132
	}
133
 
27530 tejbeer 134
	public HttpResponse getReportFile(ReporticoProject projectName, String reportName, Map<String, String> params)
135
			throws HttpHostConnectException, ProfitMandiBusinessException {
136
		if (params == null) {
137
			params = new HashMap<>();
26066 amit.gupta 138
		}
139
		params.putAll(projectAuthMap.get(projectName));
23612 amit.gupta 140
		params.put("xmlin", reportName);
141
		params.put("target_format", "CSV");
142
		params.put("execute_mode", "EXECUTE");
35623 amit 143
		return restClient.postResponse(reporticoUrl, params, new HashMap<>());
23612 amit.gupta 144
	}
27530 tejbeer 145
 
146
	public HttpResponse getJsonFile(ReporticoProject projectName, String reportName, Map<String, String> params)
147
			throws HttpHostConnectException, ProfitMandiBusinessException {
23945 amit.gupta 148
		Map<String, String> authMap = projectAuthMap.get(projectName);
149
		params.putAll(authMap);
150
		params.put("xmlin", reportName);
151
		params.put("target_format", "JSON");
152
		params.put("execute_mode", "EXECUTE");
35623 amit 153
		return restClient.postResponse(reporticoUrl, params, new HashMap<>());
23945 amit.gupta 154
	}
27530 tejbeer 155
 
156
	public <T> List<T> getReports(Class<T> className, ReporticoProject project, String reportName,
157
			Map<String, String> params) throws Exception {
158
		HttpResponse reportResponse = this.getJsonFile(project, reportName, params);
29581 manish 159
 
160
		LOGGER.info("reportResponse {}", reportResponse);
27530 tejbeer 161
		JavaType type = objectMapper.getTypeFactory().constructParametricType(ReporticoResponseModel.class, className);
162
		ObjectReader or = objectMapper.readerFor(type);
163
		ReporticoResponseModel<T> responseObj = or.readValue(reportResponse.getEntity().getContent());
29581 manish 164
 
165
		LOGGER.info("responseObj {}", responseObj);
166
 
27530 tejbeer 167
		return responseObj.getData();
168
	}
23612 amit.gupta 169
}