Coverage Report - org.openpermis.editor.policy.presenter.TargetPresenter
 
Classes in this File Line Coverage Branch Coverage Complexity
TargetPresenter
0%
0/55
0%
0/10
1.375
TargetPresenter$1
0%
0/3
N/A
1.375
 
 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  
 
 14  
 import org.jdesktop.observablecollections.ObservableList;
 15  
 import org.jdesktop.observablecollections.ObservableListListener;
 16  
 import org.slf4j.Logger;
 17  
 import org.slf4j.LoggerFactory;
 18  
 
 19  
 import org.openpermis.editor.policy.beans.PropertyChange;
 20  
 import org.openpermis.editor.policy.command.ActionRemoveCommand;
 21  
 import org.openpermis.editor.policy.command.CollectionAddCommand;
 22  
 import org.openpermis.editor.policy.command.CommandDispatcher;
 23  
 import org.openpermis.editor.policy.command.CompositeCommand;
 24  
 import org.openpermis.editor.policy.command.PoolRemoveCommand;
 25  
 import org.openpermis.editor.policy.command.ResourceDomainRemoveCommand;
 26  
 import org.openpermis.editor.policy.gui.binding.ObservableListAdapter;
 27  
 import org.openpermis.editor.policy.view.EditPartCommand;
 28  
 import org.openpermis.policy.ParameterList;
 29  
 import org.openpermis.policy.bean.ActionBean;
 30  
 import org.openpermis.policy.bean.DomainBean;
 31  
 import org.openpermis.policy.bean.TargetBean;
 32  
 
 33  
 /**
 34  
  * Presenter for a single {@link TargetBean}.
 35  
  * @since 0.1.0
 36  
  */
 37  0
 public class TargetPresenter
 38  
         extends PartPresenter<TargetBean>
 39  
 {
 40  
 
 41  
         //---- Static
 42  
 
 43  
         /**
 44  
          * The logger object of this class.
 45  
          * @since 0.1.0
 46  
          */
 47  0
         private static final Logger LOGGER =
 48  
                 LoggerFactory.getLogger(TargetPresenter.class);
 49  
 
 50  
         //---- State
 51  
 
 52  
         /**
 53  
          * The currently active target access rule.
 54  
          * @since 0.1.0
 55  
          */
 56  
         private DomainBean activeResourceDomain;
 57  
 
 58  
         /**
 59  
          * The actions of this target as observable collection.
 60  
          * @since 0.1.0
 61  
          */
 62  
         private final ObservableList<ActionBean> actions;
 63  
 
 64  
         /**
 65  
          * Updater that keeps the model in sync with the observable actions list.
 66  
          * @since 0.1.0
 67  
          */
 68  
         private final ObservableListListener actionsUpdater;
 69  
 
 70  
         //---- Constructors
 71  
 
 72  
         /**
 73  
          * Creates a new target presenter.
 74  
          * @param model the target to work on, must not be {@code null}.
 75  
          * @param context policy context that provides additional information.
 76  
          * @param dispatcher the dispatcher for commands that change the policy.
 77  
          * @since 0.1.0
 78  
          */
 79  
         public TargetPresenter (
 80  
                 TargetBean model, CommandDispatcher dispatcher, PolicyContext context
 81  
         ) {
 82  0
                 super(model, context);
 83  0
                 LOGGER.debug("Presenter for [{}].", model);
 84  0
                 updateResourceDomain();
 85  0
                 this.actions = createActionList(model);
 86  0
                 this.actionsUpdater = new ObservableListAdapter<ActionBean>() {
 87  
                         @Override
 88  0
                         protected void listChanged (ObservableList<ActionBean> list) {
 89  0
                                 updateActionsAtModel();
 90  0
                         }
 91  
                 };
 92  0
                 this.actions.addObservableListListener(this.actionsUpdater);
 93  0
         }
 94  
 
 95  
         //---- Methods
 96  
 
 97  
         /**
 98  
          * Creates the initial action list for the specified model.
 99  
          * @param model the model to create the list for.
 100  
          * @return the list, never {@code null}.
 101  
          * @since 0.1.0
 102  
          */
 103  
         private ObservableList<ActionBean> createActionList (TargetBean model) {
 104  0
                 return createCollectionAtPresenter(model.getActions());
 105  
         }
 106  
 
 107  
         /**
 108  
          * Writes the actions in the model according to the presenter.
 109  
          * @since 0.1.0
 110  
          */
 111  
         private void updateActionsAtModel () {
 112  0
                 LOGGER.debug("updateActionsAtModel");
 113  0
                 change("actions", getPartBeanFactory().createActionCollection(this.actions));
 114  0
         }
 115  
 
 116  
         /**
 117  
          * Writes the actions in the presenter according to the model.
 118  
          * @since 0.1.0
 119  
          */
 120  
         private void updateActionsAtPresenter () {
 121  0
                 LOGGER.debug("updateActionsAtPresenter");
 122  0
                 updateCollectionAtPresenter(getModel().getActions(), this.actions, this.actionsUpdater);
 123  0
         }
 124  
 
 125  
         /**
 126  
          * The list of actions of this target.
 127  
          * @return the list of actions of this target.
 128  
          * @since 0.1.0
 129  
          */
 130  
         public ObservableList<ActionBean> getActions () {
 131  0
                 return this.actions;
 132  
         }
 133  
 
 134  
         /**
 135  
          * Returns the active resource domain of this model.
 136  
          * @return the active resource domain of this model.
 137  
          * @since 0.1.0
 138  
          */
 139  
         public DomainBean getActiveResourceDomain () {
 140  0
                 return this.activeResourceDomain;
 141  
         }
 142  
 
 143  
         /**
 144  
          * Sets the active resource domain of the target of this model.
 145  
          * @param activeResourceDomainBean the new resource domain.
 146  
          * @since 0.1.0
 147  
          */
 148  
         public void setActiveResourceDomain (DomainBean activeResourceDomainBean) {
 149  0
                 if (activeResourceDomainBean != null) {
 150  0
                         LOGGER.debug("setActiveResourceDomain [{}].", activeResourceDomainBean);
 151  0
                         final DomainBean current = getModel().getResourceDomain();
 152  0
                         if (sameSerial(current, activeResourceDomainBean)) {
 153  0
                                 updateActiveResourceDomain(activeResourceDomainBean);
 154  
                         } else {
 155  0
                                 change(getModel(), "resourceDomain", activeResourceDomainBean);
 156  
                         }
 157  
                 }
 158  0
         }
 159  
 
 160  
         /**
 161  
          * Forwards model changes to the listeners of this presenter.
 162  
          * @param activeResourceDomainBean the active resource domain to set.
 163  
          * @since 0.1.0
 164  
          */
 165  
         private void updateActiveResourceDomain (DomainBean activeResourceDomainBean) {
 166  0
                 final DomainBean oldValue = this.activeResourceDomain;
 167  0
                 this.activeResourceDomain = activeResourceDomainBean;
 168  0
                 firePropertyChange("activeResourceDomain", oldValue, this.activeResourceDomain);
 169  0
         }
 170  
 
 171  
         /**
 172  
          * Removes the active resource domain in the whole policy.
 173  
          * @since 0.1.0
 174  
          */
 175  
         public void removeActiveResourceDomain (DomainBean resourceDomain) {
 176  0
                 execute(
 177  
                         new CompositeCommand(
 178  
                                 new ResourceDomainRemoveCommand(resourceDomain),
 179  
                                 new PoolRemoveCommand<DomainBean>(
 180  
                                         this.getResourceDomainPool(), resourceDomain)
 181  
                         )
 182  
                 );
 183  0
         }
 184  
 
 185  
         /**
 186  
          * Removes the active target in the whole policy.
 187  
          * @since 0.1.0
 188  
          */
 189  
         public void removeActiveAction (ActionBean activeAction) {
 190  0
                 if (activeAction != null) {
 191  0
                         execute(
 192  
                                 new CompositeCommand(
 193  
                                         new ActionRemoveCommand(activeAction),
 194  
                                         new PoolRemoveCommand<ActionBean>(this.getActionPool(), activeAction)
 195  
                                 )
 196  
                         );
 197  
                 }
 198  0
         }
 199  
 
 200  
         /**
 201  
          * @since 0.1.0
 202  
          */
 203  
         public void addResourceDomain (EditPartCommand<DomainBean> editPartCommand) {
 204  0
                 final DomainBean domain = getContext().getPartBeanFactory().createDomain(null);
 205  0
                 editPartCommand.setPart(domain);
 206  0
                 execute(
 207  
                         new CompositeCommand(
 208  
                                 createChangeCommand(getModel(), "resourceDomain", domain),
 209  
                                 editPartCommand
 210  
                         )
 211  
                 );
 212  0
         }
 213  
 
 214  
         /**
 215  
          * Returns the identity string representation of the specified domain.
 216  
          * @param domain the resource domain for which to retrieve the identity.
 217  
          * @return the identity string or an empty string if undefined.
 218  
          * @since 0.1.0
 219  
          */
 220  
         private String getIdentity (DomainBean domain) {
 221  0
                 if (domain != null) {
 222  0
                         final URI identity = domain.getIdentity();
 223  0
                         if (identity != null) {
 224  0
                                 return identity.toString();
 225  
                         }
 226  
                 }
 227  0
                 return "";
 228  
         }
 229  
 
 230  
         /**
 231  
          * @since 0.1.0
 232  
          */
 233  
         public void addAction (EditPartCommand<ActionBean> editPartCommand) {
 234  0
                 final ActionBean newAction = getContext().getPartBeanFactory().
 235  
                         createAction("", ParameterList.empty());
 236  0
                 editPartCommand.setPart(newAction);
 237  0
                 execute(
 238  
                         new CompositeCommand(
 239  
                                 new CollectionAddCommand<ActionBean>(getModel(), newAction, "actions"),
 240  
                                 editPartCommand
 241  
                         )
 242  
                 );
 243  0
         }
 244  
 
 245  
         //---- PropertyChange
 246  
 
 247  
         /**
 248  
          * Handler for resource domain changes in a target.
 249  
          * @since 0.1.0
 250  
          */
 251  
         @PropertyChange(bean = TargetBean.class, property = "resourceDomain")
 252  
         public void updateResourceDomain () {
 253  0
                 final DomainBean domain = getModel().getResourceDomain();
 254  0
                 LOGGER.debug("updateResourceDomain({}).", domain);
 255  0
                 updateActiveResourceDomain(domain);
 256  0
                 setTitleParameters(getIdentity(domain));
 257  0
         }
 258  
 
 259  
         /**
 260  
          * Handler for changes in the actions of a target.
 261  
          * @since 0.1.0
 262  
          */
 263  
         @PropertyChange(bean = TargetBean.class, property = "actions")
 264  
         public void updateActions () {
 265  0
                 LOGGER.debug("updateActions");
 266  0
                 updateActionsAtPresenter();
 267  0
         }
 268  
 
 269  
 
 270  
 }