Subversion Repositories SmartDukaan

Rev

Blame | Last modification | View Log | RSS feed

package com.spice.profitmandi.web.controller;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import com.spice.profitmandi.service.reportico.ReporticoService;
import com.spice.profitmandi.service.SshServer.SSHService;
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.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.client.RestTemplate;

import java.io.InputStream;
import java.net.URL;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/reportico")
public class ReporticoController {

    @Value("https://reports.smartdukaan.com/reports/projects/")
    private String host;

    @Qualifier("SSHServiceImpl")
    @Autowired
    private SSHService sshService;

    @Autowired
    private ReporticoService reporticoService;

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

    private final RestTemplate restTemplate = new RestTemplate();

    @GetMapping
    public String listReports(@RequestParam(value = "directory", required = false) String directoryName, Model model){

        try {
            if (directoryName == null || directoryName.isEmpty()) {
                directoryName = "/var/www/reports/projects";
            }

            Map<String, List<String>> fileList = sshService.readDirectory(directoryName);
            LOGGER.info("List of directories and files: {}",fileList);

            model.addAttribute("directories", fileList);

        } catch (Exception e) {
            LOGGER.error("Catches Error", e);
        }

        return "reportico/index";
    }

    @GetMapping("/file-content")
    public String convertXml(@RequestParam String path, Model model) throws Exception {
        String xmlString = restTemplate.getForObject(host+path, String.class);
        String content = convertXmlToJson(xmlString);
        model.addAttribute("content",content);
        return "reportico/content";
    }

    private String convertXmlToJson(String xmlString) throws Exception {
        XmlMapper xmlMapper = new XmlMapper();
        xmlMapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
        ObjectMapper objectMapper = new ObjectMapper();
        Map<String, Object> map = xmlMapper.readValue(xmlString, new TypeReference<Map<String, Object>>() {});
        return objectMapper.writeValueAsString(map);
    }

    /*@GetMapping("/file-content")
    public String xmlReader(@RequestParam String path,Model model) throws Exception {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        try {
            Document document = builder.parse(host+path);
            document.getDocumentElement().normalize();

            String sql = getTagValue(document, "SQLRaw");

            model.addAttribute("title",getTagValue(document, "ReportTitle"));
            model.addAttribute("source",getTagValue(document, "SourceType"));
            model.addAttribute("displayOrders",getTagValuesInMap(document, "DisplayOrder", "ColumnName", "OrderNumber"));
            model.addAttribute("Criteria",getTagValuesInMap(document, "CriteriaItem", "CriteriaType", "CriteriaDisplay"));
            model.addAttribute("sql",sql);
            model.addAttribute("result",executeQuery(sql));

        } catch (Exception e) {
            LOGGER.info("catch xml content", e);
            model.addAttribute("title", e.getMessage());
        }
        return "reportico/content";
    }

    public List<List<String>> executeQuery(String query) throws Exception {
        return reporticoService.executeCustomQuery(query);
    }

    private static String getTagValue(Document doc, String tagName) {
        NodeList nodeList = doc.getElementsByTagName(tagName);
        if (nodeList != null && nodeList.getLength() > 0) {
            Element element = (Element) nodeList.item(0);
            return element.getTextContent();
        }
        return null;
    }

    public static Map<String, Map<String, String>> getTagValuesInMap(Document doc, String parentName, String childTag, String childTagValue) {

        Map<String, Map<String, String>> displayOrderMap = new HashMap<>();

        NodeList displayOrderNodes = doc.getElementsByTagName(parentName);

        for (int i = 0; i < displayOrderNodes.getLength(); i++) {

            Node node = displayOrderNodes.item(i);

            if (node.getNodeType() == Node.ELEMENT_NODE) {

                Element displayOrderElement = (Element) node;

                String columnName = displayOrderElement.getElementsByTagName(childTag).item(0).getTextContent().trim();
                String orderNumber = displayOrderElement.getElementsByTagName(childTagValue).item(0).getTextContent().trim();

                Map<String, String> values = new HashMap<>();
                values.put(childTagValue, orderNumber);

                displayOrderMap.put(columnName, values);

            }

        }

        return displayOrderMap;

    }*/

}