/*
 * 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 com.google.cloud.spring.autoconfigure.datastore;

import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.nio.charset.Charset;

import com.google.cloud.datastore.Key;
import com.google.cloud.spring.data.datastore.core.mapping.DatastoreDataException;
import com.google.cloud.spring.data.datastore.core.mapping.DatastoreMappingContext;

import org.springframework.data.rest.webmvc.spi.BackendIdConverter;

/**
 * A key converter that parses Key JSON from REST requests.
 *
 * @author Chengyuan Zhao
 *
 * @since 1.2
 */
public class DatastoreKeyIdConverter implements BackendIdConverter {

	private final DatastoreMappingContext datastoreMappingContext;

	/**
	 * Constructor.
	 *
	 * @param datastoreMappingContext the mapping context.
	 */
	public DatastoreKeyIdConverter(DatastoreMappingContext datastoreMappingContext) {
		this.datastoreMappingContext = datastoreMappingContext;
	}

	@Override
	public Serializable fromRequestId(String s, Class<?> aClass) {
		try {
			return Key.fromUrlSafe(URLDecoder.decode(s, Charset.defaultCharset().name()));
		}
		catch (UnsupportedEncodingException e) {
			throw new DatastoreDataException("Could not decode URL key param: " + s);
		}
	}

	@Override
	public String toRequestId(Serializable serializable, Class<?> aClass) {
		return ((Key) serializable).toUrlSafe();
	}

	@Override
	public boolean supports(Class<?> entityType) {
		// This ID converter only covers the Datastore key type. Returning false here causes the
		// default converter from Spring Data to be used.
		try {
			return this.datastoreMappingContext.getPersistentEntity(entityType).getIdProperty().getType().equals(Key.class);
		}
		catch (NullPointerException ex) {
			return false;
		}
	}
}

@@ -808,16 +808,43 @@
 Charset;
+%0Aimport java.util.Optional;
 %0A%0Aimport
@@ -1044,16 +1044,134 @@
 ntext;%0A%0A
+import org.springframework.data.mapping.PersistentEntity;%0Aimport org.springframework.data.mapping.PersistentProperty;%0A
 import o
@@ -2344,32 +2344,43 @@
 used.%0A%09%09
-try %7B%0A%09%09%09return 
+return Optional.ofNullable(
 this.dat
@@ -2435,34 +2435,109 @@
 ype)
-.getIdProperty().getType()
+)%0A%09%09%09%09.map(PersistentEntity::getIdProperty)%0A%09%09%09%09.map(PersistentProperty::getType)%0A%09%09%09%09.map(clz -%3E clz
 .equ
@@ -2554,70 +2554,29 @@
 ass)
-;%0A%09%09%7D%0A%09%09catch (NullPointerException ex) %7B%0A%09%09%09return false;%0A%09%09%7D
+)%0A%09%09%09%09.orElse(false);
 %0A%09%7D%0A

Fix last 2 'NPE could be thrown here' bugs (#308)