Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright 2017-2019 the original author or authors.
*
* 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
*
* https://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 org.springframework.cloud.gcp.data.datastore.core.convert;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;
import com.google.cloud.datastore.BaseEntity;
import com.google.cloud.datastore.Blob;
import com.google.cloud.datastore.EntityValue;
import com.google.cloud.datastore.FullEntity;
import com.google.cloud.datastore.FullEntity.Builder;
import com.google.cloud.datastore.IncompleteKey;
import com.google.cloud.datastore.ListValue;
import com.google.cloud.datastore.Value;
import org.springframework.cloud.gcp.data.datastore.core.mapping.DatastoreDataException;
import org.springframework.cloud.gcp.data.datastore.core.mapping.DatastoreMappingContext;
import org.springframework.cloud.gcp.data.datastore.core.mapping.DatastorePersistentProperty;
import org.springframework.cloud.gcp.data.datastore.core.mapping.EmbeddedType;
import org.springframework.cloud.gcp.data.datastore.core.util.ValueUtil;
import org.springframework.core.convert.ConversionException;
import org.springframework.core.convert.converter.Converter;
import org.springframework.core.convert.support.DefaultConversionService;
import org.springframework.core.convert.support.GenericConversionService;
import org.springframework.data.convert.CustomConversions;
import org.springframework.data.util.ClassTypeInformation;
import org.springframework.data.util.TypeInformation;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
import static org.springframework.cloud.gcp.data.datastore.core.util.ValueUtil.boxIfNeeded;
/**
* In order to support {@link CustomConversions}, this class applies 2-step conversions.
* The first step produces one of
* {@link org.springframework.data.mapping.model.SimpleTypeHolder}'s simple types. The
* second step converts simple types to Datastore-native types. The second step is skipped
* if the first one produces a Datastore-native type.
*
* @author Dmitry Solomakha
* @author Chengyuan Zhao
*
* @since 1.1
*/
public class TwoStepsConversions implements ReadWriteConversions {
private static final Converter BLOB_TO_BYTE_ARRAY_CONVERTER = new Converter() {
@Override
public byte[] convert(Blob source) {
return source.toByteArray();
}
};
private static final Converter BYTE_ARRAY_TO_BLOB_CONVERTER = new Converter() {
@Override
public Blob convert(byte[] source) {
return Blob.copyFrom(source);
}
};
private final GenericConversionService conversionService;
private final GenericConversionService internalConversionService;
private final CustomConversions customConversions;
private final ObjectToKeyFactory objectToKeyFactory;
private final DatastoreMappingContext datastoreMappingContext;
private DatastoreEntityConverter datastoreEntityConverter;
private final Map>> writeConverters = new ConcurrentHashMap<>();
public TwoStepsConversions(CustomConversions customConversions,
ObjectToKeyFactory objectToKeyFactory, DatastoreMappingContext datastoreMappingContext) {
this.objectToKeyFactory = objectToKeyFactory;
this.datastoreMappingContext = datastoreMappingContext;
this.conversionService = new DefaultConversionService();
this.internalConversionService = new DefaultConversionService();
this.customConversions = customConversions;
this.customConversions.registerConvertersIn(this.conversionService);
this.internalConversionService.addConverter(BYTE_ARRAY_TO_BLOB_CONVERTER);
this.internalConversionService.addConverter(BLOB_TO_BYTE_ARRAY_CONVERTER);
}
@Override
public T convertOnRead(Object val, Class targetCollectionType, Class targetComponentType) {
return (T) convertOnRead(val, EmbeddedType.NOT_EMBEDDED,
targetCollectionType,
ClassTypeInformation.from(targetComponentType));
}
@Override
public T convertOnRead(Object val, EmbeddedType embeddedType, TypeInformation targetTypeInformation) {
TypeInformation componentTypeInformation;
Class collectionType = null;
if (targetTypeInformation.isCollectionLike()) {
componentTypeInformation = targetTypeInformation.getComponentType();
collectionType = targetTypeInformation.getType();
}
else {
componentTypeInformation = targetTypeInformation;
}
return convertOnRead(val, embeddedType, collectionType, componentTypeInformation);
}
private T convertOnRead(Object val, EmbeddedType embeddedType,
Class targetCollectionType, TypeInformation targetComponentType) {
if (val == null) {
return null;
}
BiFunction