com.cognite.client.servicesV1.parser.FileParser 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.FileMetadata;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
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 FileParser {
static final String logPrefix = "FileParser - ";
static final ObjectMapper objectMapper = new ObjectMapper();
/**
* Parses a file metadata/header json string to FileMetadata
proto object.
*
* @param json
* @return
* @throws Exception
*/
public static FileMetadata parseFileMetadata(String json) throws Exception {
JsonNode root = objectMapper.readTree(json);
FileMetadata.Builder fileMetaBuilder = FileMetadata.newBuilder();
// A file must contain id, name, isUploaded, created and updatedTime.
if (root.path("id").isIntegralNumber()) {
fileMetaBuilder.setId(root.get("id").longValue());
} else {
throw new Exception(FileParser.buildErrorMessage("id", json));
}
if (root.path("name").isTextual()) {
fileMetaBuilder.setName(root.get("name").textValue());
} else {
throw new Exception(FileParser.buildErrorMessage("name", json));
}
if (root.path("directory").isTextual()) {
fileMetaBuilder.setDirectory(root.get("directory").textValue());
}
if (root.path("uploaded").isBoolean()) {
fileMetaBuilder.setUploaded(root.get("uploaded").booleanValue());
} else {
throw new Exception(FileParser.buildErrorMessage("uploaded", json));
}
if (root.path("createdTime").isIntegralNumber()) {
fileMetaBuilder.setCreatedTime(root.get("createdTime").longValue());
} else {
throw new Exception(FileParser.buildErrorMessage("createdTime", json));
}
if (root.path("lastUpdatedTime").isIntegralNumber()) {
fileMetaBuilder.setLastUpdatedTime(root.get("lastUpdatedTime").longValue());
} else {
throw new Exception(FileParser.buildErrorMessage("lastUpdatedTime", json));
}
// The rest of the attributes are optional.
if (root.path("externalId").isTextual()) {
fileMetaBuilder.setExternalId(root.get("externalId").textValue());
}
if (root.path("uploadedTime").isIntegralNumber()) {
fileMetaBuilder.setUploadedTime(root.get("uploadedTime").longValue());
}
if (root.path("source").isTextual()) {
fileMetaBuilder.setSource(root.get("source").textValue());
}
if (root.path("mimeType").isTextual()) {
fileMetaBuilder.setMimeType(root.get("mimeType").textValue());
}
if (root.path("sourceCreatedTime").isIntegralNumber()) {
fileMetaBuilder.setSourceCreatedTime(root.get("sourceCreatedTime").longValue());
}
if (root.path("sourceModifiedTime").isIntegralNumber()) {
fileMetaBuilder.setSourceModifiedTime(root.get("sourceModifiedTime").longValue());
}
if (root.path("dataSetId").isIntegralNumber()) {
fileMetaBuilder.setDataSetId(root.get("dataSetId").longValue());
}
if (root.path("securityCategories").isArray()) {
for (JsonNode node : root.path("securityCategories")) {
if (node.isIntegralNumber()) {
fileMetaBuilder.addSecurityCategories(node.longValue());
}
}
}
if (root.path("assetIds").isArray()) {
for (JsonNode node : root.path("assetIds")) {
if (node.isIntegralNumber()) {
fileMetaBuilder.addAssetIds(node.longValue());
}
}
}
if (root.path("metadata").isObject()) {
Iterator> fieldIterator = root
.path("metadata").fields();
while (fieldIterator.hasNext()) {
Map.Entry entry = fieldIterator.next();
if (entry.getValue().isTextual()) {
fileMetaBuilder
.putMetadata(entry.getKey(), entry.getValue().textValue());
}
}
}
if (root.path("labels").isArray()) {
for (JsonNode node : root.path("labels")) {
if (node.path("externalId").isTextual()) {
fileMetaBuilder.addLabels(node.path("externalId").textValue());
}
}
}
return fileMetaBuilder.build();
}
/**
* Builds a request insert item object from FileMetadata
.
*
* An insert item object creates a new file (metadata) data object in the Cognite system.
*
* @param element
* @return
* @throws Exception
*/
public static Map toRequestInsertItem(FileMetadata element) throws Exception {
// Note that "id" cannot be a part of an insert request.
ImmutableMap.Builder mapBuilder = ImmutableMap.builder();
// Required fields
if (element.hasName()) {
mapBuilder.put("name", element.getName());
} else {
throw new Exception("Unable to find attribute [name] in the file metadata/header object. "
+ "externalId: [" + element.getExternalId() + "].");
}
// Optional fields
if (element.hasDirectory()) {
mapBuilder.put("directory", element.getDirectory());
}
if (element.hasExternalId()) {
mapBuilder.put("externalId", element.getExternalId());
}
if (element.hasSource()) {
mapBuilder.put("source", element.getSource());
}
if (element.hasMimeType()) {
mapBuilder.put("mimeType", element.getMimeType());
}
if (element.hasSourceCreatedTime()) {
mapBuilder.put("sourceCreatedTime", element.getSourceCreatedTime());
}
if (element.hasSourceModifiedTime()) {
mapBuilder.put("sourceModifiedTime", element.getSourceModifiedTime());
}
if (element.getAssetIdsCount() > 0) {
mapBuilder.put("assetIds", element.getAssetIdsList());
}
if (element.getMetadataCount() > 0) {
mapBuilder.put("metadata", element.getMetadataMap());
}
if (element.hasDataSetId()) {
mapBuilder.put("dataSetId", element.getDataSetId());
}
if (element.getLabelsCount() > 0) {
List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy