001: /*
002: * Created on 10-Jan-2006
003: */
004: package uk.org.ponder.rsf.hibernate3;
005:
006: import java.io.Serializable;
007: import java.util.ArrayList;
008: import java.util.HashMap;
009: import java.util.Iterator;
010: import java.util.List;
011: import java.util.Map;
012:
013: import uk.org.ponder.beanutil.BeanUtil;
014: import uk.org.ponder.beanutil.FallbackBeanLocator;
015: import uk.org.ponder.beanutil.entity.EntityID;
016: import uk.org.ponder.beanutil.entity.EntityIDRewriter;
017: import uk.org.ponder.beanutil.entity.NewEntityEntry;
018: import uk.org.ponder.beanutil.entity.NewEntityReceiver;
019: import uk.org.ponder.rsf.state.entity.EntityIDAssignmentListener;
020: import uk.org.ponder.util.UniversalRuntimeException;
021:
022: /**
023: * The central request-scope manager for Hibernate-mapped entities. Maintains a
024: * pool of HibernateEntityBeanLocators, one for each entity class, and grafts
025: * their paths directly onto its parent in the request-scope container, by
026: * virtue of being recognised as a FallbackBeanLocator.
027: *
028: * @author Antranig Basman (amb26@ponder.org.uk)
029: */
030:
031: public class HibernateEntityBeanManager implements FallbackBeanLocator,
032: EntityIDRewriter, NewEntityReceiver {
033: private Map locators = new HashMap();
034:
035: private List newentitylisteners = new ArrayList(1);
036: // A list of NewEntityEntries, for each new entity created this request.
037: private List newentities = new ArrayList();
038: private StaticHibernateEBM shebm;
039: private HibernateAlterationWrapper haw;
040:
041: public void setHibernateAlterationWrapper(
042: HibernateAlterationWrapper haw) {
043: this .haw = haw;
044: }
045:
046: public void setStaticHibernateEBM(StaticHibernateEBM shebm) {
047: this .shebm = shebm;
048: }
049:
050: public void init() {
051: shebm.populateRequestMap(locators);
052: for (Iterator heblit = locators.values().iterator(); heblit
053: .hasNext();) {
054: HibernateEntityBeanLocator hebl = (HibernateEntityBeanLocator) heblit
055: .next();
056: hebl.setHibernateAlterationWrapper(haw);
057: hebl.setNewEntityReceiver(this );
058: }
059: haw.registerPreCommitAction(new NewEntitySaver());
060: }
061:
062: // no need for a remove method - remember all these beans are disposable!
063: public void addEntityIDListener(
064: EntityIDAssignmentListener eialistener) {
065: newentitylisteners.add(eialistener);
066: }
067:
068: public void receiveNewEntity(NewEntityEntry entry) {
069: newentities.add(entry);
070: }
071:
072: private class NewEntitySaver implements Runnable {
073: public void run() {
074: for (int i = 0; i < newentities.size(); ++i) {
075: NewEntityEntry entry = (NewEntityEntry) newentities
076: .get(i);
077: String entityname = shebm.getEntityName(entry.newent
078: .getClass());
079: haw.getSession().save(entityname, entry.newent);
080: Serializable newid = ((HibernateEntityBeanLocator) entry.holder)
081: .getID(entry.newent);
082: for (int j = 0; j < newentitylisteners.size(); ++j) {
083: EntityIDAssignmentListener eial = (EntityIDAssignmentListener) newentitylisteners
084: .get(j);
085: eial.IDAssigned(entry.newent, entry.tempid, newid);
086: }
087: }
088: }
089: }
090:
091: // For the BeanLocator interface.
092: public Object locateBean(String path) {
093: return locators.get(path);
094: }
095:
096: // For the EntityIDRewriter interface.
097: public void postCommit(EntityID toadjust) {
098: String entityname = toadjust.entityname;
099: HibernateEntityBeanLocator hebl = (HibernateEntityBeanLocator) locators
100: .get(entityname);
101: if (toadjust.ID.startsWith(BeanUtil.NEW_ENTITY_PREFIX)) {
102: Object committed = hebl.getDeliveredBeans()
103: .get(toadjust.ID);
104: if (committed == null) {
105: throw UniversalRuntimeException.accumulate(
106: new IllegalArgumentException(),
107: "Entity of type " + toadjust.entityname
108: + " with ID " + toadjust.ID
109: + " was not created on this cycle");
110: }
111: toadjust.ID = hebl.getIDString(committed);
112: }
113: }
114:
115: }
|