001    package com.khubla.pragmatach.plugin.ebean;
002    
003    import java.io.Serializable;
004    import java.util.List;
005    import java.util.Set;
006    
007    import javax.persistence.Entity;
008    
009    import com.avaje.ebean.EbeanServer;
010    import com.avaje.ebean.EbeanServerFactory;
011    import com.avaje.ebean.Query;
012    import com.avaje.ebean.config.DataSourceConfig;
013    import com.avaje.ebean.config.ServerConfig;
014    import com.khubla.pragmatach.framework.api.PragmatachException;
015    import com.khubla.pragmatach.framework.application.Application;
016    import com.khubla.pragmatach.framework.dao.AbstractDAO;
017    import com.khubla.pragmatach.framework.scanner.AnnotationScanner;
018    
019    /**
020     * @author tome
021     */
022    public class EBeanDAO<T, I extends Serializable> extends AbstractDAO<T, I> {
023       /**
024        * create the EBean server
025        */
026       private static EbeanServer getEBeanServer() {
027          try {
028             /*
029              * make the config
030              */
031             final ServerConfig serverConfig = new ServerConfig();
032             serverConfig.setDefaultServer(true);
033             serverConfig.setName("pragmatach");
034             /*
035              * DDL options
036              */
037             final String autoCreate = Application.getConfiguration().getParameter("ebean.autocreate");
038             if (null != autoCreate) {
039                if (true == Boolean.parseBoolean(autoCreate)) {
040                   serverConfig.setDdlGenerate(true);
041                   serverConfig.setDdlRun(true);
042                }
043             }
044             final String dataSource = Application.getConfiguration().getParameter("ebean.datasource");
045             if ((null != dataSource) && (dataSource.length() > 0)) {
046                serverConfig.setDataSourceJndiName(dataSource);
047             } else {
048                /*
049                 * ebean datasource
050                 */
051                final DataSourceConfig dataSourceConfig = new DataSourceConfig();
052                dataSourceConfig.setDriver(Application.getConfiguration().getParameter("ebean.driver"));
053                dataSourceConfig.setUsername(Application.getConfiguration().getParameter("ebean.username"));
054                dataSourceConfig.setPassword(Application.getConfiguration().getParameter("ebean.password"));
055                final String url = Application.getConfiguration().getParameter("ebean.url");
056                if (null != url) {
057                   dataSourceConfig.setUrl(url);
058                } else {
059                   throw new Exception("ebean.url must be specified");
060                }
061                serverConfig.setDataSourceConfig(dataSourceConfig);
062             }
063             /*
064              * add classes
065              */
066             final Set<Class<?>> entityClasses = getEntityClasses();
067             if (null != entityClasses) {
068                for (final Class<?> clazz : entityClasses) {
069                   serverConfig.addClass(clazz);
070                }
071             }
072             /*
073              * the server
074              */
075             return EbeanServerFactory.create(serverConfig);
076          } catch (final Exception e) {
077             throw new ExceptionInInitializerError(e);
078          }
079       }
080    
081       /**
082        * the annotation scanner will have run; we can just query for annotated classes
083        */
084       protected static Set<Class<?>> getEntityClasses() throws PragmatachException {
085          try {
086             return AnnotationScanner.getAllClasses(Entity.class);
087          } catch (final Exception e) {
088             throw new PragmatachException("Exception in getAnnotatedClasses", e);
089          }
090       }
091    
092       /**
093        * EBean
094        */
095       private static EbeanServer ebeanServer = getEBeanServer();
096       /**
097        * the type
098        */
099       private final Class<T> typeClazz;
100       /**
101        * the identifier
102        */
103       private final Class<I> identifierClazz;
104    
105       public EBeanDAO(Class<T> typeClazz, Class<I> identifierClazz) {
106          this.typeClazz = typeClazz;
107          this.identifierClazz = identifierClazz;
108       }
109    
110       @Override
111       public long count() throws PragmatachException {
112          return this.find().findRowCount();
113       }
114    
115       /**
116        * delete
117        */
118       public void delete(T t) throws PragmatachException {
119          try {
120             ebeanServer.delete(t);
121          } catch (final Exception e) {
122             throw new PragmatachException("Exception in delete", e);
123          }
124       }
125    
126       /**
127        * delete
128        */
129       public void deletebyId(I i) throws PragmatachException {
130          try {
131             ebeanServer.delete(typeClazz, i);
132          } catch (final Exception e) {
133             throw new PragmatachException("Exception in deletebyId", e);
134          }
135       }
136    
137       /**
138        * find by fluent query
139        */
140       public Query<T> find() throws PragmatachException {
141          return ebeanServer.find(this.typeClazz);
142       }
143    
144       /**
145        * find by id
146        */
147       public T findById(I i) throws PragmatachException {
148          try {
149             return ebeanServer.find(typeClazz, i);
150          } catch (final Exception e) {
151             throw new PragmatachException("Exception in findById", e);
152          }
153       }
154    
155       /**
156        * findall
157        */
158       public List<T> getAll() throws PragmatachException {
159          return ebeanServer.find(this.typeClazz).findList();
160       }
161    
162       @Override
163       public List<T> getAll(int start, int count) throws PragmatachException {
164          return this.find().setFirstRow(start).setMaxRows(count).findList();
165       }
166    
167       public Class<I> getIdentifierClazz() {
168          return identifierClazz;
169       }
170    
171       public Class<T> getTypeClazz() {
172          return typeClazz;
173       }
174    
175       public void reloadConfig() {
176          ebeanServer = getEBeanServer();
177       }
178    
179       /**
180        * save object
181        */
182       public void save(T t) throws PragmatachException {
183          try {
184             ebeanServer.save(t);
185          } catch (final Exception e) {
186             throw new PragmatachException("Exception in save", e);
187          }
188       }
189    
190       /**
191        * update object
192        */
193       public void update(T t) throws PragmatachException {
194          try {
195             ebeanServer.update(t);
196          } catch (final Exception e) {
197             throw new PragmatachException("Exception in save", e);
198          }
199       }
200    }