KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > web > bean > RulesBean


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.web.bean;
18
19 import java.io.Serializable JavaDoc;
20 import java.util.ArrayList JavaDoc;
21 import java.util.Date JavaDoc;
22 import java.util.List JavaDoc;
23 import java.util.Map JavaDoc;
24
25 import javax.faces.context.FacesContext;
26 import javax.faces.event.ActionEvent;
27
28 import org.alfresco.service.cmr.action.Action;
29 import org.alfresco.service.cmr.action.ActionCondition;
30 import org.alfresco.service.cmr.repository.NodeRef;
31 import org.alfresco.service.cmr.rule.Rule;
32 import org.alfresco.service.cmr.rule.RuleService;
33 import org.alfresco.web.app.Application;
34 import org.alfresco.web.app.context.IContextListener;
35 import org.alfresco.web.app.context.UIContextService;
36 import org.alfresco.web.bean.repository.Node;
37 import org.alfresco.web.ui.common.Utils;
38 import org.alfresco.web.ui.common.component.UIActionLink;
39 import org.alfresco.web.ui.common.component.UIModeList;
40 import org.alfresco.web.ui.common.component.data.UIRichList;
41 import org.apache.commons.logging.Log;
42 import org.apache.commons.logging.LogFactory;
43
44 /**
45  * Backing bean for the manage content rules dialog
46  *
47  * @author gavinc
48  */

