Coverage Report - org.openpermis.editor.policy.presenter.TargetAccessRulePresenter
 
Classes in this File Line Coverage Branch Coverage Complexity
TargetAccessRulePresenter
0%
0/88
0%
0/16
1.276
TargetAccessRulePresenter$1
0%
0/3
N/A
1.276
TargetAccessRulePresenter$2
0%
0/3
N/A
1.276
TargetAccessRulePresenter$3
0%
0/3
N/A
1.276
 
 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 java.util.ArrayList;
 13  
 import java.util.List;
 14  
 
 15  
 import org.jdesktop.observablecollections.ObservableCollections;
 16  
 import org.jdesktop.observablecollections.ObservableList;
 17  
 import org.jdesktop.observablecollections.ObservableListListener;
 18  
 import org.slf4j.Logger;
 19  
 import org.slf4j.LoggerFactory;
 20  
 
 21  
 import org.openpermis.editor.policy.beans.PropertyChange;
 22  
 import org.openpermis.editor.policy.command.CollectionAddCommand;
 23  
 import org.openpermis.editor.policy.command.CompositeCommand;
 24  
 import org.openpermis.editor.policy.command.ObligationRemoveCommand;
 25  
 import org.openpermis.editor.policy.command.PoolRemoveCommand;
 26  
 import org.openpermis.editor.policy.command.TargetRemoveCommand;
 27  
 import org.openpermis.editor.policy.gui.binding.ObservableListAdapter;
 28  
 import org.openpermis.editor.policy.view.EditPartCommand;
 29  
 import org.openpermis.policy.Predicate;
 30  
 import org.openpermis.policy.Role;
 31  
 import org.openpermis.policy.bean.ObligationBean;
 32  
 import org.openpermis.policy.bean.RoleCollection;
 33  
 import org.openpermis.policy.bean.TargetAccessRuleBean;
 34  
 import org.openpermis.policy.bean.TargetBean;
 35  
 
 36  
 /**
 37  
  * Presenter for a single {@link TargetAccessRuleBean}.
 38  
  * @since 0.1.0
 39  
  */
 40  0
 public class TargetAccessRulePresenter
 41  
         extends PartPresenter<TargetAccessRuleBean>
 42  
 {
 43  
 
 44  
         //---- Static
 45  
 
 46  
         /**
 47  
          * The logger object of this class.
 48  
          * @since 0.1.0
 49  
          */
 50  0
         private static final Logger LOGGER =
 51  
                 LoggerFactory.getLogger(TargetAccessRulePresenter.class);
 52  
 
 53  
         //---- State
 54  
 
 55  
         /**
 56  
          * The targets of this targetaccessrule-view as observable collection.
 57  
          * @since 0.1.0
 58  
          */
 59  
         private final ObservableList<TargetBean> targets;
 60  
         
 61  
         /**
 62  
          * The obligations of this targetaccessrule-view as observable collection.
 63  
          * @since 0.3.0
 64  
          */
 65  
         private final ObservableList<ObligationBean> obligations;
 66  
 
 67  
         /**
 68  
          * The roles of this targetaccessrule-view as observable collection.
 69  
          * @since 0.1.0
 70  
          */
 71  
         private final ObservableList<Role> roles;
 72  
 
 73  
         /**
 74  
          * Updater that keeps the model in sync with the observable roles list.
 75  
          * @since 0.1.0
 76  
          */
 77  
         private final ObservableListListener rolesUpdater;
 78  
 
 79  
         /**
 80  
          * Updater that keeps the model in sync with the observable targets list.
 81  
          * @since 0.1.0
 82  
          */
 83  
         private final ObservableListListener targetsUpdater;
 84  
         
 85  
         /**
 86  
          * Updater that keeps the model in sync with the observable obligation list.
 87  
          * @since 0.3.0
 88  
          */
 89  
         private final ObservableListListener obligationsUpdater;
 90  
 
 91  
         /**
 92  
          * This target access rule's condition.
 93  
          * @since 0.3.0
 94  
          */
 95  
         private Predicate condition;
 96  
 
 97  
         //---- Constructors
 98  
 
 99  
         /**
 100  
          * Creates a new target access rule presenter.
 101  
          * @param model the target access rule to work on, must not be {@code null}.
 102  
          * @param context policy context that provides additional information.         *
 103  
          * @since 0.1.0
 104  
          */
 105  
         public TargetAccessRulePresenter (
 106  
                 TargetAccessRuleBean model, PolicyContext context
 107  
         ) {
 108  
 
 109  0
                 super(model, context);
 110  0
                 LOGGER.debug("Presenter for [{}].", model);
 111  
 
 112  0
                 this.targets = createTargetList(model);
 113  0
                 this.targetsUpdater = new ObservableListAdapter<TargetBean>() {
 114  
                         @Override
 115  0
                         protected void listChanged (ObservableList<TargetBean> list) {
 116  0
                                 updateTargetsAtModel();
 117  0
                         }
 118  
                 };
 119  0
                 this.targets.addObservableListListener(this.targetsUpdater);
 120  
                 
 121  0
                 this.obligations = createObligationList(model);
 122  0
                 this.obligationsUpdater = new ObservableListAdapter<ObligationBean>() {
 123  
                         @Override
 124  0
                         protected void listChanged (ObservableList<ObligationBean> list) {
 125  0
                                 updateObligationsAtModel();
 126  0
                         }
 127  
                 };
 128  0
                 this.obligations.addObservableListListener(this.obligationsUpdater);
 129  
 
 130  0
                 this.roles = createRoleList(model);
 131  0
                 this.rolesUpdater = new ObservableListAdapter<Role>() {
 132  
                         @Override
 133  0
                         protected void listChanged (ObservableList<Role> list) {
 134  0
                                 updateRolesAtModel();
 135  0
                         }
 136  
                 };
 137  0
                 this.roles.addObservableListListener(this.rolesUpdater);
 138  0
                 updateConditionAtPresenter();
 139  0
         }
 140  
 
 141  
         //---- Methods
 142  
 
 143  
         /**
 144  
          * Returns the condition.
 145  
          * @return the returned condition.
 146  
          * @since 0.3.0
 147  
          */
 148  
         public Predicate getCondition () {
 149  0
                 return this.condition;
 150  
         }
 151  
         
 152  
         /**
 153  
          * Creates the initial role list for the specified model.
 154  
          * @param model the model to create the list for.
 155  
          * @return the list, never {@code null}.
 156  
          * @since 0.1.0
 157  
          */
 158  
         private ObservableList<Role> createRoleList (TargetAccessRuleBean model) {
 159  0
                 return createCollectionAtPresenter(model.getRoles());
 160  
         }
 161  
 
 162  
         /**
 163  
          * Creates the initial target list for the specified model.
 164  
          * @param model the model to create the list for.
 165  
          * @return the list, never {@code null}.
 166  
          * @since 0.1.0
 167  
          */
 168  
         private ObservableList<TargetBean> createTargetList (TargetAccessRuleBean model) {
 169  0
                 final List<TargetBean> result = new ArrayList<TargetBean>();
 170  0
                 for (TargetBean target : model.getTargets()) {
 171  0
                         result.add(target);
 172  
                 }
 173  0
                 return ObservableCollections.observableList(result);
 174  
         }
 175  
         
 176  
         /**
 177  
          * Creates the initial obligation list for the specified model.
 178  
          * @param model the model to create the list for.
 179  
          * @return the list, never {@code null}.
 180  
          * @since 0.3.0
 181  
          */
 182  
         private ObservableList<ObligationBean> createObligationList (TargetAccessRuleBean model) {
 183  0
                 final List<ObligationBean> result = new ArrayList<ObligationBean>();
 184  0
                 for (ObligationBean obligation : model.getObligations()) {
 185  0
                         result.add(obligation);
 186  
                 }
 187  0
                 return ObservableCollections.observableList(result);
 188  
         }
 189  
 
 190  
         /**
 191  
          * Writes the roles in the presenter to the model.
 192  
          * @since 0.1.0
 193  
          */
 194  
         private void updateRolesAtModel () {
 195  0
                 LOGGER.debug("updateRolesAtModel");
 196  0
                 change("roles", RoleCollection.create(this.roles));
 197  0
         }
 198  
 
 199  
         /**
 200  
          * Writes the targets in the presenter to the model.
 201  
          * @since 0.1.0
 202  
          */
 203  
         private void updateTargetsAtModel () {
 204  0
                 LOGGER.debug("updateRoles");
 205  0
                 change("targets", getPartBeanFactory().createTargetCollection(this.targets));
 206  0
         }
 207  
 
 208  
         /**
 209  
          * Writes the obligations in the presenter to the model.
 210  
          * @since 0.3.0
 211  
          */
 212  
         private void updateObligationsAtModel () {
 213  0
                 LOGGER.debug("updateObligations");
 214  0
                 change("obligations", getPartBeanFactory().createObligationCollection(this.obligations));
 215  0
         }
 216  
 
 217  
         /**
 218  
          * Writes the roles in the presenter according to the model.
 219  
          * @since 0.1.0
 220  
          */
 221  
         private void updateRolesAtPresenter () {
 222  0
                 LOGGER.debug("updateRolesAtPresenter");
 223  0
                 this.roles.removeObservableListListener(this.rolesUpdater);
 224  0
                 updateAddedRolesAtPresenter(getModel().getRoles(), this.roles);
 225  0
                 updateDeletedRolesAtPresenter(getModel().getRoles(), this.roles);
 226  0
                 this.roles.addObservableListListener(this.rolesUpdater);
 227  0
         }
 228  
 
 229  
         /**
 230  
          * Writes the condition in the presenter to the model.
 231  
          * @since 0.3.0
 232  
          */
 233  
         public void updateConditionAtModel (final Predicate predicate) {
 234  0
                 LOGGER.debug("updateCondition");
 235  0
                 change("condition", predicate);
 236  0
         }
 237  
 
 238  
         /**
 239  
          * Writes the condition in the presenter according to the model.
 240  
          * @since 0.3.0
 241  
          */
 242  
         private void updateConditionAtPresenter () {
 243  0
                 LOGGER.debug("updateConditionAtPresenter");
 244  0
                 Predicate oldCondition = this.condition;
 245  0
                 this.condition = getModel().getCondition();
 246  0
                 firePropertyChange("condition", oldCondition, this.condition);
 247  0
         }
 248  
 
 249  
         /**
 250  
          * @since 0.3.0
 251  
          */
 252  
         private void updateAddedRolesAtPresenter (
 253  
                 RoleCollection modelRoles, ObservableList<Role> list) {
 254  0
                 for (Role role : modelRoles) {
 255  0
                         if (!list.contains(role)) {
 256  0
                                 list.add(role);
 257  
                         }
 258  
                 }
 259  0
         }
 260  
 
 261  
         /**
 262  
          * @since 0.3.0
 263  
          */
 264  
         private void updateDeletedRolesAtPresenter (RoleCollection modelRoles, ObservableList<Role> list
 265  
         ) {
 266  0
                 final List<Role> roleList = modelRoles.toList();
 267  0
                 for (Role role : list) {
 268  0
                         if (!roleList.contains(role)) {
 269  0
                                 list.remove(role);
 270  
                         }
 271  
                 }
 272  0
         }
 273  
 
 274  
         /**
 275  
          * Writes the targets in the presenter according to the model.
 276  
          * @since 0.1.0
 277  
          */
 278  
         private void updateTargetsAtPresenter () {
 279  0
                 updateCollectionAtPresenter(getModel().getTargets(), this.targets, this.targetsUpdater);
 280  0
         }
 281  
         
 282  
         /**
 283  
          * Writes the obligations in the presenter according to the model.
 284  
          * @since 0.3.0
 285  
          */
 286  
         private void updateObligationsAtPresenter () {
 287  0
                 updateCollectionAtPresenter(
 288  
                         getModel().getObligations(), this.obligations, this.obligationsUpdater
 289  
                 );
 290  0
         }
 291  
 
 292  
         /**
 293  
          * The list of roles of this targetaccessrule.
 294  
          * @return the list of roles of this targetaccessrule.
 295  
          * @since 0.1.0
 296  
          */
 297  
         public ObservableList<Role> getRoles () {
 298  0
                 return this.roles;
 299  
         }
 300  
 
 301  
         /**
 302  
          * The list of targets of this targetaccessrule.
 303  
          * @return the list of target of this targetaccessrule.
 304  
          * @since 0.1.0
 305  
          */
 306  
         public ObservableList<TargetBean> getTargets () {
 307  0
                 return this.targets;
 308  
         }
 309  
         
 310  
         /**
 311  
          * The list of obligations of this targetaccessrule.
 312  
          * @return the list of obligations of this targetaccessrule.
 313  
          * @since 0.3.0
 314  
          */
 315  
         public ObservableList<ObligationBean> getObligations () {
 316  0
                 return this.obligations;
 317  
         }
 318  
 
 319  
         /**
 320  
          * Removes the active target in the whole policy.
 321  
          * @since 0.1.0
 322  
          */
 323  
         public void removeActiveTarget (TargetBean activeTarget) {
 324  0
                 if (activeTarget != null) {
 325  0
                         execute(
 326  
                                 new CompositeCommand(
 327  
                                         new TargetRemoveCommand(activeTarget),
 328  
                                         new PoolRemoveCommand<TargetBean>(this.getTargetPool(), activeTarget)
 329  
                                 )
 330  
                         );
 331  
                 }
 332  0
         }
 333  
         
 334  
         /**
 335  
          * Removes the active obligation in the whole policy.
 336  
          * @since 0.3.0
 337  
          */
 338  
         public void removeActiveObligation (ObligationBean activeObligation) {
 339  0
                 if (activeObligation != null) {
 340  0
                         execute(
 341  
                                 new CompositeCommand(
 342  
                                         new ObligationRemoveCommand(activeObligation),
 343  
                                         new PoolRemoveCommand<ObligationBean>(
 344  
                                                 this.getObligationPool(), activeObligation
 345  
                                         )
 346  
                                 )
 347  
                         );
 348  
                 }
 349  0
         }
 350  
 
 351  
         /**
 352  
          * @since 0.1.0
 353  
          */
 354  
         public void addTarget (EditPartCommand<TargetBean> editPartCommand) {
 355  0
                 final TargetBean newTarget = getContext().getPartBeanFactory().createTarget(null, null);
 356  0
                 editPartCommand.setPart(newTarget);
 357  0
                 execute(
 358  
                         new CompositeCommand(
 359  
                                 new CollectionAddCommand<TargetBean>(getModel(), newTarget, "targets"),
 360  
                                 editPartCommand
 361  
                         )
 362  
                 );
 363  0
         }
 364  
         
 365  
         /**
 366  
          * @since 0.3.0
 367  
          */
 368  
         public void addObligation (EditPartCommand<ObligationBean> editPartCommand) {
 369  0
                 final ObligationBean newObligation = getContext().getPartBeanFactory().createObligation("");
 370  0
                 editPartCommand.setPart(newObligation);
 371  0
                 execute(
 372  
                         new CompositeCommand(
 373  
                                 new CollectionAddCommand<ObligationBean>(getModel(), newObligation, "obligations"),
 374  
                                 editPartCommand
 375  
                         )
 376  
                 );
 377  0
         }
 378  
 
 379  
         //---- PropertyChange
 380  
 
 381  
         /**
 382  
          * Handler for changes in the targets of a targetaccessrule bean.
 383  
          * @since 0.1.0
 384  
          */
 385  
         @PropertyChange(bean = TargetAccessRuleBean.class, property = "targets")
 386  
         public void updateTargets () {
 387  0
                 updateTargetsAtPresenter();
 388  0
         }
 389  
         
 390  
         /**
 391  
          * Handler for changes in the obligations of a targetaccessrule bean.
 392  
          * @since 0.3.0
 393  
          */
 394  
         @PropertyChange(bean = TargetAccessRuleBean.class, property = "obligations")
 395  
         public void updateObligations () {
 396  0
                 updateObligationsAtPresenter();
 397  0
         }
 398  
 
 399  
         /**
 400  
          * Handler for changes in the roles of a targetaccessrule bean.
 401  
          * @since 0.1.0
 402  
          */
 403  
         @PropertyChange(bean = TargetAccessRuleBean.class, property = "roles")
 404  
         public void updateRoles () {
 405  0
                 LOGGER.debug("updateRoles");
 406  0
                 updateRolesAtPresenter();
 407  0
         }
 408  
 
 409  
         /**
 410  
          * Handler for changes in the condition of a targetaccessrule bean.
 411  
          * @since 0.1.0
 412  
          */
 413  
         @PropertyChange(bean = TargetAccessRuleBean.class, property = "condition")
 414  
         public void updateCondition () {
 415  0
                 LOGGER.debug("updateCondition");
 416  0
                 updateConditionAtPresenter();
 417  0
         }
 418  
 
 419  
 }