001    package com.khubla.pragmatach.plugin.mongodb;
002    
003    import java.util.ArrayList;
004    import java.util.List;
005    
006    import com.khubla.pragmatach.framework.api.PragmatachException;
007    import com.khubla.pragmatach.framework.dao.AbstractDAO;
008    import com.khubla.pragmatach.plugin.mongodb.db.DBCollectionFactory;
009    import com.khubla.pragmatach.plugin.mongodb.util.ClassTypeUtils;
010    import com.mongodb.BasicDBObject;
011    import com.mongodb.DBCollection;
012    import com.mongodb.DBCursor;
013    import com.mongodb.DBObject;
014    
015    /**
016     * @author tome
017     */
018    public class MongoDBDAO<T> extends AbstractDAO<T, String> {
019       /**
020        * DBCollection
021        */
022       private DBCollection dbCollection;
023       /**
024        * the type
025        */
026       private final Class<T> typeClazz;
027       /**
028        * type utils
029        */
030       private final ClassTypeUtils typeUtils;
031       /**
032        * _id
033        */
034       public static final String ID = "_id";
035       /**
036        * persister
037        */
038       private final MongoDBObjectPersister mongoDBObjectPersister;
039    
040       public MongoDBDAO(Class<T> typeClazz) {
041          this.typeClazz = typeClazz;
042          this.typeUtils = new ClassTypeUtils(this.typeClazz);
043          mongoDBObjectPersister = new MongoDBObjectPersister(typeClazz);
044          this.dbCollection = DBCollectionFactory.getInstance().getDBCollection(typeClazz);
045       }
046    
047       @Override
048       public long count() throws PragmatachException {
049          return this.dbCollection.count();
050       }
051    
052       /**
053        * delete
054        */
055       @Override
056       public void delete(T t) throws PragmatachException {
057          try {
058             final BasicDBObject basicDBObject = new BasicDBObject();
059             basicDBObject.append(ID, this.typeUtils.getId(t));
060             this.dbCollection.findAndRemove(basicDBObject);
061          } catch (final Exception e) {
062             throw new PragmatachException("Exception in delete", e);
063          }
064       }
065    
066       /**
067        * delete
068        */
069       @Override
070       public void deletebyId(String i) throws PragmatachException {
071          try {
072             final DBObject dbObject = getObjectById(i);
073             if (null != dbObject) {
074                this.dbCollection.remove(dbObject);
075             }
076          } catch (final Exception e) {
077             throw new PragmatachException("Exception in deletebyId", e);
078          }
079       }
080    
081       /**
082        * find
083        */
084       public List<T> find(BasicDBObject query) throws PragmatachException {
085          final DBCursor cursor = this.dbCollection.find(query);
086          if (cursor.hasNext()) {
087             final ArrayList<T> ret = new ArrayList<T>();
088             while (cursor.hasNext()) {
089                ret.add(this.newInstance(cursor.next()));
090             }
091             return ret;
092          } else {
093             return null;
094          }
095       }
096    
097       /**
098        * find
099        */
100       public List<T> find(String[][] terms) throws PragmatachException {
101          final BasicDBObject query = new BasicDBObject();
102          for (final String[] term : terms) {
103             if (term[0].compareTo(this.typeUtils.getIdFieldName()) == 0) {
104                query.append(ID, term[1]);
105             } else {
106                query.append(term[0], term[1]);
107             }
108          }
109          final DBCursor cursor = this.dbCollection.find(query);
110          if (cursor.hasNext()) {
111             final ArrayList<T> ret = new ArrayList<T>();
112             while (cursor.hasNext()) {
113                ret.add(this.newInstance(cursor.next()));
114             }
115             return ret;
116          } else {
117             return null;
118          }
119       }
120    
121       /**
122        * find by id
123        */
124       @Override
125       public T findById(String i) throws PragmatachException {
126          try {
127             final DBObject dbObject = getObjectById(i);
128             if (null != dbObject) {
129                final T t = newInstance(dbObject);
130                return t;
131             } else {
132                return null;
133             }
134          } catch (final Exception e) {
135             throw new PragmatachException("Exception in findById", e);
136          }
137       }
138    
139       /**
140        * find
141        */
142       public T findOne(BasicDBObject query) throws PragmatachException {
143          final DBCursor cursor = this.dbCollection.find(query);
144          if (cursor.hasNext()) {
145             return this.newInstance(cursor.next());
146          } else {
147             return null;
148          }
149       }
150    
151       /**
152        * find
153        */
154       public T findOne(String[][] terms) throws PragmatachException {
155          final BasicDBObject query = new BasicDBObject();
156          for (final String[] term : terms) {
157             if (term[0].compareTo(this.typeUtils.getIdFieldName()) == 0) {
158                query.append(ID, term[1]);
159             } else {
160                query.append(term[0], term[1]);
161             }
162          }
163          final DBCursor cursor = this.dbCollection.find(query);
164          if (cursor.hasNext()) {
165             return this.newInstance(cursor.next());
166          } else {
167             return null;
168          }
169       }
170    
171       /**
172        * findall
173        */
174       @Override
175       public List<T> getAll() throws PragmatachException {
176          try {
177             final DBCursor cursor = this.dbCollection.find();
178             final List<T> ret = new ArrayList<T>();
179             try {
180                while (cursor.hasNext()) {
181                   final DBObject dbObject = cursor.next();
182                   final T t = newInstance(dbObject);
183                   ret.add(t);
184                }
185             } finally {
186                cursor.close();
187             }
188             return ret;
189          } catch (final Exception e) {
190             throw new PragmatachException("Exception in getAll", e);
191          }
192       }
193    
194       @Override
195       public List<T> getAll(int start, int count) throws PragmatachException {
196          return null;
197       }
198    
199       @Override
200       public Class<String> getIdentifierClazz() {
201          return String.class;
202       }
203    
204       /**
205        * get object by id
206        */
207       private DBObject getObjectById(String i) throws PragmatachException {
208          try {
209             final BasicDBObject query = new BasicDBObject(ID, i);
210             final DBCursor cursor = this.dbCollection.find(query);
211             if (cursor.hasNext()) {
212                return cursor.next();
213             } else {
214                return null;
215             }
216          } catch (final Exception e) {
217             throw new PragmatachException("Exception in getObjectById", e);
218          }
219       }
220    
221       @Override
222       public Class<T> getTypeClazz() {
223          return typeClazz;
224       }
225    
226       /**
227        * get an instance
228        */
229       @SuppressWarnings("unchecked")
230       public T newInstance(DBObject dbObject) throws PragmatachException {
231          try {
232             return (T) this.mongoDBObjectPersister.load(dbObject);
233          } catch (final Exception e) {
234             throw new PragmatachException("Exception in newInstance", e);
235          }
236       }
237    
238       @Override
239       public void reloadConfig() {
240          this.dbCollection = DBCollectionFactory.getInstance().getDBCollection(typeClazz);
241       }
242    
243       /**
244        * save object
245        */
246       @Override
247       public void save(T t) throws PragmatachException {
248          try {
249             /*
250              * save
251              */
252             this.mongoDBObjectPersister.save(t);
253          } catch (final Exception e) {
254             throw new PragmatachException("Exception in save", e);
255          }
256       }
257    
258       /**
259        * update object
260        */
261       @Override
262       public void update(T t) throws PragmatachException {
263          try {
264             this.save(t);
265          } catch (final Exception e) {
266             throw new PragmatachException("Exception in update", e);
267          }
268       }
269    }