1 56 package org.objectstyle.cayenne.access; 57 58 import java.util.Iterator ; 59 import java.util.List ; 60 61 import org.apache.log4j.Level; 62 import org.objectstyle.cayenne.CayenneException; 63 import org.objectstyle.cayenne.CayenneRuntimeException; 64 import org.objectstyle.cayenne.DataRow; 65 import org.objectstyle.cayenne.ObjectId; 66 import org.objectstyle.cayenne.map.DbAttribute; 67 import org.objectstyle.cayenne.query.InsertBatchQuery; 68 import org.objectstyle.cayenne.query.Query; 69 import org.objectstyle.cayenne.util.Util; 70 71 77 class DataDomainCommitObserver implements OperationObserver { 78 79 public void nextBatchCount(Query query, int[] resultCount) { 80 } 82 83 public void nextCount(Query query, int resultCount) { 84 } 86 87 public void nextDataRows(Query query, List dataRows) { 88 } 90 91 public void nextDataRows(Query q, ResultIterator it) { 92 } 94 95 98 public void nextGeneratedDataRows(Query query, ResultIterator keysIterator) { 99 List keys; 101 try { 102 keys = keysIterator.dataRows(true); 103 } 104 catch (CayenneException ex) { 105 throw new CayenneRuntimeException("Error reading primary key", Util 106 .unwindException(ex)); 107 } 108 109 if (!(query instanceof InsertBatchQuery)) { 110 throw new CayenneRuntimeException( 111 "Generated keys only supported for InsertBatchQuery, instead got " 112 + query); 113 } 114 115 InsertBatchQuery batch = (InsertBatchQuery) query; 116 117 ObjectId id = batch.getObjectId(); 118 if (id == null || !id.isTemporary()) { 119 return; 121 } 122 123 if (keys.size() != 1) { 124 throw new CayenneRuntimeException( 125 "One and only one PK row is expected, instead got " + keys.size()); 126 } 127 128 DataRow key = (DataRow) keys.get(0); 129 130 if (key.size() == 0) { 132 throw new CayenneRuntimeException("Empty key generated."); 133 } 134 135 137 if (key.size() > 1) { 142 throw new CayenneRuntimeException( 143 "Only a single column autogenerated PK is supported. " 144 + "Generated key: " 145 + key); 146 } 147 148 Iterator it = batch.getDbEntity().getGeneratedAttributes().iterator(); 149 while (it.hasNext()) { 150 DbAttribute attribute = (DbAttribute) it.next(); 151 152 if (attribute.isPrimaryKey()) { 155 Object value = key.values().iterator().next(); 156 157 id.getReplacementIdMap().put(attribute.getName(), value); 160 break; 161 } 162 } 163 } 164 165 public void nextGlobalException(Exception ex) { 166 throw new CayenneRuntimeException("Commit error.", Util.unwindException(ex)); 167 } 168 169 public void nextQueryException(Query query, Exception ex) { 170 throw new CayenneRuntimeException("Commit query error.", Util.unwindException(ex)); 171 } 172 173 public Level getLoggingLevel() { 174 return Query.DEFAULT_LOG_LEVEL; 175 } 176 177 public boolean isIteratedResult() { 178 return false; 179 } 180 } 181 | Popular Tags |