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.accountserver.db;
24 import java.util.ArrayList;
26 import org.bson.Document;
27 import org.iotivity.cloud.accountserver.Const;
28 import org.iotivity.cloud.util.Logger;
30 import com.mongodb.MongoClient;
31 import com.mongodb.client.MongoCollection;
32 import com.mongodb.client.MongoCursor;
33 import com.mongodb.client.MongoDatabase;
34 import com.mongodb.client.model.Filters;
38 * This class provides a set of APIs to use MongoDB APIs.
41 public class MongoDB {
43 private MongoClient mongoClient = null;
44 private MongoDatabase db = null;
47 * API creating MongoClient and initializing MongoDatabase
50 * database name to create MongoDatabase
53 public MongoDB(String dbname) throws Exception {
54 mongoClient = new MongoClient();
55 mongoClient.dropDatabase(dbname);
56 db = mongoClient.getDatabase(dbname);
60 * API creating collection
65 public void createTable(String tableName) {
66 db.createCollection(tableName);
70 * API deleting collection
75 public void deleteTable(String tableName) {
76 db.getCollection(tableName).drop();
79 public MongoDatabase getMongoDatabase() {
84 * API for storing information of authorized users
87 * information of authorized users
89 * table name of mongoDB
91 public void createResource(UserSession userSession) {
93 Document doc = createDocument(userSession);
94 MongoCollection<Document> collection = db
95 .getCollection(Const.SESSION_TABLE);
97 if (collection.findOneAndReplace(Filters.and(
98 Filters.eq(Const.USER_ID, doc.get(Const.USER_ID)),
99 Filters.eq(Const.SESSION_CODE, doc.get(Const.SESSION_CODE))),
102 collection.insertOne(doc);
108 public void createResource(UserDevice userDevice) {
110 Document doc = createDocument(userDevice);
111 MongoCollection<Document> collection = db
112 .getCollection(Const.DEVICE_TABLE);
114 if (collection.findOneAndReplace(
115 Filters.and(Filters.eq(Const.USER_ID, doc.get(Const.USER_ID)),
116 Filters.eq(Const.DEVICE_ID, doc.get(Const.DEVICE_ID))),
119 collection.insertOne(doc);
125 private Document createDocument(UserSession userSession) {
127 Document doc = new Document(Const.USER_ID, userSession.getUserId())
128 .append(Const.SESSION_CODE, userSession.getSessionCode());
133 private Document createDocument(UserDevice userDevice) {
135 Document doc = new Document(Const.USER_ID, userDevice.getUserId())
136 .append(Const.DEVICE_ID, userDevice.getDeviceId());
141 private UserSession convertSessionDocToResource(Document doc) {
143 UserSession userSession = new UserSession();
145 userSession.setUserId(doc.getString(Const.USER_ID));
146 userSession.setSessionCode(doc.getString(Const.SESSION_CODE));
151 private UserDevice convertDeviceDocToResource(Document doc) {
153 UserDevice userDevice = new UserDevice();
155 userDevice.setUserId(doc.getString(Const.USER_ID));
156 userDevice.setDeviceId(doc.getString(Const.DEVICE_ID));
161 public String getUserId(String sessionCode) {
163 String userId = null;
165 MongoCollection<Document> collection = db
166 .getCollection(Const.SESSION_TABLE);
168 MongoCursor<Document> cursor = collection
169 .find(Filters.eq(Const.SESSION_CODE, sessionCode)).iterator();
173 while (cursor.hasNext()) {
175 Document doc = cursor.next();
176 UserSession userSession = convertSessionDocToResource(doc);
178 userId = userSession.getUserId();
191 * API for getting devices according to user from mongoDB
196 * table name of mongoDB
198 public ArrayList<String> getDevices(String userId) {
200 ArrayList<String> deviceList = new ArrayList<String>();
202 MongoCollection<Document> collection = db
203 .getCollection(Const.DEVICE_TABLE);
205 MongoCursor<Document> cursor = collection
206 .find(Filters.eq(Const.USER_ID, userId)).iterator();
210 while (cursor.hasNext()) {
212 Document doc = cursor.next();
213 UserDevice userDeivce = convertDeviceDocToResource(doc);
215 deviceList.add(userDeivce.getDeviceId());
226 private ArrayList<UserSession> readSessionResources() {
228 ArrayList<UserSession> userSessionList = new ArrayList<UserSession>();
230 MongoCollection<Document> collection = db
231 .getCollection(Const.SESSION_TABLE);
232 MongoCursor<Document> cursor = collection.find().iterator();
234 while (cursor.hasNext()) {
236 Document doc = cursor.next();
237 userSessionList.add(convertSessionDocToResource(doc));
242 return userSessionList;
245 private ArrayList<UserDevice> readDeviceResources() {
247 ArrayList<UserDevice> userDeviceList = new ArrayList<UserDevice>();
249 MongoCollection<Document> collection = db
250 .getCollection(Const.DEVICE_TABLE);
251 MongoCursor<Document> cursor = collection.find().iterator();
253 while (cursor.hasNext()) {
255 Document doc = cursor.next();
256 userDeviceList.add(convertDeviceDocToResource(doc));
261 return userDeviceList;
264 public void printResources() {
266 ArrayList<UserDevice> dlist = readDeviceResources();
267 int size = dlist.size();
269 Logger.i("*Table: " + Const.DEVICE_TABLE);
270 for (int i = 0; i < size; i++) {
272 UserDevice item = dlist.get(i);
274 Logger.i("[" + i + "]" + item.getUserId() + ", "
275 + item.getDeviceId());
278 ArrayList<UserSession> slist = readSessionResources();
281 Logger.i("*Table: " + Const.SESSION_TABLE);
283 for (int i = 0; i < size; i++) {
285 UserSession item = slist.get(i);
287 Logger.i("[" + i + "]" + item.getUserId() + ", "
288 + item.getSessionCode());