Subversion Repositories SmartDukaan

Rev

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