KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > infoglue > cms > applications > workflowtool > function > GroupsProvider


1 /* ===============================================================================
2 *
3 * Part of the InfoGlue Content Management Platform (www.infoglue.org)
4 *
5 * ===============================================================================
6 *
7 * Copyright (C)
8 *
9 * This program is free software; you can redistribute it and/or modify it under
10 * the terms of the GNU General Public License version 2, as published by the
11 * Free Software Foundation. See the file LICENSE.html for more information.
12 *
13 * This program is distributed in the hope that it will be useful, but WITHOUT
14 * ANY WARRANTY, including the implied warranty of MERCHANTABILITY or FITNESS
15 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along with
18 * this program; if not, write to the Free Software Foundation, Inc. / 59 Temple
19 * Place, Suite 330 / Boston, MA 02111-1307 / USA.
20 *
21 * ===============================================================================
22 */

23 package org.infoglue.cms.applications.workflowtool.function;
24
25 import java.util.ArrayList JavaDoc;
26 import java.util.HashMap JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.List JavaDoc;
29 import java.util.Map JavaDoc;
30 import java.util.StringTokenizer JavaDoc;
31
32 import org.apache.log4j.Logger;
33 import org.infoglue.cms.controllers.kernel.impl.simple.GroupControllerProxy;
34
35 import com.opensymphony.workflow.WorkflowException;
36
37 public class GroupsProvider extends InfoglueFunction
38 {
39     private final static Logger logger = Logger.getLogger(GroupsProvider.class.getName());
40
41     /**
42      *
43      */

44     public static final String JavaDoc GROUPS_PARAMETER = "groups";
45     
46     /**
47      *
48      */

49     private static final String JavaDoc GROUPS_PROPERTYSET_PREFIX = "groups_";
50     
51     /**
52      *
53      */

54     private static final String JavaDoc MODE_ARGUMENT = "mode";
55     
56     /**
57      *
58      */

59     private static final int REQUEST_MODE = 0;
60     
61     /**
62      *
63      */

64     private static final String JavaDoc REQUEST_MODE_NAME = "request";
65     
66     /**
67      *
68      */

69     private static final int ARGUMENT_MODE = 1;
70     
71     /**
72      *
73      */

74     private static final String JavaDoc ARGUMENT_MODE_NAME = "argument";
75     
76     /**
77      *
78      */

79     private static final int PRINCIPAL_MODE = 2;
80     
81     /**
82      *
83      */

84     private static final String JavaDoc PRINCIPAL_MODE_NAME = "principal";
85     
86     /**
87      *
88      */

89     private static final String JavaDoc GROUPS_ARGUMENT = "groups";
90
91     /**
92      *
93      */

94     private int mode;
95     
96     /**
97      *
98      */

99     private List JavaDoc groups = new ArrayList JavaDoc();
100     
101     /**
102      *
103      */

104     public GroupsProvider()
105     {
106         super();
107     }
108     
109     /**
110      *
111      */

112     protected void execute() throws WorkflowException
113     {
114         switch(mode)
115         {
116         case ARGUMENT_MODE:
117             populateGroupFromAttribute();
118             break;
119         case PRINCIPAL_MODE:
120             populateGroupFromPrincipal();
121             break;
122         default:
123             populateGroupFromRequest();
124             break;
125         }
126         setParameter(GROUPS_PARAMETER, groups);
127     }
128     
129     /**
130      *
131      */

132     private void populateGroupFromAttribute() throws WorkflowException
133     {
134         logger.debug("Populating from attribute.");
135         for(final StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(getArgument(GROUPS_ARGUMENT), ","); st.hasMoreTokens(); )
136         {
137             populateGroup(st.nextToken());
138         }
139     }
140     
141     /**
142      *
143      */

144     private void populateGroupFromPrincipal() throws WorkflowException
145     {
146         logger.debug("Populating from principal.");
147         for(final Iterator JavaDoc i = getPrincipal().getGroups().iterator(); i.hasNext(); )
148         {
149             groups.add(i.next());
150         }
151     }
152     
153     /**
154      *
155      */

156     private void populateGroupFromRequest() throws WorkflowException
157     {
158         logger.debug("Populating from request.");
159         for(final Iterator JavaDoc i = getParameters().keySet().iterator(); i.hasNext(); )
160         {
161             final String JavaDoc key = i.next().toString();
162             if(key.startsWith(GROUPS_PROPERTYSET_PREFIX))
163             {
164                 populateGroup(key.substring(GROUPS_PROPERTYSET_PREFIX.length()));
165             }
166         }
167     }
168
169     /**
170      *
171      */

172     private void populateGroup(final String JavaDoc groupName) throws WorkflowException
173     {
174         try
175         {
176             groups.add(GroupControllerProxy.getController(getDatabase()).getGroup(groupName));
177         }
178         catch(Exception JavaDoc e)
179         {
180             throwException(e);
181         }
182     }
183
184     /**
185      * Method used for initializing the function; will be called before <code>execute</code> is called.
186      * <p><strong>Note</strong>! You must call <code>super.initialize()</code> first.</p>
187      *
188      * @throws WorkflowException if an error occurs during the initialization.
189      */

190     protected void initialize() throws WorkflowException
191     {
192         super.initialize();
193         this.mode = getMode(getArgument(MODE_ARGUMENT, REQUEST_MODE_NAME));
194     }
195     
196     /**
197      *
198      */

199     private int getMode(final String JavaDoc modeName)
200     {
201         final Map JavaDoc modes = new HashMap JavaDoc();
202         modes.put(REQUEST_MODE_NAME, new Integer JavaDoc(REQUEST_MODE));
203         modes.put(ARGUMENT_MODE_NAME, new Integer JavaDoc(ARGUMENT_MODE));
204         modes.put(PRINCIPAL_MODE_NAME, new Integer JavaDoc(PRINCIPAL_MODE));
205         return modes.containsKey(modeName) ? ((Integer JavaDoc) modes.get(modeName)).intValue() : REQUEST_MODE;
206     }
207 }
208
Popular Tags