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.resources.acl.invite;
24 import java.util.ArrayList;
25 import java.util.HashMap;
26 import java.util.List;
28 import org.iotivity.cloud.accountserver.Constants;
29 import org.iotivity.cloud.accountserver.db.AccountDBManager;
30 import org.iotivity.cloud.accountserver.db.InviteTable;
31 import org.iotivity.cloud.accountserver.resources.acl.group.GroupBrokerManager;
32 import org.iotivity.cloud.accountserver.resources.acl.group.GroupManager;
33 import org.iotivity.cloud.accountserver.util.TypeCastingManager;
34 import org.iotivity.cloud.base.device.Device;
35 import org.iotivity.cloud.base.exception.ServerException.BadRequestException;
36 import org.iotivity.cloud.base.protocols.IRequest;
37 import org.iotivity.cloud.base.protocols.MessageBuilder;
38 import org.iotivity.cloud.base.protocols.enums.ContentFormat;
39 import org.iotivity.cloud.base.protocols.enums.ResponseStatus;
40 import org.iotivity.cloud.util.Cbor;
44 * This class provides a set of APIs to invite a user to a group
47 public class InviteManager {
49 private TypeCastingManager<InviteTable> mTypeInvite = new TypeCastingManager<>();
51 private class InviteSubscriber {
52 InviteSubscriber(Device subscriber, IRequest request) {
53 mSubscriber = subscriber;
57 public Device mSubscriber;
58 public IRequest mRequest;
61 // <uid, subscriber list>
62 private HashMap<String, ArrayList<InviteSubscriber>> mSubscribers = new HashMap<>();
65 * API to add invitation
68 * id of user who sent invitation
70 * id of group to invite member to
74 public void addInvitation(String uid, String gid, String mid) {
76 // create invitation table
77 InviteTable newInviteTable = new InviteTable(uid, gid, mid);
79 HashMap<String, Object> condition = new HashMap<>();
80 condition.put(Constants.KEYFIELD_INVITE_USER, uid);
81 condition.put(Constants.KEYFIELD_GID, gid);
82 condition.put(Constants.KEYFIELD_INVITED_USER, mid);
84 if (AccountDBManager.getInstance()
85 .selectRecord(Constants.INVITE_TABLE, condition).isEmpty()) {
86 AccountDBManager.getInstance().insertRecord(Constants.INVITE_TABLE,
87 mTypeInvite.convertObjectToMap(newInviteTable));
88 notifyToSubscriber(uid);
89 notifyToSubscriber(mid);
94 * API to delete invitation by invited user
99 * id of group which the user was invited to
101 * value of invitation accept or deny
103 public void deleteInvitation(String mid, String gid, boolean accepted) {
104 HashMap<String, Object> condition = new HashMap<>();
105 condition.put(Constants.REQ_GROUP_ID, gid);
106 condition.put(Constants.KEYFIELD_INVITED_USER, mid);
108 InviteTable getInviteTable = new InviteTable();
110 List<HashMap<String, Object>> getInviteList = AccountDBManager
111 .getInstance().selectRecord(Constants.INVITE_TABLE, condition);
113 ArrayList<String> uidList = new ArrayList<>();
114 for (HashMap<String, Object> getInvite : getInviteList) {
115 getInviteTable = mTypeInvite.convertMaptoObject(getInvite,
117 uidList.add(getInviteTable.getInviteUser());
120 if (getInviteList == null || getInviteList.isEmpty()) {
121 throw new BadRequestException("mid or gid is not valid");
124 AccountDBManager.getInstance().deleteRecord(Constants.INVITE_TABLE,
127 /* add user into group */
130 ArrayList<String> midlist = new ArrayList<>();
133 GroupManager.getInstance().addMembersToGroup(gid, midlist);
134 GroupBrokerManager.getInstance().notifyToObservers(
135 GroupManager.getInstance().getGroupTable(gid).getMembers());
138 notifyToSubscriber(mid);
139 for (String uid : uidList) {
140 notifyToSubscriber(uid);
145 * API to cancel invitation by user who invited member
148 * id of user who sent invitation
150 * id of group to invite member to
154 public void cancelInvitation(String uid, String gid, String mid) {
156 HashMap<String, Object> condition = new HashMap<>();
158 condition.put(Constants.REQ_GROUP_ID, gid);
159 condition.put(Constants.KEYFIELD_INVITED_USER, mid);
160 condition.put(Constants.KEYFIELD_INVITE_USER, uid);
162 AccountDBManager.getInstance().deleteRecord(Constants.INVITE_TABLE,
165 notifyToSubscriber(uid);
166 notifyToSubscriber(mid);
170 * API to get invitation information
175 * @return returns invite and invited information of the user
177 public HashMap<String, Object> getInvitationInfo(String uid) {
178 HashMap<String, Object> responsePayload = new HashMap<>();
180 ArrayList<Object> invitePayloadData = null;
181 ArrayList<Object> invitedPayloadData = null;
183 List<InviteTable> inviteList = getInviteTableList(
184 Constants.KEYFIELD_INVITE_USER, uid);
185 if (!inviteList.isEmpty()) {
186 invitePayloadData = new ArrayList<>();
187 for (InviteTable invite : inviteList) {
188 HashMap<String, String> inviteElement = new HashMap<>();
189 inviteElement.put(Constants.REQ_GROUP_ID, invite.getGid());
190 inviteElement.put(Constants.REQ_MEMBER,
191 invite.getInvitedUser());
192 invitePayloadData.add(inviteElement);
196 List<InviteTable> invitedList = getInviteTableList(
197 Constants.KEYFIELD_INVITED_USER, uid);
198 if (!invitedList.isEmpty()) {
199 invitedPayloadData = new ArrayList<>();
200 for (InviteTable invited : invitedList) {
201 HashMap<String, String> invitedElement = new HashMap<>();
202 invitedElement.put(Constants.REQ_GROUP_ID, invited.getGid());
203 invitedElement.put(Constants.REQ_MEMBER,
204 invited.getInviteUser());
205 invitedPayloadData.add(invitedElement);
209 responsePayload.put(Constants.RESP_INVITE, invitePayloadData);
210 responsePayload.put(Constants.RESP_INVITED, invitedPayloadData);
212 return responsePayload;
216 * API to add subscriber of invite resource
221 * device that sent request for subscription
223 * received request for subscription
225 * @return returns invite and invited information of the user
227 public HashMap<String, Object> addSubscriber(String uid, Device subscriber,
230 InviteSubscriber newSubscriber = new InviteSubscriber(subscriber,
233 synchronized (mSubscribers) {
234 ArrayList<InviteSubscriber> subscriberList = mSubscribers.get(uid);
236 if (subscriberList == null) {
237 subscriberList = new ArrayList<>();
240 subscriberList.add(newSubscriber);
241 mSubscribers.put(uid, subscriberList);
244 return getInvitationInfo(uid);
248 * API to remove subscriber of invite resource
253 * received request for unsubscription
255 * @return returns invite and invited information of the user
257 public HashMap<String, Object> removeSubscriber(String uid,
260 synchronized (mSubscribers) {
261 if (mSubscribers.containsKey(uid)) {
263 mSubscribers.get(uid).removeIf(subscriber -> subscriber.mRequest
264 .getRequestId().equals(request.getRequestId()));
268 return getInvitationInfo(uid);
271 private void notifyToSubscriber(String id) {
273 synchronized (mSubscribers) {
274 if (!mSubscribers.containsKey(id)) {
278 Cbor<HashMap<String, Object>> cbor = new Cbor<>();
279 byte[] payload = cbor.encodingPayloadToCbor(getInvitationInfo(id));
281 for (InviteSubscriber subscriber : mSubscribers.get(id)) {
283 subscriber.mSubscriber.sendResponse(
284 MessageBuilder.createResponse(subscriber.mRequest,
285 ResponseStatus.CONTENT,
286 ContentFormat.APPLICATION_CBOR, payload));
291 private List<InviteTable> getInviteTableList(String property, String uid) {
293 InviteTable getInviteTable = new InviteTable();
294 ArrayList<InviteTable> inviteList = new ArrayList<>();
296 HashMap<String, Object> condition = new HashMap<>();
297 condition.put(property, uid);
298 ArrayList<HashMap<String, Object>> mapInviteList = AccountDBManager
299 .getInstance().selectRecord(Constants.INVITE_TABLE, condition);
300 if (mapInviteList == null) {
301 throw new BadRequestException("uid is invalid");
303 for (HashMap<String, Object> mapInviteTable : mapInviteList) {
305 getInviteTable = mTypeInvite.convertMaptoObject(mapInviteTable,
307 inviteList.add(getInviteTable);