001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.jetspeed.portlets.profiler;
018:
019: import java.io.Serializable;
020: import java.util.Iterator;
021: import java.util.Map;
022:
023: import javax.faces.context.FacesContext;
024: import javax.faces.event.ActionEvent;
025: import javax.faces.model.SelectItem;
026:
027: import org.apache.jetspeed.CommonPortletServices;
028: import org.apache.jetspeed.profiler.Profiler;
029: import org.apache.jetspeed.profiler.rules.ProfilingRule;
030: import org.apache.jetspeed.profiler.rules.RuleCriterion;
031:
032: /**
033: * Criterion state.
034: *
035: * @author <a href="mailto:taylor@apache.org">David Sean Taylor</a>
036: * @version $Id: ProfileCriterionForm.java 348264 2005-11-22 22:06:45Z taylor $
037: */
038: public class ProfileCriterionForm implements Serializable {
039: private boolean isNew = false;
040: private transient Profiler profiler = null;
041: private transient RuleCriterion criterion = null;
042: private transient ProfilingRule rule = null;
043:
044: private static final String FALLBACK_CONTINUE = "Continue";
045: private static final String FALLBACK_STOP = "Stop";
046: private static final String FALLBACK_LOOP = "Loop";
047:
048: private static final SelectItem[] RESOLVERS = {
049: new SelectItem("request"), new SelectItem("session"),
050: new SelectItem("request.session"),
051: new SelectItem("hard.coded"),
052: new SelectItem("group.role.user"), new SelectItem("user"),
053: new SelectItem("group"), new SelectItem("role"),
054: new SelectItem("mediatype"), new SelectItem("country"),
055: new SelectItem("language"), new SelectItem("roles"),
056: new SelectItem("path"), new SelectItem("page"),
057: new SelectItem("path.session"),
058: new SelectItem("user.attribute"),
059: new SelectItem("navigation"), new SelectItem("ip"),
060: new SelectItem("hostname") };
061:
062: private static final SelectItem[] FALLBACK_TYPES = {
063: new SelectItem(FALLBACK_CONTINUE),
064: new SelectItem(FALLBACK_LOOP),
065: new SelectItem(FALLBACK_STOP) };
066:
067: public ProfileCriterionForm() {
068: }
069:
070: public boolean getUpdating() {
071: return !isNew;
072: }
073:
074: public SelectItem[] getResolvers() {
075: return RESOLVERS;
076: }
077:
078: public SelectItem[] getFallbackTypes() {
079: return FALLBACK_TYPES;
080: }
081:
082: public void listen(ActionEvent event) {
083: Map appMap = FacesContext.getCurrentInstance()
084: .getExternalContext().getApplicationMap();
085: profiler = (Profiler) appMap
086: .get(CommonPortletServices.CPS_PROFILER_COMPONENT);
087: Map params = FacesContext.getCurrentInstance()
088: .getExternalContext().getRequestParameterMap();
089: String selectedRule = (String) params.get("selectedRule");
090:
091: if (selectedRule != null && profiler != null) {
092: rule = profiler.getRule(selectedRule);
093: if (rule != null) {
094: String selected = (String) params
095: .get("selectedCriterion");
096: if (selected == null || selected.length() == 0) {
097: isNew = true;
098: try {
099: // 103006 HJB, replace loadClass with bean factory
100: // method
101: // Class defaultClass =
102: // profiler.getClass().getClassLoader().loadClass("org.apache.jetspeed.profiler.rules.impl.RuleCriterionImpl");
103: // this.criterion =
104: // (RuleCriterion)defaultClass.newInstance();
105: this .criterion = profiler.createRuleCriterion();
106: } catch (Exception e) {
107: System.out
108: .println("Failed to CREATE NEW: rule = "
109: + rule.getId());
110: // TODO: forward to an error page
111: }
112: } else {
113: Iterator it = rule.getRuleCriteria().iterator();
114: while (it.hasNext()) {
115: RuleCriterion c = (RuleCriterion) it.next();
116: if (c.getName().equals(selected)) {
117: criterion = c;
118: isNew = false;
119: break;
120: }
121: }
122: }
123: }
124: }
125: }
126:
127: public String getName() {
128: if (criterion == null) {
129: return "{empty}";
130: }
131: return criterion.getName();
132: }
133:
134: public void setName(String name) {
135: if (criterion != null) {
136: this .criterion.setName(name);
137: }
138: }
139:
140: public String getValue() {
141: if (criterion == null) {
142: return "{empty}";
143: }
144: return criterion.getValue();
145: }
146:
147: public void setValue(String value) {
148: if (criterion != null) {
149: this .criterion.setValue(value);
150: }
151: }
152:
153: public String getResolver() {
154: if (criterion == null) {
155: return "{empty}";
156: }
157: return criterion.getType();
158: }
159:
160: public void setResolver(String resolver) {
161: if (criterion != null) {
162: this .criterion.setType(resolver);
163: }
164: }
165:
166: public int getFallbackOrder() {
167: if (criterion == null) {
168: return 0;
169: }
170: return criterion.getFallbackOrder();
171: }
172:
173: public void setFallbackOrder(int order) {
174: if (criterion != null) {
175: this .criterion.setFallbackOrder(order);
176: }
177: }
178:
179: public String getFallbackType() {
180: if (criterion == null) {
181: return FALLBACK_CONTINUE;
182: }
183: int type = criterion.getFallbackType();
184: switch (type) {
185: case RuleCriterion.FALLBACK_CONTINUE:
186: return FALLBACK_CONTINUE;
187: case RuleCriterion.FALLBACK_LOOP:
188: return FALLBACK_LOOP;
189: default:
190: return FALLBACK_STOP;
191: }
192: }
193:
194: public void setFallbackType(String type) {
195: if (criterion != null) {
196: if (type.equals(FALLBACK_CONTINUE)) {
197: this .criterion
198: .setFallbackType(RuleCriterion.FALLBACK_CONTINUE);
199: } else if (type.equals(FALLBACK_LOOP)) {
200: this .criterion
201: .setFallbackType(RuleCriterion.FALLBACK_LOOP);
202: } else {
203: this .criterion
204: .setFallbackType(RuleCriterion.FALLBACK_STOP);
205: }
206: }
207: }
208:
209: // actions
210: public String saveCriterion() {
211: try {
212: if (isNew) {
213: criterion.setRuleId(rule.getId());
214: rule.getRuleCriteria().add(criterion);
215: }
216: String value = criterion.getValue();
217: if (value != null) {
218: value = value.trim();
219: if (value.equals("")) {
220: criterion.setValue(null);
221: }
222: }
223: profiler.storeProfilingRule(rule);
224: isNew = false;
225: } catch (Exception e) {
226: // TODO: handle errors better
227: System.out.println("Failed to UPDATE: rule = "
228: + rule.getId());
229: return "gotoCriterionForm";
230: }
231: return "returnFromCriterion";
232: }
233:
234: public String removeCriterion() {
235: try {
236: if (!isNew) {
237: rule.getRuleCriteria().remove(criterion);
238: profiler.storeProfilingRule(rule);
239: isNew = true;
240: }
241: } catch (Exception e) {
242: // TODO: handle errors better
243: System.out.println("Failed to UPDATE: rule = "
244: + rule.getId());
245: return "gotoCriterionForm";
246: }
247: return "returnFromCriterion";
248: }
249:
250: }
|