1 56 package org.objectstyle.cayenne.query; 57 58 import java.util.ArrayList ; 59 import java.util.List ; 60 import java.util.Map ; 61 62 import org.apache.commons.collections.Factory; 63 import org.objectstyle.cayenne.CayenneRuntimeException; 64 import org.objectstyle.cayenne.ObjectId; 65 import org.objectstyle.cayenne.map.DbAttribute; 66 import org.objectstyle.cayenne.map.DbEntity; 67 68 75 public class InsertBatchQuery extends BatchQuery { 76 77 80 protected List objectIds; 81 82 protected List objectSnapshots; 83 protected List dbAttributes; 84 85 88 protected int batchIndex; 89 90 93 public InsertBatchQuery(DbEntity entity, int batchCapacity) { 94 super(entity); 95 96 this.objectSnapshots = new ArrayList (batchCapacity); 97 this.objectIds = new ArrayList (batchCapacity); 98 this.dbAttributes = new ArrayList (getDbEntity().getAttributes()); 99 this.batchIndex = -1; 100 } 101 102 106 public void reset() { 107 batchIndex = -1; 108 } 109 110 public boolean next() { 111 batchIndex++; 112 return size() > batchIndex; 113 } 114 115 public Object getValue(int dbAttributeIndex) { 116 DbAttribute attribute = (DbAttribute) dbAttributes.get(dbAttributeIndex); 117 Map currentSnapshot = (Map ) objectSnapshots.get(batchIndex); 118 Object value = currentSnapshot.get(attribute.getName()); 119 120 if (value instanceof Factory) { 123 value = ((Factory) value).create(); 124 125 if (attribute.isPrimaryKey()) { 127 if (value == null) { 129 String name = attribute.getEntity() != null ? attribute 130 .getEntity() 131 .getName() : "<null>"; 132 throw new CayenneRuntimeException("Failed to generate PK: " 133 + name 134 + "." 135 + attribute.getName()); 136 } 137 138 ObjectId id = getObjectId(); 139 if (id != null) { 140 id.getReplacementIdMap().put(attribute.getName(), value); 142 } 143 } 144 145 currentSnapshot.put(attribute.getName(), value); 147 } 148 149 return value; 150 } 151 152 155 public void add(Map snapshot) { 156 add(snapshot, null); 157 } 158 159 167 public void add(Map snapshot, ObjectId id) { 168 objectSnapshots.add(snapshot); 169 objectIds.add(id); 170 } 171 172 public int size() { 173 return objectSnapshots.size(); 174 } 175 176 public List getDbAttributes() { 177 return dbAttributes; 178 } 179 180 185 public ObjectId getObjectId() { 186 return (ObjectId) objectIds.get(batchIndex); 187 } 188 } | Popular Tags |