| 1 | |
package org.talos.model; |
| 2 | |
|
| 3 | |
import java.util.Collection; |
| 4 | |
import java.util.HashSet; |
| 5 | |
import java.util.Set; |
| 6 | |
|
| 7 | |
import javax.persistence.CascadeType; |
| 8 | |
import javax.persistence.Entity; |
| 9 | |
import javax.persistence.GeneratedValue; |
| 10 | |
import javax.persistence.GenerationType; |
| 11 | |
import javax.persistence.Id; |
| 12 | |
import javax.persistence.JoinColumn; |
| 13 | |
import javax.persistence.ManyToMany; |
| 14 | |
import javax.persistence.ManyToOne; |
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | |
|
| 21 | |
@Entity |
| 22 | |
public class Capability { |
| 23 | |
|
| 24 | |
@Id |
| 25 | |
@GeneratedValue(strategy = GenerationType.AUTO) |
| 26 | |
private Long id; |
| 27 | |
|
| 28 | |
@ManyToOne |
| 29 | |
@JoinColumn(updatable = false) |
| 30 | |
private Subject subject; |
| 31 | |
|
| 32 | |
@ManyToOne |
| 33 | |
@JoinColumn(updatable = false) |
| 34 | |
private SecureObject secureObject; |
| 35 | |
|
| 36 | |
@ManyToOne |
| 37 | |
@JoinColumn(updatable = false) |
| 38 | |
private Category category; |
| 39 | |
|
| 40 | 1218 | @ManyToMany(cascade = CascadeType.REFRESH) |
| 41 | |
private Set<Permission> permissions = new HashSet<Permission>(); |
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | 331 | protected Capability() { |
| 50 | 331 | } |
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | |
|
| 56 | |
|
| 57 | |
|
| 58 | |
|
| 59 | |
|
| 60 | 360 | public Capability(Subject subject, SecureObject secureObject) { |
| 61 | 360 | if (subject == null) { |
| 62 | 2 | throw new IllegalArgumentException("subject can't be null"); |
| 63 | |
} |
| 64 | 358 | if (secureObject == null) { |
| 65 | 2 | throw new IllegalArgumentException("secureObject can't be null"); |
| 66 | |
} |
| 67 | 356 | this.subject = subject; |
| 68 | 356 | this.secureObject = secureObject; |
| 69 | 356 | } |
| 70 | |
|
| 71 | |
|
| 72 | |
|
| 73 | |
|
| 74 | |
|
| 75 | |
|
| 76 | |
|
| 77 | |
|
| 78 | |
|
| 79 | 527 | public Capability(Subject subject, Category category) { |
| 80 | 527 | if (subject == null) { |
| 81 | 2 | throw new IllegalArgumentException("subject can't be null"); |
| 82 | |
} |
| 83 | 525 | if (category == null) { |
| 84 | 2 | throw new IllegalArgumentException("category can't be null"); |
| 85 | |
} |
| 86 | 523 | this.subject = subject; |
| 87 | 523 | this.category = category; |
| 88 | 523 | } |
| 89 | |
|
| 90 | |
|
| 91 | |
|
| 92 | |
|
| 93 | |
|
| 94 | |
|
| 95 | |
public Long getId() { |
| 96 | 264 | return this.id; |
| 97 | |
} |
| 98 | |
|
| 99 | |
|
| 100 | |
|
| 101 | |
|
| 102 | |
|
| 103 | |
|
| 104 | |
|
| 105 | |
protected void setId(Long id) { |
| 106 | 13 | this.id = id; |
| 107 | 13 | } |
| 108 | |
|
| 109 | |
|
| 110 | |
|
| 111 | |
|
| 112 | |
|
| 113 | |
|
| 114 | |
public Subject getSubject() { |
| 115 | 137 | return this.subject; |
| 116 | |
} |
| 117 | |
|
| 118 | |
|
| 119 | |
|
| 120 | |
|
| 121 | |
|
| 122 | |
|
| 123 | |
protected void setSubject(Subject subject) { |
| 124 | 8 | this.subject = subject; |
| 125 | 8 | } |
| 126 | |
|
| 127 | |
|
| 128 | |
|
| 129 | |
|
| 130 | |
|
| 131 | |
public SecureObject getSecureObject() { |
| 132 | 231 | return this.secureObject; |
| 133 | |
} |
| 134 | |
|
| 135 | |
|
| 136 | |
|
| 137 | |
|
| 138 | |
|
| 139 | |
|
| 140 | |
protected void setSecureObject(SecureObject secureObject) { |
| 141 | 8 | this.secureObject = secureObject; |
| 142 | 8 | } |
| 143 | |
|
| 144 | |
|
| 145 | |
|
| 146 | |
|
| 147 | |
|
| 148 | |
|
| 149 | |
public Set<Permission> getPermissions() { |
| 150 | 1325 | return this.permissions; |
| 151 | |
} |
| 152 | |
|
| 153 | |
|
| 154 | |
|
| 155 | |
|
| 156 | |
|
| 157 | |
|
| 158 | |
|
| 159 | |
protected void setPermissions(Set<Permission> permissions) { |
| 160 | 12 | this.permissions = permissions; |
| 161 | 12 | } |
| 162 | |
|
| 163 | |
|
| 164 | |
|
| 165 | |
|
| 166 | |
|
| 167 | |
|
| 168 | |
public Category getCategory() { |
| 169 | 199 | return this.category; |
| 170 | |
} |
| 171 | |
|
| 172 | |
|
| 173 | |
|
| 174 | |
|
| 175 | |
|
| 176 | |
|
| 177 | |
protected void setCategory(Category category) { |
| 178 | 8 | this.category = category; |
| 179 | 8 | } |
| 180 | |
|
| 181 | |
|
| 182 | |
|
| 183 | |
|
| 184 | |
|
| 185 | |
|
| 186 | |
|
| 187 | |
|
| 188 | |
|
| 189 | |
public void add(Permission permission) { |
| 190 | 737 | if (permission == null) { |
| 191 | 1 | throw new IllegalArgumentException("permission can't be null"); |
| 192 | |
} |
| 193 | 736 | this.getPermissions().add(permission); |
| 194 | 736 | permission.addCapability(this); |
| 195 | 736 | } |
| 196 | |
|
| 197 | |
|
| 198 | |
|
| 199 | |
|
| 200 | |
|
| 201 | |
|
| 202 | |
|
| 203 | |
|
| 204 | |
|
| 205 | |
|
| 206 | |
public void remove(Permission permission) { |
| 207 | 73 | if (permission == null) { |
| 208 | 1 | throw new IllegalArgumentException("permission can't be null"); |
| 209 | |
} |
| 210 | 72 | this.getPermissions().remove(permission); |
| 211 | 72 | permission.removeCapability(this); |
| 212 | 72 | } |
| 213 | |
|
| 214 | |
@Override |
| 215 | |
public int hashCode() { |
| 216 | 2410 | final int PRIME = 31; |
| 217 | 2410 | int result = 1; |
| 218 | 2410 | result = PRIME * result + ((this.subject == null) ? 0 : this.subject.hashCode()); |
| 219 | 2410 | result = PRIME * result + ((this.category == null) ? 0 : this.category.hashCode()); |
| 220 | 2410 | result = PRIME * result + ((this.secureObject == null) ? 0 : this.secureObject.hashCode()); |
| 221 | 2410 | return result; |
| 222 | |
} |
| 223 | |
|
| 224 | |
@Override |
| 225 | |
public boolean equals(Object obj) { |
| 226 | |
|
| 227 | 130 | if (this == obj) { |
| 228 | 44 | return true; |
| 229 | |
} |
| 230 | |
|
| 231 | 86 | if (!(obj instanceof Capability)) { |
| 232 | 10 | return false; |
| 233 | |
} |
| 234 | |
|
| 235 | 76 | final Capability other = (Capability) obj; |
| 236 | |
|
| 237 | 76 | if (this.getCategory() == null) { |
| 238 | 57 | if (other.getCategory() != null) { |
| 239 | 0 | return false; |
| 240 | |
} |
| 241 | 19 | } else if (!this.getCategory().equals(other.getCategory())) { |
| 242 | 4 | return false; |
| 243 | |
} |
| 244 | |
|
| 245 | 72 | if (this.getSecureObject() == null) { |
| 246 | 15 | if (other.getSecureObject() != null) { |
| 247 | 0 | return false; |
| 248 | |
} |
| 249 | 57 | } else if (!this.getSecureObject().equals(other.getSecureObject())) { |
| 250 | 22 | return false; |
| 251 | |
} |
| 252 | |
|
| 253 | 50 | if (!this.getSubject().equals(other.getSubject())) { |
| 254 | 23 | return false; |
| 255 | |
} |
| 256 | |
|
| 257 | 27 | return true; |
| 258 | |
} |
| 259 | |
|
| 260 | |
@Override |
| 261 | |
public String toString() { |
| 262 | 4 | StringBuilder builder = new StringBuilder("["); |
| 263 | 4 | builder.append("id=").append(this.getId()).append(","); |
| 264 | 4 | builder.append(this.getSubject()).append("=>"); |
| 265 | |
|
| 266 | 4 | if (this.getSecureObject() != null) { |
| 267 | 3 | builder.append(this.getSecureObject()); |
| 268 | |
} |
| 269 | |
|
| 270 | 4 | if (this.getCategory() != null) { |
| 271 | 1 | builder.append(this.getCategory()); |
| 272 | |
} |
| 273 | |
|
| 274 | 4 | builder.append(", ").append(this.getPermissions().size()).append(" permissions]"); |
| 275 | |
|
| 276 | 4 | return builder.toString(); |
| 277 | |
} |
| 278 | |
|
| 279 | |
|
| 280 | |
|
| 281 | |
|
| 282 | |
|
| 283 | |
|
| 284 | |
|
| 285 | |
|
| 286 | |
public void addAll(Collection<Permission> perms) { |
| 287 | 256 | for (Permission p : perms) { |
| 288 | 587 | this.add(p); |
| 289 | |
} |
| 290 | 256 | } |
| 291 | |
|
| 292 | |
|
| 293 | |
|
| 294 | |
|
| 295 | |
|
| 296 | |
|
| 297 | |
|
| 298 | |
|
| 299 | |
|
| 300 | |
public void removeAll(Collection<Permission> perms) { |
| 301 | 14 | for (Permission p : perms) { |
| 302 | 22 | this.remove(p); |
| 303 | |
} |
| 304 | 14 | } |
| 305 | |
|
| 306 | |
|
| 307 | |
|
| 308 | |
|
| 309 | |
|
| 310 | |
public void clear() { |
| 311 | 16 | this.getPermissions().clear(); |
| 312 | 16 | } |
| 313 | |
|
| 314 | |
|
| 315 | |
|
| 316 | |
|
| 317 | |
|
| 318 | |
|
| 319 | |
|
| 320 | |
|
| 321 | |
public Capability copy(Subject otherSubject) { |
| 322 | 9 | if (otherSubject == null) { |
| 323 | 1 | throw new IllegalArgumentException("subject can't be null"); |
| 324 | |
} |
| 325 | 8 | Capability result = new Capability(); |
| 326 | 8 | result.setSubject(otherSubject); |
| 327 | 8 | result.setSecureObject(this.getSecureObject()); |
| 328 | 8 | result.setCategory(this.getCategory()); |
| 329 | 8 | result.getPermissions().addAll(this.getPermissions()); |
| 330 | 8 | return result; |
| 331 | |
} |
| 332 | |
|
| 333 | |
|
| 334 | |
|
| 335 | |
|
| 336 | |
|
| 337 | |
|
| 338 | |
|
| 339 | |
|
| 340 | |
public Capability copy(SecureObject otherObject) { |
| 341 | 10 | Capability result = new Capability(this.getSubject(), otherObject); |
| 342 | 9 | result.getPermissions().addAll(this.getPermissions()); |
| 343 | 9 | return result; |
| 344 | |
} |
| 345 | |
|
| 346 | |
|
| 347 | |
|
| 348 | |
|
| 349 | |
|
| 350 | |
|
| 351 | |
|
| 352 | |
|
| 353 | |
public Capability copy(Category otherCategory) { |
| 354 | 7 | Capability result = new Capability(this.getSubject(), otherCategory); |
| 355 | 6 | result.getPermissions().addAll(this.getPermissions()); |
| 356 | 6 | return result; |
| 357 | |
} |
| 358 | |
|
| 359 | |
} |