Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
33973 tejus.loha 1
package com.spice.profitmandi.web.controller;
2
 
3
import com.fasterxml.jackson.core.type.TypeReference;
4
import com.fasterxml.jackson.databind.DeserializationFeature;
5
import com.fasterxml.jackson.databind.ObjectMapper;
6
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
7
import com.spice.profitmandi.service.reportico.ReporticoService;
8
import com.spice.profitmandi.service.SshServer.SSHService;
9
import org.apache.logging.log4j.LogManager;
10
import org.apache.logging.log4j.Logger;
11
import org.springframework.beans.factory.annotation.Autowired;
12
import org.springframework.beans.factory.annotation.Qualifier;
13
import org.springframework.beans.factory.annotation.Value;
14
import org.springframework.stereotype.Controller;
15
import org.springframework.ui.Model;
16
import org.springframework.web.bind.annotation.GetMapping;
17
import org.springframework.web.bind.annotation.RequestMapping;
18
import org.springframework.web.bind.annotation.RequestParam;
19
import org.springframework.web.client.RestTemplate;
20
 
21
import java.io.InputStream;
22
import java.net.URL;
23
import java.util.List;
24
import java.util.Map;
25
 
26
@Controller
27
@RequestMapping("/reportico")
28
public class ReporticoController {
29
 
30
    @Value("https://reports.smartdukaan.com/reports/projects/")
31
    private String host;
32
 
33
    @Qualifier("SSHServiceImpl")
34
    @Autowired
35
    private SSHService sshService;
36
 
37
    @Autowired
38
    private ReporticoService reporticoService;
39
 
40
    private static final Logger LOGGER = LogManager.getLogger(ReporticoController.class);
41
 
42
    private final RestTemplate restTemplate = new RestTemplate();
43
 
44
    @GetMapping
45
    public String listReports(@RequestParam(value = "directory", required = false) String directoryName, Model model){
46
 
47
        try {
48
            if (directoryName == null || directoryName.isEmpty()) {
49
                directoryName = "/var/www/reports/projects";
50
            }
51
 
52
            Map<String, List<String>> fileList = sshService.readDirectory(directoryName);
53
            LOGGER.info("List of directories and files: {}",fileList);
54
 
55
            model.addAttribute("directories", fileList);
56
 
57
        } catch (Exception e) {
58
            LOGGER.error("Catches Error", e);
59
        }
60
 
61
        return "reportico/index";
62
    }
63
 
64
    @GetMapping("/file-content")
65
    public String convertXml(@RequestParam String path, Model model) throws Exception {
66
        String xmlString = restTemplate.getForObject(host+path, String.class);
67
        String content = convertXmlToJson(xmlString);
68
        model.addAttribute("content",content);
69
        return "reportico/content";
70
    }
71
 
72
    private String convertXmlToJson(String xmlString) throws Exception {
73
        XmlMapper xmlMapper = new XmlMapper();
74
        xmlMapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
75
        ObjectMapper objectMapper = new ObjectMapper();
76
        Map<String, Object> map = xmlMapper.readValue(xmlString, new TypeReference<Map<String, Object>>() {});
77
        return objectMapper.writeValueAsString(map);
78
    }
79
 
80
    /*@GetMapping("/file-content")
81
    public String xmlReader(@RequestParam String path,Model model) throws Exception {
82
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
83
        DocumentBuilder builder = factory.newDocumentBuilder();
84
        try {
85
            Document document = builder.parse(host+path);
86
            document.getDocumentElement().normalize();
87
 
88
            String sql = getTagValue(document, "SQLRaw");
89
 
90
            model.addAttribute("title",getTagValue(document, "ReportTitle"));
91
            model.addAttribute("source",getTagValue(document, "SourceType"));
92
            model.addAttribute("displayOrders",getTagValuesInMap(document, "DisplayOrder", "ColumnName", "OrderNumber"));
93
            model.addAttribute("Criteria",getTagValuesInMap(document, "CriteriaItem", "CriteriaType", "CriteriaDisplay"));
94
            model.addAttribute("sql",sql);
95
            model.addAttribute("result",executeQuery(sql));
96
 
97
        } catch (Exception e) {
98
            LOGGER.info("catch xml content", e);
99
            model.addAttribute("title", e.getMessage());
100
        }
101
        return "reportico/content";
102
    }
103
 
104
    public List<List<String>> executeQuery(String query) throws Exception {
105
        return reporticoService.executeCustomQuery(query);
106
    }
107
 
108
    private static String getTagValue(Document doc, String tagName) {
109
        NodeList nodeList = doc.getElementsByTagName(tagName);
110
        if (nodeList != null && nodeList.getLength() > 0) {
111
            Element element = (Element) nodeList.item(0);
112
            return element.getTextContent();
113
        }
114
        return null;
115
    }
116
 
117
    public static Map<String, Map<String, String>> getTagValuesInMap(Document doc, String parentName, String childTag, String childTagValue) {
118
 
119
        Map<String, Map<String, String>> displayOrderMap = new HashMap<>();
120
 
121
        NodeList displayOrderNodes = doc.getElementsByTagName(parentName);
122
 
123
        for (int i = 0; i < displayOrderNodes.getLength(); i++) {
124
 
125
            Node node = displayOrderNodes.item(i);
126
 
127
            if (node.getNodeType() == Node.ELEMENT_NODE) {
128
 
129
                Element displayOrderElement = (Element) node;
130
 
131
                String columnName = displayOrderElement.getElementsByTagName(childTag).item(0).getTextContent().trim();
132
                String orderNumber = displayOrderElement.getElementsByTagName(childTagValue).item(0).getTextContent().trim();
133
 
134
                Map<String, String> values = new HashMap<>();
135
                values.put(childTagValue, orderNumber);
136
 
137
                displayOrderMap.put(columnName, values);
138
 
139
            }
140
 
141
        }
142
 
143
        return displayOrderMap;
144
 
145
    }*/
146
 
147
}