com.cognite.client.servicesV1.parser.AssetParser 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.Asset;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableMap;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import static com.cognite.client.servicesV1.ConnectorConstants.MAX_LOG_ELEMENT_LENGTH;
/**
* This class contains a set of methods to help parsing file objects between Cognite api representations
* (json and proto) and typed objects.
*/
public class AssetParser {
static final String logPrefix = "AssetParser - ";
static final ObjectMapper objectMapper = new ObjectMapper();
/**
* Parses an event json string to Asset
proto object.
*
* @param json
* @return
* @throws Exception
*/
public static Asset parseAsset(String json) throws Exception {
JsonNode root = objectMapper.readTree(json);
Asset.Builder assetBuilder = Asset.newBuilder();
// An asset must contain a name and id.
if (root.path("id").isIntegralNumber()) {
assetBuilder.setId(root.get("id").longValue());
} else {
throw new Exception(logPrefix + "Unable to parse attribute: id. Item exerpt: "
+ json
.substring(0, Math.min(json.length() - 1, MAX_LOG_ELEMENT_LENGTH)));
}
if (root.path("name").isTextual()) {
assetBuilder.setName(root.get("name").textValue());
} else {
throw new Exception(logPrefix + "Unable to parse attribute: name");
}
// The rest of the attributes are optional.
if (root.path("externalId").isTextual()) {
assetBuilder.setExternalId(root.get("externalId").textValue());
}
if (root.path("rootId").isIntegralNumber()) {
assetBuilder.setRootId(root.get("rootId").longValue());
}
if (root.path("parentId").isIntegralNumber()) {
assetBuilder.setParentId(root.get("parentId").longValue());
}
if (root.path("parentExternalId").isTextual()) {
assetBuilder.setParentExternalId(root.get("parentExternalId").textValue());
}
if (root.path("description").isTextual()) {
assetBuilder.setDescription(root.get("description").textValue());
}
if (root.path("source").isTextual()) {
assetBuilder.setSource(root.get("source").textValue());
}
if (root.path("createdTime").isIntegralNumber()) {
assetBuilder.setCreatedTime(root.get("createdTime").longValue());
}
if (root.path("lastUpdatedTime").isIntegralNumber()) {
assetBuilder.setLastUpdatedTime(root.get("lastUpdatedTime").longValue());
}
if (root.path("dataSetId").isIntegralNumber()) {
assetBuilder.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()) {
assetBuilder.putMetadata(entry.getKey(), entry.getValue().textValue());
}
}
}
if (root.path("labels").isArray()) {
for (JsonNode node : root.path("labels")) {
if (node.path("externalId").isTextual()) {
assetBuilder.addLabels(node.path("externalId").textValue());
}
}
}
// process the aggregates nested object
if (root.path("aggregates").isObject()) {
Asset.Aggregates.Builder aggregatesBuilder = Asset.Aggregates.newBuilder();
JsonNode aggregates = root.get("aggregates");
if (aggregates.path("childCount").isIntegralNumber()) {
aggregatesBuilder.setChildCount(aggregates.get("childCount").intValue());
}
if (aggregates.path("depth").isIntegralNumber()) {
aggregatesBuilder.setDepth(aggregates.get("depth").intValue());
}
if (aggregates.path("path").isArray()) {
for (JsonNode node : aggregates.get("path")) {
if (node.path("id").isIntegralNumber()) {
aggregatesBuilder.addPath(node.get("id").longValue());
}
}
}
assetBuilder.setAggregates(aggregatesBuilder.build());
}
return assetBuilder.build();
}
/**
* Builds a request insert item object from {@link Asset}.
*
* An insert item object creates a new asset data object in the Cognite system.
*
* @param element
* @return
*/
public static Map toRequestInsertItem(Asset element) {
Preconditions.checkArgument(element.hasName(),
"The asset must have a name in order to be created");
// Note that "id" cannot be a part of an insert request.
ImmutableMap.Builder mapBuilder = ImmutableMap.builder()
.put("name", element.getName());
if (element.hasExternalId()) {
mapBuilder.put("externalId", element.getExternalId());
}
if (element.hasDescription()) {
mapBuilder.put("description", element.getDescription());
}
if (element.hasParentExternalId()) {
mapBuilder.put("parentExternalId", element.getParentExternalId());
} else if (element.hasParentId()) {
mapBuilder.put("parentId", element.getParentId());
}
if (element.getMetadataCount() > 0) {
mapBuilder.put("metadata", element.getMetadataMap());
}
if (element.hasSource()) {
mapBuilder.put("source", element.getSource());
}
if (element.hasDataSetId()) {
mapBuilder.put("dataSetId", element.getDataSetId());
}
if (element.getLabelsCount() > 0) {
List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy