Rev 22161 | Rev 22165 | 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 javax.annotation.PostConstruct;import org.json.JSONObject;import org.springframework.beans.factory.annotation.Value;import org.springframework.stereotype.Component;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;@Componentpublic class Mongo {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;@Value("${mongo.host}")private String mongoHost;@Value("${content.mongo.host}")private String contentMongoHost;private MongoClient mongoClient;private MongoClient contentMongoClient;@PostConstructpublic void init() throws UnknownHostException{mongoClient = new MongoClient(mongoHost, MONGO_PORT);contentMongoClient = new MongoClient(mongoHost, MONGO_PORT);}public JSONObject getEntityById(long id) throws Exception{DB db = contentMongoClient.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 JSONObject getItemsByBundleId(long bundleId) throws Exception{DB db = mongoClient.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 JSONObject getItemByID(long id) throws Exception{DB db = mongoClient.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 void persistFofoRegInfo(FofoForm ff){DB db = mongoClient.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 List<FofoForm> getFofoForms(int offset, int limit) {List<FofoForm> ffList = new ArrayList<FofoForm>();DB db = mongoClient.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 String getFofoFormJsonStringByFofoId(int fofoId){DB db = mongoClient.getDB(FOFO_DB);BasicDBObject filter = new BasicDBObject();filter.append("_id", fofoId);DBCollection collection = db.getCollection(FOFO_FORM_COLLECTION);DBObject fofoDbOject = collection.findOne(filter);if(fofoDbOject != null) {return fofoDbOject.toString();} else {return null;}}public String getFofoFormJsonStringByEmail(String email){DB db = mongoClient.getDB(FOFO_DB);BasicDBObject filter = new BasicDBObject();filter.append("registeredEmail1", email);DBCollection collection = db.getCollection(FOFO_FORM_COLLECTION);DBObject fofoDbOject = collection.findOne(filter);if(fofoDbOject != null) {return fofoDbOject.toString();} else {return null;}}public String getFofoFormsJsonString(){DB db = mongoClient.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 FofoForm getFofoForm(int fofoId) {String fofoFormJsonString = getFofoFormJsonStringByFofoId(fofoId);System.out.println(fofoFormJsonString);return new Gson().fromJson(fofoFormJsonString, FofoForm.class);//return convertJSONToPojo(fofoDbOject.toString());}public FofoForm getFofoForm(String email) {String fofoFormJsonString = getFofoFormJsonStringByEmail(email);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 void updateColumnsById(Map<String, Integer> map, int fofoId){DB db = mongoClient.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);}}