com.geotab.model.serialization.ControllerDeserializer Maven / Gradle / Ivy
/*
*
* 2020 Copyright (C) Geotab Inc. All rights reserved.
*/
package com.geotab.model.serialization;
import static com.geotab.model.entity.controller.BrpFaultController.BRP_FAULT_CONTROLLER_ID;
import static com.geotab.model.entity.controller.GmcccFaultController.GMCC_FAULT_CONTROLLER_ID;
import static com.geotab.model.entity.controller.GoFaultController.GO_FAULT_CONTROLLER_ID;
import static com.geotab.model.entity.controller.LegacyFaultController.LEGACY_FAULT_CONTROLLER_ID;
import static com.geotab.model.entity.controller.NoController.NO_CONTROLLER_ID;
import static com.geotab.model.entity.controller.ProprietaryFaultController.PROPRIETARY_FAULT_CONTROLLER_ID;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.ObjectCodec;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
import com.geotab.model.Id;
import com.geotab.model.entity.controller.BrpFaultController;
import com.geotab.model.entity.controller.Controller;
import com.geotab.model.entity.controller.GmcccFaultController;
import com.geotab.model.entity.controller.GoFaultController;
import com.geotab.model.entity.controller.LegacyFaultController;
import com.geotab.model.entity.controller.NoController;
import com.geotab.model.entity.controller.ProprietaryFaultController;
import com.geotab.model.entity.source.Source;
import java.io.IOException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
@Slf4j
public class ControllerDeserializer extends JsonDeserializer {
@Override
public Controller deserialize(JsonParser jsonParser, DeserializationContext context)
throws IOException {
ObjectCodec parserCodec = jsonParser.getCodec();
JsonNode node = parserCodec.readTree(jsonParser);
if (node.isTextual()) {
String controllerId = node.textValue();
Controller controller = buildSystemController(controllerId);
if (StringUtils.isNotEmpty(controllerId) && controller == null) {
log.warn("Unknown system controller with id {}. Creating a normal controller",
controllerId);
controller = Controller.builder()
.id(controllerId)
.name(controllerId)
.version(-1L)
.code((short) 0)
.codeId((short) 0)
.build();
}
return controller;
} else if (node.isObject()) {
Id id = node.get("id") != null ? parserCodec.treeToValue(node.get("id"), Id.class) : null;
Controller controller = buildSystemController(id != null ? id.getId() : null);
if (controller != null) {
// update with the server version
Long version =
node.get("version") != null ? parserCodec.treeToValue(node.get("version"), Long.class)
: null;
controller.setVersion(version);
return controller;
}
String name = node.get("name") != null ? node.get("name").textValue() : null;
Long version =
node.get("version") != null ? parserCodec.treeToValue(node.get("version"), Long.class)
: null;
Short code = node.get("code") != null ? node.get("code").shortValue() : null;
Short codeId = node.get("codeId") != null ? node.get("codeId").shortValue() : null;
Source source =
node.get("source") != null ? parserCodec.treeToValue(node.get("source"), Source.class)
: null;
return Controller.builder()
.id(id != null ? id.getId() : null)
.name(name)
.version(version)
.code(code)
.codeId(codeId)
.source(source)
.build();
}
return null;
}
private Controller buildSystemController(String id) {
if (StringUtils.isEmpty(id)) {
return null;
}
switch (id) {
case BRP_FAULT_CONTROLLER_ID:
return BrpFaultController.getInstance();
case GMCC_FAULT_CONTROLLER_ID:
return GmcccFaultController.getInstance();
case GO_FAULT_CONTROLLER_ID:
return GoFaultController.getInstance();
case LEGACY_FAULT_CONTROLLER_ID:
return LegacyFaultController.getInstance();
case PROPRIETARY_FAULT_CONTROLLER_ID:
return ProprietaryFaultController.getInstance();
case NO_CONTROLLER_ID:
return NoController.getInstance();
default:
return null;
}
}
}