001: /*
002: * Created on 21 Aug 2007
003: */
004: package uk.org.ponder.rsf.state.entity.support;
005:
006: import org.springframework.beans.factory.BeanNameAware;
007: import org.springframework.beans.factory.FactoryBean;
008:
009: import uk.org.ponder.beanutil.BeanModelAlterer;
010: import uk.org.ponder.beanutil.WriteableBeanLocator;
011: import uk.org.ponder.beanutil.entity.EntityBeanLocator;
012: import uk.org.ponder.reflect.ReflectiveCache;
013: import uk.org.ponder.rsac.RSACBeanLocator;
014: import uk.org.ponder.rsf.state.entity.EntityNameInferrer;
015:
016: /**
017: * An "automated" implementation of BeanLocator, useful for managing "entities",
018: * addressed by a unique ID convertible to String, which are already provided with
019: * some form of (probably application-scope) "DAO"-type API. This implementation
020: * allows the DAO API to be expressed by means of EL method bindings to its
021: * various methods, from which the managed entities are mapped into this
022: * this request-scope cache.
023: * </p>
024: * Typically the user will provide a <code>fetchMethod</code> which will fetch
025: * entities by ID, and (at least one of) a <code>newMethod</code> or an
026: * <code>entityClass</code> which is used to instantiate new entities which
027: * are not persistent. <p/> At any time, the "cache" of currently addressed
028: * entities within this request may be accessed through the map
029: * <code>deliveredBeans</code>
030: * <p/> If the <code>saveEL</code> is also
031: * supplied, this locator will also implement a <code>saveAll()</code> method
032: * which will return all the delivered entities back to persistence. If this is
033: * not supplied, they are assumed to be persisted by some other means (perhaps
034: * an auto-commit of some kind).
035: *
036: * @author Antranig Basman (antranig@caret.cam.ac.uk)
037: *
038: */
039:
040: public class StaticEntityBeanLocatorImpl implements BeanNameAware,
041: FactoryBean, EntityNameInferrer {
042: // package level members for access from EntityBeanLocatorImpl
043: String fetchEL;
044: String newEL;
045: Class entityClazz;
046: String saveEL;
047: String removeEL;
048:
049: BeanModelAlterer bma;
050: RSACBeanLocator RSACbeanlocator;
051: ReflectiveCache reflectivecache;
052: private String beanName;
053:
054: public void setFetchMethod(String fetchEL) {
055: this .fetchEL = fetchEL;
056: }
057:
058: public void setNewMethod(String newEL) {
059: this .newEL = newEL;
060: }
061:
062: public void setEntityClass(Class entityClazz) {
063: this .entityClazz = entityClazz;
064: }
065:
066: public void setSaveMethod(String saveEL) {
067: this .saveEL = saveEL;
068: }
069:
070: public void setRemoveMethod(String removeEL) {
071: this .removeEL = removeEL;
072: }
073:
074: public void setBeanModelAlterer(BeanModelAlterer bma) {
075: this .bma = bma;
076: }
077:
078: public void setRSACBeanLocator(RSACBeanLocator RSACbeanlocator) {
079: this .RSACbeanlocator = RSACbeanlocator;
080: }
081:
082: public void setReflectiveCache(ReflectiveCache reflectivecache) {
083: this .reflectivecache = reflectivecache;
084: }
085:
086: public void init() {
087: if (entityClazz == null && newEL == null) {
088: throw new IllegalArgumentException(
089: "At least one of entityClass and newEL must be set");
090: }
091: }
092:
093: public Object getObject() throws Exception {
094: WriteableBeanLocator wbl = RSACbeanlocator.getDeadBeanLocator();
095: if (wbl.locateBean(beanName) != null) {
096: throw new IllegalStateException(
097: "Incorrect use of EntityBeanLocator detected. "
098: + "This implementation is designed to work only with the RSACBeanLocator, "
099: + "and must cause the delivered BeanLocator to be immediately cached "
100: + "in the RSAC on first use per request");
101: }
102: EntityBeanLocatorImpl togo = new EntityBeanLocatorImpl(this );
103: wbl.set(beanName, togo);
104: return togo;
105: }
106:
107: public Class getObjectType() {
108: return EntityBeanLocator.class;
109: }
110:
111: public boolean isSingleton() {
112: return false;
113: }
114:
115: public void setBeanName(String beanName) {
116: this .beanName = beanName;
117: }
118:
119: public String getEntityName(Class entityClazz) {
120: return (entityClazz == this.entityClazz ? beanName : null);
121: }
122: }
|