com.cognite.client.servicesV1.parser.SequenceParser Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of cdf-sdk-java Show documentation
Show all versions of cdf-sdk-java Show documentation
Java SDK for reading and writing from/to CDF resources.
/*
* Copyright (c) 2020 Cognite AS
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.cognite.client.servicesV1.parser;
import com.cognite.client.dto.*;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableBiMap;
import com.google.common.collect.ImmutableMap;
import com.google.protobuf.Int64Value;
import com.google.protobuf.StringValue;
import com.google.protobuf.Value;
import com.google.protobuf.util.Values;
import java.util.*;
import static com.cognite.client.servicesV1.ConnectorConstants.MAX_LOG_ELEMENT_LENGTH;
/**
* This class contains a set of methods to help parsing {@code Sequence} objects between Cognite api representations
* (json and proto) and typed objects.
*/
public class SequenceParser {
static final String logPrefix = "SequenceParser - ";
static final ObjectMapper objectMapper = new ObjectMapper();
private static final ImmutableBiMap valueTypeMap = ImmutableBiMap
.builder()
.put("DOUBLE", SequenceColumn.ValueType.DOUBLE)
.put("LONG", SequenceColumn.ValueType.LONG)
.put("STRING", SequenceColumn.ValueType.STRING)
.build();
/**
* Parses a sequence header json string to {@link SequenceMetadata} proto object.
*
* @param json The json representation of a sequence header
* @return The sequence header as a typed object
* @throws Exception
*/
public static SequenceMetadata parseSequenceMetadata(String json) throws Exception {
JsonNode root = objectMapper.readTree(json);
SequenceMetadata.Builder builder = SequenceMetadata.newBuilder();
String itemExcerpt = json.substring(0, Math.min(json.length() - 1, MAX_LOG_ELEMENT_LENGTH));
// A Sequence metadata object must contain an id and columns.
if (root.path("id").isIntegralNumber()) {
builder.setId(root.get("id").longValue());
} else {
throw new Exception(logPrefix + "Unable to parse attribute: id. Item excerpt: " + itemExcerpt);
}
if (root.path("columns").isArray()) {
for (JsonNode node : root.path("columns")) {
if (node.isContainerNode()) {
builder.addColumns(SequenceParser.parseSequenceColumn(node.toString()));
} else {
throw new Exception(logPrefix + "Unable to parse attribute: columns. "
+ "The column is not a json object node. Item excerpt: " + itemExcerpt);
}
}
} else {
throw new Exception(logPrefix + "Unable to parse attribute: columns. Item excerpt: " + itemExcerpt);
}
// The rest of the attributes are optional.
if (root.path("externalId").isTextual()) {
builder.setExternalId(root.get("externalId").textValue());
}
if (root.path("name").isTextual()) {
builder.setName(root.get("name").textValue());
}
if (root.path("description").isTextual()) {
builder.setDescription(root.get("description").textValue());
}
if (root.path("assetId").isIntegralNumber()) {
builder.setAssetId(root.get("assetId").longValue());
}
if (root.path("createdTime").isIntegralNumber()) {
builder.setCreatedTime(root.get("createdTime").longValue());
}
if (root.path("lastUpdatedTime").isIntegralNumber()) {
builder.setLastUpdatedTime(root.get("lastUpdatedTime").longValue());
}
if (root.path("dataSetId").isIntegralNumber()) {
builder.setDataSetId(root.get("dataSetId").longValue());
}
if (root.path("metadata").isObject()) {
Iterator> fieldIterator = root.path("metadata").fields();
while (fieldIterator.hasNext()) {
Map.Entry entry = fieldIterator.next();
if (entry.getValue().isTextual()) {
builder.putMetadata(entry.getKey(), entry.getValue().textValue());
}
}
}
return builder.build();
}
/**
* Parses a sequence body json string to {@link SequenceBody} proto object.
*
* @param json The json representation of a sequence body
* @return The sequence body as a typed object
* @throws Exception
*/
public static SequenceBody parseSequenceBody(String json) throws Exception {
JsonNode root = objectMapper.readTree(json);
SequenceBody.Builder builder = SequenceBody.newBuilder();
String itemExcerpt = json.substring(0, Math.min(json.length() - 1, MAX_LOG_ELEMENT_LENGTH));
// A Sequence metadata object must contain an id and columns.
if (root.path("id").isIntegralNumber()) {
builder.setId(root.get("id").longValue());
} else {
throw new Exception(logPrefix + "Unable to parse attribute: id. Item excerpt: " + itemExcerpt);
}
if (root.path("columns").isArray()) {
for (JsonNode node : root.path("columns")) {
if (node.isContainerNode()) {
builder.addColumns(SequenceParser.parseSequenceColumn(node.toString()));
} else {
throw new Exception(logPrefix + "Unable to parse attribute: columns. "
+ "The column is not a json object node. Item excerpt: " + itemExcerpt);
}
}
} else {
throw new Exception(logPrefix + "Unable to parse attribute: columns. Item excerpt: " + itemExcerpt);
}
if (root.path("rows").isArray()) {
for (JsonNode node : root.path("rows")) {
if (node.isContainerNode()) {
builder.addRows(SequenceParser.parseSequenceRow(node.toString()));
} else {
throw new Exception(logPrefix + "Unable to parse attribute: rows. "
+ "The row is not a json object node. Item excerpt: " + itemExcerpt);
}
}
} else {
throw new Exception(logPrefix + "Unable to parse attribute: rows. Item excerpt: " + itemExcerpt);
}
// The rest of the attributes are optional.
if (root.path("externalId").isTextual()) {
builder.setExternalId(root.get("externalId").textValue());
}
return builder.build();
}
/**
* Parses a sequence column json into a typed {@link SequenceColumn} object
*
* @param json The sequence column json object
* @return The typed sequence column
* @throws Exception
*/
private static SequenceColumn parseSequenceColumn(String json) throws Exception {
JsonNode root = objectMapper.readTree(json);
SequenceColumn.Builder builder = SequenceColumn.newBuilder();
String itemExcerpt = json.substring(0, Math.min(json.length() - 1, MAX_LOG_ELEMENT_LENGTH));
// A Sequence column object must contain an externalId.
if (root.path("externalId").isTextual()) {
builder.setExternalId(root.get("externalId").textValue());
} else {
throw new Exception(logPrefix + "Unable to parse attribute: column.externalId. Item excerpt: " + itemExcerpt);
}
// The rest of the attributes are optional.
if (root.path("name").isTextual()) {
builder.setName(root.get("name").textValue());
}
if (root.path("description").isTextual()) {
builder.setDescription(root.get("description").textValue());
}
if (root.path("valueType").isTextual()) {
Optional valueType = SequenceParser.parseValueType(root.get("valueType").textValue());
if (valueType.isPresent()) {
builder.setValueType(valueType.get());
} else {
throw new Exception(logPrefix + "Unable to parse attribute: column.valueType. Item excerpt: " + itemExcerpt);
}
}
if (root.path("createdTime").isIntegralNumber()) {
builder.setCreatedTime(root.get("createdTime").longValue());
}
if (root.path("lastUpdatedTime").isIntegralNumber()) {
builder.setLastUpdatedTime(root.get("lastUpdatedTime").longValue());
}
if (root.path("metadata").isObject()) {
Iterator> fieldIterator = root.path("metadata").fields();
while (fieldIterator.hasNext()) {
Map.Entry entry = fieldIterator.next();
if (entry.getValue().isTextual()) {
builder.putMetadata(entry.getKey(), entry.getValue().textValue());
}
}
}
return builder.build();
}
/**
* Parses a sequence row json into a typed {@link SequenceRow} object
*
* @param json The sequence row json object
* @return The typed sequence row
* @throws Exception
*/
private static SequenceRow parseSequenceRow(String json) throws Exception {
JsonNode root = objectMapper.readTree(json);
SequenceRow.Builder builder = SequenceRow.newBuilder();
String itemExcerpt = json.substring(0, Math.min(json.length() - 1, MAX_LOG_ELEMENT_LENGTH));
// A Sequence row object must contain a row number and a set of values.
if (root.path("rowNumber").isIntegralNumber()) {
builder.setRowNumber(root.get("rowNumber").longValue());
} else {
throw new Exception(logPrefix + "Unable to parse attribute: row.rowNumber. Item excerpt: " + itemExcerpt);
}
if (root.path("values").isArray()) {
for (JsonNode node : root.path("values")) {
if (node.isNumber()) {
builder.addValues(Values.of(node.doubleValue()));
} else if (node.isTextual()) {
builder.addValues(Values.of(node.textValue()));
} else if (node.isNull()) {
builder.addValues(Values.ofNull());
} else {
throw new Exception(logPrefix + "Unable to parse attribute: row.values. Item excerpt: " + itemExcerpt);
}
}
} else {
throw new Exception(logPrefix + "Unable to parse attribute: row.values. Item excerpt: " + itemExcerpt);
}
return builder.build();
}
/**
* Builds a request insert item object from SequenceMetadata
.
*
* An insert item object creates a new sequence header data object in the Cognite system.
*
* @param element
* @return
*/
public static Map toRequestInsertItem(SequenceMetadata element) {
ImmutableMap.Builder mapBuilder = ImmutableMap.builder();
if (element.hasExternalId()) {
mapBuilder.put("externalId", element.getExternalId());
}
if (element.hasName()) {
mapBuilder.put("name", element.getName());
}
if (element.hasDescription()) {
mapBuilder.put("description", element.getDescription());
}
if (element.hasAssetId()) {
mapBuilder.put("assetId", element.getAssetId());
}
if (element.getMetadataCount() > 0) {
mapBuilder.put("metadata", element.getMetadataMap());
}
if (element.getColumnsCount() > 0) {
List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy