1 19 20 package org.apache.cayenne.query; 21 22 import org.apache.commons.lang.StringUtils; 23 import org.apache.cayenne.CayenneRuntimeException; 24 import org.apache.cayenne.ObjectId; 25 import org.apache.cayenne.exp.Expression; 26 import org.apache.cayenne.exp.ExpressionFactory; 27 import org.apache.cayenne.map.EntityResolver; 28 import org.apache.cayenne.map.ObjEntity; 29 import org.apache.cayenne.reflect.ClassDescriptor; 30 import org.apache.cayenne.util.Util; 31 32 42 public class ObjectIdQuery extends IndirectQuery { 43 44 public static final int CACHE = 1; 46 public static final int CACHE_REFRESH = 2; 47 public static final int CACHE_NOREFRESH = 3; 48 49 protected ObjectId objectId; 50 protected int cachePolicy; 51 protected boolean fetchingDataRows; 52 53 protected transient EntityResolver metadataResolver; 54 protected transient QueryMetadata metadata; 55 56 private ObjectIdQuery() { 58 this.cachePolicy = CACHE_REFRESH; 59 } 60 61 64 public ObjectIdQuery(ObjectId objectID) { 65 this(objectID, false, CACHE_REFRESH); 66 } 67 68 71 public ObjectIdQuery(ObjectId objectId, boolean fetchingDataRows, int cachePolicy) { 72 if (objectId == null) { 73 throw new NullPointerException ("Null objectID"); 74 } 75 76 this.objectId = objectId; 77 this.cachePolicy = cachePolicy; 78 this.fetchingDataRows = fetchingDataRows; 79 } 80 81 84 public QueryMetadata getMetaData(final EntityResolver resolver) { 87 if (metadata == null || metadataResolver != resolver) { 89 this.metadata = new DefaultQueryMetadata() { 90 91 public ClassDescriptor getClassDescriptor() { 92 return resolver.getClassDescriptor(objectId.getEntityName()); 93 } 94 95 public ObjEntity getObjEntity() { 96 return getClassDescriptor().getEntity(); 97 } 98 99 public boolean isFetchingDataRows() { 100 return fetchingDataRows; 101 } 102 }; 103 104 this.metadataResolver = resolver; 105 } 106 107 return metadata; 108 } 109 110 public ObjectId getObjectId() { 111 return objectId; 112 } 113 114 protected Query createReplacementQuery(EntityResolver resolver) { 115 if (objectId == null) { 116 throw new CayenneRuntimeException("Can't resolve query - objectId is null."); 117 } 118 119 if (objectId.isTemporary() && !objectId.isReplacementIdAttached()) { 120 throw new CayenneRuntimeException("Can't build a query for temporary id: " 121 + objectId); 122 } 123 124 SelectQuery query = new SelectQuery(objectId.getEntityName(), ExpressionFactory 125 .matchAllDbExp(objectId.getIdSnapshot(), Expression.EQUAL_TO)); 126 127 query.setRefreshingObjects(true); 129 query.setFetchingDataRows(fetchingDataRows); 130 return query; 131 } 132 133 public int getCachePolicy() { 134 return cachePolicy; 135 } 136 137 public boolean isFetchMandatory() { 138 return cachePolicy == CACHE_REFRESH; 139 } 140 141 public boolean isFetchAllowed() { 142 return cachePolicy != CACHE_NOREFRESH; 143 } 144 145 public boolean isFetchingDataRows() { 146 return fetchingDataRows; 147 } 148 149 152 public String toString() { 153 return StringUtils.substringAfterLast(getClass().getName(), ".") + ":" + objectId; 154 } 155 156 160 public boolean equals(Object object) { 161 if (this == object) { 162 return true; 163 } 164 165 if (!(object instanceof ObjectIdQuery)) { 166 return false; 167 } 168 169 ObjectIdQuery query = (ObjectIdQuery) object; 170 171 return Util.nullSafeEquals(objectId, query.getObjectId()); 172 } 173 174 177 public int hashCode() { 178 return (objectId != null) ? objectId.hashCode() : 11; 179 } 180 } 181 | Popular Tags |