Subversion Repositories SmartDukaan

Rev

Rev 22105 | Rev 22155 | Go to most recent revision | View as "text/plain" | Blame | Compare with Previous | Last modification | View Log | RSS feed

package com.spice.profitmandi.dao.repository.dtr;

import java.lang.reflect.Type;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.json.JSONObject;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
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.util.JSON;
import com.spice.profitmandi.dao.model.FofoForm;


public class Mongo {

        private static MongoClient mongo_local;
        private static MongoClient mongo_dtr;
        private static final String CONTENT ="CONTENT";
        private static final String SITE_CONTENT = "siteContent";
        private static final String CATALOG_DB = "Catalog";
        private static final String MASTER_DATA = "MasterData";
        private static final String FOFO_DB = "Fofo";
        private static final String FOFO_FORM_COLLECTION = "RegistrationForm";
        private static final int MONGO_PORT = 27017;

        static {
                synchronized(Mongo.class){
                        try {
                                mongo_local = new MongoClient( "localhost" , MONGO_PORT );
                                mongo_dtr = new MongoClient("localhost", MONGO_PORT);
                        } catch (UnknownHostException e) {
                                e.printStackTrace();
                        }
                }
        }

        public static JSONObject getEntityById(long id) throws Exception{
                DB db = mongo_local.getDB(CONTENT);
                DBCollection collection = db.getCollection(SITE_CONTENT);
                BasicDBObject obj = new BasicDBObject();
                obj.append("_id", id);
                DBObject result = collection.findOne(obj);
                if (result==null){
                        throw new Exception();
                }
                return new JSONObject(JSON.serialize(result));
        }
        
        public static JSONObject getItemsByBundleId(long bundleId) throws Exception{
                DB db = mongo_dtr.getDB(CATALOG_DB);
                DBCollection collection = db.getCollection(MASTER_DATA);
                BasicDBObject obj = new BasicDBObject();
                BasicDBObject in_query = new BasicDBObject();
                obj.append("skuBundleId", bundleId);
                in_query.append("$in",new int[] {1,2,3,4,5,6,7});
                obj.append("source_id", in_query);
                DBObject result = collection.findOne(obj);
                if (result==null){
                        throw new Exception();
                }
                return new JSONObject(JSON.serialize(result));
        }
        
        public static JSONObject getItemByID(long id) throws Exception{
                DB db = mongo_dtr.getDB(CATALOG_DB);
                DBCollection collection = db.getCollection(MASTER_DATA);
                BasicDBObject obj = new BasicDBObject();
                obj.append("_id", id);
                DBObject result = collection.findOne(obj);
                if (result==null){
                        throw new Exception();
                }
                return new JSONObject(JSON.serialize(result));
        }
        
        public static void persistFofoRegInfo(FofoForm ff){
            DB db = mongo_dtr.getDB(FOFO_DB);
                DBCollection collection = db.getCollection(FOFO_FORM_COLLECTION);
                if(ff.get_id()==0) {
                        BasicDBObject orderBy = new BasicDBObject();
                        orderBy.put("_id", -1);
                        DBCursor cursor = collection.find().sort(orderBy).limit(1);
                        long id = 1l;
                        while (cursor.hasNext()) {
                                Gson gson = new Gson();
                                FofoForm existingFofo = gson.fromJson(cursor.next().toString(), FofoForm.class);
                                id = existingFofo.get_id() + 1;
                        }
                        ff.set_id(id);
                }
                Gson gs = new Gson();
                DBObject dbObject = (DBObject) JSON.parse(gs.toJson(ff));
                collection.save(dbObject);
        }
        
        public static List<FofoForm> getFofoForms(int offset, int limit) {
                List<FofoForm> ffList = new ArrayList<FofoForm>(); 
                DB db = mongo_dtr.getDB(FOFO_DB);
                DBCollection collection = db.getCollection(FOFO_FORM_COLLECTION);
                BasicDBObject orderBy = new BasicDBObject();
                orderBy.put("_id", -1);
                DBCursor dbc = collection.find().sort(orderBy).limit(limit).skip(offset);
                while (dbc.hasNext()) {
                        ffList.add(convertJSONToPojo(dbc.next().toString()));
                }
                return ffList;
        }
        
        public static String getFofoFormJsonStringByFofoId(int fofoId){
                DB db = mongo_dtr.getDB(FOFO_DB);
                BasicDBObject filter = new BasicDBObject();
                filter.append("_id", fofoId);
                DBCollection collection = db.getCollection(FOFO_FORM_COLLECTION);
                DBObject fofoDbOject = collection.findOne(filter);
                return fofoDbOject.toString();
        }
        
        public static String getFofoFormsJsonString(){
                DB db = mongo_dtr.getDB(FOFO_DB);
                DBCollection collection = db.getCollection(FOFO_FORM_COLLECTION);
                DBCursor cursor = collection.find();
                StringBuilder fofoFormsJsonString = new StringBuilder();
                fofoFormsJsonString.append("[");
                while(cursor.hasNext()){
                        fofoFormsJsonString.append(cursor.next().toString());
                        if(cursor.hasNext()){
                                fofoFormsJsonString.append(",");
                        }
                }
                fofoFormsJsonString.append("]");
                return fofoFormsJsonString.toString();
        }
        
        public static FofoForm getFofoForm(int fofoId) {
                String fofoFormJsonString = getFofoFormJsonStringByFofoId(fofoId);
                System.out.println(fofoFormJsonString);
                return new Gson().fromJson(fofoFormJsonString, FofoForm.class);
                //return convertJSONToPojo(fofoDbOject.toString());
        }
        
        private static FofoForm convertJSONToPojo(String json){

            Type type = new TypeToken< FofoForm >(){}.getType();

            return new Gson().fromJson(json, type);

        }
        
        public static void updateColumnsById(Map<String, Integer> map, int fofoId){
                DB db = mongo_dtr.getDB(FOFO_DB);
                BasicDBObject filter = new BasicDBObject();
                filter.append("_id", fofoId);
                DBCollection collection = db.getCollection(FOFO_FORM_COLLECTION);
                BasicDBObject updateFields = new BasicDBObject();
                for(Map.Entry<String, Integer> entry : map.entrySet()){
                        updateFields.append(entry.getKey(), entry.getValue());
                }
                BasicDBObject newDocument = new BasicDBObject();
                newDocument.append("$set", updateFields);
                collection.update(filter, newDocument);
        }

}