1
2
3
4
5 package edu.internet2.middleware.grouper.ui.customUi;
6
7 import java.util.HashMap;
8 import java.util.Map;
9
10 import org.apache.commons.lang.StringUtils;
11 import org.apache.commons.logging.Log;
12
13 import edu.internet2.middleware.grouper.Group;
14 import edu.internet2.middleware.grouper.Stem;
15 import edu.internet2.middleware.grouper.attr.AttributeDef;
16 import edu.internet2.middleware.grouper.cfg.text.GrouperTextContainer;
17 import edu.internet2.middleware.grouper.util.GrouperUtil;
18 import edu.internet2.middleware.subject.Subject;
19
20
21
22
23
24 public class CustomUiUtil {
25
26
27
28
29 public CustomUiUtil() {
30 }
31
32
33 private static final Log LOG = GrouperUtil.getLog(CustomUiUtil.class);
34
35
36
37
38
39
40
41 public static void validateGroup(String groupId, String groupName, Group group) {
42 if (StringUtils.isBlank(groupId) && StringUtils.isBlank(groupName)) {
43 return;
44 }
45 if (!StringUtils.isBlank(groupId) && !StringUtils.isBlank(groupName)) {
46 throw new RuntimeException("Cant set groupId '" + groupId + "' and groupName '" + groupName + "' at same time!");
47 }
48 if (!StringUtils.isBlank(groupId) && group == null) {
49 throw new RuntimeException("Cant find group by groupId '" + groupId + "'");
50 }
51 if (!StringUtils.isBlank(groupName) && group == null) {
52 throw new RuntimeException("Cant find group by groupName '" + groupName + "'");
53 }
54 }
55
56
57
58
59
60
61
62 public static void validateStem(String stemId, String stemName, Stem stem) {
63 if (StringUtils.isBlank(stemId) && StringUtils.isBlank(stemName)) {
64 return;
65 }
66 if (!StringUtils.isBlank(stemId) && !StringUtils.isBlank(stemName)) {
67 throw new RuntimeException("Cant set stemId '" + stemId + "' and stemName '" + stemName + "' at same time!");
68 }
69 if (!StringUtils.isBlank(stemId) && stem == null) {
70 throw new RuntimeException("Cant find stem by stemId '" + stemId + "'");
71 }
72 if (!StringUtils.isBlank(stemName) && stem == null) {
73 throw new RuntimeException("Cant find stem by stemName '" + stemName + "'");
74 }
75 }
76
77
78
79
80
81
82
83 public static void validateAttributeDef(String attributeDefId, String nameOfAttributeDef, AttributeDef attributeDef) {
84 if (StringUtils.isBlank(attributeDefId) && StringUtils.isBlank(nameOfAttributeDef)) {
85 return;
86 }
87 if (!StringUtils.isBlank(attributeDefId) && !StringUtils.isBlank(nameOfAttributeDef)) {
88 throw new RuntimeException("Cant set attributeDefId '" + attributeDefId + "' and nameOfAttributeDef '" + nameOfAttributeDef + "' at same time!");
89 }
90 if (!StringUtils.isBlank(attributeDefId) && attributeDef == null) {
91 throw new RuntimeException("Cant find attributeDef by attributeDefId '" + attributeDefId + "'");
92 }
93 if (!StringUtils.isBlank(nameOfAttributeDef) && attributeDef == null) {
94 throw new RuntimeException("Cant find attributeDef by nameOfAttributeDef '" + nameOfAttributeDef + "'");
95 }
96 }
97
98
99
100
101
102
103
104
105
106
107
108 public static String substituteExpressionLanguage(String string, Group group, Stem stem, AttributeDef attributeDef,
109 Subject subject, Map<String, Object> externalVariableMap) {
110
111 return substituteExpressionLanguage(string, group, stem, attributeDef,
112 subject, externalVariableMap, false);
113
114 }
115
116
117
118
119
120
121
122 public static void guiGroupAssign(Map<String, Object> variableMap, Group group, String key) {
123 if (group == null) {
124 return;
125 }
126 Object grouperRequestContainer = variableMap.get("grouperRequestContainer");
127
128 if (grouperRequestContainer == null) {
129 throw new RuntimeException("Why no grouperRequestContainer in variableMap?");
130 }
131
132 Class<?> guiGroupClass = GrouperUtil.forName("edu.internet2.middleware.grouper.grouperUi.beans.api.GuiGroup");
133 Object guiGroup = GrouperUtil.newInstance(guiGroupClass);
134 GrouperUtil.callMethod(guiGroupClass, guiGroup, "setGroup", Group.class, group);
135 variableMap.put(key, guiGroup);
136
137 }
138
139
140
141
142
143
144
145
146
147
148
149
150 public static String substituteExpressionLanguage(String string, Group group, Stem stem, AttributeDef attributeDef,
151 Subject subject, Map<String, Object> externalVariableMap, boolean useGuiObjects) {
152
153 if (string == null || !string.contains("${")) {
154 return string;
155 }
156 Map<String, Object> variableMap = new HashMap<String, Object>();
157
158 if (externalVariableMap != null) {
159 variableMap.putAll(externalVariableMap);
160 }
161
162 if (attributeDef != null) {
163 variableMap.put("attributeDef", attributeDef);
164 if (useGuiObjects) {
165 guiAttributeDefAssign(variableMap, attributeDef, "guiAttributeDef");
166 }
167 }
168 if (stem != null) {
169 variableMap.put("stem", stem);
170 if (useGuiObjects) {
171 guiStemAssign(variableMap, stem, "guiStem");
172 }
173 }
174 if (group != null) {
175 variableMap.put("group", group);
176 if (useGuiObjects) {
177 guiGroupAssign(variableMap, group, "guiGroup");
178 }
179 }
180 if (subject != null) {
181 variableMap.put("subject", subject);
182 }
183 variableMap.put("grouperUtil", new GrouperUtil());
184 try {
185 GrouperTextContainer.assignThreadLocalVariableMap(variableMap);
186 string = GrouperUtil.substituteExpressionLanguage(string, variableMap, true, false, false);
187 } finally {
188 GrouperTextContainer.resetThreadLocalVariableMap();
189 }
190 return string;
191 }
192
193
194
195
196
197
198 private static void guiStemAssign(Map<String, Object> variableMap, Stem stem,
199 String key) {
200 if (stem == null) {
201 return;
202 }
203 Object grouperRequestContainer = variableMap.get("grouperRequestContainer");
204
205 if (grouperRequestContainer == null) {
206 throw new RuntimeException("Why no grouperRequestContainer in variableMap?");
207 }
208
209 Class<?> guiStemClass = GrouperUtil.forName("edu.internet2.middleware.grouper.grouperUi.beans.api.GuiStem");
210 Object guiStem = GrouperUtil.newInstance(guiStemClass);
211 GrouperUtil.callMethod(guiStemClass, guiStem, "setStem", Stem.class, stem);
212 variableMap.put(key, guiStem);
213
214 }
215
216
217
218
219
220
221 private static void guiAttributeDefAssign(Map<String, Object> variableMap,
222 AttributeDef attributeDef, String key) {
223 if (attributeDef == null) {
224 return;
225 }
226 Object grouperRequestContainer = variableMap.get("grouperRequestContainer");
227
228 if (grouperRequestContainer == null) {
229 throw new RuntimeException("Why no grouperRequestContainer in variableMap?");
230 }
231
232 Class<?> guiAttributeDefClass = GrouperUtil.forName("edu.internet2.middleware.grouper.grouperUi.beans.api.GuiAttributeDef");
233 Object guiAttributeDef = GrouperUtil.newInstance(guiAttributeDefClass);
234 GrouperUtil.callMethod(guiAttributeDefClass, guiAttributeDef, "setAttributeDef", AttributeDef.class, attributeDef);
235 variableMap.put(key, guiAttributeDef);
236
237 }
238
239 }