01: /*
02: * Created on 1 Sep 2007
03: */
04: package uk.org.ponder.rsf.state.entity.support;
05:
06: import java.util.HashMap;
07: import java.util.Iterator;
08: import java.util.Map;
09:
10: import uk.org.ponder.beanutil.BeanLocator;
11: import uk.org.ponder.beanutil.PathUtil;
12: import uk.org.ponder.beanutil.entity.EntityBeanLocator;
13:
14: /** Request-scope implementation of the EntityBeanLocator, cooperating with the
15: * @link StaticEntityBeanLocatorImpl application-scope registrar.
16: *
17: * @author Antranig Basman (antranig@caret.cam.ac.uk)
18: *
19: */
20:
21: public class EntityBeanLocatorImpl implements EntityBeanLocator {
22:
23: private StaticEntityBeanLocatorImpl sebl;
24:
25: public EntityBeanLocatorImpl(StaticEntityBeanLocatorImpl sebl) {
26: this .sebl = sebl;
27: this .beanlocator = sebl.RSACbeanlocator.getBeanLocator();
28: }
29:
30: private Map delivered = new HashMap();
31:
32: private BeanLocator beanlocator;
33:
34: public Object locateBean(String name) {
35: Object togo = delivered.get(name);
36: if (togo == null) {
37: if (name.startsWith(NEW_PREFIX)) {
38: if (sebl.newEL != null) {
39: togo = sebl.bma.invokeBeanMethod(sebl.newEL,
40: beanlocator);
41: } else {
42: togo = sebl.reflectivecache
43: .construct(sebl.entityClazz);
44: }
45: } else {
46: togo = sebl.bma.invokeBeanMethod(PathUtil.composePath(
47: sebl.fetchEL, '\'' + name + '\''), beanlocator);
48: }
49: delivered.put(name, togo);
50: }
51: return togo;
52: }
53:
54: public void saveAll() {
55: if (sebl.saveEL != null) {
56: String penultimate = PathUtil.getToTailPath(sebl.saveEL);
57: String savemethod = PathUtil.getTailPath(sebl.saveEL);
58: Object saver = sebl.bma.getBeanValue(penultimate,
59: beanlocator, null);
60:
61: for (Iterator it = delivered.entrySet().iterator(); it
62: .hasNext();) {
63: String key = (String) it.next();
64: Object value = delivered.get(key);
65: sebl.reflectivecache.invokeMethod(saver, savemethod,
66: new Object[] { value });
67: }
68: }
69: }
70:
71: public boolean remove(String beanname) {
72: if (sebl.removeEL == null) {
73: throw new UnsupportedOperationException(
74: "Cannot implement removal from EntityBeanLocator without removeEL being set");
75: }
76: // TODO: support the case where the fetch method signature accepts an object
77: // rather than its ID
78: Object togo = sebl.bma.invokeBeanMethod(PathUtil.composePath(
79: sebl.removeEL, '\'' + beanname + '\''), beanlocator);
80: if (togo == null)
81: return false;
82: if (togo instanceof Boolean) {
83: return ((Boolean) togo).booleanValue();
84: }
85: return true;
86: }
87:
88: public void set(String beanname, Object toset) {
89: // Only places the object in the cache, not a particularly useful implementation,
90: // unless the save method has relevant semantics
91: delivered.put(beanname, toset);
92: }
93:
94: public Map getDeliveredBeans() {
95: return delivered;
96: }
97: }
|