001package jmri.server.json.reporter;
002
003import static jmri.server.json.reporter.JsonReporter.LAST_REPORT;
004import static jmri.server.json.reporter.JsonReporter.REPORT;
005import static jmri.server.json.reporter.JsonReporter.REPORTER;
006import static jmri.server.json.reporter.JsonReporter.REPORTERS;
007
008import com.fasterxml.jackson.databind.JsonNode;
009import com.fasterxml.jackson.databind.ObjectMapper;
010import com.fasterxml.jackson.databind.node.ObjectNode;
011import javax.servlet.http.HttpServletResponse;
012import jmri.InstanceManager;
013import jmri.Reporter;
014import jmri.ReporterManager;
015import jmri.server.json.JSON;
016import jmri.server.json.JsonException;
017import jmri.server.json.JsonNamedBeanHttpService;
018import jmri.server.json.JsonRequest;
019
020/**
021 *
022 * @author Randall Wood Copyright 2016, 2018, 2019
023 */
024public class JsonReporterHttpService extends JsonNamedBeanHttpService<Reporter> {
025
026    public JsonReporterHttpService(ObjectMapper mapper) {
027        super(mapper);
028    }
029
030    @Override
031    public ObjectNode doPost(Reporter reporter, String name, String type, JsonNode data, JsonRequest request) throws JsonException {
032        if (data.path(JSON.USERNAME).isTextual()) {
033            reporter.setUserName(data.path(JSON.USERNAME).asText());
034        }
035        if (data.path(JSON.COMMENT).isTextual()) {
036            reporter.setComment(data.path(JSON.COMMENT).asText());
037        }
038        if (!data.path(REPORT).isMissingNode()) {
039            if (data.path(REPORT).isNull()) {
040                reporter.setReport(null);
041            } else {
042                reporter.setReport(data.path(REPORT).asText());
043            }
044        }
045        return doGet(reporter, name, type, request);
046    }
047
048    @Override
049    public ObjectNode doGet(Reporter reporter, String name, String type, JsonRequest request) throws JsonException {
050        ObjectNode root = getNamedBean(reporter, name, getType(), request); // throws JsonException if reporter == null
051        ObjectNode data = root.with(JSON.DATA);
052        data.put(JSON.STATE, reporter.getState());
053        Object cr = reporter.getCurrentReport();
054        if (cr != null) {
055            String report;
056            if (cr instanceof jmri.Reportable) {
057                report = ((jmri.Reportable) cr).toReportString();
058            } else {
059                report = cr.toString();
060            }
061            data.put(REPORT, report);
062            //value matches text displayed on panel
063            data.put(JSON.VALUE, (report.isEmpty() ? Bundle.getMessage(request.locale, "Blank") : report));
064        } else {
065            data.putNull(REPORT);
066            data.put(JSON.VALUE, Bundle.getMessage(request.locale, "NoReport"));
067        }
068        Object lr = reporter.getLastReport();
069        if (lr != null) {
070            String report;
071            if (lr instanceof jmri.Reportable) {
072                report = ((jmri.Reportable) lr).toReportString();
073            } else {
074                report = lr.toString();
075            }
076            data.put(LAST_REPORT, report);
077        } else {
078            data.putNull(LAST_REPORT);
079        }
080        return root;
081    }
082
083    @Override
084    protected void doDelete(Reporter reporter, String name, String type, JsonNode data, JsonRequest request) throws JsonException {
085        super.deleteBean(reporter, name, type, data, request);
086    }
087
088    @Override
089    public JsonNode doSchema(String type, boolean server, JsonRequest request) throws JsonException {
090        switch (type) {
091            case REPORTER:
092            case REPORTERS:
093                return doSchema(type,
094                        server,
095                        "jmri/server/json/reporter/reporter-server.json",
096                        "jmri/server/json/reporter/reporter-client.json",
097                        request.id);
098            default:
099                throw new JsonException(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, Bundle.getMessage(request.locale, JsonException.ERROR_UNKNOWN_TYPE, type), request.id);
100        }
101    }
102
103    @Override
104    protected ReporterManager getProvidingManager() {
105        return InstanceManager.getDefault(ReporterManager.class);
106    }
107    
108    @Override
109    protected String getType() {
110        return REPORTER;
111    }
112}