Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package com.ga.model.abstractclasses; import com.ga.model.interfaces.Model; import com.mongodb.BasicDBObject; import com.mongodb.DB; import com.mongodb.DBCollection; import com.mongodb.DBCursor; import com.mongodb.DBObject; import com.mongodb.MongoClient; import com.mongodb.MongoClientURI; import com.mongodb.MongoException; import java.net.UnknownHostException; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Set; import java.util.logging.Level; import java.util.logging.Logger; import org.bson.types.ObjectId; /** * * @author gurpreet */ public abstract class ModelAbstract implements Model { protected static MongoClient conn = null; protected static DB db = null; protected static DBCollection collection = null; protected ObjectId id = null; private Exception ex; private DBCursor recordCursor; private BasicDBObject whereQuery; private BasicDBObject searchQuery; @Override public abstract void delete(); @Override public void update(DBObject doc, HashMap args) throws MongoException { if (args.isEmpty()) { try { Exception updateEx; updateEx = new Exception("Update Failure: Value of args cannot be empty."); throw updateEx; } catch (Exception exUpdate) { Logger.getLogger(ModelAbstract.class.getName()).log(Level.SEVERE, null, exUpdate); } } else { searchQuery = new BasicDBObject(); Set argsSet = args.entrySet(); Iterator argsIterator = argsSet.iterator(); while (argsIterator.hasNext()) { Map.Entry argsElement = (Map.Entry) argsIterator.next(); searchQuery.append(argsElement.getKey().toString(), argsElement.getValue().toString()); } ModelAbstract.collection.update(searchQuery, doc); } } @Override public ArrayList retrieve(HashMap args) { ArrayList record = new ArrayList(); if (args.isEmpty()) { recordCursor = ModelAbstract.collection.find(); while (recordCursor.hasNext()) { record.add(recordCursor.next()); } } else { whereQuery = new BasicDBObject(); Set argsSet = args.entrySet(); Iterator argsIterator = argsSet.iterator(); while (argsIterator.hasNext()) { Map.Entry argsElement = (Map.Entry) argsIterator.next(); if (argsElement.getValue() instanceof HashMap) { HashMap subArgs = new HashMap(); subArgs = (HashMap) argsElement.getValue(); Set subArgsSet = subArgs.entrySet(); Iterator subArgsIterator = subArgsSet.iterator(); Map.Entry subArgsElement = (Map.Entry) subArgsIterator.next(); whereQuery.put(argsElement.getKey().toString(), new BasicDBObject(subArgsElement.getKey().toString(), subArgsElement.getValue()) .append("$options", "im")); } else { whereQuery.put(argsElement.getKey().toString(), argsElement.getValue().toString()); } } recordCursor = ModelAbstract.collection.find(whereQuery).sort(new BasicDBObject("date", 1)); while (recordCursor.hasNext()) { record.add(recordCursor.next()); } } return record; } @Override public void insert(DBObject doc) { ModelAbstract.collection.insert(doc); } @Override public abstract void create(); @Override public void init(String dbName, String collectionName) { try { // ModelAbstract.conn = new MongoClient(new MongoClientURI("mongodb://116.193.163.66:27017/")); ModelAbstract.conn = new MongoClient(new MongoClientURI("mongodb://localhost:27017/")); if (ModelAbstract.conn != null) { ModelAbstract.db = ModelAbstract.conn.getDB(dbName); if (ModelAbstract.db != null) { ModelAbstract.collection = ModelAbstract.db.getCollection(collectionName); if (ModelAbstract.collection == null) { Exception exCollection = new Exception( "Collection \'" + collectionName + "\' does not exist in \'" + dbName + "\'."); throw exCollection; } } else { Exception exDatabase = new Exception("Database \'" + dbName + "\' not found."); throw exDatabase; } } else { ex = new MongoException("Failed to connect with database server."); throw ex; } } catch (UnknownHostException e) { Logger.getLogger(ModelAbstract.class.getName()).log(Level.SEVERE, null, e); } catch (Exception exCollection) { Logger.getLogger(ModelAbstract.class.getName()).log(Level.SEVERE, null, exCollection); } } public static MongoClient getConn() { return conn; } public static DB getDb() { return db; } public static DBCollection getCollection() { return collection; } public ObjectId getId() { return id; } }