/morphia/src/main/java/org/mongodb/morphia/dao/BasicDAO.java

https://github.com/jbyler/morphia · Java · 249 lines · 153 code · 43 blank · 53 comment · 1 complexity · e38c7f1d403eb7dab46c5d08b3180e69 MD5 · raw file

  1. package org.mongodb.morphia.dao;
  2. import com.mongodb.DBCollection;
  3. import com.mongodb.MongoClient;
  4. import com.mongodb.WriteConcern;
  5. import com.mongodb.WriteResult;
  6. import org.mongodb.morphia.Datastore;
  7. import org.mongodb.morphia.DatastoreImpl;
  8. import org.mongodb.morphia.Key;
  9. import org.mongodb.morphia.Morphia;
  10. import org.mongodb.morphia.query.Query;
  11. import org.mongodb.morphia.query.QueryResults;
  12. import org.mongodb.morphia.query.UpdateOperations;
  13. import org.mongodb.morphia.query.UpdateResults;
  14. import java.lang.reflect.ParameterizedType;
  15. import java.util.ArrayList;
  16. import java.util.Iterator;
  17. import java.util.List;
  18. /**
  19. * @author Olafur Gauti Gudmundsson
  20. * @author Scott Hernandez
  21. */
  22. public class BasicDAO<T, K> implements DAO<T, K> {
  23. //CHECKSTYLE:OFF
  24. /**
  25. * @deprecated please use the getter for this field
  26. */
  27. protected Class<T> entityClazz;
  28. /**
  29. * @deprecated please use the getter for this field
  30. */
  31. protected DatastoreImpl ds;
  32. //CHECKSTYLE:ON
  33. /**
  34. * Create a new BasicDAO
  35. *
  36. * @param entityClass the class of the POJO you want to persist using this DAO
  37. * @param mongoClient the representations of the connection to a MongoDB instance
  38. * @param morphia a Morphia instance
  39. * @param dbName the name of the database
  40. */
  41. public BasicDAO(final Class<T> entityClass, final MongoClient mongoClient, final Morphia morphia, final String dbName) {
  42. initDS(mongoClient, morphia, dbName);
  43. initType(entityClass);
  44. }
  45. /**
  46. * Create a new BasicDAO
  47. *
  48. * @param entityClass the class of the POJO you want to persist using this DAO
  49. * @param ds the Datastore which gives access to the MongoDB instance for this DAO
  50. */
  51. public BasicDAO(final Class<T> entityClass, final Datastore ds) {
  52. this.ds = (DatastoreImpl) ds;
  53. initType(entityClass);
  54. }
  55. /**
  56. * Only calls this from your derived class when you explicitly declare the generic types with concrete classes
  57. * <p/>
  58. * {@code class MyDao extends DAO<MyEntity, String>}
  59. *
  60. * @param mongoClient the representations of the connection to a MongoDB instance
  61. * @param morphia a Morphia instance
  62. * @param dbName the name of the database
  63. */
  64. @SuppressWarnings("unchecked")
  65. protected BasicDAO(final MongoClient mongoClient, final Morphia morphia, final String dbName) {
  66. initDS(mongoClient, morphia, dbName);
  67. initType(((Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0]));
  68. }
  69. @SuppressWarnings("unchecked")
  70. protected BasicDAO(final Datastore ds) {
  71. this.ds = (DatastoreImpl) ds;
  72. initType(((Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0]));
  73. }
  74. protected void initType(final Class<T> type) {
  75. entityClazz = type;
  76. ds.getMapper().addMappedClass(type);
  77. }
  78. protected void initDS(final MongoClient mongoClient, final Morphia mor, final String db) {
  79. ds = new DatastoreImpl(mor, mongoClient, db);
  80. }
  81. public DatastoreImpl getDs() {
  82. return ds;
  83. }
  84. public Class<T> getEntityClazz() {
  85. return entityClazz;
  86. }
  87. /**
  88. * Converts from a List<Key> to their id values
  89. */
  90. protected List<?> keysToIds(final List<Key<T>> keys) {
  91. final List<Object> ids = new ArrayList<Object>(keys.size() * 2);
  92. for (final Key<T> key : keys) {
  93. ids.add(key.getId());
  94. }
  95. return ids;
  96. }
  97. /**
  98. * The underlying collection for this DAO
  99. */
  100. public DBCollection getCollection() {
  101. return ds.getCollection(entityClazz);
  102. }
  103. public Query<T> createQuery() {
  104. return ds.createQuery(entityClazz);
  105. }
  106. public UpdateOperations<T> createUpdateOperations() {
  107. return ds.createUpdateOperations(entityClazz);
  108. }
  109. public Class<T> getEntityClass() {
  110. return entityClazz;
  111. }
  112. public Key<T> save(final T entity) {
  113. return ds.save(entity);
  114. }
  115. public Key<T> save(final T entity, final WriteConcern wc) {
  116. return ds.save(entity, wc);
  117. }
  118. public UpdateResults updateFirst(final Query<T> q, final UpdateOperations<T> ops) {
  119. return ds.updateFirst(q, ops);
  120. }
  121. public UpdateResults update(final Query<T> q, final UpdateOperations<T> ops) {
  122. return ds.update(q, ops);
  123. }
  124. public WriteResult delete(final T entity) {
  125. return ds.delete(entity);
  126. }
  127. public WriteResult delete(final T entity, final WriteConcern wc) {
  128. return ds.delete(entity, wc);
  129. }
  130. public WriteResult deleteById(final K id) {
  131. return ds.delete(entityClazz, id);
  132. }
  133. public WriteResult deleteByQuery(final Query<T> q) {
  134. return ds.delete(q);
  135. }
  136. public T get(final K id) {
  137. return ds.get(entityClazz, id);
  138. }
  139. @SuppressWarnings("unchecked")
  140. public List<K> findIds() {
  141. return (List<K>) keysToIds(ds.find(entityClazz).asKeyList());
  142. }
  143. @SuppressWarnings("unchecked")
  144. public List<K> findIds(final Query<T> q) {
  145. return (List<K>) keysToIds(q.asKeyList());
  146. }
  147. @SuppressWarnings("unchecked")
  148. public List<K> findIds(final String key, final Object value) {
  149. return (List<K>) keysToIds(ds.find(entityClazz, key, value).asKeyList());
  150. }
  151. public Key<T> findOneId() {
  152. return findOneId(ds.find(entityClazz));
  153. }
  154. public Key<T> findOneId(final String key, final Object value) {
  155. return findOneId(ds.find(entityClazz, key, value));
  156. }
  157. public Key<T> findOneId(final Query<T> query) {
  158. Iterator<Key<T>> keys = query.fetchKeys().iterator();
  159. return keys.hasNext() ? keys.next() : null;
  160. }
  161. public boolean exists(final String key, final Object value) {
  162. return exists(ds.find(entityClazz, key, value));
  163. }
  164. public boolean exists(final Query<T> q) {
  165. return ds.getCount(q) > 0;
  166. }
  167. public long count() {
  168. return ds.getCount(entityClazz);
  169. }
  170. public long count(final String key, final Object value) {
  171. return count(ds.find(entityClazz, key, value));
  172. }
  173. public long count(final Query<T> q) {
  174. return ds.getCount(q);
  175. }
  176. public T findOne(final String key, final Object value) {
  177. return ds.find(entityClazz, key, value).get();
  178. }
  179. /* (non-Javadoc)
  180. * @see org.mongodb.morphia.DAO#findOne(org.mongodb.morphia.query.Query)
  181. */
  182. public T findOne(final Query<T> q) {
  183. return q.get();
  184. }
  185. /* (non-Javadoc)
  186. * @see org.mongodb.morphia.DAO#find()
  187. */
  188. public QueryResults<T> find() {
  189. return createQuery();
  190. }
  191. /* (non-Javadoc)
  192. * @see org.mongodb.morphia.DAO#find(org.mongodb.morphia.query.Query)
  193. */
  194. public QueryResults<T> find(final Query<T> q) {
  195. return q;
  196. }
  197. /* (non-Javadoc)
  198. * @see org.mongodb.morphia.DAO#getDatastore()
  199. */
  200. public Datastore getDatastore() {
  201. return ds;
  202. }
  203. public void ensureIndexes() {
  204. ds.ensureIndexes(entityClazz);
  205. }
  206. }