1 19 20 package org.apache.cayenne; 21 22 import java.util.HashMap ; 23 import java.util.Iterator ; 24 import java.util.List ; 25 import java.util.Map ; 26 27 import org.apache.commons.lang.builder.ToStringBuilder; 28 import org.apache.cayenne.map.DbAttribute; 29 import org.apache.cayenne.map.DbEntity; 30 import org.apache.cayenne.map.DbRelationship; 31 import org.apache.cayenne.map.ObjEntity; 32 import org.apache.cayenne.util.Util; 33 34 42 public class DataRow extends HashMap { 43 44 47 private static volatile long currentVersion = DataObject.DEFAULT_VERSION + 1; 49 50 protected long version = currentVersion++; 51 protected long replacesVersion = DataObject.DEFAULT_VERSION; 52 53 public DataRow(Map map) { 54 super(map); 55 } 56 57 public DataRow(int initialCapacity) { 58 super(initialCapacity); 59 } 60 61 public long getVersion() { 62 return version; 63 } 64 65 public long getReplacesVersion() { 66 return replacesVersion; 67 } 68 69 72 public void setReplacesVersion(long replacesVersion) { 73 this.replacesVersion = replacesVersion; 74 } 75 76 80 public DataRow applyDiff(DataRow diff) { 81 DataRow merged = new DataRow(this); 82 83 Iterator it = diff.entrySet().iterator(); 84 while (it.hasNext()) { 85 Map.Entry entry = (Map.Entry ) it.next(); 86 merged.put(entry.getKey(), entry.getValue()); 87 } 88 89 return merged; 90 } 91 92 99 public DataRow createDiff(DataRow row) { 100 101 DataRow diff = null; 103 104 Iterator entries = entrySet().iterator(); 105 while (entries.hasNext()) { 106 Map.Entry entry = (Map.Entry ) entries.next(); 107 108 Object key = entry.getKey(); 109 Object currentValue = entry.getValue(); 110 Object rowValue = row.get(key); 111 112 if (!Util.nullSafeEquals(currentValue, rowValue)) { 113 if (diff == null) { 114 diff = new DataRow(this.size()); 115 } 116 diff.put(key, rowValue); 117 } 118 } 119 120 return diff; 121 } 122 123 127 public ObjectId createObjectId(ObjEntity entity) { 129 return createObjectId(entity.getName(), entity.getDbEntity()); 130 } 131 132 public ObjectId createObjectId(String entityName, DbEntity entity) { 134 return createObjectId(entityName, entity, null); 135 } 136 137 142 public ObjectId createTargetObjectId(String entityName, DbRelationship relationship) { 143 144 if (relationship.isToMany()) { 145 throw new CayenneRuntimeException("Only 'to one' can have a target ObjectId."); 146 } 147 148 Map target = relationship.targetPkSnapshotWithSrcSnapshot(this); 149 return (target != null) ? new ObjectId(entityName, target) : null; 150 } 151 152 163 public ObjectId createObjectId(String entityName, DbEntity entity, String namePrefix) { 165 166 boolean prefix = namePrefix != null && namePrefix.length() > 0; 167 168 171 List pk = entity.getPrimaryKey(); 172 if (pk.size() == 1) { 173 DbAttribute attribute = (DbAttribute) pk.get(0); 174 175 String key = (prefix) ? namePrefix + attribute.getName() : attribute 176 .getName(); 177 178 Object val = this.get(key); 179 if (val == null) { 180 throw new CayenneRuntimeException("Null value for '" 181 + key 182 + "'. Snapshot: " 183 + this 184 + ". Prefix: " 185 + namePrefix); 186 } 187 188 return new ObjectId(entityName, attribute.getName(), val); 190 } 191 192 194 Map idMap = new HashMap (pk.size() * 2); 195 Iterator it = pk.iterator(); 196 while (it.hasNext()) { 197 DbAttribute attribute = (DbAttribute) it.next(); 198 199 String key = (prefix) ? namePrefix + attribute.getName() : attribute 200 .getName(); 201 202 Object val = this.get(key); 203 if (val == null) { 204 throw new CayenneRuntimeException("Null value for '" 205 + key 206 + "'. Snapshot: " 207 + this 208 + ". Prefix: " 209 + namePrefix); 210 } 211 212 idMap.put(attribute.getName(), val); 214 } 215 216 return new ObjectId(entityName, idMap); 217 } 218 219 public String toString() { 220 return new ToStringBuilder(this).append("values", super.toString()).append( 221 " version", 222 version).append(" replaces", replacesVersion).toString(); 223 } 224 } 225 | Popular Tags |