| 20424 |
kshitij.so |
1 |
package com.hotspotstore.storage;
|
|
|
2 |
|
|
|
3 |
import java.net.UnknownHostException;
|
|
|
4 |
import java.util.ArrayList;
|
|
|
5 |
import java.util.Calendar;
|
|
|
6 |
import java.util.List;
|
|
|
7 |
|
|
|
8 |
import org.json.JSONArray;
|
|
|
9 |
import org.json.JSONObject;
|
|
|
10 |
|
|
|
11 |
import com.google.gson.Gson;
|
|
|
12 |
import com.hotspotstore.model.Banner;
|
|
|
13 |
import com.hotspotstore.model.Snippet;
|
|
|
14 |
import com.mongodb.BasicDBObject;
|
|
|
15 |
import com.mongodb.DB;
|
|
|
16 |
import com.mongodb.DBCollection;
|
|
|
17 |
import com.mongodb.DBCursor;
|
|
|
18 |
import com.mongodb.DBObject;
|
|
|
19 |
import com.mongodb.MongoClient;
|
|
|
20 |
import com.mongodb.util.JSON;
|
|
|
21 |
|
|
|
22 |
|
|
|
23 |
public class Mongo {
|
|
|
24 |
|
|
|
25 |
private static MongoClient mongo;
|
|
|
26 |
private static final String HOTSPOT_DB = "Hotspot";
|
|
|
27 |
private static final String BANNERS = "Banners";
|
|
|
28 |
private static final String CONTENT ="CONTENT";
|
|
|
29 |
private static final String SITE_CONTENT = "siteContent";
|
|
|
30 |
private static final String HOTSPOT_ENTITES = "HotspotEntity";
|
|
|
31 |
private static final String SNIPPETS = "Snippets";
|
|
|
32 |
|
|
|
33 |
static {
|
|
|
34 |
try {
|
|
|
35 |
mongo = new MongoClient( "localhost" , 27017 );
|
|
|
36 |
} catch (UnknownHostException e) {
|
|
|
37 |
e.printStackTrace();
|
|
|
38 |
}
|
|
|
39 |
}
|
|
|
40 |
|
|
|
41 |
|
|
|
42 |
public static void addBanner(Banner b){
|
|
|
43 |
DB db = mongo.getDB(HOTSPOT_DB);
|
|
|
44 |
DBCollection collection = db.getCollection(BANNERS);
|
|
|
45 |
BasicDBObject orderBy = new BasicDBObject();
|
|
|
46 |
orderBy.put("_id", -1);
|
|
|
47 |
DBCursor cursor = collection.find().sort(orderBy).limit(1);
|
|
|
48 |
long id = 1l;
|
|
|
49 |
while (cursor.hasNext()) {
|
|
|
50 |
Gson gson = new Gson();
|
|
|
51 |
Banner bannerObj = gson.fromJson(cursor.next().toString(), Banner.class);
|
|
|
52 |
id = bannerObj.get_id() + 1;
|
|
|
53 |
}
|
|
|
54 |
b.set_id(id);
|
|
|
55 |
Gson gs = new Gson();
|
|
|
56 |
DBObject dbObject = (DBObject) JSON.parse(gs.toJson(b));
|
|
|
57 |
collection.insert(dbObject);
|
|
|
58 |
}
|
|
|
59 |
|
|
|
60 |
public static Banner getBannerById(long id) throws Exception{
|
|
|
61 |
DB db = mongo.getDB(HOTSPOT_DB);
|
|
|
62 |
DBCollection collection = db.getCollection(BANNERS);
|
|
|
63 |
BasicDBObject obj = new BasicDBObject();
|
|
|
64 |
obj.append("_id", id);
|
|
|
65 |
DBObject result = collection.findOne(obj);
|
|
|
66 |
if (result==null){
|
|
|
67 |
throw new Exception();
|
|
|
68 |
}
|
|
|
69 |
Gson gson = new Gson();
|
|
|
70 |
Banner banner = gson.fromJson(result.toString(), Banner.class);
|
|
|
71 |
return banner;
|
|
|
72 |
}
|
|
|
73 |
|
|
|
74 |
public static ArrayList<Banner> getAllBanners(){
|
|
|
75 |
DB db = mongo.getDB(HOTSPOT_DB);
|
|
|
76 |
DBCollection collection = db.getCollection(BANNERS);
|
|
|
77 |
ArrayList<Banner> banners = new ArrayList<Banner>();
|
|
|
78 |
DBCursor result = collection.find();
|
|
|
79 |
Gson gson = new Gson();
|
|
|
80 |
while (result.hasNext()) {
|
|
|
81 |
Banner banner = gson.fromJson(result.next().toString(), Banner.class);
|
|
|
82 |
banners.add(banner);
|
|
|
83 |
}
|
|
|
84 |
return banners;
|
|
|
85 |
}
|
|
|
86 |
|
|
|
87 |
public static ArrayList<Banner> getAllActiveBanners(){
|
|
|
88 |
DB db = mongo.getDB(HOTSPOT_DB);
|
|
|
89 |
DBCollection collection = db.getCollection(BANNERS);
|
|
|
90 |
ArrayList<Banner> banners = new ArrayList<Banner>();
|
|
|
91 |
BasicDBObject obj = new BasicDBObject();
|
|
|
92 |
obj.append("active", true);
|
|
|
93 |
BasicDBObject orderBy = new BasicDBObject();
|
|
|
94 |
orderBy.put("priority", 1);
|
|
|
95 |
DBCursor result = collection.find(obj).sort(orderBy);
|
|
|
96 |
Gson gson = new Gson();
|
|
|
97 |
while (result.hasNext()) {
|
|
|
98 |
Banner banner = gson.fromJson(result.next().toString(), Banner.class);
|
|
|
99 |
banners.add(banner);
|
|
|
100 |
}
|
|
|
101 |
return banners;
|
|
|
102 |
}
|
|
|
103 |
|
|
|
104 |
public static void deleteBanner(long id){
|
|
|
105 |
DB db = mongo.getDB(HOTSPOT_DB);
|
|
|
106 |
DBCollection collection = db.getCollection(BANNERS);
|
|
|
107 |
BasicDBObject obj = new BasicDBObject();
|
|
|
108 |
obj.append("_id", id);
|
|
|
109 |
collection.remove(obj);
|
|
|
110 |
}
|
|
|
111 |
|
|
|
112 |
public static void updateBanner(Banner b){
|
|
|
113 |
DB db = mongo.getDB(HOTSPOT_DB);
|
|
|
114 |
DBCollection collection = db.getCollection(BANNERS);
|
|
|
115 |
Gson gs = new Gson();
|
|
|
116 |
DBObject dbObject = (DBObject) JSON.parse(gs.toJson(b));
|
|
|
117 |
collection.update(new BasicDBObject().append("_id", b.get_id()), dbObject);
|
|
|
118 |
}
|
|
|
119 |
|
|
|
120 |
public static JSONObject getEntityById(long id) throws Exception{
|
|
|
121 |
DB db = mongo.getDB(CONTENT);
|
|
|
122 |
DBCollection collection = db.getCollection(SITE_CONTENT);
|
|
|
123 |
BasicDBObject obj = new BasicDBObject();
|
|
|
124 |
obj.append("_id", id);
|
|
|
125 |
DBObject result = collection.findOne(obj);
|
|
|
126 |
if (result==null){
|
|
|
127 |
throw new Exception();
|
|
|
128 |
}
|
|
|
129 |
return new JSONObject(JSON.serialize(result));
|
|
|
130 |
}
|
|
|
131 |
|
|
|
132 |
public static void addEntityToHotspot(long entityId, String title){
|
|
|
133 |
DB db = mongo.getDB(HOTSPOT_DB);
|
|
|
134 |
DBCollection collection = db.getCollection(HOTSPOT_ENTITES);
|
|
|
135 |
BasicDBObject obj = new BasicDBObject();
|
|
|
136 |
obj.append("_id", entityId);
|
|
|
137 |
obj.append("title", title);
|
|
|
138 |
collection.insert(obj);
|
|
|
139 |
}
|
|
|
140 |
|
|
|
141 |
public static void addSnippetInfo(Snippet s){
|
|
|
142 |
DB db = mongo.getDB(HOTSPOT_DB);
|
|
|
143 |
DBCollection collection = db.getCollection(SNIPPETS);
|
|
|
144 |
Gson gs = new Gson();
|
|
|
145 |
DBObject dbObject = (DBObject) JSON.parse(gs.toJson(s));
|
|
|
146 |
collection.insert(dbObject);
|
|
|
147 |
}
|
|
|
148 |
|
|
|
149 |
public static JSONObject getHotspotEntity(Long entityId) throws Exception{
|
|
|
150 |
DB db = mongo.getDB(HOTSPOT_DB);
|
|
|
151 |
DBCollection collection = db.getCollection(HOTSPOT_ENTITES);
|
|
|
152 |
BasicDBObject obj = new BasicDBObject();
|
|
|
153 |
obj.append("_id", entityId);
|
|
|
154 |
DBObject result = collection.findOne(obj);
|
|
|
155 |
if (result==null){
|
|
|
156 |
throw new Exception();
|
|
|
157 |
}
|
|
|
158 |
return new JSONObject(JSON.serialize(result));
|
|
|
159 |
}
|
|
|
160 |
|
|
|
161 |
public static List<Snippet> getSnippets(){
|
|
|
162 |
DB db = mongo.getDB(HOTSPOT_DB);
|
|
|
163 |
DBCollection collection = db.getCollection(SNIPPETS);
|
|
|
164 |
ArrayList<Snippet> snippets = new ArrayList<Snippet>();
|
|
|
165 |
DBCursor result = collection.find();
|
|
|
166 |
Gson gson = new Gson();
|
|
|
167 |
while (result.hasNext()) {
|
|
|
168 |
Snippet snippet = gson.fromJson(result.next().toString(), Snippet.class);
|
|
|
169 |
snippets.add(snippet);
|
|
|
170 |
}
|
|
|
171 |
return snippets;
|
|
|
172 |
}
|
|
|
173 |
|
|
|
174 |
public static JSONArray getTermsForAutoSuggest(){
|
|
|
175 |
DB db = mongo.getDB(CONTENT);
|
|
|
176 |
DBCollection collection = db.getCollection(SITE_CONTENT);
|
|
|
177 |
BasicDBObject obj = new BasicDBObject();
|
|
|
178 |
obj.append("title", 1);
|
|
|
179 |
JSONArray jsonArray = new JSONArray();
|
|
|
180 |
DBCursor result = collection.find(new BasicDBObject(),obj);
|
|
|
181 |
System.out.println(result.getQuery());
|
|
|
182 |
while (result.hasNext()) {
|
|
|
183 |
jsonArray.put(result);
|
|
|
184 |
}
|
|
|
185 |
return jsonArray;
|
|
|
186 |
}
|
|
|
187 |
|
|
|
188 |
public static void main(String[] args) throws Exception{
|
|
|
189 |
JSONArray x = getTermsForAutoSuggest();
|
|
|
190 |
System.out.println(x);
|
|
|
191 |
}
|
|
|
192 |
|
|
|
193 |
}
|