1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.jetspeed.portlets.profiler;
18
19 import java.io.Serializable;
20 import java.util.Iterator;
21 import java.util.Map;
22
23 import javax.faces.context.FacesContext;
24 import javax.faces.event.ActionEvent;
25 import javax.faces.model.SelectItem;
26
27 import org.apache.jetspeed.CommonPortletServices;
28 import org.apache.jetspeed.profiler.Profiler;
29 import org.apache.jetspeed.profiler.rules.ProfilingRule;
30 import org.apache.jetspeed.profiler.rules.RuleCriterion;
31
32
33 /***
34 * Criterion state.
35 *
36 * @author <a href="mailto:taylor@apache.org">David Sean Taylor</a>
37 * @version $Id: ProfileCriterionForm.java 348264 2005-11-22 22:06:45Z taylor $
38 */
39 public class ProfileCriterionForm implements Serializable
40 {
41 private boolean isNew = false;
42 private transient Profiler profiler = null;
43 private transient RuleCriterion criterion = null;
44 private transient ProfilingRule rule = null;
45
46 private static final String FALLBACK_CONTINUE = "Continue";
47 private static final String FALLBACK_STOP = "Stop";
48 private static final String FALLBACK_LOOP = "Loop";
49
50 private static final SelectItem[] RESOLVERS =
51 {
52 new SelectItem("request"),
53 new SelectItem("session"),
54 new SelectItem("request.session"),
55 new SelectItem("hard.coded"),
56 new SelectItem("group.role.user"),
57 new SelectItem("user"),
58 new SelectItem("group"),
59 new SelectItem("role"),
60 new SelectItem("mediatype"),
61 new SelectItem("country"),
62 new SelectItem("language"),
63 new SelectItem("roles"),
64 new SelectItem("path"),
65 new SelectItem("page"),
66 new SelectItem("path.session"),
67 new SelectItem("user.attribute"),
68 new SelectItem("navigation"),
69 new SelectItem("ip"),
70 new SelectItem("hostname")
71 };
72
73 private static final SelectItem[] FALLBACK_TYPES =
74 {
75 new SelectItem(FALLBACK_CONTINUE),
76 new SelectItem(FALLBACK_LOOP),
77 new SelectItem(FALLBACK_STOP)
78 };
79
80 public ProfileCriterionForm()
81 {
82 }
83
84 public boolean getUpdating()
85 {
86 return !isNew;
87 }
88
89 public SelectItem[] getResolvers()
90 {
91 return RESOLVERS;
92 }
93
94 public SelectItem[] getFallbackTypes()
95 {
96 return FALLBACK_TYPES;
97 }
98
99 public void listen(ActionEvent event)
100 {
101 Map appMap = FacesContext.getCurrentInstance().getExternalContext().getApplicationMap();
102 profiler = (Profiler)appMap.get(CommonPortletServices.CPS_PROFILER_COMPONENT);
103 Map params = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
104 String selectedRule = (String)params.get("selectedRule");
105
106 if (selectedRule != null && profiler != null)
107 {
108 rule = profiler.getRule(selectedRule);
109 if (rule != null)
110 {
111 String selected = (String)params.get("selectedCriterion");
112 if (selected == null || selected.length() == 0)
113 {
114 isNew = true;
115 try
116 {
117
118
119
120
121
122
123 this.criterion = profiler.createRuleCriterion();
124 }
125 catch (Exception e)
126 {
127 System.out.println("Failed to CREATE NEW: rule = " + rule.getId());
128
129 }
130 }
131 else
132 {
133 Iterator it = rule.getRuleCriteria().iterator();
134 while (it.hasNext())
135 {
136 RuleCriterion c = (RuleCriterion)it.next();
137 if (c.getName().equals(selected))
138 {
139 criterion = c;
140 isNew = false;
141 break;
142 }
143 }
144 }
145 }
146 }
147 }
148
149 public String getName()
150 {
151 if (criterion == null)
152 {
153 return "{empty}";
154 }
155 return criterion.getName();
156 }
157
158 public void setName(String name)
159 {
160 if (criterion != null)
161 {
162 this.criterion.setName(name);
163 }
164 }
165
166 public String getValue()
167 {
168 if (criterion == null)
169 {
170 return "{empty}";
171 }
172 return criterion.getValue();
173 }
174
175 public void setValue(String value)
176 {
177 if (criterion != null)
178 {
179 this.criterion.setValue(value);
180 }
181 }
182
183 public String getResolver()
184 {
185 if (criterion == null)
186 {
187 return "{empty}";
188 }
189 return criterion.getType();
190 }
191
192 public void setResolver(String resolver)
193 {
194 if (criterion != null)
195 {
196 this.criterion.setType(resolver);
197 }
198 }
199
200 public int getFallbackOrder()
201 {
202 if (criterion == null)
203 {
204 return 0;
205 }
206 return criterion.getFallbackOrder();
207 }
208
209 public void setFallbackOrder(int order)
210 {
211 if (criterion != null)
212 {
213 this.criterion.setFallbackOrder(order);
214 }
215 }
216
217 public String getFallbackType()
218 {
219 if (criterion == null)
220 {
221 return FALLBACK_CONTINUE;
222 }
223 int type = criterion.getFallbackType();
224 switch (type)
225 {
226 case RuleCriterion.FALLBACK_CONTINUE:
227 return FALLBACK_CONTINUE;
228 case RuleCriterion.FALLBACK_LOOP:
229 return FALLBACK_LOOP;
230 default:
231 return FALLBACK_STOP;
232 }
233 }
234
235 public void setFallbackType(String type)
236 {
237 if (criterion != null)
238 {
239 if (type.equals(FALLBACK_CONTINUE))
240 {
241 this.criterion.setFallbackType(RuleCriterion.FALLBACK_CONTINUE);
242 }
243 else if (type.equals(FALLBACK_LOOP))
244 {
245 this.criterion.setFallbackType(RuleCriterion.FALLBACK_LOOP);
246 }
247 else
248 {
249 this.criterion.setFallbackType(RuleCriterion.FALLBACK_STOP);
250 }
251 }
252 }
253
254
255 public String saveCriterion()
256 {
257 try
258 {
259 if (isNew)
260 {
261 criterion.setRuleId(rule.getId());
262 rule.getRuleCriteria().add(criterion);
263 }
264 String value = criterion.getValue();
265 if (value != null)
266 {
267 value = value.trim();
268 if (value.equals(""))
269 {
270 criterion.setValue(null);
271 }
272 }
273 profiler.storeProfilingRule(rule);
274 isNew = false;
275 }
276 catch (Exception e)
277 {
278
279 System.out.println("Failed to UPDATE: rule = " + rule.getId());
280 return "gotoCriterionForm";
281 }
282 return "returnFromCriterion";
283 }
284
285 public String removeCriterion()
286 {
287 try
288 {
289 if (!isNew)
290 {
291 rule.getRuleCriteria().remove(criterion);
292 profiler.storeProfilingRule(rule);
293 isNew = true;
294 }
295 }
296 catch (Exception e)
297 {
298
299 System.out.println("Failed to UPDATE: rule = " + rule.getId());
300 return "gotoCriterionForm";
301 }
302 return "returnFromCriterion";
303 }
304
305 }