001package jmri.server.json.idtag;
002
003import com.fasterxml.jackson.databind.JsonNode;
004import com.fasterxml.jackson.databind.ObjectMapper;
005import com.fasterxml.jackson.databind.node.ObjectNode;
006import com.fasterxml.jackson.databind.util.StdDateFormat;
007
008import static jmri.server.json.idtag.JsonIdTag.IDTAG;
009
010import java.util.Date;
011import javax.servlet.http.HttpServletResponse;
012import jmri.IdTag;
013import jmri.IdTagManager;
014import jmri.InstanceManager;
015import jmri.NamedBean;
016import jmri.ProvidingManager;
017import jmri.Reporter;
018import jmri.ReporterManager;
019import jmri.server.json.JSON;
020import jmri.server.json.JsonException;
021import jmri.server.json.JsonNamedBeanHttpService;
022import jmri.server.json.JsonRequest;
023import jmri.server.json.reporter.JsonReporter;
024
025/**
026 *
027 * @author Randall Wood Copyright 2019
028 */
029public class JsonIdTagHttpService extends JsonNamedBeanHttpService<IdTag> {
030
031    public JsonIdTagHttpService(ObjectMapper mapper) {
032        super(mapper);
033    }
034
035    @Override
036    public ObjectNode doGet(IdTag idTag, String name, String type, JsonRequest request) throws JsonException {
037        ObjectNode root = this.getNamedBean(idTag, name, getType(), request); // throws JsonException if idTag == null
038        ObjectNode data = root.with(JSON.DATA);
039        if (idTag != null) {
040            int state = idTag.getState();
041            if (state == NamedBean.UNKNOWN) {
042                data.put(JSON.STATE, JSON.UNKNOWN);
043            } else {
044                data.put(JSON.STATE, state);
045            }
046            Reporter reporter = idTag.getWhereLastSeen();
047            data.put(JsonReporter.REPORTER, reporter != null ? reporter.getSystemName() : null);
048            Date date = idTag.getWhenLastSeen();
049            data.put(JSON.TIME, date != null ? new StdDateFormat().format(date) : null);
050        }
051        return root;
052    }
053
054    @Override
055    public ObjectNode doPost(IdTag idTag, String name, String type, JsonNode data, JsonRequest request) throws JsonException {
056        JsonNode node = data.path(JsonReporter.REPORTER);
057        if (node.isNull()) {
058            idTag.setWhereLastSeen(null);
059        } else if (node.isTextual()) {
060            Reporter reporter = InstanceManager.getDefault(ReporterManager.class).getBySystemName(node.asText());
061            if (reporter != null) {
062                idTag.setWhereLastSeen(reporter);
063            } else {
064                throw new JsonException(HttpServletResponse.SC_NOT_FOUND, Bundle.getMessage(request.locale, JsonException.ERROR_NOT_FOUND, JsonReporter.REPORTER, node.asText()), request.id);
065            }
066        }
067        return doGet(idTag, name, type, request);
068    }
069
070    @Override
071    public void doDelete(IdTag bean, String name, String type, JsonNode data, JsonRequest request) throws JsonException {
072        super.deleteBean(bean, name, type, data, request);
073    }
074
075    @Override
076    public JsonNode doSchema(String type, boolean server, JsonRequest request) throws JsonException {
077        if (IDTAG.equals(type)) {
078            return doSchema(type,
079                    server,
080                    "jmri/server/json/idtag/idTag-server.json",
081                    "jmri/server/json/idtag/idTag-client.json",
082                    request.id);
083        } else {
084            throw new JsonException(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, Bundle.getMessage(request.locale, JsonException.ERROR_UNKNOWN_TYPE, type), request.id);
085        }
086    }
087
088    @Override
089    protected String getType() {
090        return IDTAG;
091    }
092
093    @Override
094    protected ProvidingManager<IdTag> getProvidingManager() {
095        return InstanceManager.getDefault(IdTagManager.class);
096    }
097}