PageRenderTime 25ms CodeModel.GetById 11ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 1ms

/src/com/google/appengine/datanucleus/mapping/InsertMappingConsumer.java

http://datanucleus-appengine.googlecode.com/
Java | 103 lines | 60 code | 15 blank | 28 comment | 13 complexity | 5748f12b54e4f8d1c4fae7944f29fbfb MD5 | raw file
  1/**********************************************************************
  2Copyright (c) 2009 Google Inc.
  3
  4Licensed under the Apache License, Version 2.0 (the "License");
  5you may not use this file except in compliance with the License.
  6You may obtain a copy of the License at
  7
  8http://www.apache.org/licenses/LICENSE-2.0
  9
 10Unless required by applicable law or agreed to in writing, software
 11distributed under the License is distributed on an "AS IS" BASIS,
 12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13See the License for the specific language governing permissions and
 14limitations under the License.
 15**********************************************************************/
 16package com.google.appengine.datanucleus.mapping;
 17
 18import org.datanucleus.metadata.AbstractClassMetaData;
 19import org.datanucleus.metadata.AbstractMemberMetaData;
 20import org.datanucleus.metadata.ColumnMetaData;
 21import org.datanucleus.store.mapped.DatastoreField;
 22import org.datanucleus.store.mapped.mapping.JavaTypeMapping;
 23import org.datanucleus.store.mapped.mapping.MappingCallbacks;
 24import org.datanucleus.store.mapped.mapping.MappingConsumer;
 25import org.datanucleus.store.mapped.mapping.PersistableMapping;
 26import org.datanucleus.store.mapped.mapping.ReferenceMapping;
 27
 28import com.google.appengine.datanucleus.Utils;
 29
 30import java.util.ArrayList;
 31import java.util.List;
 32import java.util.Map;
 33
 34/**
 35 * {@link MappingConsumer} implementation that is used for the insertions.
 36 * Largely based on DataNucleus RDBMS InsertMappingConsumer.
 37 *
 38 * @author Max Ross <maxr@google.com>
 39 */
 40public class InsertMappingConsumer implements MappingConsumer {
 41
 42  private final List<MappingCallbacks> mc = new ArrayList<MappingCallbacks>();
 43  private final Map<Integer, AbstractMemberMetaData> fieldIndexToMemberMetaData = Utils.newHashMap();
 44  private final AbstractClassMetaData cmd;
 45
 46  // running tally of the number of fields we've consumed
 47  // we assume that fields are consumed in fieldIndex order
 48  private int numFields = 0;
 49
 50  public InsertMappingConsumer(AbstractClassMetaData cmd) {
 51    this.cmd = cmd;
 52  }
 53
 54  public void preConsumeMapping(int highestFieldNumber) {
 55  }
 56
 57  public void consumeMapping(JavaTypeMapping m, AbstractMemberMetaData fmd) {
 58    if (!fmd.getAbstractClassMetaData().isSameOrAncestorOf(cmd)) {
 59      // Make sure we only accept mappings from the correct part of any inheritance tree
 60      return;
 61    }
 62    if (m.getNumberOfDatastoreMappings() == 0 &&
 63        (m instanceof PersistableMapping || m instanceof ReferenceMapping)) {
 64      // Reachable Fields (that relate to this object but have no column in the table)
 65    } else {
 66      // Fields to be "inserted" (that have a datastore column)
 67      // Check if the field is "insertable" (either using JPA column, or JDO extension)
 68      if (fmd.hasExtension("insertable") && fmd.getValueForExtension("insertable").equalsIgnoreCase("false")) {
 69        return;
 70      }
 71      ColumnMetaData[] colmds = fmd.getColumnMetaData();
 72      if (colmds != null && colmds.length > 0) {
 73        for (ColumnMetaData colmd : colmds) {
 74          if (!colmd.getInsertable()) {
 75            // Not to be inserted
 76            return;
 77          }
 78        }
 79      }
 80    }
 81
 82    fieldIndexToMemberMetaData.put(numFields++, fmd);
 83
 84    if (m instanceof MappingCallbacks) {
 85        mc.add((MappingCallbacks) m);
 86    }
 87  }
 88
 89  public void consumeMapping(JavaTypeMapping m, int mappingType) {}
 90  public void consumeUnmappedDatastoreField(DatastoreField fld) {}
 91
 92  public List<MappingCallbacks> getMappingCallbacks() {
 93    return mc;
 94  }
 95
 96  public AbstractMemberMetaData getMemberMetaDataForIndex(int index) {
 97    return fieldIndexToMemberMetaData.get(index);
 98  }
 99
100  public Map<Integer, AbstractMemberMetaData> getFieldIndexToMemberMetaData() {
101    return fieldIndexToMemberMetaData;
102  }
103}