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>();
128 .setDeviceName(doc.getString(Constants.RS_DEVICE_NAME));
129 publishPayloadFormat.setDi(doc.getString(Constants.RS_DEVICE_ID));
130 publishPayloadFormat.setBaseUri(doc.getString(Constants.RS_BASE_URI));
131 publishPayloadFormat.setBitmap(doc.getInteger(Constants.RS_BITMAP));
132 publishPayloadFormat.setPort(doc.getInteger(Constants.RS_HOSTING_PORT));
133 publishPayloadFormat.setIns(doc.getInteger(Constants.RS_INS));
134 publishPayloadFormat.setRts(doc.getString(Constants.RS_RTS));
135 publishPayloadFormat.setDrel(doc.getString(Constants.RS_DREL));
136 publishPayloadFormat.setTtl(doc.getInteger(Constants.RS_TTL));
138 linksPayloadFormat.setHref(doc.getString(Constants.RS_HREF));
140 .setRt((ArrayList<String>) doc.get(Constants.RS_RESOURCE_TYPE));
142 .setItf((ArrayList<String>) doc.get(Constants.RS_INTERFACE));
143 linksPayloadFormat.setRel(doc.getString(Constants.RS_REL));
144 linksPayloadFormat.setObs(doc.getBoolean(Constants.RS_OBS));
145 linksPayloadFormat.setTitle(doc.getString(Constants.RS_TITLE));
146 linksPayloadFormat.setUri(doc.getString(Constants.RS_URI));
147 linksPayloadFormat.setIns(doc.getInteger(Constants.RS_INS));
149 .setMt((ArrayList<String>) doc.get(Constants.RS_MEDIA_TYPE));
151 list.add(linksPayloadFormat);
152 publishPayloadFormat.setLinks(list);
154 return publishPayloadFormat;
158 * API for storing information of published resources
160 * @param publishPayloadFormat
161 * information of published resources to store in collection
165 public void createResource(PublishPayloadFormat publishPayloadFormat,
167 ArrayList<Document> docList = createDocuments(publishPayloadFormat);
168 Iterator<Document> docIter = docList.iterator();
170 MongoCollection<Document> collection = db.getCollection(tablename);
172 while (docIter.hasNext()) {
173 Document doc = docIter.next();
175 if (collection.findOneAndReplace(
176 Filters.and(Filters.eq(Constants.RS_DEVICE_ID,
177 doc.get(Constants.RS_DEVICE_ID)),
178 Filters.eq(Constants.RS_INS, doc.get(Constants.RS_INS))),
181 collection.insertOne(doc);
187 * API for finding resources matched filterValue of filterKey in collection
190 * field name in collection
192 * field value about field name
195 * @return ArrayList<PublishPayloadFormat> - array list of resource
198 public ArrayList<PublishPayloadFormat> readResource(String filterKey,
199 String filterValue, String tablename) {
200 MongoCollection<Document> collection = db.getCollection(tablename);
201 ArrayList<PublishPayloadFormat> resourceFormatList = new ArrayList<PublishPayloadFormat>();
202 MongoCursor<Document> cursor = collection
203 .find(Filters.eq(filterKey, filterValue)).iterator();
205 while (cursor.hasNext()) {
206 Document doc = cursor.next();
207 resourceFormatList.add(convertDocumentToResourceFormat(doc));
213 return resourceFormatList;
217 * API for finding resources matched filterValue of filterKey and a
218 * particular device ID in collection
223 * field name in collection
225 * field value about field name
228 * @return ArrayList<PublishPayloadFormat> - array list of resource
231 public ArrayList<PublishPayloadFormat> readResourceAboutDid(String di,
232 String filterKey, String filterValue, String tablename) {
233 MongoCollection<Document> collection = db.getCollection(tablename);
234 ArrayList<PublishPayloadFormat> resourceFormatList = new ArrayList<PublishPayloadFormat>();
235 MongoCursor<Document> cursor = collection
236 .find(Filters.and(Filters.eq(Constants.RS_DEVICE_ID, di),
237 Filters.eq(filterKey, filterValue)))
240 while (cursor.hasNext()) {
241 Document doc = cursor.next();
242 resourceFormatList.add(convertDocumentToResourceFormat(doc));
248 return resourceFormatList;
252 * API for deleting resources about a particular device ID in collection
259 public void deleteResourceAboutDid(String di, String tablename) {
261 MongoCollection<Document> collection = db.getCollection(tablename);
263 collection.findOneAndDelete(Filters.eq(Constants.RS_DEVICE_ID, di));
267 * API for deleting resources about a particular device ID and ins in
277 public void deleteResourceAboutDidAndIns(String di, String ins,
280 MongoCollection<Document> collection = db.getCollection(tablename);
282 collection.findOneAndDelete(
283 Filters.and(Filters.eq(Constants.RS_DEVICE_ID, di),
284 Filters.eq(Constants.RS_INS, ins)));