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 *******************************************************************************/
016
017package org.mitre.uma.model;
018
019import java.util.Collection;
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.JoinTable;
034import javax.persistence.OneToMany;
035import javax.persistence.Table;
036
037/**
038 * A set of claims required to fulfill a given permission.
039 *
040 * @author jricher
041 *
042 */
043@Entity
044@Table(name = "policy")
045public class Policy {
046
047        private Long id;
048        private String name;
049        private Collection<Claim> claimsRequired;
050        private Set<String> scopes;
051
052        /**
053         * @return the id
054         */
055        @Id
056        @GeneratedValue(strategy = GenerationType.IDENTITY)
057        @Column(name = "id")
058        public Long getId() {
059                return id;
060        }
061
062        /**
063         * @param id the id to set
064         */
065        public void setId(Long id) {
066                this.id = id;
067        }
068
069        /**
070         * @return the name
071         */
072        @Basic
073        @Column(name = "name")
074        public String getName() {
075                return name;
076        }
077
078        /**
079         * @param name the name to set
080         */
081        public void setName(String name) {
082                this.name = name;
083        }
084
085        /**
086         * @return the claimsRequired
087         */
088        @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
089        @JoinTable(
090                        name = "claim_to_policy",
091                        joinColumns = @JoinColumn(name = "policy_id"),
092                        inverseJoinColumns = @JoinColumn(name = "claim_id")
093                        )
094        public Collection<Claim> getClaimsRequired() {
095                return claimsRequired;
096        }
097
098        /**
099         * @param claimsRequired the claimsRequired to set
100         */
101        public void setClaimsRequired(Collection<Claim> claimsRequired) {
102                this.claimsRequired = claimsRequired;
103        }
104
105        /**
106         * @return the scopes
107         */
108        @ElementCollection(fetch = FetchType.EAGER)
109        @Column(name = "scope")
110        @CollectionTable(
111                        name = "policy_scope",
112                        joinColumns = @JoinColumn(name = "owner_id")
113                        )
114        public Set<String> getScopes() {
115                return scopes;
116        }
117
118        /**
119         * @param scopes the scopes to set
120         */
121        public void setScopes(Set<String> scopes) {
122                this.scopes = scopes;
123        }
124
125        /* (non-Javadoc)
126         * @see java.lang.Object#toString()
127         */
128        @Override
129        public String toString() {
130                return "Policy [id=" + id + ", name=" + name + ", claimsRequired=" + claimsRequired + ", scopes=" + scopes + "]";
131        }
132
133        /* (non-Javadoc)
134         * @see java.lang.Object#hashCode()
135         */
136        @Override
137        public int hashCode() {
138                final int prime = 31;
139                int result = 1;
140                result = prime * result + ((claimsRequired == null) ? 0 : claimsRequired.hashCode());
141                result = prime * result + ((id == null) ? 0 : id.hashCode());
142                result = prime * result + ((name == null) ? 0 : name.hashCode());
143                result = prime * result + ((scopes == null) ? 0 : scopes.hashCode());
144                return result;
145        }
146
147        /* (non-Javadoc)
148         * @see java.lang.Object#equals(java.lang.Object)
149         */
150        @Override
151        public boolean equals(Object obj) {
152                if (this == obj) {
153                        return true;
154                }
155                if (obj == null) {
156                        return false;
157                }
158                if (getClass() != obj.getClass()) {
159                        return false;
160                }
161                Policy other = (Policy) obj;
162                if (claimsRequired == null) {
163                        if (other.claimsRequired != null) {
164                                return false;
165                        }
166                } else if (!claimsRequired.equals(other.claimsRequired)) {
167                        return false;
168                }
169                if (id == null) {
170                        if (other.id != null) {
171                                return false;
172                        }
173                } else if (!id.equals(other.id)) {
174                        return false;
175                }
176                if (name == null) {
177                        if (other.name != null) {
178                                return false;
179                        }
180                } else if (!name.equals(other.name)) {
181                        return false;
182                }
183                if (scopes == null) {
184                        if (other.scopes != null) {
185                                return false;
186                        }
187                } else if (!scopes.equals(other.scopes)) {
188                        return false;
189                }
190                return true;
191        }
192
193}