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);
251 * API for finding resources matched filterValue of filterKey and a
252 * particular device ID in collection
257 * field name in collection
259 * field value about field name
262 * @return ArrayList<PublishPayloadFormat> - array list of resource
265 public ArrayList<HashMap<Object, Object>> readResourceAboutDid(String di,
266 String filterKey, String filterValue, String tableName) {
267 MongoCollection<Document> collection = db.getCollection(tableName);
268 ArrayList<HashMap<Object, Object>> resList = null;
269 MongoCursor<Document> cursor = collection
270 .find(Filters.and(Filters.eq(Constants.DEVICE_ID, di),
271 Filters.eq(filterKey, filterValue)))
274 if (cursor.hasNext()) {
275 resList = new ArrayList<>();
278 while (cursor.hasNext()) {
279 Document doc = cursor.next();
280 resList.add(convertDocumentToHashMap(doc));
289 public Object readInsAboutDid(String di, String href, String tableName) {
290 MongoCollection<Document> collection = db.getCollection(tableName);
291 MongoCursor<Document> cursor = collection
292 .find(Filters.and(Filters.eq(Constants.DEVICE_ID, di),
293 Filters.eq(Constants.HREF, href)))
296 while (cursor.hasNext()) {
297 Document doc = cursor.next();
298 return doc.get(Constants.INS);
307 * API for deleting resources about a particular device ID in collection
314 public ArrayList<ResPresencePayload> deleteResourceAboutDi(String di,
317 MongoCollection<Document> collection = db.getCollection(tableName);
319 MongoCursor<Document> cursor = collection
320 .find(Filters.eq(Constants.DEVICE_ID, di)).iterator();
322 ArrayList<ResPresencePayload> resPayloadList = new ArrayList<>();
325 while (cursor.hasNext()) {
326 Document doc = cursor.next();
328 makeResourcePresencePayload(doc, Constants.RES_DELETE));
336 collection.deleteMany(Filters.eq(Constants.DEVICE_ID, di));
338 return resPayloadList;
342 * API for deleting resources about a particular device ID and ins in
352 public ArrayList<ResPresencePayload> deleteResourceAboutDiAndIns(String di,
353 String ins, String tableName) {
355 MongoCollection<Document> collection = db.getCollection(tableName);
357 MongoCursor<Document> cursor = collection
358 .find(Filters.and(Filters.eq(Constants.DEVICE_ID, di),
359 Filters.eq(Constants.INS, ins)))
362 ArrayList<ResPresencePayload> resPayloadList = new ArrayList<>();
365 while (cursor.hasNext()) {
366 Document doc = cursor.next();
368 makeResourcePresencePayload(doc, Constants.RES_DELETE));
376 collection.deleteOne(Filters.and(Filters.eq(Constants.DEVICE_ID, di),
377 Filters.eq(Constants.INS, ins)));
379 return resPayloadList;