1 56 package org.objectstyle.cayenne; 57 58 import java.util.HashMap ; 59 import java.util.Iterator ; 60 import java.util.List ; 61 import java.util.Map ; 62 63 import org.apache.commons.lang.builder.ToStringBuilder; 64 import org.objectstyle.cayenne.conf.Configuration; 65 import org.objectstyle.cayenne.map.DbAttribute; 66 import org.objectstyle.cayenne.map.DbEntity; 67 import org.objectstyle.cayenne.map.DbRelationship; 68 import org.objectstyle.cayenne.map.ObjEntity; 69 import org.objectstyle.cayenne.util.Util; 70 71 79 80 public class DataRow extends HashMap { 83 84 87 private static volatile long currentVersion = DataObject.DEFAULT_VERSION + 1; 89 90 protected long version = currentVersion++; 91 protected long replacesVersion = DataObject.DEFAULT_VERSION; 92 93 public DataRow(Map map) { 94 super(map); 95 } 96 97 public DataRow(int initialCapacity) { 98 super(initialCapacity); 99 } 100 101 public long getVersion() { 102 return version; 103 } 104 105 public long getReplacesVersion() { 106 return replacesVersion; 107 } 108 109 112 public void setReplacesVersion(long replacesVersion) { 113 this.replacesVersion = replacesVersion; 114 } 115 116 120 public DataRow applyDiff(DataRow diff) { 121 DataRow merged = new DataRow(this); 122 123 Iterator it = diff.entrySet().iterator(); 124 while (it.hasNext()) { 125 Map.Entry entry = (Map.Entry ) it.next(); 126 merged.put(entry.getKey(), entry.getValue()); 127 } 128 129 return merged; 130 } 131 132 139 public DataRow createDiff(DataRow row) { 140 141 DataRow diff = null; 143 144 Iterator entries = entrySet().iterator(); 145 while (entries.hasNext()) { 146 Map.Entry entry = (Map.Entry ) entries.next(); 147 148 Object key = entry.getKey(); 149 Object currentValue = entry.getValue(); 150 Object rowValue = row.get(key); 151 152 if (!Util.nullSafeEquals(currentValue, rowValue)) { 153 if (diff == null) { 154 diff = new DataRow(this.size()); 155 } 156 diff.put(key, rowValue); 157 } 158 } 159 160 return diff; 161 } 162 163 168 public ObjectId createObjectId(ObjEntity entity) { 169 return createObjectId( 170 entity.getJavaClass(Configuration.getResourceLoader()), 171 entity.getDbEntity()); 172 } 173 174 public ObjectId createObjectId(Class objectClass, DbEntity entity) { 175 176 179 List pk = entity.getPrimaryKey(); 180 if (pk.size() == 1) { 181 DbAttribute attr = (DbAttribute) pk.get(0); 182 Object val = this.get(attr.getName()); 183 if (val == null) { 184 throw new CayenneRuntimeException( 185 "Null value for '" + attr.getName() + "'. Snapshot: " + this); 186 } 187 188 return new ObjectId(objectClass, attr.getName(), val); 189 } 190 191 193 Map idMap = new HashMap (pk.size() * 2); 194 Iterator it = pk.iterator(); 195 while (it.hasNext()) { 196 DbAttribute attr = (DbAttribute) it.next(); 197 Object val = this.get(attr.getName()); 198 if (val == null) { 199 throw new CayenneRuntimeException( 200 "Null value for '" + attr.getName() + "'. Snapshot: " + this); 201 } 202 203 idMap.put(attr.getName(), val); 204 } 205 206 return new ObjectId(objectClass, idMap); 207 } 208 209 214 public ObjectId createTargetObjectId( 215 Class targetClass, 216 DbRelationship relationship) { 217 218 if (relationship.isToMany()) { 219 throw new CayenneRuntimeException("Only 'to one' can have a target ObjectId."); 220 } 221 222 Map target = relationship.targetPkSnapshotWithSrcSnapshot(this); 223 return (target != null) ? new ObjectId(targetClass, target) : null; 224 } 225 226 public String toString() { 227 return new ToStringBuilder(this).append("version", version).append( 228 " replaces", 229 replacesVersion).append(" values", super.toString()).toString(); 230 } 231 } 232 | Popular Tags |