Coverage Report - org.talos.model.Capability
 
Classes in this File Line Coverage Branch Coverage Complexity
Capability
98%
97/99
100%
23/23
0
 
 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  
  * This is the class that ties all other Talos entities together. A Capability is uniquely defined by a Subject and either a
 18  
  * SecureObject or a Category. Each capability holds the permissions that are granted to its subject on its target object (or
 19  
  * category). Note: you normally don't need to use Capabilities directly, unless you are extending Talos.
 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  
          * Empty constructor needed by Hibernate.
 45  
          * 
 46  
          * @see #Capability(Subject, Category)
 47  
          * @see #Capability(Subject, SecureObject)
 48  
          */
 49  331
         protected Capability() {
 50  331
         }
 51  
 
 52  
         /**
 53  
          * Constructor that gets a subject and an object. Both arguments must be non-null, persistent instances.
 54  
          * 
 55  
          * @param subject
 56  
          *            the capability's subject
 57  
          * @param secureObject
 58  
          *            the capability's object
 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  
          * Constructor that gets a subject and a category. Both arguments must be non-null, persistent instances.
 73  
          * 
 74  
          * @param subject
 75  
          *            the capability's subject
 76  
          * @param category
 77  
          *            the capability's category
 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  
          * Gets the capability's persistent id.
 92  
          * 
 93  
          * @return the capability's id
 94  
          */
 95  
         public Long getId() {
 96  264
                 return this.id;
 97  
         }
 98  
 
 99  
         /**
 100  
          * Sets the capability's persistent id. Will usually be called by the persistence layer.
 101  
          * 
 102  
          * @param id
 103  
          *            the new id for this object.
 104  
          */
 105  
         protected void setId(Long id) {
 106  13
                 this.id = id;
 107  13
         }
 108  
 
 109  
         /**
 110  
          * Gets the capability's subject.
 111  
          * 
 112  
          * @return the subject
 113  
          */
 114  
         public Subject getSubject() {
 115  137
                 return this.subject;
 116  
         }
 117  
 
 118  
         /**
 119  
          * Sets the capability's subject. This will usually be called by the persistence layer. 
 120  
          * 
 121  
          * @param subject the subject
 122  
          */
 123  
         protected void setSubject(Subject subject) {
 124  8
                 this.subject = subject;
 125  8
         }
 126  
 
 127  
         /**
 128  
          * Gets the capability's secure object. 
 129  
          * @return the object
 130  
          */
 131  
         public SecureObject getSecureObject() {
 132  231
                 return this.secureObject;
 133  
         }
 134  
 
 135  
         /**
 136  
          * Sets the capability's object. This will usually be called by the persistence layer. 
 137  
          * 
 138  
          * @param secureObject the object
 139  
          */
 140  
         protected void setSecureObject(SecureObject secureObject) {
 141  8
                 this.secureObject = secureObject;
 142  8
         }
 143  
 
 144  
         /**
 145  
          * Gets the capability's permissions.
 146  
          * 
 147  
          * @return the permissions
 148  
          */
 149  
         public Set<Permission> getPermissions() {
 150  1325
                 return this.permissions;
 151  
         }
 152  
 
 153  
         /**
 154  
          * Sets the capability's permissions. This will usually be called by the persistence layer. 
 155  
          * 
 156  
          * @param permissions the set of permissions
 157  
          */
 158  
 
 159  
         protected void setPermissions(Set<Permission> permissions) {
 160  12
                 this.permissions = permissions;
 161  12
         }
 162  
 
 163  
         /**
 164  
          * Gets the capability's category. 
 165  
          * 
 166  
          * @return the category
 167  
          */
 168  
         public Category getCategory() {
 169  199
                 return this.category;
 170  
         }
 171  
 
 172  
         /**
 173  
          * Sets the capability's category. This will usually be called by the persistence layer. 
 174  
          * 
 175  
          * @param category the category
 176  
          */
 177  
         protected void setCategory(Category category) {
 178  8
                 this.category = category;
 179  8
         }
 180  
 
 181  
         /**
 182  
          * Adds a permission to this capability. The argument must be a non-null, persistent instance.
 183  
          * 
 184  
          * @param permission a permission to add.
 185  
          * @see #getPermissions()
 186  
          * @see #remove(Permission)
 187  
          * @see #addAll(Collection)
 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  
          * Removes a permission from this capability.
 199  
          * 
 200  
          * @param permission a permission to remove.
 201  
          * 
 202  
          * @see #getPermissions()
 203  
          * @see #add(Permission)
 204  
          * @see #removeAll(Collection)
 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  
                 // Note: subject is never null
 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  
          * Adds several permissions to this capability.
 281  
          * 
 282  
          * @param perms a list of permissions
 283  
          * @see #add(Permission)
 284  
          * @see #removeAll(Collection)
 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  
          * Removes several permissions from this capability.
 294  
          * 
 295  
          * @param perms a list of permissions
 296  
          * @see #remove(Permission)
 297  
          * @see #addAll(Collection)
 298  
          * @see #clear()
 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  
          * Clears this Capability's permission set.
 308  
          * @see #getPermissions()
 309  
          */
 310  
         public void clear() {
 311  16
                 this.getPermissions().clear();
 312  16
         }
 313  
 
 314  
         /**
 315  
          * Creates a new, transient Capability with the given subject and this capability's secure 
 316  
          * object (if any), category (if any) and permissions (if any).
 317  
          * 
 318  
          * @param otherSubject the new subject
 319  
          * @return a new Capability
 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  
          * Creates a new, transient Capability with the given object and this capability's subject
 335  
          * and permissions (if any).
 336  
          * 
 337  
          * @param otherObject the new subject
 338  
          * @return a new Capability
 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  
          * Creates a new, transient Capability with the given category and this capability's subject
 348  
          * and permissions (if any).
 349  
          * 
 350  
          * @param otherCategory the new subject
 351  
          * @return a new Capability
 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  
 }