1 19 20 package org.apache.cayenne.query; 21 22 import java.util.List ; 23 import java.util.Map ; 24 25 import org.apache.cayenne.CayenneRuntimeException; 26 import org.apache.cayenne.ObjectId; 27 import org.apache.cayenne.map.DbAttribute; 28 import org.apache.cayenne.map.DbEntity; 29 import org.apache.cayenne.map.EntityResolver; 30 import org.apache.commons.collections.Factory; 31 32 40 public abstract class BatchQuery implements Query { 41 42 45 protected int batchIndex; 46 47 50 protected DbEntity dbEntity; 51 52 protected String name; 53 54 public BatchQuery(DbEntity dbEntity) { 55 this.dbEntity = dbEntity; 56 this.batchIndex = -1; 57 } 58 59 public String getName() { 60 return name; 61 } 62 63 public void setName(String name) { 64 this.name = name; 65 } 66 67 72 public QueryMetadata getMetaData(EntityResolver resolver) { 73 return new DefaultQueryMetadata() { 74 75 public DbEntity getDbEntity() { 76 return dbEntity; 77 } 78 }; 79 } 80 81 84 public void route(QueryRouter router, EntityResolver resolver, Query substitutedQuery) { 85 router.route( 86 router.engineForDataMap(dbEntity.getDataMap()), 87 this, 88 substitutedQuery); 89 } 90 91 96 public SQLAction createSQLAction(SQLActionVisitor visitor) { 97 return visitor.batchAction(this); 98 } 99 100 105 public boolean isUsingOptimisticLocking() { 106 return false; 107 } 108 109 112 public DbEntity getDbEntity() { 113 return dbEntity; 114 } 115 116 119 public boolean isEmpty() { 120 return size() == 0; 121 } 122 123 126 public abstract List getDbAttributes(); 127 128 131 public void reset() { 132 batchIndex = -1; 133 } 134 135 140 public boolean next() { 141 batchIndex++; 142 return size() > batchIndex; 143 } 144 145 150 public abstract Object getValue(int valueIndex); 151 152 155 public abstract int size(); 156 157 163 protected Object getValue(Map valueMap, DbAttribute attribute) { 164 165 Object value = valueMap.get(attribute.getName()); 166 167 if (value instanceof Factory) { 170 value = ((Factory) value).create(); 171 172 if (attribute.isPrimaryKey()) { 174 if (value == null) { 176 String name = attribute.getEntity() != null ? attribute 177 .getEntity() 178 .getName() : "<null>"; 179 throw new CayenneRuntimeException("Failed to generate PK: " 180 + name 181 + "." 182 + attribute.getName()); 183 } 184 185 ObjectId id = getObjectId(); 186 if (id != null) { 187 id.getReplacementIdMap().put(attribute.getName(), value); 189 } 190 } 191 192 valueMap.put(attribute.getName(), value); 194 } 195 196 return value; 197 } 198 199 209 public ObjectId getObjectId() { 210 return null; 211 } 212 } 213 | Popular Tags |