001/*******************************************************************************
002 * Copyright 2017 The MIT Internet Trust Consortium
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *   http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *******************************************************************************/
016package org.mitre.uma.model;
017
018import java.util.Collection;
019import java.util.HashSet;
020import java.util.Set;
021
022import javax.persistence.Basic;
023import javax.persistence.CascadeType;
024import javax.persistence.CollectionTable;
025import javax.persistence.Column;
026import javax.persistence.ElementCollection;
027import javax.persistence.Entity;
028import javax.persistence.FetchType;
029import javax.persistence.GeneratedValue;
030import javax.persistence.GenerationType;
031import javax.persistence.Id;
032import javax.persistence.JoinColumn;
033import javax.persistence.NamedQueries;
034import javax.persistence.NamedQuery;
035import javax.persistence.OneToMany;
036import javax.persistence.Table;
037
038@Entity
039@Table(name = "resource_set")
040@NamedQueries ({
041        @NamedQuery(name = ResourceSet.QUERY_BY_OWNER, query = "select r from ResourceSet r where r.owner = :" + ResourceSet.PARAM_OWNER),
042        @NamedQuery(name = ResourceSet.QUERY_BY_OWNER_AND_CLIENT, query = "select r from ResourceSet r where r.owner = :" + ResourceSet.PARAM_OWNER + " and r.clientId = :" + ResourceSet.PARAM_CLIENTID),
043        @NamedQuery(name = ResourceSet.QUERY_BY_CLIENT, query = "select r from ResourceSet r where r.clientId = :" + ResourceSet.PARAM_CLIENTID),
044        @NamedQuery(name = ResourceSet.QUERY_ALL, query = "select r from ResourceSet r")
045})
046public class ResourceSet {
047
048        public static final String QUERY_BY_OWNER = "ResourceSet.queryByOwner";
049        public static final String QUERY_BY_OWNER_AND_CLIENT = "ResourceSet.queryByOwnerAndClient";
050        public static final String QUERY_BY_CLIENT = "ResourceSet.queryByClient";
051        public static final String QUERY_ALL = "ResourceSet.queryAll";
052
053        public static final String PARAM_OWNER = "owner";
054        public static final String PARAM_CLIENTID = "clientId";
055
056        private Long id;
057        private String name;
058        private String uri;
059        private String type;
060        private Set<String> scopes = new HashSet<>();
061        private String iconUri;
062
063        private String owner; // username of the person responsible for the registration (either directly or via OAuth token)
064        private String clientId; // client id of the protected resource that registered this resource set via OAuth token
065
066        private Collection<Policy> policies = new HashSet<>();
067
068        /**
069         * @return the id
070         */
071        @Id
072        @GeneratedValue(strategy = GenerationType.IDENTITY)
073        @Column(name = "id")
074        public Long getId() {
075                return id;
076        }
077
078        /**
079         * @param id the id to set
080         */
081        public void setId(Long id) {
082                this.id = id;
083        }
084
085        /**
086         * @return the name
087         */
088        @Basic
089        @Column(name = "name")
090        public String getName() {
091                return name;
092        }
093
094        /**
095         * @param name the name to set
096         */
097        public void setName(String name) {
098                this.name = name;
099        }
100
101        /**
102         * @return the uri
103         */
104        @Basic
105        @Column(name = "uri")
106        public String getUri() {
107                return uri;
108        }
109
110        /**
111         * @param uri the uri to set
112         */
113        public void setUri(String uri) {
114                this.uri = uri;
115        }
116
117        /**
118         * @return the type
119         */
120        @Basic
121        @Column(name = "rs_type")
122        public String getType() {
123                return type;
124        }
125
126        /**
127         * @param type the type to set
128         */
129        public void setType(String type) {
130                this.type = type;
131        }
132
133        /**
134         * @return the scopes
135         */
136        @ElementCollection(fetch = FetchType.EAGER)
137        @Column(name = "scope")
138        @CollectionTable(
139                        name = "resource_set_scope",
140                        joinColumns = @JoinColumn(name = "owner_id")
141                        )
142        public Set<String> getScopes() {
143                return scopes;
144        }
145
146        /**
147         * @param scopes the scopes to set
148         */
149        public void setScopes(Set<String> scopes) {
150                this.scopes = scopes;
151        }
152
153        /**
154         * @return the iconUri
155         */
156        @Basic
157        @Column(name = "icon_uri")
158        public String getIconUri() {
159                return iconUri;
160        }
161
162        /**
163         * @param iconUri the iconUri to set
164         */
165        public void setIconUri(String iconUri) {
166                this.iconUri = iconUri;
167        }
168
169        /**
170         * @return the owner
171         */
172        @Basic
173        @Column(name = "owner")
174        public String getOwner() {
175                return owner;
176        }
177
178        /**
179         * @param owner the owner to set
180         */
181        public void setOwner(String owner) {
182                this.owner = owner;
183        }
184
185        /**
186         * @return the clientId
187         */
188        @Basic
189        @Column(name = "client_id")
190        public String getClientId() {
191                return clientId;
192        }
193
194        /**
195         * @param clientId the clientId to set
196         */
197        public void setClientId(String clientId) {
198                this.clientId = clientId;
199        }
200
201        /**
202         * @return the claimsRequired
203         */
204        @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
205        @JoinColumn(name = "resource_set_id")
206        public Collection<Policy> getPolicies() {
207                return policies;
208        }
209
210        /**
211         * @param policies the claimsRequired to set
212         */
213        public void setPolicies(Collection<Policy> policies) {
214                this.policies = policies;
215        }
216
217        /* (non-Javadoc)
218         * @see java.lang.Object#toString()
219         */
220        @Override
221        public String toString() {
222                return "ResourceSet [id=" + id + ", name=" + name + ", uri=" + uri + ", type=" + type + ", scopes=" + scopes + ", iconUri=" + iconUri + ", owner=" + owner + ", clientId=" + clientId + ", policies=" + policies + "]";
223        }
224
225        /* (non-Javadoc)
226         * @see java.lang.Object#hashCode()
227         */
228        @Override
229        public int hashCode() {
230                final int prime = 31;
231                int result = 1;
232                result = prime * result + ((clientId == null) ? 0 : clientId.hashCode());
233                result = prime * result + ((iconUri == null) ? 0 : iconUri.hashCode());
234                result = prime * result + ((id == null) ? 0 : id.hashCode());
235                result = prime * result + ((name == null) ? 0 : name.hashCode());
236                result = prime * result + ((owner == null) ? 0 : owner.hashCode());
237                result = prime * result + ((policies == null) ? 0 : policies.hashCode());
238                result = prime * result + ((scopes == null) ? 0 : scopes.hashCode());
239                result = prime * result + ((type == null) ? 0 : type.hashCode());
240                result = prime * result + ((uri == null) ? 0 : uri.hashCode());
241                return result;
242        }
243
244        /* (non-Javadoc)
245         * @see java.lang.Object#equals(java.lang.Object)
246         */
247        @Override
248        public boolean equals(Object obj) {
249                if (this == obj) {
250                        return true;
251                }
252                if (obj == null) {
253                        return false;
254                }
255                if (getClass() != obj.getClass()) {
256                        return false;
257                }
258                ResourceSet other = (ResourceSet) obj;
259                if (clientId == null) {
260                        if (other.clientId != null) {
261                                return false;
262                        }
263                } else if (!clientId.equals(other.clientId)) {
264                        return false;
265                }
266                if (iconUri == null) {
267                        if (other.iconUri != null) {
268                                return false;
269                        }
270                } else if (!iconUri.equals(other.iconUri)) {
271                        return false;
272                }
273                if (id == null) {
274                        if (other.id != null) {
275                                return false;
276                        }
277                } else if (!id.equals(other.id)) {
278                        return false;
279                }
280                if (name == null) {
281                        if (other.name != null) {
282                                return false;
283                        }
284                } else if (!name.equals(other.name)) {
285                        return false;
286                }
287                if (owner == null) {
288                        if (other.owner != null) {
289                                return false;
290                        }
291                } else if (!owner.equals(other.owner)) {
292                        return false;
293                }
294                if (policies == null) {
295                        if (other.policies != null) {
296                                return false;
297                        }
298                } else if (!policies.equals(other.policies)) {
299                        return false;
300                }
301                if (scopes == null) {
302                        if (other.scopes != null) {
303                                return false;
304                        }
305                } else if (!scopes.equals(other.scopes)) {
306                        return false;
307                }
308                if (type == null) {
309                        if (other.type != null) {
310                                return false;
311                        }
312                } else if (!type.equals(other.type)) {
313                        return false;
314                }
315                if (uri == null) {
316                        if (other.uri != null) {
317                                return false;
318                        }
319                } else if (!uri.equals(other.uri)) {
320                        return false;
321                }
322                return true;
323        }
324
325
326
327
328
329}