1 16 17 package org.springframework.orm.ojb; 18 19 import java.sql.SQLException ; 20 import java.util.Collection ; 21 import java.util.Iterator ; 22 23 import org.apache.ojb.broker.Identity; 24 import org.apache.ojb.broker.PBKey; 25 import org.apache.ojb.broker.PersistenceBroker; 26 import org.apache.ojb.broker.PersistenceBrokerException; 27 import org.apache.ojb.broker.accesslayer.LookupException; 28 import org.apache.ojb.broker.query.Query; 29 30 import org.springframework.dao.DataAccessException; 31 import org.springframework.dao.DataAccessResourceFailureException; 32 import org.springframework.orm.ObjectRetrievalFailureException; 33 34 67 public class PersistenceBrokerTemplate extends OjbAccessor implements PersistenceBrokerOperations { 68 69 private boolean allowCreate = true; 70 71 72 77 public PersistenceBrokerTemplate() { 78 } 79 80 86 public PersistenceBrokerTemplate(boolean allowCreate) { 87 setAllowCreate(allowCreate); 88 afterPropertiesSet(); 89 } 90 91 95 public PersistenceBrokerTemplate(PBKey pbKey) { 96 setPbKey(pbKey); 97 afterPropertiesSet(); 98 } 99 100 106 public PersistenceBrokerTemplate(PBKey pbKey, boolean allowCreate) { 107 setPbKey(pbKey); 108 setAllowCreate(allowCreate); 109 afterPropertiesSet(); 110 } 111 112 122 public void setAllowCreate(boolean allowCreate) { 123 this.allowCreate = allowCreate; 124 } 125 126 129 public boolean isAllowCreate() { 130 return allowCreate; 131 } 132 133 134 public Object execute(PersistenceBrokerCallback action) throws DataAccessException { 135 PersistenceBroker pb = getPersistenceBroker(); 136 try { 137 return action.doInPersistenceBroker(pb); 138 } 139 catch (PersistenceBrokerException ex) { 140 throw convertOjbAccessException(ex); 141 } 142 catch (LookupException ex) { 143 throw new DataAccessResourceFailureException("Could not retrieve resource", ex); 144 } 145 catch (SQLException ex) { 146 throw convertJdbcAccessException(ex); 147 } 148 catch (RuntimeException ex) { 149 throw ex; 151 } 152 finally { 153 releasePersistenceBroker(pb); 154 } 155 } 156 157 public Collection executeFind(PersistenceBrokerCallback action) throws DataAccessException { 158 return (Collection ) execute(action); 159 } 160 161 162 public Object getObjectById(final Class entityClass, final Object idValue) throws DataAccessException { 163 return execute(new PersistenceBrokerCallback() { 164 public Object doInPersistenceBroker(PersistenceBroker pb) throws PersistenceBrokerException { 165 Identity id = pb.serviceIdentity().buildIdentity(entityClass, idValue); 166 Object result = pb.getObjectByIdentity(id); 167 if (result == null) { 168 throw new ObjectRetrievalFailureException(entityClass, idValue); 169 } 170 return result; 171 } 172 }); 173 } 174 175 public Object getObjectByQuery(final Query query) throws DataAccessException { 176 return execute(new PersistenceBrokerCallback() { 177 public Object doInPersistenceBroker(PersistenceBroker pb) throws PersistenceBrokerException { 178 return pb.getObjectByQuery(query); 179 } 180 }); 181 } 182 183 public Collection getCollectionByQuery(final Query query) throws DataAccessException { 184 return executeFind(new PersistenceBrokerCallback() { 185 public Object doInPersistenceBroker(PersistenceBroker pb) throws PersistenceBrokerException { 186 return pb.getCollectionByQuery(query); 187 } 188 }); 189 } 190 191 public Iterator getIteratorByQuery(final Query query) throws DataAccessException { 192 return (Iterator ) execute(new PersistenceBrokerCallback() { 193 public Object doInPersistenceBroker(PersistenceBroker pb) throws PersistenceBrokerException { 194 return pb.getIteratorByQuery(query); 195 } 196 }); 197 } 198 199 public Iterator getReportQueryIteratorByQuery(final Query query) { 200 return (Iterator ) execute(new PersistenceBrokerCallback() { 201 public Object doInPersistenceBroker(PersistenceBroker pb) throws PersistenceBrokerException { 202 return pb.getReportQueryIteratorByQuery(query); 203 } 204 }); 205 } 206 207 public int getCount(final Query query) throws DataAccessException { 208 Integer count = (Integer ) execute(new PersistenceBrokerCallback() { 209 public Object doInPersistenceBroker(PersistenceBroker pb) throws PersistenceBrokerException { 210 return new Integer (pb.getCount(query)); 211 } 212 }); 213 return count.intValue(); 214 } 215 216 public void removeFromCache(final Object entityOrId) throws DataAccessException { 217 execute(new PersistenceBrokerCallback() { 218 public Object doInPersistenceBroker(PersistenceBroker pb) throws PersistenceBrokerException { 219 pb.removeFromCache(entityOrId); 220 return null; 221 } 222 }); 223 } 224 225 public void clearCache() throws DataAccessException { 226 execute(new PersistenceBrokerCallback() { 227 public Object doInPersistenceBroker(PersistenceBroker pb) throws PersistenceBrokerException { 228 pb.clearCache(); 229 return null; 230 } 231 }); 232 } 233 234 public void store(final Object entity) throws DataAccessException { 235 execute(new PersistenceBrokerCallback() { 236 public Object doInPersistenceBroker(PersistenceBroker pb) throws PersistenceBrokerException { 237 pb.store(entity); 238 return null; 239 } 240 }); 241 } 242 243 public void delete(final Object entity) throws DataAccessException { 244 execute(new PersistenceBrokerCallback() { 245 public Object doInPersistenceBroker(PersistenceBroker pb) throws PersistenceBrokerException { 246 pb.delete(entity); 247 return null; 248 } 249 }); 250 } 251 252 public void deleteByQuery(final Query query) throws DataAccessException { 253 execute(new PersistenceBrokerCallback() { 254 public Object doInPersistenceBroker(PersistenceBroker pb) throws PersistenceBrokerException { 255 pb.deleteByQuery(query); 256 return null; 257 } 258 }); 259 } 260 261 262 274 protected PersistenceBroker getPersistenceBroker() 275 throws DataAccessResourceFailureException, IllegalStateException { 276 277 return OjbFactoryUtils.getPersistenceBroker(getPbKey(), isAllowCreate()); 278 } 279 280 290 protected void releasePersistenceBroker(PersistenceBroker pb) { 291 OjbFactoryUtils.releasePersistenceBroker(pb, getPbKey()); 292 } 293 294 295 } 296 | Popular Tags |