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.accountserver.Constants;
33 import org.iotivity.cloud.base.exception.ServerException.InternalServerErrorException;
37 * This class provides a set of APIs managing database.
40 public class AccountDBManager {
42 private static AccountDBManager accoutDBManager = new AccountDBManager();
44 private MongoDB mongoDB;
46 private HashMap<String, ArrayList<String>> keyField = new HashMap<String, ArrayList<String>>();
48 private AccountDBManager() {
55 private void createDatabase() {
59 mongoDB = new MongoDB(Constants.DB_NAME);
60 } catch (Exception e) {
62 throw new InternalServerErrorException(
63 "Database or Table create failed!");
67 private void createTables() {
69 mongoDB.createTable(Constants.USER_TABLE);
70 mongoDB.createTable(Constants.TOKEN_TABLE);
71 mongoDB.createTable(Constants.GROUP_TABLE);
72 mongoDB.createTable(Constants.DEVICE_TABLE);
73 mongoDB.createTable(Constants.ACL_TABLE);
74 mongoDB.createTable(Constants.ACLTEMPLATE_TABLE);
77 private void createIndexes() {
79 ArrayList<String> keys = new ArrayList<>();
80 keys.add(Constants.KEYFIELD_UUID);
82 mongoDB.createIndex(Constants.USER_TABLE, keys);
83 keyField.put(Constants.USER_TABLE, keys);
85 keys = new ArrayList<>();
86 keys.add(Constants.KEYFIELD_UUID);
87 keys.add(Constants.KEYFIELD_DID);
89 mongoDB.createIndex(Constants.TOKEN_TABLE, keys);
90 keyField.put(Constants.TOKEN_TABLE, keys);
92 keys = new ArrayList<>();
93 keys.add(Constants.KEYFIELD_GID);
95 mongoDB.createIndex(Constants.GROUP_TABLE, keys);
96 keyField.put(Constants.GROUP_TABLE, keys);
98 keys = new ArrayList<>();
99 keys.add(Constants.KEYFIELD_UUID);
100 keys.add(Constants.KEYFIELD_DID);
102 mongoDB.createIndex(Constants.DEVICE_TABLE, keys);
103 keyField.put(Constants.DEVICE_TABLE, keys);
105 keys = new ArrayList<>();
106 keys.add(Constants.KEYFIELD_ACLID);
108 mongoDB.createIndex(Constants.ACL_TABLE, keys);
109 keyField.put(Constants.ACL_TABLE, keys);
111 keys = new ArrayList<>();
112 keys.add(Constants.KEYFIELD_GTYPE);
114 mongoDB.createIndex(Constants.ACLTEMPLATE_TABLE, keys);
115 keyField.put(Constants.ACLTEMPLATE_TABLE, keys);
119 public static AccountDBManager getInstance() {
121 return accoutDBManager;
125 * API for inserting a record into DB table. the record will not be inserted
129 * table name to be inserted
131 * record to be inserted
133 public void insertRecord(String tableName, HashMap<String, Object> insert) {
135 if (!_insertRecord(tableName, insert))
136 throw new InternalServerErrorException(
137 "Database record insert failed");
141 * API for inserting a record into DB table. the record will be replaced if
145 * table name to be inserted
147 * record to be inserted
149 public void insertAndReplaceRecord(String tableName,
150 HashMap<String, Object> replace) {
152 if (!_insertAndReplaceRecord(tableName, replace))
153 throw new InternalServerErrorException(
154 "Database record insert failed");
158 * API for selecting records from DB table.
161 * table name to be inserted
163 * condition record to be selected
164 * @return selected records
166 public ArrayList<HashMap<String, Object>> selectRecord(String tableName,
167 HashMap<String, Object> condition) {
169 return _selectRecord(tableName, condition);
173 * API for deleting records from DB table.
176 * table name to be inserted
178 * condition record to be deleted
180 public void deleteRecord(String tableName,
181 HashMap<String, Object> condition) {
183 if (!_deleteRecord(tableName, condition))
184 throw new InternalServerErrorException(
185 "Database record delete failed");
189 * API for updating a record into DB table.
192 * table name to be inserted
194 * record to be updated
196 public void updateRecord(String tableName,
197 HashMap<String, Object> replace) {
199 if (!_updateRecord(tableName, replace))
200 throw new InternalServerErrorException(
201 "Database record update failed");
205 private Boolean _insertRecord(String tableName,
206 HashMap<String, Object> record) {
208 Document doc = createDocument(record);
210 return mongoDB.insertRecord(tableName, doc);
213 private Boolean _insertAndReplaceRecord(String tableName,
214 HashMap<String, Object> record) {
216 Document doc = createDocument(record);
217 Document filter = getKeyFilter(tableName, record);
219 return mongoDB.insertAndReplaceRecord(tableName, filter, doc);
222 private Boolean _deleteRecord(String tableName,
223 HashMap<String, Object> condition) {
225 Document doc = createDocument(condition);
227 return mongoDB.deleteRecord(tableName, doc);
230 private Boolean _updateRecord(String tableName,
231 HashMap<String, Object> record) {
233 Document replace = createDocument(record);
234 Document filter = getKeyFilter(tableName, record);
236 return mongoDB.updateRecord(tableName, filter, replace);
239 private ArrayList<HashMap<String, Object>> _selectRecord(String tableName,
240 HashMap<String, Object> record) {
242 Document doc = createDocument(record);
244 return mongoDB.selectRecord(tableName, doc);
247 private Document getKeyFilter(String tableName,
248 HashMap<String, Object> record) {
250 Document filterDoc = new Document();
252 ArrayList<String> keys = keyField.get(tableName);
254 for (String key : keys) {
256 String value = (String) record.get(key);
257 filterDoc.append(key, value);
263 private Document createDocument(HashMap<String, Object> record) {
265 Document doc = new Document();
266 Set<Entry<String, Object>> resEntrySet = record.entrySet();
267 Iterator<Entry<String, Object>> entryIter = resEntrySet.iterator();
269 while (entryIter.hasNext()) {
270 Map.Entry<String, Object> entry = (Map.Entry<String, Object>) entryIter
272 doc.append(entry.getKey().toString(), entry.getValue());