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