View Javadoc
1   /*******************************************************************************
2    * Copyright 2012 Internet2
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *   http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   ******************************************************************************/
16  /*
17  Copyright 2004-2007 University Corporation for Advanced Internet Development, Inc.
18  Copyright 2004-2007 The University Of Bristol
19  
20  Licensed under the Apache License, Version 2.0 (the "License");
21  you may not use this file except in compliance with the License.
22  You may obtain a copy of the License at
23  
24    http://www.apache.org/licenses/LICENSE-2.0
25  
26  Unless required by applicable law or agreed to in writing, software
27  distributed under the License is distributed on an "AS IS" BASIS,
28  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
29  See the License for the specific language governing permissions and
30  limitations under the License.
31  */
32  
33  package edu.internet2.middleware.grouper.ui.util;
34  
35  import java.lang.reflect.Constructor;
36  import java.util.HashMap;
37  import java.util.HashSet;
38  import java.util.Iterator;
39  import java.util.LinkedHashSet;
40  import java.util.ResourceBundle;
41  import java.util.Set;
42  
43  import org.apache.commons.beanutils.DynaBean;
44  import org.apache.commons.beanutils.DynaProperty;
45  import org.apache.commons.beanutils.WrapDynaBean;
46  
47  import edu.internet2.middleware.grouper.GrouperSession;
48  import edu.internet2.middleware.grouper.ui.GroupOrStem;
49  import edu.internet2.middleware.grouper.ui.UIThreadLocal;
50  import edu.internet2.middleware.subject.Subject;
51  
52  
53  /**
54   * Base class for using a Map as a wrapper to an object
55   * <p />
56   * 
57   * @author Gary Brown.
58   * @version $Id: ObjectAsMap.java,v 1.10 2009-10-16 12:16:32 isgwb Exp $
59   */
60  public class ObjectAsMap extends HashMap {
61  	protected String objType = null;
62  
63  	protected Object wrappedObject = null;
64  	protected transient DynaBean dynaBean=null;
65  	protected String dateFormat="dd MMM yyyy HH:mm:ss";
66  	public ObjectAsMap() {
67  		
68  	}
69  	public ObjectAsMap(Object obj, String type) {
70  		wrappedObject=obj;
71  		objType=type;
72  	}
73  	
74  	/**
75  	 * @return notional type of object
76  	 */
77  	public String getObjectType() {
78  		return objType;
79  	}
80  
81  	/**
82  	 * @return object that was wrapped
83  	 */
84  	public Object getWrappedObject() {
85  		return this.wrappedObject;
86  	}
87  
88  	/**
89  	 * @param key
90  	 *            to get
91  	 * @return value assumed to be String
92  	 */
93  	public String getString(String key) {
94  		return (String) get(key);
95  	}
96  
97  	/*
98  	 * (non-Javadoc)
99  	 * 
100 	 * @see java.util.Map#get(java.lang.Object)
101 	 */
102 	public Object get(Object key) {
103 		if ("wrappedObject".equals(key)) {
104 			return this.getWrappedObject();
105 		}
106 		return super.get(key);
107 	}
108 	protected Object getByIntrospection(Object key) {
109 		if (dynaBean==null) dynaBean=new WrapDynaBean(wrappedObject);
110 		try {
111 			return dynaBean.get(key.toString());
112 		}catch(Exception e){return null;}
113 	}
114 	
115 	/**
116 	 * Rather than use a constructor directly, the UI reads the implementation type
117 	 * from media.properties. This allows sites to provide alternative implementations
118 	 * @param type
119 	 * @param object
120 	 * @param grouperSession
121 	 * @return subclass as configured in media.properties
122 	 */
123 	public static ObjectAsMap getInstance(String type, Object object, GrouperSession grouperSession) {
124 		
125 		ResourceBundle mediaBundle = (ResourceBundle) UIThreadLocal.get("mediaBundle");
126 		String claz = mediaBundle.getString("objectasmap." + type + ".impl");
127 		
128 		try {
129 			Class impl = Class.forName(claz);
130 			Constructor c = impl.getConstructor(object.getClass(), GrouperSession.class);
131 			ObjectAsMap result = (ObjectAsMap) c.newInstance(object, grouperSession);
132 		
133 			return result;
134 		} catch (Exception e) {
135 			throw new RuntimeException(e);
136 		}
137 	}
138 	
139 /**
140  * Rather than use a constructor directly, the UI reads the implementation type
141  * from media.properties. This allows sites to provide alternative implementations
142  * @param type
143  * @param subject
144  * @param grouperSession
145  * @param groupOrStem
146  * @param privilege
147  * @return subclass as configured in media.properties
148  */
149 public static ObjectAsMap getInstance(String type, Subject subject, GrouperSession grouperSession,GroupOrStem groupOrStem,String privilege) {
150 		
151 		ResourceBundle mediaBundle = (ResourceBundle) UIThreadLocal.get("mediaBundle");
152 		String claz = mediaBundle.getString("objectasmap." + type + ".impl");
153 		
154 		try {
155 			Class impl = Class.forName(claz);
156 			Constructor c = impl.getConstructor(GrouperSession.class,Subject.class,groupOrStem.getClass(),privilege.getClass());
157 			ObjectAsMap result = (ObjectAsMap) c.newInstance( grouperSession,subject,groupOrStem,privilege);
158 		
159 			return result;
160 		} catch (Exception e) {
161 			throw new RuntimeException(e);
162 		}
163 	}
164 	
165 /**
166  * Rather than use a constructor directly, the UI reads the implementation type
167  * from media.properties. This allows sites to provide alternative implementations
168  * @param type
169  * @param object
170  * @return subclass as configured in media.properties
171  */
172 public static ObjectAsMap getInstance(String type, Object object) {
173 	
174 	ResourceBundle mediaBundle = (ResourceBundle) UIThreadLocal.get("mediaBundle");
175 	String claz = mediaBundle.getString("objectasmap." + type + ".impl");
176 	
177 	try {
178 		Class impl = Class.forName(claz);
179 		Constructor c = impl.getConstructor(object.getClass());
180 		ObjectAsMap result = (ObjectAsMap) c.newInstance(object);
181 	
182 		return result;
183 	} catch (Exception e) {
184 		throw new RuntimeException(e);
185 	}
186 }
187 
188 /**
189  * Rather than use a constructor directly, the UI reads the implementation type
190  * from media.properties. This allows sites to provide alternative implementations
191  * @param type
192  * @param object
193  * @return subclass as configured in media.properties
194  */
195 public static ObjectAsMap getInstance(String type, Subject object) {
196 	
197 	ResourceBundle mediaBundle = (ResourceBundle) UIThreadLocal.get("mediaBundle");
198 	String claz = mediaBundle.getString("objectasmap." + type + ".impl");
199 	
200 	try {
201 		Class impl = Class.forName(claz);
202 		Constructor c = impl.getConstructor(Subject.class);
203 		ObjectAsMap result = (ObjectAsMap) c.newInstance(object);
204 	
205 		return result;
206 	} catch (Exception e) {
207 		throw new RuntimeException(e);
208 	}
209 }
210 	
211 	public Set keySet() {
212 		// TODO Auto-generated method stub
213 		Set keys = new LinkedHashSet();
214 		keys.addAll(super.keySet());
215 		keys.addAll(getExtraKeys());
216 		if (dynaBean==null) dynaBean=new WrapDynaBean(wrappedObject);
217 		DynaProperty[] props = dynaBean.getDynaClass().getDynaProperties();
218 		for(int i=0;i<props.length;i++) {
219 			if(isValidDynaProperty(props[i])) keys.add(props[i].getName());
220 		}
221 		return keys;
222 	}
223 	
224 	private boolean isValidDynaProperty(DynaProperty prop) {
225 		if(prop.isIndexed() || prop.isMapped()) return false;
226 		Class type = prop.getType();
227 		if(type.equals(String.class)) return true;
228 		if(type.equals(Boolean.class)) return true;
229 		if(type.equals(Integer.class)) return true;
230 		return false;
231 	}
232 	
233 	protected java.util.Set getExtraKeys() {
234 		return new HashSet();
235 	}
236 	@Override
237 	public boolean isEmpty() {
238 		// TODO Auto-generated method stub
239 		return keySet().size()==0;
240 	}
241 	@Override
242 	public int size() {
243 		// TODO Auto-generated method stub
244 		return keySet().size();
245 	}
246 	@Override
247 	public Set entrySet() {
248 		// TODO Auto-generated method stub
249 		HashMap map = new HashMap();
250 		
251 		Iterator it = keySet().iterator();
252 		Object key;
253 		while(it.hasNext()) {
254 			key = it.next();
255 			map.put(key, get(key));
256 		}
257 		return map.entrySet();
258 	}
259 	public String getDateFormat() {
260 		return dateFormat;
261 	}
262 	public void setDateFormat(String dateFormat) {
263 		this.dateFormat = dateFormat;
264 	}
265 	
266 	
267 }