Coverage Report - org.openpermis.policy.bean.basic.BasicTarget
 
Classes in this File Line Coverage Branch Coverage Complexity
BasicTarget
79%
39/49
90%
20/22
1.929
 
 1  
 /*
 2  
  * Copyright (c) 2009, Swiss Federal Department of Defence Civil Protection and Sport
 3  
  *                     (http://www.vbs.admin.ch)
 4  
  * Copyright (c) 2009, Ergon Informatik AG (http://www.ergon.ch)
 5  
  * All rights reserved.
 6  
  *
 7  
  * Licensed under the Open Permis License which accompanies this distribution,
 8  
  * and is available at http://www.openpermis.org/BSDlicenceKent.txt
 9  
  */
 10  
 package org.openpermis.policy.bean.basic;
 11  
 
 12  
 import static org.openpermis.policy.bean.basic.BasicUtilities.equalObjects;
 13  
 import static org.openpermis.policy.bean.basic.BasicUtilities.getIdentityDetails;
 14  
 import static org.openpermis.policy.bean.basic.BasicUtilities.getNameDetails;
 15  
 import static org.openpermis.policy.bean.basic.BasicUtilities.multiHashCode;
 16  
 
 17  
 import java.net.URI;
 18  
 import java.util.List;
 19  
 
 20  
 import org.openpermis.policy.Action;
 21  
 import org.openpermis.policy.PartProblemReporter;
 22  
 import org.openpermis.policy.PartProblemReporter.ProblemMessage;
 23  
 import org.openpermis.policy.bean.ActionBean;
 24  
 import org.openpermis.policy.bean.ActionBeanCollection;
 25  
 import org.openpermis.policy.bean.DomainBean;
 26  
 import org.openpermis.policy.bean.PartBean;
 27  
 import org.openpermis.policy.bean.SerialNumber;
 28  
 import org.openpermis.policy.bean.TargetBean;
 29  
 
 30  
 /**
 31  
  * A target combines a resource or a set of resources to be protected with a set
 32  
  * of actions that are possible on these resources.
 33  
  * @since 0.1.0
 34  
  */
 35  
 public class BasicTarget
 36  
         extends BasicPartBean
 37  
         implements TargetBean
 38  
 {
 39  
 
 40  
         //---- Static
 41  
 
 42  
         private static final long serialVersionUID = -4155346349094013032L;
 43  
 
 44  
         //---- State
 45  
 
 46  
         /**
 47  
          * The resource domain of the resources protected by this target.
 48  
          */
 49  
         private DomainBean domain;
 50  
 
 51  
         /**
 52  
          * The actions which can be performed on this target.
 53  
          */
 54  
         private ActionBeanCollection actions;
 55  
 
 56  
         //---- Constructors
 57  
 
 58  
         /**
 59  
          * Creates a new target.
 60  
          * @param serialNumber the serial number of this part.
 61  
          * @param resourceDomain the resource domain of the resources protected by this target.
 62  
          * @param actions the action collection containing the actions which can be performed on the 
 63  
          * target.
 64  
          * @since 0.3.0
 65  
          */
 66  
         protected BasicTarget (
 67  
                 SerialNumber serialNumber,
 68  
                 DomainBean resourceDomain,
 69  
                 ActionBeanCollection actions
 70  
         ) {
 71  591
                 super(TargetBean.class, serialNumber);
 72  591
                 this.actions = new BasicActionCollection(serialNumber.next());
 73  591
                 setResourceDomain(resourceDomain);
 74  591
                 setActions(actions);
 75  591
         }
 76  
 
 77  
         //---- Methods
 78  
         
 79  
         //---- Target
 80  
 
 81  
         /**
 82  
          * @since 0.1.0
 83  
          */
 84  
         public Action findAction (URI resourceUri, String actionName, List<?> arguments) {
 85  52
                 if (getResourceDomain() != null && getResourceDomain().contains(resourceUri)) {
 86  52
                         return getActions().findMatch(actionName, arguments);
 87  
                 }
 88  0
                 return null;
 89  
         }
 90  
         
 91  
         //---- TargetBean
 92  
         
 93  
         /**
 94  
          * @since 0.1.0
 95  
          */
 96  
         public DomainBean getResourceDomain () {
 97  1675
                 return this.domain;
 98  
         }
 99  
 
 100  
         /**
 101  
          * @since 0.1.0
 102  
          */
 103  
         public void setResourceDomain (DomainBean resourceDomain) {
 104  591
                 final DomainBean oldResourceDomain = getResourceDomain();
 105  591
                 this.domain = resourceDomain;
 106  591
                 firePropertyChange("resourceDomain", oldResourceDomain, getResourceDomain());
 107  591
         }
 108  
 
 109  
         /**
 110  
          * @since 0.3.0
 111  
          */
 112  
         public ActionBeanCollection getActions () {
 113  1560
                 return this.actions;
 114  
         }
 115  
 
 116  
         /**
 117  
          * @since 0.3.0
 118  
          */
 119  
         public void setActions (ActionBeanCollection actions) {
 120  593
                 final ActionBeanCollection oldActions = getActions();
 121  593
                 if (actions == null) {
 122  2
                         this.actions = new BasicActionCollection(getSerialNumber().next());
 123  
                 } else {
 124  591
                         this.actions = actions;
 125  
                 }
 126  593
                 firePropertyChange("actions", oldActions, getActions());
 127  593
         }
 128  
 
 129  
         /**
 130  
          * Remove a child element.
 131  
          * @since 0.1.0
 132  
          */
 133  
         @Deprecated
 134  
         public void removeChild (ActionBean part) {
 135  0
                 final ActionBeanCollection oldCollection = this.getActions();
 136  0
                 final List<ActionBean> list = oldCollection.toList();
 137  0
                 list.remove(part);
 138  0
                 setActions(oldCollection.create(list));
 139  0
         }
 140  
 
 141  
         //---- BasicPart
 142  
 
 143  
         /**
 144  
          * @since 0.1.0
 145  
          */
 146  
         @Override
 147  
         public boolean isPartValid (PartProblemReporter reporter) {
 148  
                 boolean valid;
 149  
 
 150  
                 // init
 151  9
                 valid = true;
 152  
 
 153  
                 // check for resourcedomain
 154  9
                 if (!isChildValid(reporter, getResourceDomain())) {
 155  1
                         valid = false;
 156  1
                         reportProblem(reporter, ProblemMessage.illegalResourceDomain);
 157  
                 }
 158  
 
 159  
                 // check for actions
 160  9
                 if (!isChildCollectionValid(reporter, getActions(), true, true, true, true, false)) {
 161  3
                         valid = false;
 162  3
                         reportProblem(reporter, ProblemMessage.illegalActions);
 163  
                 }
 164  
 
 165  
                 // return
 166  9
                 return valid;
 167  
         }
 168  
 
 169  
         /**
 170  
          * @since 0.1.0
 171  
          */
 172  
         @Override
 173  
         protected boolean comparablePart (BasicPart part) {
 174  216
                 return part instanceof TargetBean;
 175  
         }
 176  
 
 177  
         /**
 178  
          * @since 0.1.0
 179  
          */
 180  
         @Override
 181  
         protected boolean equalPart (BasicPart part) {
 182  108
                 final TargetBean target = (TargetBean) part;
 183  108
                 return
 184  
                         equalObjects(getResourceDomain(), target.getResourceDomain()) &&
 185  
                         equalObjects(getActions(), target.getActions());
 186  
         }
 187  
 
 188  
         /**
 189  
          * @since 0.1.0
 190  
          */
 191  
         @Override
 192  
         protected int partHashCode () {
 193  68
                 return multiHashCode(
 194  
                         getResourceDomain() == null ? 0 : getResourceDomain().hashCode(),
 195  
                         getActions().hashCode()
 196  
                 );
 197  
         }
 198  
 
 199  
         /**
 200  
          * @since 0.1.0
 201  
          */
 202  
         @Override
 203  
         protected String getSimpleClassName () {
 204  0
                 return TargetBean.class.getSimpleName();
 205  
         }
 206  
 
 207  
         /**
 208  
          * @since 0.1.0
 209  
          */
 210  
         @Override
 211  
         protected void appendPartDetails (StringBuilder sb) {
 212  0
                 appendDetails(sb, "resourceDomain", getIdentityDetails(getResourceDomain()));
 213  0
                 appendDetails(sb, "actions", getNameDetails(getActions()));
 214  0
         }
 215  
         
 216  
         //---- BasicPartBean
 217  
 
 218  
         /**
 219  
          * @since 0.1.0
 220  
          */
 221  
         @Override
 222  
         public PartBean findBySerialNumber (SerialNumber partSerialNumber) {
 223  5
                 PartBean bean = getResourceDomain().findBySerialNumber(partSerialNumber);
 224  5
                 if (bean != null) {
 225  1
                         return bean;
 226  
                 }
 227  
 
 228  4
                 for (final ActionBean action : getActions()) {
 229  4
                         bean = action.findBySerialNumber(partSerialNumber);
 230  4
                         if (bean != null) {
 231  1
                                 return bean;
 232  
                         }
 233  
                 }
 234  
 
 235  3
                 return super.findBySerialNumber(partSerialNumber);
 236  
         }
 237  
 
 238  
 }