PageRenderTime 43ms CodeModel.GetById 16ms app.highlight 20ms RepoModel.GetById 1ms app.codeStats 1ms

/src/com/google/appengine/datanucleus/query/QueryEntityPKFetchFieldManager.java

http://datanucleus-appengine.googlecode.com/
Java | 125 lines | 79 code | 19 blank | 27 comment | 13 complexity | 1b7628dddb116f66cd53717e2087dd7a MD5 | raw file
  1/**********************************************************************
  2Copyright (c) 2011 Andy Jefferson and others. All rights reserved.
  3Licensed under the Apache License, Version 2.0 (the "License");
  4you may not use this file except in compliance with the License.
  5You may obtain a copy of the License at
  6
  7    http://www.apache.org/licenses/LICENSE-2.0
  8
  9Unless required by applicable law or agreed to in writing, software
 10distributed under the License is distributed on an "AS IS" BASIS,
 11WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 12See the License for the specific language governing permissions and
 13limitations under the License.
 14
 15Contributors:
 16   ...
 17**********************************************************************/
 18package com.google.appengine.datanucleus.query;
 19
 20import org.datanucleus.exceptions.NucleusFatalUserException;
 21import org.datanucleus.metadata.AbstractClassMetaData;
 22import org.datanucleus.metadata.AbstractMemberMetaData;
 23import org.datanucleus.store.fieldmanager.AbstractFieldManager;
 24
 25import com.google.appengine.api.datastore.Entity;
 26import com.google.appengine.api.datastore.Key;
 27import com.google.appengine.api.datastore.KeyFactory;
 28import com.google.appengine.datanucleus.DatastoreManager;
 29import com.google.appengine.datanucleus.MetaDataUtils;
 30
 31/**
 32 * Simple implementation of a field manager for fetching values out of queries.
 33 * This version simply retrieves PK fields, so we can get hold of the identity.
 34 * FetchFieldManager really ought to be rewritten to follow this style rather than having
 35 * stacks of FetchState or whatever.
 36 */
 37public class QueryEntityPKFetchFieldManager extends AbstractFieldManager {
 38
 39    /** Metadata for the candidate class. */
 40    AbstractClassMetaData cmd;
 41
 42    Entity datastoreEntity;
 43
 44    public QueryEntityPKFetchFieldManager(AbstractClassMetaData cmd, Entity entity) {
 45      this.cmd = cmd;
 46      this.datastoreEntity = entity;
 47    }
 48
 49    public boolean fetchBooleanField(int fieldNumber) {
 50      return (Boolean) fetchObjectField(fieldNumber);
 51    }
 52
 53    public byte fetchByteField(int fieldNumber) {
 54      return (Byte) fetchObjectField(fieldNumber);
 55    }
 56
 57    public char fetchCharField(int fieldNumber) {
 58      return (Character) fetchObjectField(fieldNumber);
 59    }
 60
 61    public double fetchDoubleField(int fieldNumber) {
 62      return (Double) fetchObjectField(fieldNumber);
 63    }
 64
 65    public float fetchFloatField(int fieldNumber) {
 66      return (Float) fetchObjectField(fieldNumber);
 67    }
 68
 69    public int fetchIntField(int fieldNumber) {
 70      return (Integer) fetchObjectField(fieldNumber);
 71    }
 72
 73    public long fetchLongField(int fieldNumber) {
 74      return (Long) fetchObjectField(fieldNumber);
 75    }
 76
 77    public short fetchShortField(int fieldNumber) {
 78      return (Short) fetchObjectField(fieldNumber);
 79    }
 80
 81    public String fetchStringField(int fieldNumber) {
 82      if (isPK(fieldNumber)) {
 83        return fetchStringPKField(fieldNumber);
 84      }
 85      return null;
 86    }
 87
 88    public Object fetchObjectField(int fieldNumber) {
 89      AbstractMemberMetaData ammd = cmd.getMetaDataForManagedMemberAtAbsolutePosition(fieldNumber);
 90
 91      if (isPK(fieldNumber)) {
 92        if (ammd.getType().equals(Key.class)) {
 93          // If this is a pk field, transform the Key into its String representation.
 94          return datastoreEntity.getKey();
 95        } else if (ammd.getType().equals(Long.class)) {
 96          return datastoreEntity.getKey().getId();
 97        }
 98      }
 99      return null;
100    }
101
102    private String fetchStringPKField(int fieldNumber) {
103      if (MetaDataUtils.isEncodedPKField(cmd, fieldNumber)) {
104        // If this is an encoded pk field, transform the Key into its String representation.
105        return KeyFactory.keyToString(datastoreEntity.getKey());
106      } else {
107        if (datastoreEntity.getKey().isComplete() && datastoreEntity.getKey().getName() == null) {
108          // This is trouble, probably an incorrect mapping.
109          throw new NucleusFatalUserException(
110              "The primary key for " + cmd.getFullClassName() + " is an unencoded "
111              + "string but the key of the corresponding entity in the datastore does not have a "
112              + "name.  You may want to either change the primary key to be an encoded string "
113              + "(add the \"" + DatastoreManager.ENCODED_PK + "\" extension), change the "
114              + "primary key to be of type " + Key.class.getName() + ", or, if you're certain that "
115              + "this class will never have a parent, change the primary key to be of type Long.");
116        }
117        return datastoreEntity.getKey().getName();
118      }
119    }
120
121    protected boolean isPK(int fieldNumber) {
122      int[] pkPositions = cmd.getPKMemberPositions();
123      return pkPositions != null && pkPositions[0] == fieldNumber;
124    }
125}