PageRenderTime 1ms CodeModel.GetById 26ms app.highlight 3ms RepoModel.GetById 0ms app.codeStats 0ms

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