1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
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  
55  
56  
57  
58  
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  
76  
77  	public String getObjectType() {
78  		return objType;
79  	}
80  
81  	
82  
83  
84  	public Object getWrappedObject() {
85  		return this.wrappedObject;
86  	}
87  
88  	
89  
90  
91  
92  
93  	public String getString(String key) {
94  		return (String) get(key);
95  	}
96  
97  	
98  
99  
100 
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 
117 
118 
119 
120 
121 
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 
141 
142 
143 
144 
145 
146 
147 
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 
167 
168 
169 
170 
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 
190 
191 
192 
193 
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 		
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 		
239 		return keySet().size()==0;
240 	}
241 	@Override
242 	public int size() {
243 		
244 		return keySet().size();
245 	}
246 	@Override
247 	public Set entrySet() {
248 		
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 }