1 20 21 package com.methodhead.persistable; 22 23 import java.sql.SQLException ; 24 25 import java.util.Iterator ; 26 import java.util.List ; 27 import java.util.Map ; 28 import org.apache.commons.beanutils.DynaClass; 29 30 34 public class KeyedPersistable 35 extends 36 Persistable { 37 38 40 public KeyedPersistable( 41 DynaClass dynaClass ) { 42 43 super( dynaClass ); 44 } 45 46 48 50 52 56 public void saveNew( 57 Key key ) { 58 59 key_ = key; 60 key_.setProperties( this ); 61 super.saveNew(); 62 } 63 64 69 public void save() { 70 71 if ( key_ == null ) 72 throw new PersistableException( 73 "Key has not been set; call load() or saveNew() first." ); 74 75 super.save( key_.getWhereClause() ); 76 } 77 78 82 public void load( 83 Key key ) 84 throws 85 PersistableException { 86 87 key_ = key; 88 super.load( key_.getWhereClause() ); 89 } 90 91 95 public void load( 96 String whereClause ) { 97 throw new PersistableException( 98 "This version of load() is illegal for KeyedPersistable; use " + 99 "the version that accepts a KeyFactory as an argument." ); 100 } 101 102 108 public void load( 109 String whereClause, 110 KeyFactory keyFactory ) { 111 112 super.load( whereClause ); 113 114 key_ = keyFactory.newInstance(); 115 key_.setKeyValue( this ); 116 } 117 118 124 public static List loadAll( 125 DynaClass dynaClass, 126 String whereClause, 127 String orderByClause ) { 128 throw new PersistableException( 129 "This version of loadAll() is illegal for KeyedPersistable; use " + 130 "the version that accepts a KeyFactory as an argument." ); 131 } 132 133 138 public static List loadAll( 139 DynaClass dynaClass, 140 String whereClause, 141 String orderByClause, 142 KeyFactory keyFactory ) 143 throws 144 PersistableException { 145 146 List l = Persistable.loadAll( dynaClass, whereClause, orderByClause ); 147 148 Iterator iter = l.iterator(); 152 while ( iter.hasNext() ) { 153 KeyedPersistable p = ( KeyedPersistable )iter.next(); 154 Key k = keyFactory.newInstance(); 155 k.setKeyValue( p ); 156 p.setKey( k ); 157 } 158 159 return l; 160 } 161 162 166 public void delete() { 167 168 if ( key_ == null ) 169 throw new PersistableException( 170 "Key has not been set; call load() or saveNew() first." ); 171 172 super.deleteAll( getDynaClass(), key_.getWhereClause() ); 173 } 174 175 177 public void setKey( Key key ) { 178 key_ = key; 179 } 180 181 public Key getKey() { 182 return key_; 183 } 184 185 187 protected Key key_ = null; 188 } 189 | Popular Tags |