Subversion Repositories SmartDukaan

Rev

Rev 27262 | Rev 27530 | 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;
5
import java.util.Map;
24698 amit.gupta 6
import java.util.Set;
23612 amit.gupta 7
 
8
import org.apache.http.HttpResponse;
9
import org.apache.http.conn.HttpHostConnectException;
23651 amit.gupta 10
import org.springframework.beans.factory.annotation.Autowired;
26375 amit.gupta 11
import org.springframework.beans.factory.annotation.Value;
24694 amit.gupta 12
import org.springframework.core.io.ByteArrayResource;
13
import org.springframework.core.io.InputStreamSource;
14
import org.springframework.http.HttpEntity;
15
import org.springframework.http.HttpHeaders;
16
import org.springframework.http.HttpMethod;
17
import org.springframework.http.ResponseEntity;
23612 amit.gupta 18
import org.springframework.stereotype.Component;
24694 amit.gupta 19
import org.springframework.web.client.RestTemplate;
24698 amit.gupta 20
import org.springframework.web.util.UriComponentsBuilder;
23612 amit.gupta 21
 
22
import com.spice.profitmandi.common.enumuration.ReporticoProject;
23
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
24
import com.spice.profitmandi.common.web.client.RestClient;
25
 
26
@Component
27
public class ReporticoService {
28
 
23763 amit.gupta 29
	//private String reporticoUrl="http://192.168.134.118/reports/run.php";
26379 amit.gupta 30
	@Value("${reportico.url}")
26378 amit.gupta 31
	private String reporticoUrl;
23612 amit.gupta 32
 
23651 amit.gupta 33
	@Autowired
34
	RestClient restClient;
35
 
24694 amit.gupta 36
	@Autowired
37
	RestTemplate restTemplate;
38
 
23612 amit.gupta 39
	public static final Map<ReporticoProject, Map<String, String>> projectAuthMap = Collections.unmodifiableMap(
40
			new HashMap<ReporticoProject, Map<String, String>>() {
41
				{
42
					put(ReporticoProject.FOCO, new HashMap<String, String>() {
43
						{
44
							put("project", ReporticoProject.FOCO.getValue());
27263 amit.gupta 45
							put("project_password", "rBRTJYXlTZ");
23612 amit.gupta 46
						}
47
					});
23637 amit.gupta 48
					put(ReporticoProject.FOCOR, new HashMap<String, String>() {
49
						{
50
							put("project", ReporticoProject.FOCOR.getValue());
51
							put("project_password", "focor");
52
						}
53
					});
23951 amit.gupta 54
					put(ReporticoProject.WAREHOUSENEW, new HashMap<String, String>() {
55
						{
56
							put("project", ReporticoProject.WAREHOUSENEW.getValue());
57
							put("project_password", "warehousenew");
58
						}
59
					});
23612 amit.gupta 60
				}
61
			});
62
 
24509 amit.gupta 63
 
26066 amit.gupta 64
	public HttpResponse getReportFile(ReporticoProject projectName, String reportName) 
65
			throws HttpHostConnectException, ProfitMandiBusinessException {
23612 amit.gupta 66
		Map<String, String> authMap = projectAuthMap.get(projectName);
67
		Map<String, String> params = new HashMap<>(authMap);
68
		params.put("xmlin", reportName);
69
		params.put("target_format", "CSV");
70
		params.put("execute_mode", "EXECUTE");
71
		return restClient.getResponse(reporticoUrl, params, new HashMap<>());
72
	}
73
 
24694 amit.gupta 74
	public InputStreamSource getReportInputStreamSource(ReporticoProject projectName, String reportName) throws HttpHostConnectException, ProfitMandiBusinessException {
75
 
76
		Map<String, String> authMap = projectAuthMap.get(projectName);
77
 
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");
24698 amit.gupta 82
		UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(reporticoUrl);
83
		Set<String> keys = params.keySet();
24694 amit.gupta 84
 
24698 amit.gupta 85
		for(String key : keys){
86
			builder.queryParam(key, params.get(key));
87
		}
24694 amit.gupta 88
		HttpEntity<String> requestEntity = new HttpEntity<String>("", new HttpHeaders());
24698 amit.gupta 89
		ResponseEntity<byte[]> responseEntity = restTemplate.exchange(builder.build().encode().toUri(), HttpMethod.GET, requestEntity, byte[].class);
24694 amit.gupta 90
		return new ByteArrayResource(responseEntity.getBody());
91
 
92
	}
25419 amit.gupta 93
 
94
	public InputStreamSource getReportInputStreamSource(ReporticoProject projectName, String reportName, Map<String, String> params) throws HttpHostConnectException, ProfitMandiBusinessException {
95
 
96
		Map<String, String> authMap = projectAuthMap.get(projectName);
97
 
98
		params.putAll(authMap);
99
		params.put("xmlin", reportName);
100
		params.put("target_format", "CSV");
101
		params.put("execute_mode", "EXECUTE");
102
		UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(reporticoUrl);
103
		Set<String> keys = params.keySet();
104
 
105
		for(String key : keys){
106
			builder.queryParam(key, params.get(key));
107
		}
108
		HttpEntity<String> requestEntity = new HttpEntity<String>("", new HttpHeaders());
109
		ResponseEntity<byte[]> responseEntity = restTemplate.exchange(builder.build().encode().toUri(), HttpMethod.GET, requestEntity, byte[].class);
110
		return new ByteArrayResource(responseEntity.getBody());
111
 
112
	}
24694 amit.gupta 113
 
23612 amit.gupta 114
	public HttpResponse getReportFile(int fofoId, ReporticoProject projectName, String reportName) throws HttpHostConnectException, ProfitMandiBusinessException {
26066 amit.gupta 115
		Map<String, String> params = new HashMap<>();
116
		params.put("MANUAL_fofoId", String.valueOf(fofoId));
117
		return this.getReportFile(projectName, reportName, params);
118
	}
119
 
120
 
121
	public HttpResponse getReportFile(ReporticoProject projectName, String reportName, Map<String, String> params) throws HttpHostConnectException, ProfitMandiBusinessException {
122
		if(params==null) {
123
			params=new HashMap<>();
124
		}
125
		params.putAll(projectAuthMap.get(projectName));
23612 amit.gupta 126
		params.put("xmlin", reportName);
127
		params.put("target_format", "CSV");
128
		params.put("execute_mode", "EXECUTE");
129
		return restClient.getResponse(reporticoUrl, params, new HashMap<>());
130
	}
23945 amit.gupta 131
 
132
	public HttpResponse getJsonFile(ReporticoProject projectName, String reportName, Map<String, String> params) throws HttpHostConnectException, ProfitMandiBusinessException {
133
		Map<String, String> authMap = projectAuthMap.get(projectName);
134
		params.putAll(authMap);
135
		params.put("xmlin", reportName);
136
		params.put("target_format", "JSON");
137
		params.put("execute_mode", "EXECUTE");
138
		return restClient.getResponse(reporticoUrl, params, new HashMap<>());
139
	}
23612 amit.gupta 140
}