PageRenderTime 60ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 1ms

/org.itbelts.domain/src/main/java/org/itbelts/dao/mongodb/AbstractMongoDbDAO.java

https://bitbucket.org/itbelts/itbelts
Java | 111 lines | 60 code | 16 blank | 35 comment | 7 complexity | 41168276314e0100f64da3ad4c9f3ce9 MD5 | raw file
  1. package org.itbelts.dao.mongodb;
  2. import java.net.UnknownHostException;
  3. import java.util.ArrayList;
  4. import java.util.List;
  5. import java.util.logging.Logger;
  6. import org.itbelts.exception.ITBeltsInternalException;
  7. import com.google.gson.Gson;
  8. import com.google.gson.GsonBuilder;
  9. import com.mongodb.BasicDBObject;
  10. import com.mongodb.DB;
  11. import com.mongodb.DBCursor;
  12. import com.mongodb.DBObject;
  13. import com.mongodb.Mongo;
  14. import com.mongodb.util.JSON;
  15. /**
  16. * Base class for anything that needs to access our local MongoDB. <br>
  17. * <br>
  18. * <u><i>Version History</i></u>
  19. *
  20. * <pre>
  21. * v2014.6.0 02-jun-2014 - DKBR813 - initial release
  22. *
  23. * </pre>
  24. *
  25. * @version v2014.6.0 02-jun-2014
  26. * @author <a href="mailto:koenbruyndonckx@gmail.com"> Koen Bruyndonckx </a>
  27. */
  28. public abstract class AbstractMongoDbDAO {
  29. private static final Logger LOGGER = Logger.getLogger( AbstractMongoDbDAO.class.getName() );
  30. private static DB itbelts;
  31. /**
  32. * Return a connection to the local itbelts MongoDB. You should make sure MONGOD is running and listening on port 8889
  33. *
  34. * @return
  35. */
  36. public static DB getClient() {
  37. if ( itbelts == null ) {
  38. try {
  39. Mongo theDB = new Mongo( "localhost", 8889 );
  40. itbelts = theDB.getDB( "itbelts" );
  41. } catch ( UnknownHostException e ) {
  42. throw new ITBeltsInternalException( "Problem connecting to the itbelts database at port 8889. You should make sure mongod is running.", e );
  43. }
  44. }
  45. return itbelts;
  46. }
  47. /**
  48. * Basic list retrieval.
  49. *
  50. * @param aCollection
  51. * The name of the collection in which to search
  52. * @param aType
  53. * The Type of the every element in the list
  54. * @param aQuery
  55. * optional query to limit the list
  56. * @return List<T> The list of the given type
  57. */
  58. protected <T> List<T> readFrom( String aCollection, Class<T> aType, DBObject aQuery, DBObject aFieldList ) {
  59. try {
  60. LOGGER.info( "Reading from " + aCollection );
  61. List<T> theList = new ArrayList<T>();
  62. if ( aQuery == null ) {
  63. aQuery = new BasicDBObject();
  64. }
  65. if ( aFieldList == null ) {
  66. aFieldList = new BasicDBObject();
  67. }
  68. LOGGER.info( "Using query " + aQuery + " and field list " + aFieldList );
  69. DBCursor cursor = getClient().getCollection( aCollection ).find( aQuery, aFieldList );
  70. try {
  71. Gson g = new Gson();
  72. while ( cursor.hasNext() ) {
  73. theList.add( g.fromJson( cursor.next().toString(), aType ) );
  74. }
  75. } finally {
  76. cursor.close();
  77. }
  78. return theList;
  79. } catch ( Exception e ) {
  80. throw new ITBeltsInternalException( "Problem reading from collection " + aCollection, e );
  81. }
  82. }
  83. /**
  84. * Store an object into the DB.
  85. *
  86. * @param aCollection
  87. * @param anObject
  88. */
  89. protected void sendTo( String aCollection, Object anObject ) {
  90. LOGGER.info( "Posting to " + aCollection + " --> object " + anObject );
  91. String json = new GsonBuilder().disableHtmlEscaping().create().toJson( anObject );
  92. LOGGER.info( json );
  93. getClient().getCollection( aCollection ).insert( (DBObject) JSON.parse( json ) );
  94. }
  95. }