[IOT-1556] Changes in cloud according to the stateless request/response model (group...
[platform/upstream/iotivity.git] / cloud / account / src / main / java / org / iotivity / cloud / accountserver / db / GroupTable.java
1 /*
2  * //******************************************************************
3  * //
4  * // Copyright 2016 Samsung Electronics All Rights Reserved.
5  * //
6  * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
7  * //
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
11  * //
12  * //      http://www.apache.org/licenses/LICENSE-2.0
13  * //
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.
19  * //
20  * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
21  */
22 package org.iotivity.cloud.accountserver.db;
23
24 import java.util.ArrayList;
25 import java.util.Arrays;
26 import java.util.HashMap;
27
28 import org.iotivity.cloud.accountserver.Constants;
29 import org.iotivity.cloud.base.exception.ServerException.BadRequestException;
30
31 public class GroupTable {
32     private String            gid       = null;
33     private String            gname     = null;
34     private String            owner     = null;
35     private ArrayList<String> members   = null;
36     private ArrayList<String> masters   = null;
37     private ArrayList<String> devices   = null;
38     private ArrayList<Object> resources = null;
39     private ArrayList<String> subgroups = null;
40     private String            parent    = null;
41     private ArrayList<Object> gacl      = null;
42
43     public GroupTable() {
44         setGacl(makeGAcl());
45     }
46
47     private ArrayList<Object> makeGAcl() {
48         HashMap<String, Object> publicGAcl = new HashMap<>();
49         publicGAcl.put(Constants.KEYFIELD_ACE_PERMISSION, (int) 31);
50         ArrayList<HashMap<String, Object>> resources = new ArrayList<>();
51         HashMap<String, Object> resource = new HashMap<>();
52         resource.put(Constants.KEYFIELD_ACE_RESOURCE_HREF, "*");
53         resource.put(Constants.KEYFIELD_ACE_RESOURCE_RT,
54                 new ArrayList<String>());
55         resource.put(Constants.KEYFIELD_ACE_RESOURCE_IF,
56                 Arrays.asList(Constants.DEFAULT_INTERFACE));
57         resources.add(resource);
58         publicGAcl.put(Constants.KEYFIELD_GROUP_RESOURCES, resources);
59
60         ArrayList<Object> gaclList = new ArrayList<>();
61         gaclList.add(publicGAcl);
62
63         return gaclList;
64     }
65
66     public GroupTable(String gid, String gname, String owner,
67             ArrayList<String> masters, ArrayList<String> members,
68             ArrayList<String> devices, ArrayList<Object> resources,
69             ArrayList<String> subgroups, String parent,
70             ArrayList<Object> gacl) {
71         this.gid = gid;
72         this.gname = gname;
73         this.owner = owner;
74         this.members = members;
75         this.masters = masters;
76         this.devices = devices;
77         this.resources = resources;
78         this.subgroups = subgroups;
79         this.parent = parent;
80         this.gacl = gacl;
81     }
82
83     /**
84      * API to get group ID
85      * 
86      * @return group ID
87      */
88     public String getGid() {
89         return gid;
90     }
91
92     /**
93      * API to set group ID
94      * 
95      * @param gid
96      *            group ID to be set
97      */
98     public void setGid(String gid) {
99         this.gid = gid.toString();
100     }
101
102     public String getGname() {
103         return gname;
104     }
105
106     public void setGname(String gname) {
107         this.gname = gname.toString();
108     }
109
110     public String getOwner() {
111         return owner;
112     }
113
114     public void setOwner(String owner) {
115         this.owner = owner.toString();
116     }
117
118     public ArrayList<String> getMasters() {
119         return masters;
120     }
121
122     public void setMasters(ArrayList<String> masters) {
123         this.masters = masters;
124     }
125
126     public ArrayList<String> getMembers() {
127         return members;
128     }
129
130     public void setMembers(ArrayList<String> members) {
131         this.members = members;
132     }
133
134     public ArrayList<String> getDevices() {
135         return devices;
136     }
137
138     public void setDevices(ArrayList<String> devices) {
139         this.devices = devices;
140     }
141
142     public ArrayList<Object> getResources() {
143         return resources;
144     }
145
146     public void setResources(ArrayList<Object> resources) {
147         this.resources = resources;
148     }
149
150     public ArrayList<String> getSubgroups() {
151         return subgroups;
152     }
153
154     public void setSubgroups(ArrayList<String> subgroups) {
155         this.subgroups = subgroups;
156     }
157
158     public String getParent() {
159         return parent;
160     }
161
162     public void setParent(String parentGid) {
163         this.parent = parentGid;
164     }
165
166     /**
167      * API to get group ACL
168      * 
169      * @return group ACL
170      */
171     public ArrayList<Object> getGacl() {
172         return gacl;
173     }
174
175     /**
176      * API to set group ACL of the group
177      * 
178      * @param gacl
179      *            group ACL to be set
180      */
181     public void setGacl(ArrayList<Object> gacl) {
182         this.gacl = gacl;
183     }
184
185     public <T> T getPropertyValue(String property) {
186         switch (property) {
187             case Constants.REQ_GROUP_ID:
188                 return (T) gid;
189             case Constants.KEYFIELD_GROUP_NAME:
190                 return (T) gname;
191             case Constants.KEYFIELD_GROUP_OWNER:
192                 return (T) owner;
193             case Constants.KEYFIELD_GROUP_MEMBERS:
194                 return (T) members;
195             case Constants.KEYFIELD_GROUP_MASTERS:
196                 return (T) masters;
197             case Constants.KEYFIELD_GROUP_DEVICES:
198                 return (T) devices;
199             case Constants.KEYFIELD_GROUP_RESOURCES:
200                 return (T) resources;
201             case Constants.KEYFIELD_GROUP_SUBGROUPS:
202                 return (T) subgroups;
203             case Constants.KEYFIELD_GROUP_GACL:
204                 return (T) gacl;
205             default:
206                 throw new BadRequestException(
207                         property + " is not supported in the group");
208         }
209     }
210
211     public void setPropertyValue(String property, Object value) {
212         switch (property) {
213             case Constants.REQ_GROUP_ID:
214                 this.gid = (String) value;
215                 break;
216             case Constants.KEYFIELD_GROUP_NAME:
217                 this.gname = (String) value;
218                 break;
219             case Constants.KEYFIELD_GROUP_OWNER:
220                 this.owner = (String) value;
221                 break;
222             case Constants.KEYFIELD_GROUP_MEMBERS:
223                 this.members = (ArrayList<String>) value;
224                 break;
225             case Constants.KEYFIELD_GROUP_MASTERS:
226                 this.masters = (ArrayList<String>) value;
227                 break;
228             case Constants.KEYFIELD_GROUP_DEVICES:
229                 this.devices = (ArrayList<String>) value;
230                 break;
231             case Constants.KEYFIELD_GROUP_RESOURCES:
232                 this.resources = (ArrayList<Object>) value;
233                 break;
234             case Constants.KEYFIELD_GROUP_SUBGROUPS:
235                 this.subgroups = (ArrayList<String>) value;
236                 break;
237             case Constants.KEYFIELD_GROUP_GACL:
238                 this.gacl = (ArrayList<Object>) value;
239                 break;
240             default:
241                 throw new BadRequestException(
242                         property + " is not supported in the group");
243         }
244     }
245 }