All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.geotab.model.serialization.SourceDeserializer Maven / Gradle / Ivy

/*
 *
 * 2020 Copyright (C) Geotab Inc. All rights reserved.
 */

package com.geotab.model.serialization;

import static com.geotab.model.entity.source.SourceAiModel.SOURCE_AI_MODEL_ID;
import static com.geotab.model.entity.source.SourceAiModelObsolete.SOURCE_AI_MODEL_OBSOLETE_ID;
import static com.geotab.model.entity.source.SourceBrp.SOURCE_BRP_ID;
import static com.geotab.model.entity.source.SourceBrpObsolete.SOURCE_BRP_OBSOLETE_ID;
import static com.geotab.model.entity.source.SourceGmccc.SOURCE_GMCCC_ID;
import static com.geotab.model.entity.source.SourceGmcccObsolete.SOURCE_GMCCC_OBSOLETE_ID;
import static com.geotab.model.entity.source.SourceGo.SOURCE_GEOTAB_GO_ID;
import static com.geotab.model.entity.source.SourceGoObsolete.GEOTAB_GO_OBSOLETE_ID;
import static com.geotab.model.entity.source.SourceJ1708.SOURCE_J1708_ID;
import static com.geotab.model.entity.source.SourceJ1708Obsolete.SOURCE_J1708_OBSOLETE_ID;
import static com.geotab.model.entity.source.SourceJ1939.SOURCE_J1939_ID;
import static com.geotab.model.entity.source.SourceJ1939Obsolete.SOURCE_J1939_OBSOLETE_ID;
import static com.geotab.model.entity.source.SourceLegacy.SOURCE_LEGACY_ID;
import static com.geotab.model.entity.source.SourceLegacyObsolete.SOURCE_LEGACY_OBSOLETE_ID;
import static com.geotab.model.entity.source.SourceObd.SOURCE_OBD_ID;
import static com.geotab.model.entity.source.SourceObdObsolete.SOURCE_OBD_OBSOLETE_ID;
import static com.geotab.model.entity.source.SourceObdSa.SOURCE_OBD_SA_ID;
import static com.geotab.model.entity.source.SourceObdSaObsolete.SOURCE_OBD_SA_OBSOLETE_ID;
import static com.geotab.model.entity.source.SourceProprietary.SOURCE_PROPRIETARY_ID;
import static com.geotab.model.entity.source.SourceProprietaryObsolete.SOURCE_PROPRIETARY_OBSOLETE_ID;
import static com.geotab.model.entity.source.SourceSystem.SOURCE_SYSTEM_ID;
import static com.geotab.model.entity.source.SourceSystemObsolete.SOURCE_SYSTEM_OBSOLETE_ID;
import static com.geotab.model.entity.source.SourceThirdParty.SOURCE_THIRD_PARTY_ID;
import static com.geotab.model.entity.source.SourceThirdPartyObsolete.SOURCE_THIRD_PARTY_OBSOLETE_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.source.Source;
import com.geotab.model.entity.source.SourceAiModel;
import com.geotab.model.entity.source.SourceAiModelObsolete;
import com.geotab.model.entity.source.SourceBrp;
import com.geotab.model.entity.source.SourceBrpObsolete;
import com.geotab.model.entity.source.SourceGmccc;
import com.geotab.model.entity.source.SourceGmcccObsolete;
import com.geotab.model.entity.source.SourceGo;
import com.geotab.model.entity.source.SourceGoObsolete;
import com.geotab.model.entity.source.SourceJ1708;
import com.geotab.model.entity.source.SourceJ1708Obsolete;
import com.geotab.model.entity.source.SourceJ1939;
import com.geotab.model.entity.source.SourceJ1939Obsolete;
import com.geotab.model.entity.source.SourceLegacy;
import com.geotab.model.entity.source.SourceLegacyObsolete;
import com.geotab.model.entity.source.SourceObd;
import com.geotab.model.entity.source.SourceObdObsolete;
import com.geotab.model.entity.source.SourceObdSa;
import com.geotab.model.entity.source.SourceObdSaObsolete;
import com.geotab.model.entity.source.SourceProprietary;
import com.geotab.model.entity.source.SourceProprietaryObsolete;
import com.geotab.model.entity.source.SourceSystem;
import com.geotab.model.entity.source.SourceSystemObsolete;
import com.geotab.model.entity.source.SourceThirdParty;
import com.geotab.model.entity.source.SourceThirdPartyObsolete;
import java.io.IOException;
import org.apache.commons.lang3.StringUtils;

public class SourceDeserializer extends JsonDeserializer {

  @Override
  public Source deserialize(JsonParser jsonParser, DeserializationContext context)
      throws IOException {
    ObjectCodec parserCodec = jsonParser.getCodec();
    JsonNode node = parserCodec.readTree(jsonParser);

    if (node.isTextual()) {
      return buildSource(node.textValue());
    } else if (node.isObject()) {
      Id id = node.get("id") != null ? parserCodec.treeToValue(node.get("id"), Id.class) : null;
      return buildSource(id != null ? id.getId() : null);
    }

    return null;
  }

  private Source buildSource(String id) {
    if (StringUtils.isEmpty(id)) {
      return null;
    }

    switch (id) {
      case SOURCE_J1708_ID:
        return SourceJ1708.getInstance();
      case SOURCE_J1939_ID:
        return SourceJ1939.getInstance();
      case SOURCE_OBD_ID:
        return SourceObd.getInstance();
      case SOURCE_GEOTAB_GO_ID:
        return SourceGo.getInstance();
      case SOURCE_SYSTEM_ID:
        return SourceSystem.getInstance();
      case SOURCE_THIRD_PARTY_ID:
        return SourceThirdParty.getInstance();
      case SOURCE_OBD_SA_ID:
        return SourceObdSa.getInstance();
      case SOURCE_PROPRIETARY_ID:
        return SourceProprietary.getInstance();
      case SOURCE_LEGACY_ID:
        return SourceLegacy.getInstance();
      case SOURCE_GMCCC_ID:
        return SourceGmccc.getInstance();
      case SOURCE_BRP_ID:
        return SourceBrp.getInstance();
      case SOURCE_J1708_OBSOLETE_ID:
        return SourceJ1708Obsolete.getInstance();
      case SOURCE_J1939_OBSOLETE_ID:
        return SourceJ1939Obsolete.getInstance();
      case SOURCE_OBD_OBSOLETE_ID:
        return SourceObdObsolete.getInstance();
      case GEOTAB_GO_OBSOLETE_ID:
        return SourceGoObsolete.getInstance();
      case SOURCE_SYSTEM_OBSOLETE_ID:
        return SourceSystemObsolete.getInstance();
      case SOURCE_THIRD_PARTY_OBSOLETE_ID:
        return SourceThirdPartyObsolete.getInstance();
      case SOURCE_OBD_SA_OBSOLETE_ID:
        return SourceObdSaObsolete.getInstance();
      case SOURCE_PROPRIETARY_OBSOLETE_ID:
        return SourceProprietaryObsolete.getInstance();
      case SOURCE_GMCCC_OBSOLETE_ID:
        return SourceGmcccObsolete.getInstance();
      case SOURCE_LEGACY_OBSOLETE_ID:
        return SourceLegacyObsolete.getInstance();
      case SOURCE_BRP_OBSOLETE_ID:
        return SourceBrpObsolete.getInstance();
      case SOURCE_AI_MODEL_ID:
        return SourceAiModel.getInstance();
      case SOURCE_AI_MODEL_OBSOLETE_ID:
        return SourceAiModelObsolete.getInstance();
      default:
        return Source.builder().id(id).name(id).build();
    }
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy