1 56 package org.objectstyle.cayenne.query; 57 58 import java.util.ArrayList ; 59 import java.util.Collection ; 60 import java.util.Collections ; 61 import java.util.Iterator ; 62 import java.util.List ; 63 import java.util.Map ; 64 65 import org.apache.commons.collections.IteratorUtils; 66 import org.objectstyle.cayenne.map.DbAttribute; 67 import org.objectstyle.cayenne.map.DbEntity; 68 69 74 public class UpdateBatchQuery extends BatchQuery { 75 private List qualifierSnapshots; 76 private List updateSnapshots; 77 78 private List updatedAttributes; 79 private List qualifierAttributes; 80 private Collection nullQualifierNames; 81 82 private List dbAttributes; 83 84 private Iterator qualifierIterator = IteratorUtils.EMPTY_ITERATOR; 85 private Iterator updateIterator = IteratorUtils.EMPTY_ITERATOR; 86 private Map currentUpdate = Collections.EMPTY_MAP; 87 private Map currentQualifier = Collections.EMPTY_MAP; 88 89 private boolean usingOptimisticLocking; 90 91 100 public UpdateBatchQuery( 101 DbEntity dbEntity, 102 List qualifierAttributes, 103 List updatedAttribute, 104 Collection nullQualifierNames, 105 int batchCapacity) { 106 107 super(dbEntity); 108 109 this.updatedAttributes = updatedAttribute; 110 this.qualifierAttributes = qualifierAttributes; 111 this.nullQualifierNames = 112 nullQualifierNames != null ? nullQualifierNames : Collections.EMPTY_SET; 113 114 qualifierSnapshots = new ArrayList (batchCapacity); 115 updateSnapshots = new ArrayList (batchCapacity); 116 117 dbAttributes = 118 new ArrayList (updatedAttributes.size() + qualifierAttributes.size()); 119 dbAttributes.addAll(updatedAttributes); 120 dbAttributes.addAll(qualifierAttributes); 121 } 122 123 129 public boolean isNull(DbAttribute attribute) { 130 return nullQualifierNames.contains(attribute.getName()); 131 } 132 133 138 public boolean isUsingOptimisticLocking() { 139 return usingOptimisticLocking; 140 } 141 142 145 public void setUsingOptimisticLocking(boolean usingOptimisticLocking) { 146 this.usingOptimisticLocking = usingOptimisticLocking; 147 } 148 149 public void reset() { 150 qualifierIterator = qualifierSnapshots.iterator(); 151 updateIterator = updateSnapshots.iterator(); 152 currentQualifier = Collections.EMPTY_MAP; 153 currentUpdate = Collections.EMPTY_MAP; 154 } 155 156 public boolean next() { 157 if (!qualifierIterator.hasNext()) { 158 return false; 159 } 160 161 currentQualifier = (Map ) qualifierIterator.next(); 162 currentQualifier = 163 (currentQualifier != null) ? currentQualifier : Collections.EMPTY_MAP; 164 currentUpdate = (Map ) updateIterator.next(); 165 currentUpdate = (currentUpdate != null) ? currentUpdate : Collections.EMPTY_MAP; 166 return true; 167 } 168 169 public Object getValue(int dbAttributeIndex) { 170 DbAttribute attribute = (DbAttribute) dbAttributes.get(dbAttributeIndex); 171 String name = attribute.getName(); 172 173 return (dbAttributeIndex < updatedAttributes.size()) 176 ? currentUpdate.get(name) 177 : currentQualifier.get(name); 178 } 179 180 187 public void add(Map qualifierSnapshot, Map updateSnapshot) { 188 qualifierSnapshots.add(qualifierSnapshot); 189 updateSnapshots.add(updateSnapshot); 190 } 191 192 public int size() { 193 return qualifierSnapshots.size(); 194 } 195 196 public List getDbAttributes() { 197 return dbAttributes; 198 } 199 200 203 public List getUpdatedAttributes() { 204 return Collections.unmodifiableList(updatedAttributes); 205 } 206 207 210 public List getQualifierAttributes() { 211 return Collections.unmodifiableList(qualifierAttributes); 212 } 213 214 219 public Map getCurrentQualifier() { 220 return currentQualifier; 221 } 222 } 223 | Popular Tags |