2 * //******************************************************************
4 * // Copyright 2016 Samsung Electronics All Rights Reserved.
6 * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
8 * // Licensed under the Apache License, Version 2.0 (the "License");
9 * // you may not use this file except in compliance with the License.
10 * // You may obtain a copy of the License at
12 * // http://www.apache.org/licenses/LICENSE-2.0
14 * // Unless required by applicable law or agreed to in writing, software
15 * // distributed under the License is distributed on an "AS IS" BASIS,
16 * // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * // See the License for the specific language governing permissions and
18 * // limitations under the License.
20 * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
22 package org.iotivity.cloud.rdserver.db;
24 import java.util.ArrayList;
25 import java.util.HashMap;
26 import java.util.Iterator;
28 import java.util.Map.Entry;
31 import org.bson.Document;
32 import org.iotivity.cloud.rdserver.Constants;
33 import org.iotivity.cloud.rdserver.resources.presence.resource.ResPresencePayload;
35 import com.mongodb.MongoClient;
36 import com.mongodb.client.MongoCollection;
37 import com.mongodb.client.MongoCursor;
38 import com.mongodb.client.MongoDatabase;
39 import com.mongodb.client.model.Filters;
43 * This class provides a set of APIs to use MongoDB APIs.
46 public class MongoDB {
48 private MongoClient mongoClient = null;
49 private MongoDatabase db = null;
52 * API creating MongoClient and initializing MongoDatabase
55 * database name to create MongoDatabase
58 public MongoDB(String dbname) throws Exception {
59 mongoClient = new MongoClient();
60 mongoClient.dropDatabase(dbname);
61 db = mongoClient.getDatabase(dbname);
65 * API creating collection
70 public void createTable(String tableName) {
71 deleteTable(tableName);
72 db.createCollection(tableName);
76 * API deleting collection
81 public void deleteTable(String tableName) {
82 db.getCollection(tableName).drop();
85 private Document createDocument(HashMap<Object, Object> storeRes) {
87 Document doc = new Document();
88 Set<Entry<Object, Object>> resEntrySet = storeRes.entrySet();
89 Iterator<Entry<Object, Object>> entryIter = resEntrySet.iterator();
91 while (entryIter.hasNext()) {
92 Map.Entry<Object, Object> entry = (Map.Entry<Object, Object>) entryIter
94 doc.append(entry.getKey().toString(), entry.getValue());
100 private ArrayList<Document> createDocuments(
101 ArrayList<HashMap<Object, Object>> storeResList) {
103 Iterator<HashMap<Object, Object>> resListIter = storeResList.iterator();
105 ArrayList<Document> docList = new ArrayList<>();
107 while (resListIter.hasNext()) {
108 Document doc = new Document();
110 HashMap<Object, Object> storeRes = resListIter.next();
111 Set<Entry<Object, Object>> resEntrySet = storeRes.entrySet();
112 Iterator<Entry<Object, Object>> entryIter = resEntrySet.iterator();
114 while (entryIter.hasNext()) {
115 Map.Entry<Object, Object> entry = (Map.Entry<Object, Object>) entryIter
117 doc.append(entry.getKey().toString(), entry.getValue());
125 private HashMap<Object, Object> convertDocumentToHashMap(Document doc) {
126 HashMap<Object, Object> resourceMap = new HashMap<Object, Object>();
128 Set<Entry<String, Object>> entrySet = doc.entrySet();
129 Iterator<Entry<String, Object>> entryIter = entrySet.iterator();
130 while (entryIter.hasNext()) {
131 Map.Entry<String, Object> entry = (Map.Entry<String, Object>) entryIter
133 if (entry.getValue() != null) {
134 resourceMap.put(entry.getKey().toString(), entry.getValue());
142 * API for storing information of published resources
144 * @param publishPayloadFormat
145 * information of published resources to store in collection
149 public ArrayList<ResPresencePayload> createRDResource(
150 ArrayList<HashMap<Object, Object>> storeResList, String tableName) {
151 ArrayList<Document> docList = createDocuments(storeResList);
152 Iterator<Document> docIter = docList.iterator();
154 MongoCollection<Document> collection = db.getCollection(tableName);
156 ArrayList<ResPresencePayload> resPayloadList = new ArrayList<>();
158 while (docIter.hasNext()) {
159 Document doc = docIter.next();
162 if (collection.findOneAndReplace(
164 Filters.eq(Constants.DEVICE_ID,
165 doc.get(Constants.DEVICE_ID)),
166 Filters.eq(Constants.INS, doc.get(Constants.INS))),
168 collection.insertOne(doc);
169 trigger = Constants.RES_CREATE;
172 trigger = Constants.RES_CHANGE;
176 resPayloadList.add(makeResourcePresencePayload(doc, trigger));
178 return resPayloadList;
181 public void createDevicePresenceResource(HashMap<Object, Object> storeRes,
184 Document doc = createDocument(storeRes);
185 MongoCollection<Document> collection = db.getCollection(tableName);
189 Filters.and(Filters.eq(Constants.DEVICE_ID,
190 doc.get(Constants.DEVICE_ID))),
193 collection.insertOne(doc);
199 private ResPresencePayload makeResourcePresencePayload(Document doc,
202 ResPresencePayload resPayload = new ResPresencePayload();
204 resPayload.setTrg(trigger);
206 Object rt = doc.get(Constants.RESOURCE_TYPE);
208 resPayload.setRt(rt.toString());
210 Object href = doc.get(Constants.HREF);
212 Object di = doc.get(Constants.DEVICE_ID);
214 resPayload.setHref(href.toString());
217 Object ttl = doc.get(Constants.RESOURCE_TTL);
219 resPayload.setTtl((int) ttl);
224 public String readDeviceState(String deviceId, String tableName) {
226 String deviceState = null;
228 MongoCollection<Document> collection = db.getCollection(tableName);
230 MongoCursor<Document> cursor = collection
231 .find(Filters.eq(Constants.DEVICE_ID, deviceId))
236 while (cursor.hasNext()) {
237 Document doc = cursor.next();
238 deviceState = doc.getString(Constants.PRESENCE_STATE);
250 public ArrayList<HashMap<Object, Object>> readResourceAboutDid(String di, String tableName) {
251 MongoCollection<Document> collection = db.getCollection(tableName);
252 ArrayList<HashMap<Object, Object>> resList = null;
253 MongoCursor<Document> cursor = collection
254 .find(Filters.eq(Constants.DEVICE_ID, di))
257 if (cursor.hasNext()) {
258 resList = new ArrayList<>();
260 while (cursor.hasNext()) {
261 Document doc = cursor.next();
262 resList.add(convertDocumentToHashMap(doc));
273 * API for finding resources matched filterValue of filterKey and a
274 * particular device ID in collection
279 * field name in collection
281 * field value about field name
284 * @return ArrayList<PublishPayloadFormat> - array list of resource
287 public ArrayList<HashMap<Object, Object>> readResourceAboutDidAndFilter(String di,
288 String filterKey, String filterValue, String tableName) {
289 MongoCollection<Document> collection = db.getCollection(tableName);
290 ArrayList<HashMap<Object, Object>> resList = null;
291 MongoCursor<Document> cursor = collection
292 .find(Filters.and(Filters.eq(Constants.DEVICE_ID, di),
293 Filters.eq(filterKey, filterValue)))
296 if (cursor.hasNext()) {
297 resList = new ArrayList<>();
299 while (cursor.hasNext()) {
300 Document doc = cursor.next();
301 resList.add(convertDocumentToHashMap(doc));
311 public Object readInsAboutDid(String di, String href, String tableName) {
312 MongoCollection<Document> collection = db.getCollection(tableName);
313 MongoCursor<Document> cursor = collection
314 .find(Filters.and(Filters.eq(Constants.DEVICE_ID, di),
315 Filters.eq(Constants.HREF, href)))
318 while (cursor.hasNext()) {
319 Document doc = cursor.next();
320 return doc.get(Constants.INS);
329 * API for deleting resources about a particular device ID in collection
336 public ArrayList<ResPresencePayload> deleteResourceAboutDi(String di,
339 MongoCollection<Document> collection = db.getCollection(tableName);
341 MongoCursor<Document> cursor = collection
342 .find(Filters.eq(Constants.DEVICE_ID, di)).iterator();
344 ArrayList<ResPresencePayload> resPayloadList = new ArrayList<>();
347 while (cursor.hasNext()) {
348 Document doc = cursor.next();
350 makeResourcePresencePayload(doc, Constants.RES_DELETE));
358 collection.deleteMany(Filters.eq(Constants.DEVICE_ID, di));
360 return resPayloadList;
364 * API for deleting resources about a particular device ID and ins in
374 public ArrayList<ResPresencePayload> deleteResourceAboutDiAndIns(String di,
375 String ins, String tableName) {
377 MongoCollection<Document> collection = db.getCollection(tableName);
379 MongoCursor<Document> cursor = collection
380 .find(Filters.and(Filters.eq(Constants.DEVICE_ID, di),
381 Filters.eq(Constants.INS, ins)))
384 ArrayList<ResPresencePayload> resPayloadList = new ArrayList<>();
387 while (cursor.hasNext()) {
388 Document doc = cursor.next();
390 makeResourcePresencePayload(doc, Constants.RES_DELETE));
398 collection.deleteOne(Filters.and(Filters.eq(Constants.DEVICE_ID, di),
399 Filters.eq(Constants.INS, ins)));
401 return resPayloadList;