Coverage Report - org.openpermis.editor.policy.presenter.PolicyRoleRefPool
 
Classes in this File Line Coverage Branch Coverage Complexity
PolicyRoleRefPool
57%
11/19
20%
2/10
2.25
 
 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.presenter;
 11  
 
 12  
 import static org.jdesktop.observablecollections.ObservableCollections.observableList;
 13  
 
 14  
 import java.util.ArrayList;
 15  
 import java.util.List;
 16  
 
 17  
 import org.jdesktop.observablecollections.ObservableList;
 18  
 import org.slf4j.Logger;
 19  
 import org.slf4j.LoggerFactory;
 20  
 
 21  
 import org.openpermis.policy.Role;
 22  
 import org.openpermis.policy.bean.PolicyBean;
 23  
 
 24  
 /**
 25  
  * Pool-Collections for a Policy.
 26  
  * @since 0.3.0
 27  
  */
 28  
 public class PolicyRoleRefPool
 29  
 {
 30  
 
 31  
         //---- Static
 32  
 
 33  
         /**
 34  
          * The logger object of this class.
 35  
          * @since 0.3.0
 36  
          */
 37  1
         private static final Logger LOGGER =
 38  
                 LoggerFactory.getLogger(PolicyRoleRefPool.class);
 39  
 
 40  
         //---- State
 41  
 
 42  
 
 43  
         /**
 44  
          * An observable list of parts.
 45  
          * @since 0.3.0
 46  
          */
 47  
         private final ObservableList<Role> poolList;
 48  
 
 49  
 
 50  
         //---- Constructors
 51  
 
 52  
         /**
 53  
          * Creates a new pool.
 54  
          * @since 0.3.0
 55  
          */
 56  
         public PolicyRoleRefPool (PolicyBean policy) {
 57  1
                 super();
 58  
 
 59  1
                 this.poolList = observableList(new ArrayList<Role>());
 60  1
                 mergePool(policy, false);
 61  1
         }
 62  
 
 63  
         //---- Methods
 64  
 
 65  
         /**
 66  
          * Merges the specified list of parts with the rules supplied.
 67  
          * @param policy the policy.
 68  
          * @param expunge indicates if unused parts are removed from the list.
 69  
          * @since 0.3.0
 70  
          */
 71  
         public void mergePool (PolicyBean policy, boolean expunge) {
 72  1
                 mergePool(policy.getRoleRefList(), expunge);
 73  1
         }
 74  
 
 75  
         /**
 76  
          * Merges the specified list of parts with the rules supplied.
 77  
          * @param addList elements to add/update in the pool.
 78  
          * @param expunge indicates if unused parts are removed from the list.
 79  
          * @since 0.3.0
 80  
          */
 81  
         public void mergePool (List<Role> addList, boolean expunge) {
 82  
                 // first get serials of all pool-parts
 83  1
                 List<Role> oldList = new ArrayList<Role>(this.poolList);
 84  
 
 85  
                 // add
 86  1
                 for (final Role roleref : addList) {
 87  0
                         if (this.poolList.contains(roleref)) {
 88  0
                                 oldList.remove(roleref);
 89  
                         } else {
 90  0
                                 LOGGER.debug(
 91  
                                         "Adding RoleRef [{}] [{}].",
 92  
                                         roleref.getRoleHierarchy().getIdentity(), roleref.getName());
 93  0
                                 this.poolList.add(roleref);
 94  
                         }
 95  
                 }
 96  
 
 97  
                 // clean rest of elements, if wished
 98  1
                 if (expunge && (oldList.size() > 0)) {
 99  0
                         for (Role role : oldList) {
 100  0
                                 LOGGER.debug(
 101  
                                         "Removing role [{}] [{}].",
 102  
                                         role.getRoleHierarchy().getIdentity(),
 103  
                                         role.getName()
 104  
                                 );
 105  0
                                 this.poolList.remove(role);
 106  
                         }
 107  
                 }
 108  1
         }
 109  
 
 110  
         /**
 111  
          * @since 0.3.0
 112  
          */
 113  
         public ObservableList<Role> getPoolList () {
 114  0
                 return this.poolList;
 115  
         }
 116  
 
 117  
 
 118  
 }