001    package net.databinder.ao;
002    
003    import java.sql.SQLException;
004    import java.util.HashMap;
005    import java.util.Map;
006    
007    import net.databinder.DataApplicationBase;
008    import net.java.ao.DatabaseProvider;
009    import net.java.ao.EntityManager;
010    
011    import org.slf4j.Logger;
012    import org.slf4j.LoggerFactory;
013    
014    /** Optional application base for ActiveObjects. */
015    public abstract class DataApplication extends DataApplicationBase implements ActiveObjectsApplication {
016            
017            private Logger logger = LoggerFactory.getLogger(DataApplication.class);
018    
019            /* Entity manager available to this application, default has key of null. */
020            private Map<Object, EntityManager> entityManagers = new HashMap<Object, EntityManager>();
021    
022            /** 
023             * Initializes a default ActiveObjects entity manager. This is called 
024             * automatically during start-up. Applications with one entity manager
025             * will not normally need to override.
026             */
027            protected void dataInit() {
028                    initEntityManager(null);
029            }
030            
031            /**
032             * Initializes entity manager and generates schema if in development mode.
033             */
034            protected void initEntityManager(Object key) {
035                    EntityManager entityManager = buildEntityManager(key, buildDatabaseProvider(key));
036                    setEntityManager(key, entityManager);
037                    if (isDevelopment()) try {
038                            generateSchema(entityManager, key);
039                    } catch (SQLException e) {
040                            logger.error("Error generating schema", e);
041                    }
042            }
043    
044            /**
045             * @param provider provider returned by buildDatabaseProvider(key)
046             * @return instantiated EntityManager in default implementation, override for subclass
047             */
048            protected EntityManager buildEntityManager(Object key, DatabaseProvider provider) {
049                    return new EntityManager(provider);
050            }
051    
052            /**
053             * @return database provider for key, ignore key if app needs only one provider
054             */
055            protected abstract DatabaseProvider buildDatabaseProvider(Object key);  
056    
057            /** Generate schema if desired, called only in development mode. */
058            protected void generateSchema(EntityManager entityManager, Object key) throws SQLException { }
059            
060            /** Sets entity manager in map. */
061            protected void setEntityManager(Object key, EntityManager entityManager) {
062                    entityManagers.put(key, entityManager);
063            }
064            
065            /** @return entity manager for given key */
066            public EntityManager getEntityManager(Object key) {
067                    return entityManagers.get(key);
068            }
069    }