Coverage Report - org.openpermis.editor.policy.command.CollectionChangeCommand
 
Classes in this File Line Coverage Branch Coverage Complexity
CollectionChangeCommand
0%
0/17
0%
0/2
1.167
 
 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 java.util.List;
 13  
 
 14  
 import org.openpermis.editor.policy.beans.PropertyAccess;
 15  
 import org.openpermis.policy.bean.BeanCollection;
 16  
 import org.openpermis.policy.bean.PartBean;
 17  
 import org.openpermis.policy.bean.PolicyBean;
 18  
 
 19  
 
 20  
 /**
 21  
  * Command to edit a part.
 22  
  * @param <P> the current beantype
 23  
  * @since 0.1.0
 24  
  */
 25  
 public abstract class CollectionChangeCommand<P extends PartBean> extends AbstractCommand
 26  
 {
 27  
 
 28  
         //---- State
 29  
 
 30  
         /**
 31  
          * The new part.
 32  
          * @since 0.1.0
 33  
          */
 34  
         private PartBean parentBean;
 35  
 
 36  
         /**
 37  
          * The bean to be aded/removed.
 38  
          * @since 0.1.0
 39  
          */
 40  
         private P elementBean;
 41  
 
 42  
         /**
 43  
          * The collection property.
 44  
          * @since 0.1.0
 45  
          */
 46  
         private String property;
 47  
 
 48  
         //---- Constructors
 49  
 
 50  
         /**
 51  
          * Creates a new command.         *
 52  
          * @param parentBean bean holding the collection
 53  
          * @param elementBean bean to add/remove
 54  
          * @param property the property holding the collection
 55  
          * @since 0.1.0
 56  
          */
 57  
         public CollectionChangeCommand (
 58  
                 PartBean parentBean, P elementBean, String property) {
 59  0
                 super("CollectionChangeCommand");
 60  0
                 this.parentBean = parentBean;
 61  0
                 this.elementBean = elementBean;
 62  0
                 this.property = property;
 63  0
         }
 64  
 
 65  
         //---- Methods
 66  
 
 67  
 
 68  
         /**
 69  
          * @return the elementBean.
 70  
          * @since 0.1.0
 71  
          */
 72  
         public P getElementBean () {
 73  0
                 return this.elementBean;
 74  
         }
 75  
 
 76  
         /**
 77  
          * @param doExec wether to "exec" or to "undo".
 78  
          * @param list list to change.
 79  
          * @since 0.3.0
 80  
          */
 81  
         abstract void operateElement (boolean doExec, List<P> list);
 82  
 
 83  
         /**
 84  
          * @since 0.1.0
 85  
          */
 86  
         @SuppressWarnings("unchecked")
 87  
         private void changeCollection (boolean doExec) {
 88  0
                 final PropertyAccess pa = new PropertyAccess(this.parentBean);
 89  
                 
 90  0
                 if (pa.get(this.property) instanceof BeanCollection<?>) {
 91  0
                         final BeanCollection<P> oldCollection = (BeanCollection<P>) pa.get(this.property);
 92  0
                         final List<P> list = oldCollection.toList();
 93  0
                         operateElement(doExec, list);
 94  0
                         pa.set(this.property, oldCollection.create(list));
 95  
                 }
 96  0
         }
 97  
 
 98  
 
 99  
         //---- Command
 100  
 
 101  
         /**
 102  
          * @since 0.1.0
 103  
          */
 104  
         @Override
 105  
         public void execute (PolicyBean policyBean) {
 106  0
                 changeCollection(true);
 107  0
         }
 108  
 
 109  
         /**
 110  
          * @since 0.1.0
 111  
          */
 112  
         @Override
 113  
         public void undo (PolicyBean policyBean) {
 114  0
                 changeCollection(false);
 115  0
         }
 116  
 
 117  
 }