Key decodedKey;
Object result = null;
try {
decodedKey = KeyFactory.stringToKey((String) val);
if (!decodedKey.isComplete()) {
throw new NucleusFatalUserException(
"Received a request to find an object of kind " + kind + " but the provided "
+ "identifier is the String representation of an incomplete Key for kind "
+ decodedKey.getKind());
}
} catch (IllegalArgumentException iae) {
if (pkType.equals(Long.class) || pkType.equals(long.class)) {
// We were given an unencoded String and the pk type is Long.
// There's no way that can be valid
throw new NucleusFatalUserException(
"Received a request to find an object of type " + cls.getName() + " identified by the String "
+ val + ", but the primary key of " + cls.getName() + " is of type Long.");
}
// this is ok, it just means we were only given the name
decodedKey = KeyFactory.createKey(kind, (String) val);
}
if (!decodedKey.getKind().equals(kind)) {
throw new NucleusFatalUserException(
"Received a request to find an object of kind " + kind + " but the provided "
+ "identifier is the String representation of a Key for kind "
+ decodedKey.getKind());
}
if (pkType.equals(String.class)) {
if (pkMemberMetaData.hasExtension(DatastoreManager.ENCODED_PK)) {
// Need to make sure we pass on an encoded pk
result = KeyFactory.keyToString(decodedKey);
} else {
if (decodedKey.getParent() != null) {
throw new NucleusFatalUserException(
"Received a request to find an object of type " + cls.getName() + ". The primary "
+ "key for this type is an unencoded String, which means instances of this type "
+ "never have parents. However, the encoded string representation of the Key that "
+ "was provided as an argument has a parent.");
}
// Pk is an unencoded string so need to pass on just the name
// component. However, we need to make sure the provided key actually
// contains a name component.
if (decodedKey.getName() == null) {
throw new NucleusFatalUserException(
"Received a request to find an object of type " + cls.getName() + ". The primary "
+ "key for this type is an unencoded String. However, the encoded string "
+ "representation of the Key that was provided as an argument has its id field "
+ "set, not its name. This makes it an invalid key for this class.");
}
result = decodedKey.getName();
}
} else if (pkType.equals(Long.class) || pkType.equals(long.class)) {
if (decodedKey.getParent() != null) {
throw new NucleusFatalUserException(
"Received a request to find an object of type " + cls.getName() + ". The primary "
+ "key for this type is a Long, which means instances of this type "
+ "never have parents. However, the encoded string representation of the Key that "
+ "was provided as an argument has a parent.");
}
if (decodedKey.getName() != null) {
throw new NucleusFatalUserException(
"Received a request to find an object of type " + cls.getName() + " identified by the "
+ "encoded String representation of "
+ decodedKey + ", but the primary key of " + cls.getName() + " is of type Long and the "
+ "encoded String has its name component set. It must have its id component set "
+ "instead in order to be legal.");