49 public class RulesBean implements IContextListener
50 {
51    private static final String JavaDoc MSG_ERROR_DELETE_RULE = "error_delete_rule";
52    private static final String JavaDoc LOCAL = "local";
53    private static final String JavaDoc INHERITED = "inherited";
54    
55    private static Log logger = LogFactory.getLog(RulesBean.class);
56    
57    private String JavaDoc viewMode = INHERITED;
58    protected BrowseBean browseBean;
59    protected RuleService ruleService;
60    private List JavaDoc<WrappedRule> rules;
61    private Rule currentRule;
62    private UIRichList richList;
63    
64    
65    /**
66     * Default constructor
67     */

68    public RulesBean()
69    {
70       UIContextService.getInstance(FacesContext.getCurrentInstance()).registerBean(this);
71    }
72    
73    /**
74     * Returns the current view mode the list of rules is in
75     *
76     * @return The current view mode
77     */

78    public String JavaDoc getViewMode()
79    {
80       return this.viewMode;
81    }
82    
83    /**
84     * @return The space to work against
85     */

86    public Node getSpace()
87    {
88       return this.browseBean.getActionSpace();
89    }
90    
91    /**
92     * Returns the list of rules to display
93     *
94     * @return
95     */

96    public List JavaDoc<WrappedRule> getRules()
97    {
98       boolean includeInherited = true;
99       
100       if (this.viewMode.equals(LOCAL))
101       {
102          includeInherited = false;
103       }
104
105       // get the rules from the repository
106
List JavaDoc<Rule> repoRules = this.ruleService.getRules(getSpace().getNodeRef(), includeInherited);
107       this.rules = new ArrayList JavaDoc<WrappedRule>(repoRules.size());
108       
109       // wrap them all passing the current space
110
for (Rule rule : repoRules)
111       {
112          WrappedRule wrapped = new WrappedRule(rule, getSpace().getNodeRef());
113          this.rules.add(wrapped);
114       }
115       
116       return this.rules;
117    }
118    
119    /**
120     * Handles a rule being clicked ready for an action i.e. edit or delete
121     *
122     * @param event The event representing the click
123     */

124    public void setupRuleAction(ActionEvent event)
125    {
126       UIActionLink link = (UIActionLink)event.getComponent();
127       Map JavaDoc<String JavaDoc, String JavaDoc> params = link.getParameterMap();
128       String JavaDoc id = params.get("id");
129       if (id != null && id.length() != 0)
130       {
131          if (logger.isDebugEnabled())
132             logger.debug("Rule clicked, it's id is: " + id);
133          
134          this.currentRule = this.ruleService.getRule(
135                getSpace().getNodeRef(), id);
136          
137          // refresh list
138
contextUpdated();
139       }
140    }
141    
142    /**
143     * Returns the current rule
144     *
145     * @return The current rule
146     */

147    public Rule getCurrentRule()
148    {
149       return this.currentRule;
150    }
151    
152    /**
153     * Handler called upon the completion of the Delete Rule page
154     *
155     * @return outcome
156     */

157    public String JavaDoc deleteOK()
158    {
159       String JavaDoc outcome = null;
160       
161       if (this.currentRule != null)
162       {
163          try
164          {
165             String JavaDoc ruleTitle = this.currentRule.getTitle();
166             
167             this.ruleService.removeRule(getSpace().getNodeRef(),
168                   this.currentRule);
169             
170             // clear the current rule
171
this.currentRule = null;
172             
173             // setting the outcome will show the browse view again
174
outcome = "manageRules";
175             
176             if (logger.isDebugEnabled())
177                logger.debug("Deleted rule '" + ruleTitle + "'");
178          }
179          catch (Throwable JavaDoc err)
180          {
181             Utils.addErrorMessage(Application.getMessage(
182                   FacesContext.getCurrentInstance(), MSG_ERROR_DELETE_RULE) + err.getMessage(), err);
183          }
184       }
185       else
186       {
187          logger.warn("WARNING: deleteOK called without a current Rule!");
188       }
189       
190       return outcome;
191    }
192    
193    /**
194     * Change the current view mode based on user selection
195     *
196     * @param event ActionEvent
197     */

198    public void viewModeChanged(ActionEvent event)
199    {
200       UIModeList viewList = (UIModeList)event.getComponent();
201       this.viewMode = viewList.getValue().toString();
202       
203       // force the list to be re-queried when the page is refreshed
204
if (this.richList != null)
205       {
206          this.richList.setValue(null);
207       }
208    }
209
210    /**
211     * Sets the UIRichList component being used by this backing bean
212     *
213     * @param richList UIRichList component
214     */

215    public void setRichList(UIRichList richList)
216    {
217       this.richList = richList;
218    }
219    
220    /**
221     * Returns the UIRichList component being used by this backing bean
222     *
223     * @return UIRichList component
224     */

225    public UIRichList getRichList()
226    {
227       return this.richList;
228    }
229    
230    /**
231     * @param browseBean The BrowseBean to set.
232     */

233    public void setBrowseBean(BrowseBean browseBean)
234    {
235       this.browseBean = browseBean;
236    }
237    
238    /**
239     * @param ruleService Sets the rule service to use
240     */

241    public void setRuleService(RuleService ruleService)
242    {
243       this.ruleService = ruleService;
244    }
245
246    
247    // ------------------------------------------------------------------------------
248
// IContextListener implementation
249

250    /**
251     * @see org.alfresco.web.app.context.IContextListener#contextUpdated()
252     */

253    public void contextUpdated()
254    {
255       if (this.richList != null)
256       {
257          this.richList.setValue(null);
258       }
259    }
260    
261    
262    /**
263     * Inner class to wrap the Rule objects so we can expose a flag to indicate whether
264     * the rule is a local or inherited rule
265     */

266    public class WrappedRule
267    {
268       private Rule rule;
269       private NodeRef ruleNode;
270       
271       /**
272        * Constructs a RuleWrapper object
273        *
274        * @param rule The rule we are wrapping
275        * @param ruleNode The node the rules belong to
276        */

277       public WrappedRule(Rule rule, NodeRef ruleNode)
278       {
279          this.rule = rule;
280          this.ruleNode = ruleNode;
281       }
282       
283       /**
284        * Returns the rule being wrapped
285        *
286        * @return The wrapped Rule
287        */

288       public Rule getRule()
289       {
290          return this.rule;
291       }
292       
293       /**
294        * Determines whether the current rule is a local rule or
295        * has been inherited from a parent
296        *
297        * @return true if the rule is defined on the current node
298        */

299       public boolean getLocal()
300       {
301          return ruleNode.equals(this.rule.getOwningNodeRef());
302       }
303
304       /** Methods to support sorting of the rules list in a table */
305       
306       /**
307        * Returns the rule id
308        *
309        * @return The id
310        */

311       public String JavaDoc getId()
312       {
313          return this.rule.getId();
314       }
315       
316       /**
317        * Returns the rule title
318        *
319        * @return The title
320        */

321       public String JavaDoc getTitle()
322       {
323          return this.rule.getTitle();
324       }
325       
326       /**
327        * Returns the rule description
328        *
329        * @return The description
330        */

331       public String JavaDoc getDescription()
332       {
333          return this.rule.getDescription();
334       }
335       
336       /**
337        * Returns the created date
338        *
339        * @return The created date
340        */

341       public Date JavaDoc getCreatedDate()
342       {
343          return this.rule.getCreatedDate();
344       }
345       
346       /**
347        * Returns the modfified date
348        *
349        * @return The modified date
350        */

351       public Date JavaDoc getModifiedDate()
352       {
353          return this.rule.getModifiedDate();
354       }
355    }
356 }
357
Popular Tags