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;
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.util.Log;
34 import com.mongodb.MongoClient;
35 import com.mongodb.client.MongoCollection;
36 import com.mongodb.client.MongoCursor;
37 import com.mongodb.client.MongoDatabase;
38 import com.mongodb.client.model.IndexOptions;
39 import com.mongodb.client.result.DeleteResult;
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 {
60 mongoClient = new MongoClient();
61 mongoClient.dropDatabase(dbname);
62 db = mongoClient.getDatabase(dbname);
66 * API for creating collection
71 public void createTable(String tableName) {
73 db.createCollection(tableName);
77 * API for creating index
82 * key fields of collection
84 public void createIndex(String tablename, ArrayList<String> keys) {
86 Document doc = new Document();
88 for (String key : keys) {
93 IndexOptions options = new IndexOptions();
96 db.getCollection(tablename).createIndex(doc, options);
100 * API for deleting collection
105 public void deleteTable(String tableName) {
107 db.getCollection(tableName).drop();
111 * API for getting database object
114 public MongoDatabase getMongoDatabase() {
119 public Boolean insertRecord(String tableName, Document doc) {
121 if (tableName == null || doc == null)
124 MongoCollection<Document> collection = db.getCollection(tableName);
128 if (collection.find(doc).first() == null) {
130 collection.insertOne(doc);
134 Log.w("DB insert failed due to duplecated one.");
138 } catch (Exception e) {
144 showRecord(tableName);
149 public Boolean insertAndReplaceRecord(String tableName, Document filter,
152 if (tableName == null || filter == null || doc == null)
155 MongoCollection<Document> collection = db.getCollection(tableName);
159 if (collection.findOneAndReplace(filter, doc) == null) {
161 collection.insertOne(doc);
164 } catch (Exception e) {
170 showRecord(tableName);
175 public Boolean updateRecord(String tableName, Document filter,
178 if (tableName == null || filter == null || record == null)
181 MongoCollection<Document> collection = db.getCollection(tableName);
183 if (collection.findOneAndReplace(filter, record) == null) {
185 Log.w("DB update failed due to no matched record!");
189 showRecord(tableName);
194 public Boolean deleteRecord(String tableName, Document record) {
196 if (tableName == null || record == null)
199 MongoCollection<Document> collection = db.getCollection(tableName);
203 DeleteResult result = collection.deleteMany(record);
205 if (result.getDeletedCount() == 0) {
206 Log.w("DB delete failed due to no mached record!");
210 } catch (Exception e) {
216 showRecord(tableName);
221 public ArrayList<HashMap<String, Object>> selectRecord(String tableName,
224 if (tableName == null || doc == null)
227 MongoCollection<Document> collection = db.getCollection(tableName);
228 MongoCursor<Document> cursor = collection.find(doc).iterator();
230 ArrayList<HashMap<String, Object>> recordList = new ArrayList<HashMap<String, Object>>();
234 while (cursor.hasNext()) {
235 Document selectedDoc = cursor.next();
236 recordList.add(convertDocumentToHashMap(selectedDoc));
247 private HashMap<String, Object> convertDocumentToHashMap(Document doc) {
248 HashMap<String, Object> resourceMap = new HashMap<String, Object>();
250 Set<Entry<String, Object>> entrySet = doc.entrySet();
251 Iterator<Entry<String, Object>> entryIter = entrySet.iterator();
253 while (entryIter.hasNext()) {
255 Map.Entry<String, Object> entry = (Map.Entry<String, Object>) entryIter
258 String entryKey = entry.getKey();
260 // remove a mongoDB index
261 if (entry.getValue() != null && !entryKey.equals("_id")) {
263 resourceMap.put(entry.getKey(), entry.getValue());
270 private void showRecord(String tableName) {
272 MongoCollection<Document> collection = db.getCollection(tableName);
273 MongoCursor<Document> cursor = collection.find().iterator();
275 Log.i("<" + tableName + ">");
277 HashMap<String, Object> records = null;
279 while (cursor.hasNext()) {
281 Document doc = cursor.next();
282 records = convertDocumentToHashMap(doc);
284 Log.i("[" + index + "] " + records.toString());