Coverage Report - org.apache.myfaces.shared.util.el.ActionsMap
 
Classes in this File Line Coverage Branch Coverage Complexity
ActionsMap
0%
0/37
0%
0/16
1.632
ActionsMap$CommandEntry
0%
0/10
0%
0/4
1.632
 
 1  
 /*
 2  
  * Licensed to the Apache Software Foundation (ASF) under one
 3  
  * or more contributor license agreements.  See the NOTICE file
 4  
  * distributed with this work for additional information
 5  
  * regarding copyright ownership.  The ASF licenses this file
 6  
  * to you under the Apache License, Version 2.0 (the
 7  
  * "License"); you may not use this file except in compliance
 8  
  * with the License.  You may obtain a copy of the License at
 9  
  *
 10  
  *   http://www.apache.org/licenses/LICENSE-2.0
 11  
  *
 12  
  * Unless required by applicable law or agreed to in writing,
 13  
  * software distributed under the License is distributed on an
 14  
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 15  
  * KIND, either express or implied.  See the License for the
 16  
  * specific language governing permissions and limitations
 17  
  * under the License.
 18  
  */
 19  
 package org.apache.myfaces.shared.util.el;
 20  
 
 21  
 import java.util.Collection;
 22  
 import java.util.Collections;
 23  
 import java.util.HashSet;
 24  
 import java.util.Iterator;
 25  
 import java.util.Map;
 26  
 import java.util.Set;
 27  
 
 28  
 /**
 29  
  * You can use this class to trigger an action when a boolean is set to true.
 30  
  *
 31  
  * Example : in JSF pages, for dataTable, to remove elements :
 32  
  * Backing bean (#{inboxFace}).
 33  
  * public ActionsMap getRemoveEmailUnid(){
 34  
  *         return new ActionsMap(){
 35  
  *            public void performAction(String unid) {
 36  
  *                InboxMailDAO<TInboxMail> dao = getInboxMailDAO();
 37  
  *                TInboxMail email = dao.getByPrimaryKey( unid );
 38  
  *                dao.remove( email );
 39  
  *            }
 40  
  *        };
 41  
  *    }
 42  
  * JSF page :
 43  
  * &lt;h:selectBooleanCheckbox value="#{inboxFace.removeEmailUnid[email.unid]}"/&gt;
 44  
  */
 45  
 public abstract class ActionsMap implements Map
 46  
 {
 47  
 
 48  
     private Set keys;
 49  
 
 50  
     public ActionsMap()
 51  0
     {
 52  
         // NoOp
 53  0
     }
 54  
 
 55  
     public ActionsMap(Set keys)
 56  0
     {
 57  0
         this.keys = keys;
 58  0
     }
 59  
 
 60  
     /**
 61  
      * This method should fire the command.
 62  
      */
 63  
     public abstract void performAction(String command);
 64  
 
 65  
     public int size()
 66  
     {
 67  0
         return keys.size();
 68  
     }
 69  
 
 70  
     public boolean isEmpty()
 71  
     {
 72  0
         return keys.isEmpty();
 73  
     }
 74  
 
 75  
     public boolean containsKey(Object key)
 76  
     {
 77  0
         return keys.contains( key );
 78  
     }
 79  
 
 80  
     public boolean containsValue(Object value)
 81  
     {
 82  0
         if( ! (value instanceof Boolean) )
 83  
         {
 84  0
             return false;
 85  
         }
 86  0
         return ((Boolean)value).booleanValue();
 87  
     }
 88  
 
 89  
     public Object get( Object key)
 90  
     {
 91  0
         return Boolean.FALSE;
 92  
     }
 93  
 
 94  
     public Boolean put(String key, Boolean value)
 95  
     {
 96  0
         if( value!=null && value.booleanValue() )
 97  
         {
 98  0
             performAction(key);
 99  
         }
 100  0
         return Boolean.FALSE;
 101  
     }
 102  
 
 103  
     public Object remove(Object key)
 104  
     {
 105  0
         if( keys.remove( key ) )
 106  
         {
 107  0
             return Boolean.FALSE;
 108  
         }
 109  0
         return null;
 110  
     }
 111  
 
 112  
     public void putAll(Map map)
 113  
     {
 114  0
         Iterator it = map.entrySet().iterator();
 115  
 
 116  0
         while (it.hasNext())
 117  
         {
 118  0
             Entry entry = (Entry) it.next();
 119  0
             Object obj = entry.getValue();
 120  0
             if( (obj instanceof Boolean) && ((Boolean) obj).booleanValue() )
 121  
             {
 122  0
                 performAction((String) entry.getKey());
 123  
             }
 124  0
         }
 125  0
     }
 126  
 
 127  
     public void clear()
 128  
     {
 129  0
         keys.clear();
 130  0
     }
 131  
 
 132  
     public Set keySet()
 133  
     {
 134  0
         return keys;
 135  
     }
 136  
 
 137  
     public Collection values()
 138  
     {
 139  0
         return Collections.nCopies(keys.size(), Boolean.FALSE);
 140  
     }
 141  
 
 142  
     public Set entrySet()
 143  
     {
 144  0
         Set set = new HashSet( keys.size() );
 145  
 
 146  0
         Iterator it = keys.iterator();
 147  
 
 148  0
         while (it.hasNext())
 149  
         {
 150  0
             String command = (String) it.next();
 151  0
             set.add( new CommandEntry(command) );
 152  0
         }
 153  
 
 154  0
         return set;
 155  
     }
 156  
 
 157  
     private class CommandEntry implements Entry
 158  
     {
 159  
 
 160  
         private final String command;
 161  0
         private boolean commandPerformed = false;
 162  
 
 163  
         public CommandEntry(String command)
 164  0
         {
 165  0
             this.command = command;
 166  0
         }
 167  
 
 168  
         public Object getKey()
 169  
         {
 170  0
             return command;
 171  
         }
 172  
 
 173  
         public Object getValue()
 174  
         {
 175  0
             return Boolean.valueOf(commandPerformed);
 176  
         }
 177  
 
 178  
         public Object setValue(Object performCommand)
 179  
         {
 180  0
             if( (performCommand instanceof Boolean) && ((Boolean)performCommand).booleanValue() )
 181  
             {
 182  0
                 performAction( command );
 183  0
                 commandPerformed = true;
 184  
             }
 185  0
             return Boolean.valueOf(commandPerformed);
 186  
         }
 187  
     }
 188  
 }
 189