Subversion Repositories SmartDukaan

Rev

Rev 28508 | Rev 29581 | 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;
23651 amit.gupta 11
import org.springframework.beans.factory.annotation.Autowired;
26375 amit.gupta 12
import org.springframework.beans.factory.annotation.Value;
24694 amit.gupta 13
import org.springframework.core.io.ByteArrayResource;
14
import org.springframework.core.io.InputStreamSource;
15
import org.springframework.http.HttpEntity;
16
import org.springframework.http.HttpHeaders;
17
import org.springframework.http.HttpMethod;
18
import org.springframework.http.ResponseEntity;
23612 amit.gupta 19
import org.springframework.stereotype.Component;
24694 amit.gupta 20
import org.springframework.web.client.RestTemplate;
24698 amit.gupta 21
import org.springframework.web.util.UriComponentsBuilder;
23612 amit.gupta 22
 
27530 tejbeer 23
import com.fasterxml.jackson.databind.JavaType;
24
import com.fasterxml.jackson.databind.ObjectMapper;
25
import com.fasterxml.jackson.databind.ObjectReader;
23612 amit.gupta 26
import com.spice.profitmandi.common.enumuration.ReporticoProject;
27
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
27530 tejbeer 28
import com.spice.profitmandi.common.model.ReporticoResponseModel;
23612 amit.gupta 29
import com.spice.profitmandi.common.web.client.RestClient;
30
 
31
@Component
32
public class ReporticoService {
27530 tejbeer 33
 
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());
28511 amit.gupta 53
							put("project_password", "$ecreT@4321");
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());
65
							put("project_password", "warehousenew");
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");
78
		return restClient.getResponse(reporticoUrl, params, new HashMap<>());
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");
24698 amit.gupta 90
		UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(reporticoUrl);
91
		Set<String> keys = params.keySet();
27530 tejbeer 92
 
93
		for (String key : keys) {
24698 amit.gupta 94
			builder.queryParam(key, params.get(key));
95
		}
24694 amit.gupta 96
		HttpEntity<String> requestEntity = new HttpEntity<String>("", new HttpHeaders());
27530 tejbeer 97
		ResponseEntity<byte[]> responseEntity = restTemplate.exchange(builder.build().encode().toUri(), HttpMethod.GET,
98
				requestEntity, byte[].class);
24694 amit.gupta 99
		return new ByteArrayResource(responseEntity.getBody());
27530 tejbeer 100
 
24694 amit.gupta 101
	}
25419 amit.gupta 102
 
27530 tejbeer 103
	public InputStreamSource getReportInputStreamSource(ReporticoProject projectName, String reportName,
104
			Map<String, String> params) throws HttpHostConnectException, ProfitMandiBusinessException {
105
 
25419 amit.gupta 106
		Map<String, String> authMap = projectAuthMap.get(projectName);
27530 tejbeer 107
 
25419 amit.gupta 108
		params.putAll(authMap);
109
		params.put("xmlin", reportName);
110
		params.put("target_format", "CSV");
111
		params.put("execute_mode", "EXECUTE");
112
		UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(reporticoUrl);
113
		Set<String> keys = params.keySet();
27530 tejbeer 114
 
115
		for (String key : keys) {
25419 amit.gupta 116
			builder.queryParam(key, params.get(key));
117
		}
118
		HttpEntity<String> requestEntity = new HttpEntity<String>("", new HttpHeaders());
27530 tejbeer 119
		ResponseEntity<byte[]> responseEntity = restTemplate.exchange(builder.build().encode().toUri(), HttpMethod.GET,
120
				requestEntity, byte[].class);
25419 amit.gupta 121
		return new ByteArrayResource(responseEntity.getBody());
27530 tejbeer 122
 
25419 amit.gupta 123
	}
27530 tejbeer 124
 
125
	public HttpResponse getReportFile(int fofoId, ReporticoProject projectName, String reportName)
126
			throws HttpHostConnectException, ProfitMandiBusinessException {
26066 amit.gupta 127
		Map<String, String> params = new HashMap<>();
128
		params.put("MANUAL_fofoId", String.valueOf(fofoId));
129
		return this.getReportFile(projectName, reportName, params);
130
	}
131
 
27530 tejbeer 132
	public HttpResponse getReportFile(ReporticoProject projectName, String reportName, Map<String, String> params)
133
			throws HttpHostConnectException, ProfitMandiBusinessException {
134
		if (params == null) {
135
			params = new HashMap<>();
26066 amit.gupta 136
		}
137
		params.putAll(projectAuthMap.get(projectName));
23612 amit.gupta 138
		params.put("xmlin", reportName);
139
		params.put("target_format", "CSV");
140
		params.put("execute_mode", "EXECUTE");
141
		return restClient.getResponse(reporticoUrl, params, new HashMap<>());
142
	}
27530 tejbeer 143
 
144
	public HttpResponse getJsonFile(ReporticoProject projectName, String reportName, Map<String, String> params)
145
			throws HttpHostConnectException, ProfitMandiBusinessException {
23945 amit.gupta 146
		Map<String, String> authMap = projectAuthMap.get(projectName);
147
		params.putAll(authMap);
148
		params.put("xmlin", reportName);
149
		params.put("target_format", "JSON");
150
		params.put("execute_mode", "EXECUTE");
151
		return restClient.getResponse(reporticoUrl, params, new HashMap<>());
152
	}
27530 tejbeer 153
 
154
	public <T> List<T> getReports(Class<T> className, ReporticoProject project, String reportName,
155
			Map<String, String> params) throws Exception {
156
		HttpResponse reportResponse = this.getJsonFile(project, reportName, params);
157
		JavaType type = objectMapper.getTypeFactory().constructParametricType(ReporticoResponseModel.class, className);
158
		ObjectReader or = objectMapper.readerFor(type);
159
		ReporticoResponseModel<T> responseObj = or.readValue(reportResponse.getEntity().getContent());
160
		return responseObj.getData();
161
	}
23612 amit.gupta 162
}