All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.loom.appengine.converter.KeyConverter Maven / Gradle / Ivy

The newest version!
package org.loom.appengine.converter;

import javax.inject.Singleton;

import org.apache.commons.lang.StringUtils;
import org.loom.converter.AbstractConverter;
import org.loom.converter.LocaleUnawareConverter;
import org.loom.i18n.Messages;
import org.loom.i18n.MessagesRepository;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;

/**
 * Converts a Google AppEngine Key to/from  a String.
 * This converter relies in the default Key serialization mechanism included in GAE.
 * Implements {@link LocaleUnawareConverter} to be able to use Keys as inside Maps
 * @author icoloma
 *
 */
@Singleton
public class KeyConverter extends AbstractConverter implements LocaleUnawareConverter {

	public KeyConverter() {
		super(Key.class);
	}

	@Override
	public Object getAsObject(String paramName, String paramValue,
			Messages messages, MessagesRepository repository) {
		try {
			return getAsObject(paramValue);
		} catch (IllegalArgumentException e) {
			addErrorMessage(messages, paramName, paramValue, "loom.conversion.keyFailed");
			return null;
		}
	}

	@Override
	public String getAsText(Object value, MessagesRepository repository) {
		return value == null? null : KeyFactory.keyToString((Key) value);
	}

	@Override
	public Object getAsObject(String paramValue) {
		return StringUtils.isEmpty(paramValue)? null : KeyFactory.stringToKey(paramValue);
	}
	

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy