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.Constants;
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 {
55 mongoClient = new MongoClient();
56 mongoClient.dropDatabase(dbname);
57 db = mongoClient.getDatabase(dbname);
61 * API creating collection
66 public void createTable(String tableName) {
68 db.createCollection(tableName);
72 * API deleting collection
77 public void deleteTable(String tableName) {
79 db.getCollection(tableName).drop();
83 * API getting database object
86 public MongoDatabase getMongoDatabase() {
92 * API for storing session information of user
95 * session information of user
97 public void createResource(UserSession userSession) {
99 Document doc = createDocument(userSession);
100 MongoCollection<Document> collection = db
101 .getCollection(Constants.SESSION_TABLE);
103 if (collection.findOneAndReplace(Filters.and(
104 Filters.eq(Constants.USER_ID, doc.get(Constants.USER_ID)),
105 Filters.eq(Constants.SESSION_CODE, doc.get(Constants.SESSION_CODE))),
108 collection.insertOne(doc);
115 * API for inserting device information of user
118 * device information of user
120 public void createResource(UserDevice userDevice) {
122 Document doc = createDocument(userDevice);
123 MongoCollection<Document> collection = db
124 .getCollection(Constants.DEVICE_TABLE);
126 if (collection.findOneAndReplace(Filters.and(
127 Filters.eq(Constants.USER_ID, doc.get(Constants.USER_ID)),
128 Filters.eq(Constants.DEVICE_ID, doc.get(Constants.DEVICE_ID))), doc) == null) {
130 collection.insertOne(doc);
137 * API for getting user identifier corresponding with session code from
142 * @return String - user identifier
144 public String getUserId(String sessionCode) {
146 String userId = null;
148 MongoCollection<Document> collection = db
149 .getCollection(Constants.SESSION_TABLE);
151 MongoCursor<Document> cursor = collection.find(
152 Filters.eq(Constants.SESSION_CODE, sessionCode)).iterator();
156 while (cursor.hasNext()) {
158 Document doc = cursor.next();
159 UserSession userSession = convertSessionDocToResource(doc);
161 userId = userSession.getUserId();
174 * API for getting devices corresponding with user identifier from database
179 public ArrayList<String> getDevices(String userId) {
181 ArrayList<String> deviceList = new ArrayList<String>();
183 MongoCollection<Document> collection = db
184 .getCollection(Constants.DEVICE_TABLE);
186 MongoCursor<Document> cursor = collection.find(
187 Filters.eq(Constants.USER_ID, userId)).iterator();
191 while (cursor.hasNext()) {
193 Document doc = cursor.next();
194 UserDevice userDeivce = convertDeviceDocToResource(doc);
196 deviceList.add(userDeivce.getDeviceId());
207 public void printResources() {
209 ArrayList<UserDevice> dlist = readDeviceResources();
210 int size = dlist.size();
212 Logger.i("*Table: " + Constants.DEVICE_TABLE);
213 for (int i = 0; i < size; i++) {
215 UserDevice item = dlist.get(i);
217 Logger.i("[" + i + "]" + item.getUserId() + ", "
218 + item.getDeviceId());
221 ArrayList<UserSession> slist = readSessionResources();
224 Logger.i("*Table: " + Constants.SESSION_TABLE);
226 for (int i = 0; i < size; i++) {
228 UserSession item = slist.get(i);
230 Logger.i("[" + i + "]" + item.getUserId() + ", "
231 + item.getSessionCode());
236 private Document createDocument(UserSession userSession) {
238 Document doc = new Document(Constants.USER_ID, userSession.getUserId())
239 .append(Constants.SESSION_CODE, userSession.getSessionCode());
244 private Document createDocument(UserDevice userDevice) {
246 Document doc = new Document(Constants.USER_ID, userDevice.getUserId())
247 .append(Constants.DEVICE_ID, userDevice.getDeviceId());
252 private UserSession convertSessionDocToResource(Document doc) {
254 UserSession userSession = new UserSession();
256 userSession.setUserId(doc.getString(Constants.USER_ID));
257 userSession.setSessionCode(doc.getString(Constants.SESSION_CODE));
262 private UserDevice convertDeviceDocToResource(Document doc) {
264 UserDevice userDevice = new UserDevice();
266 userDevice.setUserId(doc.getString(Constants.USER_ID));
267 userDevice.setDeviceId(doc.getString(Constants.DEVICE_ID));
272 private ArrayList<UserSession> readSessionResources() {
274 ArrayList<UserSession> userSessionList = new ArrayList<UserSession>();
276 MongoCollection<Document> collection = db
277 .getCollection(Constants.SESSION_TABLE);
278 MongoCursor<Document> cursor = collection.find().iterator();
280 while (cursor.hasNext()) {
282 Document doc = cursor.next();
283 userSessionList.add(convertSessionDocToResource(doc));
288 return userSessionList;
291 private ArrayList<UserDevice> readDeviceResources() {
293 ArrayList<UserDevice> userDeviceList = new ArrayList<UserDevice>();
295 MongoCollection<Document> collection = db
296 .getCollection(Constants.DEVICE_TABLE);
297 MongoCursor<Document> cursor = collection.find().iterator();
299 while (cursor.hasNext()) {
301 Document doc = cursor.next();
302 userDeviceList.add(convertDeviceDocToResource(doc));
307 return userDeviceList;