KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > tigris > scarab > workflow > CheapWorkflow


1 package org.tigris.scarab.workflow;
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 Collab.Net <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 Collab.Net.
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 Collab.Net.
47  */

48
49 import org.tigris.scarab.om.*;
50 import org.tigris.scarab.util.Log;
51
52 import org.apache.torque.TorqueException;
53 import org.apache.fulcrum.security.entity.Role;
54 import org.apache.fulcrum.security.util.AccessControlList;
55 import org.apache.fulcrum.security.util.GroupSet;
56 import org.apache.fulcrum.security.TurbineSecurity;
57 import org.apache.fulcrum.security.entity.Group;
58
59 import java.util.Iterator JavaDoc;
60 import java.util.List JavaDoc;
61
62 /**
63  * Simple implementation of Workflow, relies on the Transition tables, where every record defines a
64  * transition available for a given Role, from an option to another.
65  * <ul>
66  * <li>If there are no transitions defined, it will always return true</li>
67  * <li>If the "from" option of a transition is null, it will mean "Any option", and the "To" will
68  * be available from any option.</li>
69  * <li>If the "to" option of a transition is null, any option will be available from the "From" option.</li>
70  * <li>If both the 'to' and 'from' options are null, the role will be able to change freely from one value
71  * to another.</li>
72  * If any transition happens to have associated Conditions, will only be available when this condition
73  * evals to true.
74  * </ul>
75  */

76 public class CheapWorkflow extends DefaultWorkflow{
77  
78     /**
79      * Returns true if the transition from the option fromOption to toOption is
80      * allowed for the current user.
81      *
82      */

83     public boolean canMakeTransition(ScarabUser user,
84             AttributeOption fromOption, AttributeOption toOption, Issue issue)
85     {
86         boolean result = false;
87         List allTransitions = null;
88         Module module = null;
89         try
90         {
91             if (fromOption.equals(toOption))
92             {
93                 result = true;
94             }
95             else
96             {
97                 if (TransitionPeer.hasDefinedTransitions(toOption
98                         .getAttribute()))
99                 {
100                     allTransitions = TransitionPeer.getTransitions(fromOption,
101                             toOption);
102                     allTransitions = filterConditionalTransitions(allTransitions, issue);
103                     module = issue.getModule();
104                     Iterator JavaDoc iter = allTransitions.iterator();
105                     while (!result && iter.hasNext())
106                     {
107                         Object JavaDoc obj = iter.next();
108                         Transition tran = (Transition) obj;
109                         Role requiredRole = tran.getRole();
110                         if (requiredRole != null)
111                         { // A role is required for this transition to be
112
// allowed
113
try
114                             {
115                                 List modules = user.getModules();
116                                 AccessControlList acl = TurbineSecurity
117                                         .getACL(user);
118                                 GroupSet allGroups = TurbineSecurity
119                                         .getAllGroups();
120                                 Group group = allGroups.getGroup(module
121                                         .getName());
122                                 result = acl.hasRole(requiredRole, group);
123                             }
124                             catch (Exception JavaDoc e)
125                             {
126                                 Log.get(this.getClass().getName())
127                                         .error(
128                                                 "canMakeTransition: getModules(): "
129                                                         + e);
130                             }
131                         }
132                         else
133                         {
134                             result = true;
135                         }
136                     }
137                 }
138                 else
139                 {
140                     result = true;
141                 }
142             }
143         }
144         catch (TorqueException te)
145         {
146             Log.get(this.getClass().getName())
147                     .error("canMakeTransition: " + te);
148         }
149         return result;
150     }
151     
152     /**
153      * Filter the allowed transitions so only those not-conditioned, those whose condition
154      * fulfill, and those not restricted by the blocking condition, will remain.
155      *
156      * @param transitions
157      * @param issue
158      * @return
159      * @throws TorqueException
160      */

161     public List filterConditionalTransitions(List transitions, Issue issue) throws TorqueException
162     {
163         try
164         {
165             boolean blockedIssue = issue.isBlocked();
166             if (transitions != null)
167             {
168                 for (int i=transitions.size()-1; i>=0; i--)
169                 {
170                     Transition tran = (Transition)transitions.get(i);
171                     if (blockedIssue && tran.getDisabledIfBlocked())
172                     {
173                         transitions.remove(i);
174                         continue;
175                         
176                     }
177                     List conditions = tran.getConditions();
178                     if (null != conditions && conditions.size() > 0)
179                     {
180                         boolean bRemove = true;
181                         for (Iterator JavaDoc itReq = conditions.iterator(); bRemove && itReq.hasNext(); )
182                         {
183                             Condition cond = (Condition)itReq.next();
184                             Attribute requiredAttribute = cond.getAttributeOption().getAttribute();
185                             Integer JavaDoc optionId = cond.getOptionId();
186                             Integer JavaDoc issueOptionId = issue.getAttributeValue(requiredAttribute).getOptionId();
187                             if (issueOptionId != null && issueOptionId.equals(optionId))
188                             {
189                                 bRemove = false;
190                             }
191                         }
192                         if (bRemove)
193                         {
194                             transitions.remove(i);
195                         }
196                     }
197                 }
198             }
199         }
200         catch (Exception JavaDoc e)
201         {
202             Log.get().error("filterConditionalTransitions: " + e);
203         }
204
205         return transitions;
206     }
207 }
208
Popular Tags