Coverage Report - org.openpermis.editor.policy.command.PropertyChangeCommand
 
Classes in this File Line Coverage Branch Coverage Complexity
PropertyChangeCommand
0%
0/22
0%
0/10
2.8
 
 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.editor.policy.command;
 11  
 
 12  
 import org.openpermis.editor.policy.beans.PropertyAccess;
 13  
 import org.openpermis.policy.bean.PolicyBean;
 14  
 
 15  
 
 16  
 /**
 17  
  * Command that performs a property change.
 18  
  * @since 0.1.0
 19  
  */
 20  
 public class PropertyChangeCommand
 21  
         extends AbstractCommand
 22  
 {
 23  
 
 24  
         //---- Static
 25  
 
 26  
         /**
 27  
          * Creates a name for the command.
 28  
          * @param bean the bean to modify.
 29  
          * @param property the property to set.
 30  
          * @return the name for the command.
 31  
          * @since 0.1.0
 32  
          */
 33  
         private static final String createCommandName (Object bean, String property) {
 34  0
                 return bean == null ? "" : bean.getClass().getSimpleName() + "." + property;
 35  
         }
 36  
 
 37  
         //---- State
 38  
 
 39  
         /**
 40  
          * The bean to modify.
 41  
          * @since 0.1.0
 42  
          */
 43  
         private final Object bean;
 44  
 
 45  
         /**
 46  
          * The property to set.
 47  
          * @since 0.1.0
 48  
          */
 49  
         private final String property;
 50  
 
 51  
         /**
 52  
          * The value to set at the bean.
 53  
          * @since 0.1.0
 54  
          */
 55  
         private final Object newValue;
 56  
 
 57  
         /**
 58  
          * The old value of the bean.
 59  
          * @since 0.1.0
 60  
          */
 61  
         private Object oldValue;
 62  
 
 63  
         //---- Constructors
 64  
 
 65  
         /**
 66  
          * Creates a property change command for the specified bean and property.
 67  
          * @param bean the bean to modify, must not be {@code null}.
 68  
          * @param property the property to set, must not be {@code null} or empty.
 69  
          * @param value the value to set at the property.
 70  
          * @since 0.1.0
 71  
          */
 72  
         public PropertyChangeCommand (Object bean, String property, Object value) {
 73  0
                 super(createCommandName(bean, property));
 74  0
                 if (bean == null) {
 75  0
                         throw new IllegalArgumentException("Bean must not be [null].");
 76  
                 }
 77  0
                 if (property == null) {
 78  0
                         throw new IllegalArgumentException("Property must not be [null].");
 79  0
                 } else if (property.length() == 0) {
 80  0
                         throw new IllegalArgumentException("Property must not be empty.");
 81  
                 }
 82  0
                 this.bean = bean;
 83  0
                 this.property = property;
 84  0
                 this.newValue = value;
 85  0
                 validate();
 86  0
         }
 87  
 
 88  
         //---- Methods
 89  
 
 90  
         /**
 91  
          * Validates that the bean specified has the given property with set and get access.
 92  
          * @throws IllegalArgumentException if the bean/property combo is invalid.
 93  
          * @since 0.1.0
 94  
          */
 95  
         private void validate () {
 96  0
                 if (!new PropertyAccess(this.bean).canGetSetProperty(this.property)) {
 97  0
                         throw new IllegalArgumentException(
 98  
                                 "Bean [" + this.bean.getClass().getName() +
 99  
                                 "] does not have property [" + this.property +
 100  
                                 "] with read/write access."
 101  
                         );
 102  
                 }
 103  0
         }
 104  
 
 105  
         //---- Command
 106  
 
 107  
         /**
 108  
          * @since 0.1.0
 109  
          */
 110  
         @Override
 111  
         public void execute (PolicyBean policy) {
 112  0
                 final PropertyAccess pa = new PropertyAccess(this.bean);
 113  0
                 this.oldValue = pa.get(this.property);
 114  0
                 pa.set(this.property, this.newValue);
 115  0
         }
 116  
 
 117  
         /**
 118  
          * @since 0.1.0
 119  
          */
 120  
         @Override
 121  
         public void undo (PolicyBean policy) {
 122  0
                 new PropertyAccess(this.bean).set(this.property, this.oldValue);
 123  0
         }
 124  
 
 125  
 }