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;
24 import java.util.ArrayList;
25 import java.util.Iterator;
27 import org.bson.Document;
28 import org.iotivity.cloud.rdserver.resources.LinksPayloadFormat;
29 import org.iotivity.cloud.rdserver.resources.PublishPayloadFormat;
31 import com.mongodb.MongoClient;
32 import com.mongodb.client.MongoCollection;
33 import com.mongodb.client.MongoCursor;
34 import com.mongodb.client.MongoDatabase;
35 import com.mongodb.client.model.Filters;
39 * This class provides a set of APIs to use MongoDB APIs.
42 public class MongoDB {
44 private MongoClient mongoClient = null;
45 private MongoDatabase db = null;
48 * API creating MongoClient and initializing MongoDatabase
51 * database name to create MongoDatabase
54 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) {
67 db.createCollection(tableName);
71 * API deleting collection
76 public void deleteTable(String tableName) {
77 db.getCollection(tableName).drop();
80 private ArrayList<Document> createDocuments(
81 PublishPayloadFormat publishPayloadFormat) {
83 Iterator<LinksPayloadFormat> linksPayloadFormatIter = publishPayloadFormat
84 .getLinks().iterator();
85 ArrayList<Document> docList = new ArrayList<Document>();
87 while (linksPayloadFormatIter.hasNext()) {
88 LinksPayloadFormat links = linksPayloadFormatIter.next();
89 Document doc = new Document(Constants.RS_DEVICE_NAME,
90 publishPayloadFormat.getDeviceName())
91 .append(Constants.RS_DEVICE_ID,
92 publishPayloadFormat.getDi())
93 .append(Constants.RS_BASE_URI,
94 publishPayloadFormat.getBaseUri())
95 .append(Constants.RS_BITMAP,
96 publishPayloadFormat.getBitmap())
97 .append(Constants.RS_HOSTING_PORT,
98 publishPayloadFormat.getPort())
99 .append(Constants.RS_INS,
100 publishPayloadFormat.getIns())
101 .append(Constants.RS_RTS,
102 publishPayloadFormat.getRts())
103 .append(Constants.RS_DREL,
104 publishPayloadFormat.getDrel())
105 .append(Constants.RS_TTL,
106 publishPayloadFormat.getTtl())
107 .append(Constants.RS_HREF, links.getHref())
108 .append(Constants.RS_RESOURCE_TYPE, links.getRt())
109 .append(Constants.RS_INTERFACE, links.getItf())
110 .append(Constants.RS_REL, links.getRel())
111 .append(Constants.RS_OBS, links.isObs())
112 .append(Constants.RS_TITLE, links.getTitle())
113 .append(Constants.RS_URI, links.getUri())
114 .append(Constants.RS_INS, links.getIns())
115 .append(Constants.RS_MEDIA_TYPE, links.getMt());
122 private PublishPayloadFormat convertDocumentToResourceFormat(Document doc) {
123 PublishPayloadFormat publishPayloadFormat = new PublishPayloadFormat();
124 LinksPayloadFormat linksPayloadFormat = new LinksPayloadFormat();
125 ArrayList<LinksPayloadFormat> list = new ArrayList<LinksPayloadFormat>();
129 tmp = doc.get(Constants.RS_DEVICE_NAME);
132 .setDeviceName(tmp.toString());
135 tmp = doc.get(Constants.RS_DEVICE_ID);
137 publishPayloadFormat.setDi(tmp.toString());
140 tmp = doc.get(Constants.RS_BASE_URI);
142 publishPayloadFormat.setBaseUri(tmp.toString());
145 tmp = doc.get(Constants.RS_BITMAP);
147 publishPayloadFormat.setBitmap((int)tmp);
150 tmp = doc.get(Constants.RS_HOSTING_PORT);
152 publishPayloadFormat.setPort((int)tmp);
155 tmp = doc.get(Constants.RS_INS);
157 publishPayloadFormat.setIns((int)tmp);
160 tmp = doc.get(Constants.RS_RTS);
162 publishPayloadFormat.setRts(tmp.toString());
165 tmp = doc.get(Constants.RS_DREL);
167 publishPayloadFormat.setDrel(tmp.toString());
170 tmp = doc.get(Constants.RS_TTL);
172 publishPayloadFormat.setTtl((int)tmp);
175 tmp = doc.get(Constants.RS_HREF);
177 linksPayloadFormat.setHref(tmp.toString());
180 tmp = doc.get(Constants.RS_RESOURCE_TYPE);
183 .setRt((ArrayList<String>) tmp);
186 tmp = doc.get(Constants.RS_INTERFACE);
189 .setItf((ArrayList<String>) tmp);
192 tmp = doc.get(Constants.RS_REL);
194 linksPayloadFormat.setRel(tmp.toString());
197 tmp = doc.get(Constants.RS_OBS);
199 linksPayloadFormat.setObs((boolean)tmp);
202 tmp = doc.get(Constants.RS_TITLE);
204 linksPayloadFormat.setTitle(tmp.toString());
207 tmp = doc.get(Constants.RS_URI);
209 linksPayloadFormat.setUri(tmp.toString());
212 tmp = doc.get(Constants.RS_INS);
214 linksPayloadFormat.setIns((int)tmp);
217 tmp = doc.get(Constants.RS_MEDIA_TYPE);
220 .setMt((ArrayList<String>) tmp);
223 list.add(linksPayloadFormat);
224 publishPayloadFormat.setLinks(list);
226 return publishPayloadFormat;
230 * API for storing information of published resources
232 * @param publishPayloadFormat
233 * information of published resources to store in collection
237 public void createResource(PublishPayloadFormat publishPayloadFormat,
239 ArrayList<Document> docList = createDocuments(publishPayloadFormat);
240 Iterator<Document> docIter = docList.iterator();
242 MongoCollection<Document> collection = db.getCollection(tablename);
244 while (docIter.hasNext()) {
245 Document doc = docIter.next();
247 if (collection.findOneAndReplace(
248 Filters.and(Filters.eq(Constants.RS_DEVICE_ID,
249 doc.get(Constants.RS_DEVICE_ID)),
250 Filters.eq(Constants.RS_INS, doc.get(Constants.RS_INS))),
253 collection.insertOne(doc);
259 * API for finding resources matched filterValue of filterKey in collection
262 * field name in collection
264 * field value about field name
267 * @return ArrayList<PublishPayloadFormat> - array list of resource
270 public ArrayList<PublishPayloadFormat> readResource(String filterKey,
271 String filterValue, String tablename) {
272 MongoCollection<Document> collection = db.getCollection(tablename);
273 ArrayList<PublishPayloadFormat> resourceFormatList = new ArrayList<PublishPayloadFormat>();
274 MongoCursor<Document> cursor = collection
275 .find(Filters.eq(filterKey, filterValue)).iterator();
277 while (cursor.hasNext()) {
278 Document doc = cursor.next();
279 resourceFormatList.add(convertDocumentToResourceFormat(doc));
285 return resourceFormatList;
289 * API for finding resources matched filterValue of filterKey and a
290 * particular device ID in collection
295 * field name in collection
297 * field value about field name
300 * @return ArrayList<PublishPayloadFormat> - array list of resource
303 public ArrayList<PublishPayloadFormat> readResourceAboutDid(String di,
304 String filterKey, String filterValue, String tablename) {
305 MongoCollection<Document> collection = db.getCollection(tablename);
306 ArrayList<PublishPayloadFormat> resourceFormatList = new ArrayList<PublishPayloadFormat>();
307 MongoCursor<Document> cursor = collection
308 .find(Filters.and(Filters.eq(Constants.RS_DEVICE_ID, di),
309 Filters.eq(filterKey, filterValue)))
312 while (cursor.hasNext()) {
313 Document doc = cursor.next();
314 resourceFormatList.add(convertDocumentToResourceFormat(doc));
320 return resourceFormatList;
324 * API for deleting resources about a particular device ID in collection
331 public void deleteResourceAboutDid(String di, String tablename) {
333 MongoCollection<Document> collection = db.getCollection(tablename);
335 collection.findOneAndDelete(Filters.eq(Constants.RS_DEVICE_ID, di));
339 * API for deleting resources about a particular device ID and ins in
349 public void deleteResourceAboutDidAndIns(String di, String ins,
352 MongoCollection<Document> collection = db.getCollection(tablename);
354 collection.findOneAndDelete(
355 Filters.and(Filters.eq(Constants.RS_DEVICE_ID, di),
356 Filters.eq(Constants.RS_INS, ins)));