View Javadoc

1   /* Copyright 2005 The JA-SIG Collaborative.  All rights reserved.
2   *  See license distributed with this file and
3   *  available online at http://www.uportal.org/license.html
4   */
5   
6   package org.jasig.services.persondir.support;
7   
8   import java.util.Collections;
9   import java.util.HashMap;
10  import java.util.HashSet;
11  import java.util.Iterator;
12  import java.util.Map;
13  import java.util.Set;
14  
15  import org.jasig.services.persondir.IPersonAttributeDao;
16  
17  
18  /***
19   * Looks up the user's attribute Map in the backingMap. If using the {@link IPersonAttributeDao#getUserAttributes(Map)}
20   * method the attribute value returned for the key {@link #getDefaultAttributeName()} will
21   * be used as the key for the backingMap.
22   * 
23   * <br>
24   * <br>
25   * Configuration:
26   * <table border="1">
27   *     <tr>
28   *         <th align="left">Property</th>
29   *         <th align="left">Description</th>
30   *         <th align="left">Required</th>
31   *         <th align="left">Default</th>
32   *     </tr>
33   *     <tr>
34   *         <td align="right" valign="top">backingMap</td>
35   *         <td>
36   *             Sets the backing map to use to return user attributes from. The backing map
37   *             should have keys of type {@link String} which are the uid for the user. The
38   *             values should be of type {@link Map} which follow the Map restrictions decribed
39   *             by {@link IPersonAttributeDao#getUserAttributes(Map)}.
40   *         </td>
41   *         <td valign="top">No</td>
42   *         <td valign="top">{@link Collections#EMPTY_MAP}</td>
43   *     </tr>
44   * </table>
45   * 
46   * @version $Revision: 2892 $ $Date: 2006-12-19 13:25:03 -0600 (Tue, 19 Dec 2006) $
47   */
48  public class ComplexStubPersonAttributeDao extends AbstractDefaultAttributePersonAttributeDao {
49      /*
50       * Map from userids to Maps.  The Map values are maps from
51       * attribute names to attribute values.
52       */
53      private Map backingMap = Collections.EMPTY_MAP;
54      
55      /*
56       * Set of possible all attribute names that map to an attribute
57       * value for some user in our backing map.
58       */
59      private Set possibleUserAttributeNames = Collections.EMPTY_SET;
60      
61      /***
62       * Creates a new, empty, dao.
63       */
64      public ComplexStubPersonAttributeDao() {
65      }
66      
67      /***
68       * Creates a new DAO with the specified backing map.
69       * @param backingMap The backingMap to call {@link #setBackingMap(Map)} with.
70       */
71      public ComplexStubPersonAttributeDao(Map backingMap) {
72          this.setBackingMap(backingMap);
73      }
74      
75      
76      /***
77       * @return Returns the backingMap.
78       */
79      public Map getBackingMap() {
80          return this.backingMap;
81      }
82      /***
83       * @param backingMap The backingMap to set.
84       */
85      public void setBackingMap(Map backingMap) {
86          if (backingMap == null) {
87              this.backingMap = Collections.EMPTY_MAP;
88              this.possibleUserAttributeNames = Collections.EMPTY_SET;
89          }
90          else {
91              this.backingMap = Collections.unmodifiableMap(new HashMap(backingMap));
92              this.initializePossibleAttributeNames();
93          }
94      }
95      
96      /*
97       * @see org.jasig.services.persondir.IPersonAttributeDao#getPossibleUserAttributeNames()
98       */
99      public Set getPossibleUserAttributeNames() {
100         return this.possibleUserAttributeNames;
101     }
102     
103     /*
104      * @see org.jasig.services.persondir.IPersonAttributeDao#getUserAttributes(java.util.Map)
105      */
106     public Map getUserAttributes(final Map seed) {
107         if (seed == null) {
108             throw new IllegalArgumentException("Illegal to invoke getUserAttributes(Map) with a null argument.");
109         }
110 
111         final String defaultAttrName = this.getDefaultAttributeName();
112         final String seedValue = (String)seed.get(defaultAttrName);
113         return (Map)this.backingMap.get(seedValue);
114     }
115 
116     /***
117      * Compute the set of attribute names that map to a value for at least one
118      * user in our backing map and store it as the instance variable 
119      * possibleUserAttributeNames.
120      */
121     private void initializePossibleAttributeNames() {
122         final Set possibleAttribNames = new HashSet();
123         
124         for (final Iterator iter = this.backingMap.values().iterator(); iter.hasNext(); ) {
125             final Map attributeMapForSomeUser = (Map)iter.next();
126             possibleAttribNames.addAll(attributeMapForSomeUser.keySet());
127         }
128         
129         this.possibleUserAttributeNames = Collections.unmodifiableSet(possibleAttribNames);
130     }
131 }