1 19 20 21 package org.apache.cayenne.access.trans; 22 23 import java.util.Collections ; 24 import java.util.HashMap ; 25 import java.util.Iterator ; 26 import java.util.Map ; 27 28 import org.apache.cayenne.CayenneRuntimeException; 29 import org.apache.cayenne.ObjectId; 30 import org.apache.cayenne.Persistent; 31 import org.apache.cayenne.exp.Expression; 32 import org.apache.cayenne.map.DbAttribute; 33 import org.apache.cayenne.map.DbEntity; 34 import org.apache.cayenne.map.DbJoin; 35 import org.apache.cayenne.map.DbRelationship; 36 37 40 public class DataObjectMatchTranslator { 41 protected Map attributes; 42 protected Map values; 43 protected String operation; 44 protected Expression expression; 45 protected DbRelationship relationship; 46 47 public Expression getExpression() { 48 return expression; 49 } 50 51 public void setExpression(Expression expression) { 52 this.expression = expression; 53 } 54 55 public void reset() { 56 attributes = null; 57 values = null; 58 operation = null; 59 expression = null; 60 relationship = null; 61 } 62 63 67 public void setRelationship(DbRelationship rel) { 68 this.relationship = rel; 69 attributes = new HashMap (rel.getJoins().size() * 2); 70 71 if (rel.isToMany() || !rel.isToPK()) { 72 73 DbEntity ent = (DbEntity) rel.getTargetEntity(); 75 Iterator pk = ent.getPrimaryKey().iterator(); 76 77 while (pk.hasNext()) { 79 DbAttribute pkAttr = (DbAttribute) pk.next(); 80 attributes.put(pkAttr.getName(), pkAttr); 81 } 82 } else { 83 84 Iterator joins = rel.getJoins().iterator(); 86 while (joins.hasNext()) { 87 DbJoin join = (DbJoin) joins.next(); 88 89 attributes.put(join.getTargetName(), join.getSource()); 91 } 92 } 93 } 94 95 public void setDataObject(Persistent obj) { 96 if (obj == null) { 97 values = Collections.EMPTY_MAP; 98 return; 99 } 100 101 setObjectId(obj.getObjectId()); 102 } 103 104 107 public void setObjectId(ObjectId id) { 108 if (id == null) { 109 throw new CayenneRuntimeException( 110 "Null ObjectId, probably an attempt to use TRANSIENT object as a query parameter."); 111 } 112 else if (id.isTemporary()) { 113 throw new CayenneRuntimeException( 114 "Temporary id, probably an attempt to use NEW object as a query parameter."); 115 } 116 else { 117 values = id.getIdSnapshot(); 118 } 119 } 120 121 public Iterator keys() { 122 if (attributes == null) { 123 throw new IllegalStateException ( 124 "An attempt to use uninitialized DataObjectMatchTranslator: " 125 + "[attributes: null, values: " 126 + values 127 + "]"); 128 } 129 130 return attributes.keySet().iterator(); 131 } 132 133 public DbRelationship getRelationship() { 134 return relationship; 135 } 136 137 public DbAttribute getAttribute(String key) { 138 return (DbAttribute) attributes.get(key); 139 } 140 141 public Object getValue(String key) { 142 return values.get(key); 143 } 144 145 public void setOperation(String operation) { 146 this.operation = operation; 147 } 148 149 public String getOperation() { 150 return operation; 151 } 152 } 153 | Popular Tags |