Coverage Report - org.talos.model.GroupPermission
 
Classes in this File Line Coverage Branch Coverage Complexity
GroupPermission
100%
29/29
100%
4/4
0
 
 1  
 package org.talos.model;
 2  
 
 3  
 import java.util.ArrayList;
 4  
 import java.util.Collection;
 5  
 import java.util.HashSet;
 6  
 import java.util.Set;
 7  
 
 8  
 import javax.persistence.DiscriminatorValue;
 9  
 import javax.persistence.Entity;
 10  
 import javax.persistence.ManyToMany;
 11  
 
 12  
 /**
 13  
  * This class represents the permission to perform a group of actions. A
 14  
  * GroupPermission can imply many different SimplePermissions. For instance, if a group permission called
 15  
  * "modify" has simple permissions "add" and "remove", then a subject which is granted "modify" automatically
 16  
  * becomes capable of "add" and "remove".
 17  
  * 
 18  
  * @see #getAllowedPermissions()
 19  
  */
 20  
 @Entity
 21  
 @DiscriminatorValue("Group")
 22  
 public class GroupPermission extends Permission implements NamedObject {
 23  
 
 24  368
         @ManyToMany
 25  
         private Set<SimplePermission> simplePermissions = new HashSet<SimplePermission>();
 26  
 
 27  
         /**
 28  
          * Empty constructor needed by Hibernate.
 29  
          * @see #GroupPermission(String) 
 30  
          */
 31  83
         protected GroupPermission() {
 32  
                 // empty constructor needed by Hibernate.
 33  83
         }
 34  
 
 35  
         /**
 36  
          * Constructor with a name
 37  
          * 
 38  
          * @param name a non-null string
 39  
          */
 40  
         public GroupPermission(String name) {
 41  287
                 super(name);
 42  285
         }
 43  
 
 44  
         /**
 45  
          * Gets the simple permissions contained by this group permission.
 46  
          * 
 47  
          * @return the set of simple permissions contained by this group
 48  
          * @see #getAllowedPermissions()
 49  
          */
 50  
         protected Set<SimplePermission> getSimplePermissions() {
 51  674
                 return simplePermissions;
 52  
         }
 53  
 
 54  
         /**
 55  
          * Sets the simple permissions contained by this group permission. This method is usually called
 56  
          * only by the persistence layer.
 57  
          * 
 58  
          * @param simplePermissions a set of simple permissions
 59  
          */
 60  
         protected void setSimplePermissions(Set<SimplePermission> simplePermissions) {
 61  1
                 this.simplePermissions = simplePermissions;
 62  1
         }
 63  
 
 64  
         /**
 65  
          * Adds a simple permission to this group permission. Must be a non-null, persistent 
 66  
          * SimplePermission instance.
 67  
          * 
 68  
          * @param simplePermission the simple permission to add to this group.
 69  
          * 
 70  
          * @see SimplePermission#add(GroupPermission)
 71  
          * @see #remove(SimplePermission)
 72  
          * @see #addAll(Collection)
 73  
          * @see #getAllowedPermissions()
 74  
          */
 75  
         public void add(SimplePermission simplePermission) {
 76  404
                 if (simplePermission == null) {
 77  1
                         throw new IllegalArgumentException("simplePermission can't be null");
 78  
                 }
 79  403
                 this.getSimplePermissions().add(simplePermission);
 80  403
                 simplePermission.add(this);
 81  403
         }
 82  
 
 83  
         /**
 84  
          * Removes a simple permission from this group permission. Must be a non-null, persistent 
 85  
          * SimplePermission instance.
 86  
          * 
 87  
          * @param simplePermission the simple permission to remove from this group.
 88  
          * 
 89  
          * @see SimplePermission#remove(GroupPermission)
 90  
          * @see #add(SimplePermission)
 91  
          * @see #getAllowedPermissions()
 92  
          */
 93  
         public void remove(SimplePermission simplePermission) {
 94  52
                 if (simplePermission == null) {
 95  1
                         throw new IllegalArgumentException("simplePermission can't be null");
 96  
                 }
 97  51
                 this.getSimplePermissions().remove(simplePermission);
 98  51
                 simplePermission.remove(this);
 99  51
         }
 100  
 
 101  
         /**
 102  
          * Gets all permissions allowed by this permission. This includes the group permission
 103  
          * itself plus all simple permissions it contains.
 104  
          * @return a collection of permissions.
 105  
          */
 106  
         @Override
 107  
         public Collection<Permission> getAllowedPermissions() {
 108  18
                 Collection<Permission> result = new ArrayList<Permission>(this.getSimplePermissions());
 109  18
                 result.add(this);
 110  18
                 return result;
 111  
         }
 112  
 
 113  
         /**
 114  
          * Gets the type of permission. 
 115  
          * @return always returns "GroupPermission"
 116  
          */
 117  
         @Override
 118  
         public String getType() {
 119  2386
                 return "GroupPermission";
 120  
         }
 121  
 
 122  
         @Override
 123  
         public Permission copy(String name) {
 124  2
                 GroupPermission result = new GroupPermission(name);
 125  2
                 for (SimplePermission simple : this.getSimplePermissions()) {
 126  6
                         result.add(simple);
 127  
                 }
 128  2
                 return result;
 129  
         }
 130  
 
 131  
         /**
 132  
          * Adds several simple permissions to this group.
 133  
          * 
 134  
          * @param perms a list of simple permissions
 135  
          * @see #add(SimplePermission)
 136  
          * @see #getAllowedPermissions()
 137  
          */
 138  
         public void addAll(Collection<? extends SimplePermission> perms) {
 139  104
                 for (SimplePermission p : perms) {
 140  267
                         this.add(p);
 141  
                 }
 142  104
         }
 143  
 }