KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > tigris > scarab > actions > admin > Approval


1 package org.tigris.scarab.actions.admin;
2
3 /* ================================================================
4  * Copyright (c) 2000-2003 CollabNet. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are
8  * met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in the
15  * documentation and/or other materials provided with the distribution.
16  *
17  * 3. The end-user documentation included with the redistribution, if
18  * any, must include the following acknowlegement: "This product includes
19  * software developed by CollabNet <http://www.Collab.Net/>."
20  * Alternately, this acknowlegement may appear in the software itself, if
21  * and wherever such third-party acknowlegements normally appear.
22  *
23  * 4. The hosted project names must not be used to endorse or promote
24  * products derived from this software without prior written
25  * permission. For written permission, please contact info@collab.net.
26  *
27  * 5. Products derived from this software may not use the "Tigris" or
28  * "Scarab" names nor may "Tigris" or "Scarab" appear in their names without
29  * prior written permission of CollabNet.
30  *
31  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
32  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
33  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
34  * IN NO EVENT SHALL COLLAB.NET OR ITS CONTRIBUTORS BE LIABLE FOR ANY
35  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
37  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
38  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
39  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
40  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
41  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42  *
43  * ====================================================================
44  *
45  * This software consists of voluntary contributions made by many
46  * individuals on behalf of CollabNet.
47  */

48
49 import java.util.Iterator JavaDoc;
50 import java.util.List JavaDoc;
51
52 import org.apache.fulcrum.parser.ParameterParser;
53 import org.apache.fulcrum.security.TurbineSecurity;
54 import org.apache.fulcrum.security.util.AccessControlList;
55 import org.apache.fulcrum.security.util.DataBackendException;
56 import org.apache.torque.om.NumberKey;
57 import org.apache.turbine.RunData;
58 import org.apache.turbine.TemplateContext;
59 import org.apache.turbine.Turbine;
60 import org.tigris.scarab.actions.base.RequireLoginFirstAction;
61 import org.tigris.scarab.om.IssueTemplateInfo;
62 import org.tigris.scarab.om.IssueTemplateInfoPeer;
63 import org.tigris.scarab.om.Module;
64 import org.tigris.scarab.om.PendingGroupUserRole;
65 import org.tigris.scarab.om.Query;
66 import org.tigris.scarab.om.QueryPeer;
67 import org.tigris.scarab.om.ScarabUser;
68 import org.tigris.scarab.om.ScarabUserManager;
69 import org.tigris.scarab.om.Scope;
70 import org.tigris.scarab.services.security.ScarabSecurity;
71 import org.tigris.scarab.tools.ScarabLocalizationTool;
72 import org.tigris.scarab.tools.ScarabRequestTool;
73 import org.tigris.scarab.tools.SecurityAdminTool;
74 import org.tigris.scarab.tools.localization.L10NKeySet;
75 import org.tigris.scarab.tools.localization.L10NMessage;
76 import org.tigris.scarab.tools.localization.LocalizationKey;
77 import org.tigris.scarab.util.Email;
78 import org.tigris.scarab.util.EmailContext;
79 import org.tigris.scarab.util.ScarabConstants;
80 import org.tigris.scarab.util.ScarabException;
81
82 /**
83  * This class is responsible for managing the approval process.
84  *
85  * @author <a HREF="mailto:elicia@collab.net">Elicia David</a>
86  * @version $Id: Approval.java 9504 2005-03-21 00:30:01Z dabbous $
87  */

