Subversion Repositories SmartDukaan

Rev

Rev 33107 | View as "text/plain" | Blame | Compare with Previous | Last modification | View Log | RSS feed

package com.spice.profitmandi.common.services;

import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectReader;
import com.spice.profitmandi.common.enumuration.ReporticoProject;
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
import com.spice.profitmandi.common.model.ReporticoResponseModel;
import com.spice.profitmandi.common.web.client.RestClient;
import org.apache.http.HttpResponse;
import org.apache.http.conn.HttpHostConnectException;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
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 java.util.*;

@Component
public class ReporticoService {

        private static final Logger LOGGER = LogManager.getLogger(ReporticoService.class);

        // private String reporticoUrl="http://192.168.134.118/reports/run.php";
        @Value("${reportico.url}")
        private String reporticoUrl;

        @Autowired
        RestClient restClient;

        @Autowired
        private ObjectMapper objectMapper;

        @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", "Sm@rtR3portDukaan");
                                                }
                                        });
                                        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", "$Warehouse@123New");
                                                }
                                        });
                                }
                        });

        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, null);
        }

        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<>());
        }

        public <T> List<T> getReports(Class<T> className, ReporticoProject project, String reportName,
                        Map<String, String> params) throws Exception {
                HttpResponse reportResponse = this.getJsonFile(project, reportName, params);

                LOGGER.info("reportResponse {}", reportResponse);
                JavaType type = objectMapper.getTypeFactory().constructParametricType(ReporticoResponseModel.class, className);
                ObjectReader or = objectMapper.readerFor(type);
                ReporticoResponseModel<T> responseObj = or.readValue(reportResponse.getEntity().getContent());
                
                LOGGER.info("responseObj {}", responseObj);

                return responseObj.getData();
        }
}