Coverage Report - org.openpermis.editor.policy.presenter.RoleAssignmentRulePresenter
 
Classes in this File Line Coverage Branch Coverage Complexity
RoleAssignmentRulePresenter
0%
0/105
0%
0/20
1.367
RoleAssignmentRulePresenter$1
0%
0/3
N/A
1.367
 
 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.net.URI;
 13  
 import java.util.List;
 14  
 
 15  
 import org.jdesktop.observablecollections.ObservableList;
 16  
 import org.jdesktop.observablecollections.ObservableListListener;
 17  
 import org.slf4j.Logger;
 18  
 import org.slf4j.LoggerFactory;
 19  
 
 20  
 import org.openpermis.basic.TimePeriodConstraint;
 21  
 import org.openpermis.editor.policy.beans.PropertyChange;
 22  
 import org.openpermis.editor.policy.command.AuthorityRemoveCommand;
 23  
 import org.openpermis.editor.policy.command.CompositeCommand;
 24  
 import org.openpermis.editor.policy.command.PoolRemoveCommand;
 25  
 import org.openpermis.editor.policy.command.SubjectDomainRemoveCommand;
 26  
 import org.openpermis.editor.policy.gui.binding.ObservableListAdapter;
 27  
 import org.openpermis.editor.policy.view.EditPartCommand;
 28  
 import org.openpermis.policy.Identifiable;
 29  
 import org.openpermis.policy.Role;
 30  
 import org.openpermis.policy.bean.AuthorityBean;
 31  
 import org.openpermis.policy.bean.DomainBean;
 32  
 import org.openpermis.policy.bean.RoleAssignmentRuleBean;
 33  
 import org.openpermis.policy.bean.RoleCollection;
 34  
 
 35  
 /**
 36  
  * Presenter for a single {@link RoleAssignmentRuleBean}.
 37  
  * @since 0.3.0
 38  
  */
 39  0
 public class RoleAssignmentRulePresenter
 40  
         extends PartPresenter<RoleAssignmentRuleBean>
 41  
 {
 42  
 
 43  
         //---- Static
 44  
 
 45  
         /**
 46  
          * @since 0.3.0
 47  
          */
 48  0
         private static final Logger LOGGER =
 49  
                 LoggerFactory.getLogger(RoleAssignmentRulePresenter.class);
 50  
 
 51  
         //---- State
 52  
 
 53  
         /**
 54  
          * The currently active authority.
 55  
          * @since 0.3.0
 56  
          */
 57  
         private AuthorityBean activeAuthority;
 58  
 
 59  
         /**
 60  
          * The currently active subject domain.
 61  
          * @since 0.3.0
 62  
          */
 63  
         private DomainBean activeSubjectDomain;
 64  
 
 65  
         /**
 66  
          * @since 0.3.0
 67  
          */
 68  
         private final ObservableList<Role> roles;
 69  
 
 70  
         /**
 71  
          * @since 0.3.0
 72  
          */
 73  
         private final ObservableListListener rolesUpdater;
 74  
         
 75  
         /**
 76  
          * @since 0.3.0
 77  
          */
 78  
         private int delegationDepth;
 79  
 
 80  
         /**
 81  
          * @since 0.3.0
 82  
          */
 83  
         private TimePeriodConstraint constraint;
 84  
 
 85  
         //---- Constructors
 86  
 
 87  
         /**
 88  
          * Creates a new role assignment rule presenter.
 89  
          * @param model the {@link RoleAssignmentRuleBean} to work on, must not be {@code null}.
 90  
          * @param context policy context that provides additional information.
 91  
          * @since 0.3.0
 92  
          */
 93  
         public RoleAssignmentRulePresenter (
 94  
                 RoleAssignmentRuleBean model, PolicyContext context
 95  
         ) {
 96  
 
 97  0
                 super(model, context);
 98  0
                 LOGGER.debug("Presenter for [{}].", model);
 99  
 
 100  0
                 this.roles = createRoleList(model);
 101  0
                 this.rolesUpdater = new ObservableListAdapter<Role>() {
 102  
                         @Override
 103  0
                         protected void listChanged (ObservableList<Role> list) {
 104  0
                                 updateRolesAtModel();
 105  0
                         }
 106  
                 };
 107  0
                 this.roles.addObservableListListener(this.rolesUpdater);
 108  
 
 109  0
                 updateAuthority();
 110  0
                 updateDelegationDepth();
 111  0
                 updateResourceDomain();
 112  0
                 updateResourceDomain();
 113  0
                 updateConstraint();
 114  0
         }
 115  
 
 116  
         /**
 117  
          * Returns the active subject domain of this model.
 118  
          * @return the active subject domain of this model.
 119  
          * @since new
 120  
          */
 121  
         public DomainBean getActiveSubjectDomain () {
 122  0
                 return this.activeSubjectDomain;
 123  
         }
 124  
 
 125  
         /**
 126  
          * Sets the active subject domain of the target of this model.
 127  
          * @param activeSubjectDomainBean the new resource domain.
 128  
          * @since 0.3.0
 129  
          */
 130  
         public void setActiveResourceDomain (DomainBean activeSubjectDomainBean) {
 131  0
                 if (activeSubjectDomainBean != null) {
 132  0
                         LOGGER.debug("setActiveSubjectDomain [{}].", activeSubjectDomainBean);
 133  0
                         final DomainBean current = getModel().getSubjectDomain();
 134  0
                         if (sameSerial(current, activeSubjectDomainBean)) {
 135  0
                                 updateActiveSubjectDomain(activeSubjectDomainBean);
 136  
                         } else {
 137  0
                                 change(getModel(), "subjectDomain", activeSubjectDomainBean);
 138  
                         }
 139  
                 }
 140  0
         }
 141  
         
 142  
         /**
 143  
          * Forwards model changes to the listeners of this presenter.
 144  
          * @param activeSubjectDomainBean the active resource domain to set.
 145  
          * @since 0.3.0
 146  
          */
 147  
         private void updateActiveSubjectDomain (DomainBean activeSubjectDomainBean) {
 148  0
                 final DomainBean oldValue = this.activeSubjectDomain;
 149  0
                 this.activeSubjectDomain = activeSubjectDomainBean;
 150  0
                 firePropertyChange("activeSubjectDomain", oldValue, this.activeSubjectDomain);
 151  0
         }
 152  
 
 153  
         /**
 154  
          * Removes the active subject domain in the whole policy.
 155  
          * @since 0.3.0
 156  
          */
 157  
         public void removeActiveSubjectDomain (DomainBean subjectDomain) {
 158  0
                 execute(
 159  
                         new CompositeCommand(
 160  
                                 new SubjectDomainRemoveCommand(subjectDomain),
 161  
                                 new PoolRemoveCommand<DomainBean>(
 162  
                                         this.getResourceDomainPool(), subjectDomain)
 163  
                         )
 164  
                 );
 165  0
         }
 166  
         
 167  
         /**
 168  
          * Adds a subject domain.
 169  
          * @param editPartCommand adding a domain implies opening an editor.
 170  
          * @since 0.3.0
 171  
          */
 172  
         public void addSubjectDomain (EditPartCommand<DomainBean> editPartCommand) {
 173  0
                 final DomainBean domain = getContext().getPartBeanFactory().createDomain(null);
 174  0
                 editPartCommand.setPart(domain);
 175  0
                 execute(
 176  
                         new CompositeCommand(
 177  
                                 createChangeCommand(getModel(), "subjectDomain", domain),
 178  
                                 editPartCommand
 179  
                         )
 180  
                 );
 181  0
         }
 182  
 
 183  
         /**
 184  
          * Returns the active authority of this model.
 185  
          * @return the active authority of this model.
 186  
          * @since new
 187  
          */
 188  
         public AuthorityBean getActiveAuthority () {
 189  0
                 return this.activeAuthority;
 190  
         }
 191  
 
 192  
         /**
 193  
          * Sets the active authority of the target of this model.
 194  
          * @param newActiveAuthority the new authority.
 195  
          * @since 0.3.0
 196  
          */
 197  
         public void setActiveAuthority (AuthorityBean newActiveAuthority) {
 198  0
                 if (newActiveAuthority != null) {
 199  0
                         LOGGER.debug("setActiveAuthority [{}].", newActiveAuthority);
 200  0
                         final AuthorityBean current = getModel().getAuthority();
 201  0
                         if (sameSerial(current, newActiveAuthority)) {
 202  0
                                 updateActiveAuthority(newActiveAuthority);
 203  
                         } else {
 204  0
                                 change(getModel(), "authority", newActiveAuthority);
 205  
                         }
 206  
                 }
 207  0
         }
 208  
         
 209  
         /**
 210  
          * Forwards model changes to the listeners of this presenter.
 211  
          * @param newActiveAuthority the active authority.
 212  
          * @since 0.3.0
 213  
          */
 214  
         private void updateActiveAuthority (AuthorityBean newActiveAuthority) {
 215  0
                 final AuthorityBean oldValue = this.activeAuthority;
 216  0
                 this.activeAuthority = newActiveAuthority;
 217  0
                 firePropertyChange("activeAuthority", oldValue, this.activeAuthority);
 218  0
         }
 219  
 
 220  
         /**
 221  
          * Removes the active authority in the whole policy.
 222  
          * @since 0.3.0
 223  
          */
 224  
         public void removeActiveAuthority (AuthorityBean authority) {
 225  0
                 execute(
 226  
                         new CompositeCommand(
 227  
                                 new AuthorityRemoveCommand(authority),
 228  
                                 new PoolRemoveCommand<AuthorityBean>(
 229  
                                         this.getAuthorityPool(), authority)
 230  
                         )
 231  
                 );
 232  0
         }
 233  
         
 234  
         /**
 235  
          * Adds an authority.
 236  
          * @param editPartCommand adding an authority implies opening an editor.
 237  
          * @since 0.3.0
 238  
          */
 239  
         public void addAuthority (EditPartCommand<AuthorityBean> editPartCommand) {
 240  0
                 final AuthorityBean authority = getContext().getPartBeanFactory().createAuthority(null);
 241  0
                 editPartCommand.setPart(authority);
 242  0
                 execute(
 243  
                         new CompositeCommand(
 244  
                                 createChangeCommand(getModel(), "authority", authority),
 245  
                                 editPartCommand
 246  
                         )
 247  
                 );
 248  0
         }
 249  
         
 250  
 
 251  
         /**
 252  
          * Returns the identity string representation of the specified identifiable.
 253  
          * @param identifiable the identifiable for which to retrieve the identity.
 254  
          * @return the identity string or an empty string if undefined.
 255  
          * @since 0.3.0
 256  
          */
 257  
         private String getIdentity (Identifiable identifiable) {
 258  0
                 if (identifiable != null) {
 259  0
                         final URI identity = identifiable.getIdentity();
 260  0
                         if (identity != null) {
 261  0
                                 return identity.toString();
 262  
                         }
 263  
                 }
 264  0
                 return "";
 265  
         }
 266  
 
 267  
         /**
 268  
          * Creates the initial role list for the specified model.
 269  
          * @param model the model to create the list for.
 270  
          * @return the list, never {@code null}.
 271  
          * @since 0.3.0
 272  
          */
 273  
         private ObservableList<Role> createRoleList (RoleAssignmentRuleBean model) {
 274  0
                 return createCollectionAtPresenter(model.getRoles());
 275  
         }
 276  
 
 277  
 
 278  
         /**
 279  
          * Writes the roles in the presenter to the model.
 280  
          * @since 0.3.0
 281  
          */
 282  
         private void updateRolesAtModel () {
 283  0
                 LOGGER.debug("updateRolesAtModel");
 284  0
                 change("roles", RoleCollection.create(this.roles));
 285  0
         }
 286  
 
 287  
         /**
 288  
          * Writes the roles in the presenter according to the model.
 289  
          * @since 0.3.0
 290  
          */
 291  
         private void updateRolesAtPresenter () {
 292  0
                 LOGGER.debug("updateRolesAtPresenter");
 293  0
                 this.roles.removeObservableListListener(this.rolesUpdater);
 294  0
                 updateAddedRolesAtPresenter(getModel().getRoles(), this.roles);
 295  0
                 updateDeletedRolesAtPresenter(getModel().getRoles(), this.roles);
 296  0
                 this.roles.addObservableListListener(this.rolesUpdater);
 297  0
         }
 298  
         
 299  
         /**
 300  
          * @since 0.3.0
 301  
          */
 302  
         private void updateAddedRolesAtPresenter (
 303  
                 RoleCollection modelRoles, ObservableList<Role> list) {
 304  0
                 for (Role role : modelRoles) {
 305  0
                         if (!list.contains(role)) {
 306  0
                                 list.add(role);
 307  
                         }
 308  
                 }
 309  0
         }
 310  
 
 311  
         /**
 312  
          * @since 0.3.0
 313  
          */
 314  
         private void updateDeletedRolesAtPresenter (RoleCollection modelRoles, ObservableList<Role> list
 315  
         ) {
 316  0
                 final List<Role> roleList = modelRoles.toList();
 317  0
                 for (Role role : list) {
 318  0
                         if (!roleList.contains(role)) {
 319  0
                                 list.remove(role);
 320  
                         }
 321  
                 }
 322  0
         }
 323  
         
 324  
         /**
 325  
          * The list of roles of this {@link RoleAssignmentRuleBean}.
 326  
          * @return the list of roles of this  {@link RoleAssignmentRuleBean}.
 327  
          * @since 0.3.0
 328  
          */
 329  
         public ObservableList<Role> getRoles () {
 330  0
                 return this.roles;
 331  
         }
 332  
         
 333  
         /**
 334  
          * Updates the delegation depth in the model.
 335  
          * @param newDelegationDepth the new delegation depth.
 336  
          * @since 0.3.0
 337  
          */
 338  
         public void updateDelegationDepthAtModel (int newDelegationDepth) {
 339  0
                 change("delegationDepth", Integer.valueOf(newDelegationDepth));
 340  0
         }
 341  
 
 342  
         /**
 343  
          * Gets the delegation depth.
 344  
          * @return  the delegation depth.
 345  
          * @since 0.3.0
 346  
          */
 347  
         public int getDelegationDepth () {
 348  0
                 return this.delegationDepth;
 349  
         }
 350  
 
 351  
         /**
 352  
          * Sets the delegation depth.
 353  
          * @param newDelegationDepth the delegation depth.
 354  
          * @since 0.3.0
 355  
          */
 356  
         public void updateDelegationDepth (int newDelegationDepth) {
 357  0
                 final int oldValue = this.delegationDepth;
 358  0
                 this.delegationDepth = newDelegationDepth;
 359  0
                 firePropertyChange(
 360  
                         "delegationDepth", Integer.valueOf(oldValue), Integer.valueOf(this.delegationDepth)
 361  
                 );
 362  0
         }
 363  
 
 364  
         /**
 365  
          * @since 0.3.0
 366  
          */
 367  
         public TimePeriodConstraint getConstraint () {
 368  0
                 return this.constraint;
 369  
         }
 370  
 
 371  
         /**
 372  
          * Updates the constraint in the model.
 373  
          * @param newConstraint the constraint.
 374  
          * @since 0.3.0
 375  
          */
 376  
         public void updateConstraintAtModel (TimePeriodConstraint newConstraint) {
 377  0
                 change("constraint", newConstraint);
 378  0
         }
 379  
 
 380  
         /**
 381  
          * Sets the constraint.
 382  
          * @param newConstraint the constraint.
 383  
          * @since 0.3.0
 384  
          */
 385  
         public void updateConstraint (TimePeriodConstraint newConstraint) {
 386  0
                 final TimePeriodConstraint oldValue = this.constraint;
 387  0
                 this.constraint = newConstraint;
 388  0
                 firePropertyChange(
 389  
                         "constraint", oldValue, this.constraint
 390  
                 );
 391  0
         }
 392  
         
 393  
         //---- PropertyChange
 394  
 
 395  
         /**
 396  
          * Handler for authority domain changes in a target.
 397  
          * @since 0.3.0
 398  
          */
 399  
         @PropertyChange(bean = RoleAssignmentRuleBean.class, property = "authority")
 400  
         public void updateAuthority () {
 401  0
                 final AuthorityBean authority = getModel().getAuthority();
 402  0
                 LOGGER.debug("updateAuthority({}).", authority);
 403  0
                 updateActiveAuthority(authority);
 404  0
                 setTitleParameters(getIdentity(authority));
 405  0
         }
 406  
         
 407  
         /**
 408  
          * Handler for subject domain changes in a target.
 409  
          * @since 0.3.0
 410  
          */
 411  
         @PropertyChange(bean = RoleAssignmentRuleBean.class, property = "subjectDomain")
 412  
         public void updateResourceDomain () {
 413  0
                 final DomainBean domain = getModel().getSubjectDomain();
 414  0
                 LOGGER.debug("updateSubjectDomain({}).", domain);
 415  0
                 updateActiveSubjectDomain(domain);
 416  0
                 setTitleParameters(getIdentity(domain));
 417  0
         }
 418  
         
 419  
         /**
 420  
          * Handler for changes in the roles of a role assignment rule bean.
 421  
          * @since new
 422  
          */
 423  
         @PropertyChange(bean = RoleAssignmentRuleBean.class, property = "roles")
 424  
         public void updateRoles () {
 425  0
                 LOGGER.debug("updateRoles");
 426  0
                 updateRolesAtPresenter();
 427  0
         }
 428  
 
 429  
         /**
 430  
          * Handler for changes in the delegationDepth of a {@link RoleAssignmentRuleBean} bean.
 431  
          * @since 0.3.0
 432  
          */
 433  
         @PropertyChange(bean = RoleAssignmentRuleBean.class, property = "delegationDepth")
 434  
         public void updateDelegationDepth () {
 435  0
                 this.updateDelegationDepth(getModel().getDelegationDepth());
 436  0
         }
 437  
 
 438  
         /**
 439  
          * Handler for changes in the {@link TimePeriodConstraint} of a {@link RoleAssignmentRuleBean}
 440  
          * bean.
 441  
          * @since 0.3.0
 442  
          */
 443  
         @PropertyChange(bean = RoleAssignmentRuleBean.class, property = "constraint")
 444  
         public void updateConstraint () {
 445  0
                 this.updateConstraint(getModel().getConstraint());
 446  0
         }
 447  
 
 448  
 }