View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.jetspeed.portlets.profiler;
18  
19  import java.io.Serializable;
20  import java.util.Collection;
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.ProfilerException;
30  import org.apache.jetspeed.profiler.rules.ProfilingRule;
31  
32  /***
33   * Profile state.
34   *
35   * @author <a href="mailto:taylor@apache.org">David Sean Taylor</a>
36   * @version $Id: ProfileRuleForm.java 348264 2005-11-22 22:06:45Z taylor $
37   */
38  public class ProfileRuleForm
39         implements Serializable
40  {
41      private boolean isNew = false;
42      private transient Profiler profiler = null;
43      private transient ProfilingRule rule = null;
44      private static final SelectItem[] CLASSNAMES =
45      {
46              new SelectItem("org.apache.jetspeed.profiler.rules.impl.StandardProfilingRule"),
47              new SelectItem("org.apache.jetspeed.profiler.rules.impl.RoleFallbackProfilingRule")            
48      };
49      
50      public ProfileRuleForm()
51      {
52      }
53      
54      public boolean getUpdating()
55      {
56          return !isNew;
57      }
58      
59      public void listen(ActionEvent event)
60      {        
61          Map appMap = FacesContext.getCurrentInstance().getExternalContext().getApplicationMap();
62          profiler = (Profiler)appMap.get(CommonPortletServices.CPS_PROFILER_COMPONENT);
63          Map params = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
64          String selected = (String)params.get("selectedRule");
65          if (selected != null && profiler != null)
66          {
67              rule = profiler.getRule(selected);
68              isNew = false;
69          }
70      }
71      
72      public SelectItem[] getClassnames()
73      {
74          return CLASSNAMES;
75      }
76      
77      public String getTitle()
78      {        
79          if (rule == null)
80          {
81              return "{empty}";
82          }
83          return rule.getTitle();
84      }
85  
86      public void setTitle(String title)
87      {
88          if (rule != null)
89          {
90              this.rule.setTitle(title);
91          }        
92      }
93      
94      public String getClassname()
95      {        
96          if (rule == null)
97          {
98              return "{empty}";
99          }
100         return rule.getClassname();
101     }
102 
103     public void setClassname(String classname)
104     {
105         if (rule != null)
106         {
107             this.rule.setClassname(classname);
108         }        
109     }
110     
111     public String getId()
112     {
113         if (rule == null)
114         {
115             return "{empty}";
116         }        
117         return rule.getId();
118     }
119     
120     public void setId(String id)
121     {
122         if (rule != null)
123         {
124             this.rule.setId(id);
125         }        
126     }
127     
128     // actions
129     
130     public String saveProfile()
131     {
132         try
133         {
134             profiler.storeProfilingRule(this.rule);
135             isNew = false;
136         }
137         catch (ProfilerException e)
138         {
139             System.out.println("Failed to UPDATE: rule = " + rule.getId());
140             // TODO: forward to an error page
141         }
142         return null;
143     }
144 
145     public String removeProfile()
146     {
147         try
148         {
149             profiler.deleteProfilingRule(rule);
150         }
151         catch (ProfilerException e)
152         {
153             System.out.println("Failed to REMOVE: rule = " + rule.getId());
154             // TODO: forward to an error page
155         }
156         return null;
157     }
158 
159     public String createNewProfile()
160     {
161         try
162         {
163             Class defaultClass = profiler.getClass().getClassLoader().loadClass("org.apache.jetspeed.profiler.rules.impl.StandardProfilingRule");
164             this.rule = (ProfilingRule)defaultClass.newInstance();
165         }
166         catch (Exception e)
167         {
168             System.out.println("Failed to CREATE NEW: rule = " + rule.getId());
169             // TODO: forward to an error page            
170         }
171         this.setId("");
172         this.setTitle("");
173         this.setClassname("org.apache.jetspeed.profiler.rules.impl.StandardProfilingRule");
174         isNew = true;
175         return null;
176     }
177     
178     public Collection getCriteria()
179     {
180         return rule.getRuleCriteria();        
181     }
182 }