Subversion Repositories SmartDukaan

Rev

Rev 27024 | Rev 27262 | Go to most recent revision | View as "text/plain" | Blame | Compare with Previous | Last modification | View Log | RSS feed

package com.spice.profitmandi.common.services;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import org.apache.http.HttpResponse;
import org.apache.http.conn.HttpHostConnectException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.core.io.InputStreamSource;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;

import com.spice.profitmandi.common.enumuration.ReporticoProject;
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
import com.spice.profitmandi.common.web.client.RestClient;

@Component
public class ReporticoService {
        
        //private String reporticoUrl="http://192.168.134.118/reports/run.php";
        @Value("${reportico.url}")
        private String reporticoUrl;
        
        @Autowired
        RestClient restClient;
        
        @Autowired
        RestTemplate restTemplate;
        
        public static final Map<ReporticoProject, Map<String, String>> projectAuthMap = Collections.unmodifiableMap(
                        new HashMap<ReporticoProject, Map<String, String>>() {
                                {
                                        put(ReporticoProject.FOCO, new HashMap<String, String>() {
                                                {
                                                        put("project", ReporticoProject.FOCO.getValue());
                                                        put("project_password", "smart2@)@)");
                                                }
                                        });
                                        put(ReporticoProject.FOCOR, new HashMap<String, String>() {
                                                {
                                                        put("project", ReporticoProject.FOCOR.getValue());
                                                        put("project_password", "focor");
                                                }
                                        });
                                        put(ReporticoProject.WAREHOUSENEW, new HashMap<String, String>() {
                                                {
                                                        put("project", ReporticoProject.WAREHOUSENEW.getValue());
                                                        put("project_password", "warehousenew");
                                                }
                                        });
                                }
                        });
        
        
        public HttpResponse getReportFile(ReporticoProject projectName, String reportName) 
                        throws HttpHostConnectException, ProfitMandiBusinessException {
                Map<String, String> authMap = projectAuthMap.get(projectName);
                Map<String, String> params = new HashMap<>(authMap);
                params.put("xmlin", reportName);
                params.put("target_format", "CSV");
                params.put("execute_mode", "EXECUTE");
                return restClient.getResponse(reporticoUrl, params, new HashMap<>());
        }
        
        public InputStreamSource getReportInputStreamSource(ReporticoProject projectName, String reportName) throws HttpHostConnectException, ProfitMandiBusinessException {
                
                Map<String, String> authMap = projectAuthMap.get(projectName);
                
                Map<String, String> params = new HashMap<>(authMap);
                params.put("xmlin", reportName);
                params.put("target_format", "CSV");
                params.put("execute_mode", "EXECUTE");
                UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(reporticoUrl);
                Set<String> keys = params.keySet();
                
                for(String key : keys){
                        builder.queryParam(key, params.get(key));
                }
                HttpEntity<String> requestEntity = new HttpEntity<String>("", new HttpHeaders());
                ResponseEntity<byte[]> responseEntity = restTemplate.exchange(builder.build().encode().toUri(), HttpMethod.GET, requestEntity, byte[].class);
                return new ByteArrayResource(responseEntity.getBody());
                
        }

        public InputStreamSource getReportInputStreamSource(ReporticoProject projectName, String reportName, Map<String, String> params) throws HttpHostConnectException, ProfitMandiBusinessException {
                
                Map<String, String> authMap = projectAuthMap.get(projectName);
                
                params.putAll(authMap);
                params.put("xmlin", reportName);
                params.put("target_format", "CSV");
                params.put("execute_mode", "EXECUTE");
                UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(reporticoUrl);
                Set<String> keys = params.keySet();
                
                for(String key : keys){
                        builder.queryParam(key, params.get(key));
                }
                HttpEntity<String> requestEntity = new HttpEntity<String>("", new HttpHeaders());
                ResponseEntity<byte[]> responseEntity = restTemplate.exchange(builder.build().encode().toUri(), HttpMethod.GET, requestEntity, byte[].class);
                return new ByteArrayResource(responseEntity.getBody());
                
        }
        
        public HttpResponse getReportFile(int fofoId, ReporticoProject projectName, String reportName) throws HttpHostConnectException, ProfitMandiBusinessException {
                Map<String, String> params = new HashMap<>();
                params.put("MANUAL_fofoId", String.valueOf(fofoId));
                return this.getReportFile(projectName, reportName, params);
        }

        
        public HttpResponse getReportFile(ReporticoProject projectName, String reportName, Map<String, String> params) throws HttpHostConnectException, ProfitMandiBusinessException {
                if(params==null) {
                        params=new HashMap<>();
                }
                params.putAll(projectAuthMap.get(projectName));
                params.put("xmlin", reportName);
                params.put("target_format", "CSV");
                params.put("execute_mode", "EXECUTE");
                return restClient.getResponse(reporticoUrl, params, new HashMap<>());
        }
        
        public HttpResponse getJsonFile(ReporticoProject projectName, String reportName, Map<String, String> params) throws HttpHostConnectException, ProfitMandiBusinessException {
                Map<String, String> authMap = projectAuthMap.get(projectName);
                params.putAll(authMap);
                params.put("xmlin", reportName);
                params.put("target_format", "JSON");
                params.put("execute_mode", "EXECUTE");
                return restClient.getResponse(reporticoUrl, params, new HashMap<>());
        }
}