88 public class Approval extends RequireLoginFirstAction
89 {
90     private static final String JavaDoc REJECT = "reject";
91     private static final String JavaDoc APPROVE = "approve";
92     private static final String JavaDoc COMMENT = "comment";
93
94     private static final Integer JavaDoc QUERY = new Integer JavaDoc(0);
95     private static final Integer JavaDoc ISSUE_ENTRY_TEMPLATE = new Integer JavaDoc(1);
96     private static final Integer JavaDoc COMMENTED = new Integer JavaDoc(2);
97
98     private static final Integer JavaDoc REJECTED = QUERY;
99     private static final Integer JavaDoc APPROVED = ISSUE_ENTRY_TEMPLATE;
100     
101     public void doSubmit(RunData data, TemplateContext context)
102         throws Exception JavaDoc
103     {
104         ScarabRequestTool scarabR = (ScarabRequestTool)context
105             .get(ScarabConstants.SCARAB_REQUEST_TOOL);
106         ScarabLocalizationTool l10n = getLocalizationTool(context);
107         ScarabUser user = (ScarabUser)data.getUser();
108         Module module = scarabR.getCurrentModule();
109         String JavaDoc globalComment = data.getParameters().getString("global_comment");
110        
111         ParameterParser params = data.getParameters();
112         Object JavaDoc[] keys = params.getKeys();
113         String JavaDoc key;
114         String JavaDoc action = null;
115         Integer JavaDoc actionWord = null;
116         Integer JavaDoc artifact = null;
117         String JavaDoc artifactName = null;
118         String JavaDoc comment = null;
119         ScarabUser toUser = null;
120         String JavaDoc userId;
121         boolean success = true;
122
123         for (int i =0; i<keys.length; i++)
124         {
125             action="none";
126             key = keys[i].toString();
127             if (key.startsWith("query_id_"))
128             {
129                String JavaDoc queryId = key.substring(9);
130                Query query = QueryPeer.retrieveByPK(new NumberKey(queryId));
131
132                action = params.getString("query_action_" + queryId);
133                comment = params.getString("query_comment_" + queryId);
134
135                userId = params.getString("query_user_" + queryId);
136                toUser = scarabR.getUser(userId);
137                artifact = QUERY;
138                artifactName = query.getName();
139
140                if (query.getApproved())
141                {
142                    success = false;
143                    boolean isApproved = Scope.MODULE__PK.equals(query.getScopeId());
144                    LocalizationKey l10nKey = (isApproved) ?
145                        L10NKeySet.ItemAlreadyApproved:L10NKeySet.ItemAlreadyRejected;
146                    L10NMessage l10nMessage = new L10NMessage(l10nKey,artifactName);
147                    scarabR.setAlertMessage(l10nMessage);
148                }
149                else
150                {
151                    if (action.equals(REJECT))
152                    {
153                        try
154                        {
155                            query.approve(user, false);
156                        }
157                        catch (ScarabException e)
158                        {
159                            L10NMessage msg = new L10NMessage(L10NKeySet.ExceptionGeneric,e);
160                            scarabR.setAlertMessage(msg);
161                        }
162                        actionWord = REJECTED;
163                    }
164                    else if (action.equals(APPROVE))
165                    {
166                        try
167                        {
168                            query.approve(user, true);
169                        }
170                        catch(ScarabException e)
171                        {
172                            L10NMessage msg = new L10NMessage(L10NKeySet.ExceptionGeneric,e);
173                            scarabR.setAlertMessage(msg);
174                        }
175                        actionWord = APPROVED;
176                        }
177                        else if (action.equals(COMMENT))
178                        {
179                            actionWord = COMMENTED;
180                        }
181                    }
182                }
183                else if (key.startsWith("template_id_"))
184                {
185                    String JavaDoc templateId = key.substring(12);
186                    IssueTemplateInfo info = IssueTemplateInfoPeer
187                                      .retrieveByPK(new NumberKey(templateId));
188
189                action = params.getString("template_action_" + templateId);
190                comment = params.getString("template_comment_" + templateId);
191
192                userId = params.getString("template_user_" + templateId);
193                toUser = scarabR.getUser(userId);
194                artifact = ISSUE_ENTRY_TEMPLATE;
195                artifactName = info.getName();
196
197                if (info.getApproved())
198                {
199                    success = false;
200                    boolean isApproved = Scope.MODULE__PK.equals(info.getScopeId());
201                    LocalizationKey l10nKey = (isApproved) ?
202                        L10NKeySet.ItemAlreadyApproved:L10NKeySet.ItemAlreadyRejected;
203                    L10NMessage l10nMessage = new L10NMessage(l10nKey,artifactName);
204                    scarabR.setAlertMessage(l10nMessage);
205                }
206                else
207                {
208                    if (action.equals(REJECT))
209                    {
210                        try
211                        {
212                            info.approve(user, false);
213                        }
214                        catch(ScarabException e)
215                        {
216                            L10NMessage msg = new L10NMessage(L10NKeySet.ExceptionGeneric,e);
217                            scarabR.setAlertMessage(msg);
218                        }
219                        actionWord = REJECTED;
220                    }
221                    else if (action.equals(APPROVE))
222                    {
223                        try
224                        {
225                            info.approve(user, true);
226                        }
227                        catch(ScarabException e)
228                        {
229                            L10NMessage msg = new L10NMessage(L10NKeySet.ExceptionGeneric,e);
230                            scarabR.setAlertMessage(msg);
231                        }
232                        actionWord = APPROVED;
233                    }
234                    else if (action.equals(COMMENT))
235                    {
236                        actionWord = COMMENTED;
237                    }
238                 }
239                 if (!action.equals("none") && success)
240                 {
241                     // send email
242
EmailContext ectx = new EmailContext();
243                     ectx.setUser(user);
244                     // add specific data to context for email template
245
ectx.put("artifactIndex", artifact);
246                     ectx.put("artifactName", artifactName);
247                     ectx.put("actionIndex", actionWord);
248                     ectx.put("comment", comment);
249                     ectx.put("globalComment", globalComment);
250
251                     String JavaDoc template = Turbine.getConfiguration().
252                         getString("scarab.email.approval.template",
253                                   "Approval.vm");
254                     try
255                     {
256                         Email.sendEmail(ectx, module, user,
257                                         module.getSystemEmail(),
258                                         toUser, template);
259                     }
260                     catch (Exception JavaDoc e)
261                     {
262                         L10NMessage l10nMessage =new L10NMessage(EMAIL_ERROR,e);
263                         scarabR.setAlertMessage(l10nMessage);
264                     }
265                 }
266             }
267         }
268     }
269
270     public void doApproveroles(RunData data, TemplateContext context)
271         throws Exception JavaDoc
272     {
273         String JavaDoc template = getCurrentTemplate(data, null);
274         String JavaDoc nextTemplate = getNextTemplate(data, template);
275         ScarabRequestTool scarabR = getScarabRequestTool(context);
276         ScarabLocalizationTool l10n = getLocalizationTool(context);
277         Module module = scarabR.getCurrentModule();
278         if (((ScarabUser)data.getUser())
279             .hasPermission(ScarabSecurity.USER__APPROVE_ROLES, module))
280         {
281             SecurityAdminTool scarabA = getSecurityAdminTool(context);
282             List JavaDoc pendings = scarabA.getPendingGroupUserRoles(module);
283             Iterator JavaDoc i = pendings.iterator();
284             while (i.hasNext())
285             {
286                 PendingGroupUserRole pending = (PendingGroupUserRole)i.next();
287                 ScarabUser user =
288                     ScarabUserManager.getInstance(pending.getUserId());
289
290                 String JavaDoc checked = data.getParameters()
291                 .getString("user_id_"+user.getUserName());
292
293                 if(checked != null && checked.equals("on"))
294                 {
295                     String JavaDoc role = data.getParameters()
296                         .getString(user.getUserName());
297                     if (role != null && role.length() > 0)
298                     {
299                         if (role.equalsIgnoreCase(l10n.get(L10NKeySet.Deny)))
300                         {
301                             pending.delete();
302                         }
303                         else
304                         {
305                             try
306                             {
307                                 TurbineSecurity.grant(user,
308                                   (org.apache.fulcrum.security.entity.Group)module,
309                                   TurbineSecurity.getRole(role));
310                             }
311                             catch (DataBackendException e)
312                             {
313                                 // maybe the role request was approved
314
// by another admin?
315
AccessControlList acl = TurbineSecurity
316                                         .getACL(user);
317                                 if (acl
318                                         .hasRole(
319                                                 TurbineSecurity.getRole(role),
320                                                 (org.apache.fulcrum.security.entity.Group) module))
321                                 {
322                                     String JavaDoc[] args = {role,
323                                             user.getUserName(),
324                                             module.getRealName()};
325                                     String JavaDoc msg = l10n
326                                             .format(
327                                                     "RolePreviouslyApprovedForUserInModule",
328                                                     args);
329                                     String JavaDoc info = (String JavaDoc) scarabR
330                                             .getInfoMessage();
331                                     if (info == null)
332                                     {
333                                         info = msg;
334                                     }
335                                     else
336                                     {
337                                         info += " " + msg;
338                                     }
339
340                                     scarabR.setInfoMessage(info);
341                                 }
342                                 else
343                                 {
344                                     throw e; //EXCEPTION
345
}
346                             }
347                             pending.delete();
348                         }
349                     }
350                 }
351             }
352             scarabR.setConfirmMessage(L10NKeySet.AllRolesProcessed);
353         }
354         setTarget(data, nextTemplate);
355     }
356
357     /**
358      * Helper method to retrieve the ScarabRequestTool from the Context
359      */

360     private SecurityAdminTool getSecurityAdminTool(TemplateContext context)
361     {
362         return (SecurityAdminTool)context
363             .get(ScarabConstants.SECURITY_ADMIN_TOOL);
364     }
365 }
366
Popular Tags