Subversion Repositories SmartDukaan

Rev

Rev 24694 | Rev 24763 | 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;
24698 amit.gupta 9
import org.apache.http.client.methods.HttpGet;
23612 amit.gupta 10
import org.apache.http.conn.HttpHostConnectException;
23651 amit.gupta 11
import org.springframework.beans.factory.annotation.Autowired;
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";
30
	private String reporticoUrl="http://50.116.10.120/reports/run.php";
23612 amit.gupta 31
 
23651 amit.gupta 32
	@Autowired
33
	RestClient restClient;
34
 
24694 amit.gupta 35
	@Autowired
36
	RestTemplate restTemplate;
37
 
23612 amit.gupta 38
	public static final Map<ReporticoProject, Map<String, String>> projectAuthMap = Collections.unmodifiableMap(
39
			new HashMap<ReporticoProject, Map<String, String>>() {
40
				{
41
					put(ReporticoProject.FOCO, new HashMap<String, String>() {
42
						{
43
							put("project", ReporticoProject.FOCO.getValue());
24224 amit.gupta 44
							put("project_password", "smart1234");
23612 amit.gupta 45
						}
46
					});
23637 amit.gupta 47
					put(ReporticoProject.FOCOR, new HashMap<String, String>() {
48
						{
49
							put("project", ReporticoProject.FOCOR.getValue());
50
							put("project_password", "focor");
51
						}
52
					});
23951 amit.gupta 53
					put(ReporticoProject.WAREHOUSENEW, new HashMap<String, String>() {
54
						{
55
							put("project", ReporticoProject.WAREHOUSENEW.getValue());
56
							put("project_password", "warehousenew");
57
						}
58
					});
23612 amit.gupta 59
				}
60
			});
61
 
24509 amit.gupta 62
 
23612 amit.gupta 63
	public HttpResponse getReportFile(ReporticoProject projectName, String reportName) throws HttpHostConnectException, ProfitMandiBusinessException {
64
 
65
		Map<String, String> authMap = projectAuthMap.get(projectName);
66
 
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
	}
93
 
23612 amit.gupta 94
	public HttpResponse getReportFile(int fofoId, ReporticoProject projectName, String reportName) throws HttpHostConnectException, ProfitMandiBusinessException {
95
 
96
		Map<String, String> authMap = projectAuthMap.get(projectName);
97
 
98
		Map<String, String> params = new HashMap<>(authMap);
99
		params.put("xmlin", reportName);
100
		params.put("target_format", "CSV");
101
		params.put("execute_mode", "EXECUTE");
23637 amit.gupta 102
		params.put("MANUAL_fofoId", String.valueOf(fofoId));
23612 amit.gupta 103
		return restClient.getResponse(reporticoUrl, params, new HashMap<>());
104
	}
23945 amit.gupta 105
 
106
	public HttpResponse getJsonFile(ReporticoProject projectName, String reportName, Map<String, String> params) throws HttpHostConnectException, ProfitMandiBusinessException {
107
 
108
		Map<String, String> authMap = projectAuthMap.get(projectName);
109
 
110
		params.putAll(authMap);
111
		params.put("xmlin", reportName);
112
		params.put("target_format", "JSON");
113
		params.put("execute_mode", "EXECUTE");
114
		return restClient.getResponse(reporticoUrl, params, new HashMap<>());
115
	}
23612 amit.gupta 116
}