KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > tigris > scarab > actions > MoveIssue


1 package org.tigris.scarab.actions;
2
3 /* ================================================================
4  * Copyright (c) 2000-2002 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 java.util.ArrayList JavaDoc;
50 import java.util.HashSet JavaDoc;
51 import java.util.Iterator JavaDoc;
52 import java.util.List JavaDoc;
53 import java.util.Set JavaDoc;
54
55 import org.apache.fulcrum.intake.model.Group;
56 import org.apache.fulcrum.parser.ParameterParser;
57 import org.apache.turbine.RunData;
58 import org.apache.turbine.TemplateContext;
59 import org.apache.turbine.Turbine;
60 import org.apache.turbine.tool.IntakeTool;
61 import org.tigris.scarab.actions.base.BaseModifyIssue;
62 import org.tigris.scarab.om.AttributePeer;
63 import org.tigris.scarab.om.AttributeValuePeer;
64 import org.tigris.scarab.om.Issue;
65 import org.tigris.scarab.om.IssueType;
66 import org.tigris.scarab.om.IssueTypeManager;
67 import org.tigris.scarab.om.Module;
68 import org.tigris.scarab.om.ModuleManager;
69 import org.tigris.scarab.om.ScarabUser;
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.localization.L10NKeySet;
74 import org.tigris.scarab.tools.localization.L10NMessage;
75 import org.tigris.scarab.util.Email;
76 import org.tigris.scarab.util.EmailContext;
77 import org.tigris.scarab.util.Log;
78 import org.tigris.scarab.util.ScarabConstants;
79
80 /**
81  * This class is responsible for moving/copying an issue
82  * from one module to another.
83  *
84  * @author <a HREF="mailto:elicia@collab.net">Elicia David</a>
85  * @author <a HREF="mailto:jon@collab.net">Jon S. Stevens</a>
86  * @version $Id: MoveIssue.java 9671 2005-05-02 19:29:43Z jorgeuriarte $
87  */

88 public class MoveIssue extends BaseModifyIssue
89 {
90
91     /**
92      * From MoveIssue.vm -> MoveIssue2.vm, we only need to validate the inputs.
93      * Intake + Pull is so friggen cool.
94      */

95     public void doValidate(RunData data, TemplateContext context)
96         throws Exception JavaDoc
97     {
98         boolean collisionOccurred = isCollision(data, context);
99         context.put("collisionDetectedOnMoveAttempt",
100                     collisionOccurred ? Boolean.TRUE : Boolean.FALSE);
101         if (collisionOccurred)
102         {
103             // Report the collision to the user.
104
doCancel(data, context);
105             return;
106         }
107
108         IntakeTool intake = getIntakeTool(context);
109         if (!intake.isAllValid())
110         {
111             return;
112         }
113
114         ScarabLocalizationTool l10n = getLocalizationTool(context);
115         ScarabRequestTool scarabR = getScarabRequestTool(context);
116         String JavaDoc[] issueIds = data.getParameters().getStrings("issue_ids");
117         List JavaDoc issues = new ArrayList JavaDoc();
118         Issue issue = null;
119         if (issueIds == null || issueIds.length == 0)
120         {
121             scarabR.setAlertMessage(L10NKeySet.SelectIssueToMove);
122             return;
123         }
124         else
125         {
126             for (int i= 0; i<issueIds.length; i++)
127             {
128                 issues.add(scarabR.getIssue(issueIds[i]));
129             }
130             issue = (Issue)issues.get(0);
131         }
132
133         Module oldModule = issue.getModule();
134         Group moveIssue = intake.get("MoveIssue",
135                           IntakeTool.DEFAULT_KEY, false);
136         String JavaDoc[] modIssueTypes =
137             data.getParameters().getStrings("mod_issuetype");
138         String JavaDoc modIssueType = null;
139         if (modIssueTypes != null)
140         {
141             for (int i=0; i<modIssueTypes.length; i++)
142             {
143                 String JavaDoc testOption = modIssueTypes[i];
144                 if (testOption != null && testOption.length() > 0)
145                 {
146                     if (modIssueType == null)
147                     {
148                         modIssueType = testOption;
149                     }
150                     else
151                     {
152                         scarabR.setAlertMessage(L10NKeySet.OnlySelectOneDestination);
153                         return;
154                     }
155                 }
156             }
157
158         }
159
160         if (modIssueType == null)
161         {
162             scarabR.setAlertMessage(L10NKeySet.SelectModuleAndIssueType);
163             return;
164         }
165         
166         Integer JavaDoc newModuleId = null;
167         Integer JavaDoc newIssueTypeId = null;
168         Module newModule = null;
169         try
170         {
171             newModuleId = new Integer JavaDoc(modIssueType.
172                       substring(0, modIssueType.indexOf('_')));
173             newIssueTypeId = new Integer JavaDoc(modIssueType.
174                       substring(modIssueType.indexOf('_')+1, modIssueType.length()));
175             newModule = ModuleManager
176                                .getInstance(newModuleId);
177
178         }
179         catch (Exception JavaDoc e)
180         {
181             scarabR.setAlertMessage(L10NKeySet.SelectModuleAndIssueType);
182             return;
183         }
184           
185         String JavaDoc selectAction = moveIssue.get("Action").toString();
186         ScarabUser user = (ScarabUser)data.getUser();
187         boolean changeModule = !newModuleId.equals(oldModule.getModuleId());
188         boolean changeIssueType = !newIssueTypeId
189             .equals(issue.getIssueType().getIssueTypeId());
190
191         // Check permissions
192
// Must have ISSUE_ENTER in new module
193
// If moving to a new module, must have ISSUE_MOVE in old module
194
// If moving to a new issue type, must have ISSUE_EDIT in old module
195
if (!user.hasPermission(ScarabSecurity.ISSUE__ENTER, newModule))
196         {
197             data.setMessage(l10n.get(NO_PERMISSION_MESSAGE));
198             return;
199         }
200         if ("move".equals(selectAction))
201         {
202             if (changeModule &&
203                 !user.hasPermission(ScarabSecurity.ISSUE__MOVE, oldModule) ||
204                 (changeIssueType &&
205                 !user.hasPermission(ScarabSecurity.ISSUE__EDIT, oldModule)))
206             {
207                 data.setMessage(l10n.get(NO_PERMISSION_MESSAGE));
208                 return;
209             }
210         }
211         // Do not allow user to move issue if source and destination
212
// Module and issue type are the same
213
if ("move".equals(selectAction) && !changeModule && !changeIssueType)
214         {
215             scarabR.setAlertMessage(L10NKeySet.CannotMoveToSameModule);
216             return;
217         }
218        
219         context.put("newModuleId", newModuleId.toString());
220         context.put("newIssueTypeId", newIssueTypeId.toString());
221         String JavaDoc nextTemplate = getNextTemplate(data);
222         setTarget(data, nextTemplate);
223     }
224
225     /**
226      * Deals with moving or copying an issue from one module to
227      * another module.
228      */

229     public void doSaveissue(RunData data, TemplateContext context)
230         throws Exception JavaDoc
231     {
232         boolean collisionOccurred = isCollision(data, context);
233         context.put("collisionDetectedOnMoveAttempt", collisionOccurred ? Boolean.TRUE : Boolean.FALSE);
234         if (collisionOccurred)
235         {
236             // Report the collision to the user.
237
setTarget(data, "ViewIssue.vm");
238             return;
239         }
240
241         IntakeTool intake = getIntakeTool(context);
242         ScarabRequestTool scarabR = getScarabRequestTool(context);
243         if (!intake.isAllValid())
244         {
245             return;
246         }
247
248         ScarabLocalizationTool l10n = getLocalizationTool(context);
249         String JavaDoc[] issueIds = data.getParameters().getStrings("issue_ids");
250         List JavaDoc issues = new ArrayList JavaDoc();
251         Issue issue = null;
252         if (issueIds == null || issueIds.length == 0)
253         {
254             scarabR.setAlertMessage(L10NKeySet.SelectIssueToMove);
255             return;
256         }
257         else
258         {
259             for (int i= 0; i<issueIds.length; i++)
260             {
261                 issues.add(scarabR.getIssue(issueIds[i]));
262             }
263             issue = (Issue)issues.get(0);
264         }
265
266         Module oldModule = issue.getModule();
267         IssueType oldIssueType = issue.getIssueType();
268         Group moveIssue = intake.get("MoveIssue",
269                           IntakeTool.DEFAULT_KEY, false);
270         Integer JavaDoc newModuleId = ((Integer JavaDoc) moveIssue.get("ModuleId").
271             getValue());
272         Integer JavaDoc newIssueTypeId = ((Integer JavaDoc) moveIssue.get("IssueTypeId").
273             getValue());
274         Module newModule = ModuleManager
275                .getInstance(newModuleId);
276         IssueType newIssueType = IssueTypeManager
277                .getInstance(newIssueTypeId);
278         String JavaDoc selectAction = moveIssue.get("Action").toString();
279         ScarabUser user = (ScarabUser)data.getUser();
280
281         // Get selected non-matching attributes to save in comment
282
List JavaDoc commentAttrs = new ArrayList JavaDoc();
283         List JavaDoc commentUserValues = new ArrayList JavaDoc();
284         ParameterParser params = data.getParameters();
285         Object JavaDoc[] keys = params.getKeys();
286         for (int i=0; i<keys.length; i++)
287         {
288             String JavaDoc key = (String JavaDoc) keys[i];
289             if (key.startsWith("comment_attr_ids_"))
290             {
291                 commentAttrs.add(scarabR
292                         .getAttribute(new Integer JavaDoc(key.substring(17))));
293             }
294             if (key.startsWith("comment_user_attval_"))
295             {
296                 Long JavaDoc valueId = new Long JavaDoc(key.substring(20));
297                 commentUserValues.add(AttributeValuePeer.retrieveByPK(valueId));
298             }
299         }
300         String JavaDoc reason = params.getString("reason");
301         if (reason == null || reason.trim().length() == 0)
302         {
303             scarabR.setAlertMessage(L10NKeySet.ReasonRequired);
304             return;
305         }
306
307         Issue newIssue = null;
308         for (int i=0; i<issues.size(); i++)
309         {
310             issue = (Issue)issues.get(i);
311             // Do the copy/move
312
try
313             {
314                 newIssue = issue.move(newModule, newIssueType,
315                                       selectAction, user,
316                                       reason, commentAttrs, commentUserValues);
317             }
318             catch (Exception JavaDoc e)
319             {
320                 L10NMessage l10nMessage = new L10NMessage(L10NKeySet.ErrorExceptionMessage,e);
321                 scarabR.setAlertMessage(l10nMessage);
322                 Log.get().warn("Exception during issue copy/move", e);
323                 return;
324             }
325
326             
327             // Send notification email
328
EmailContext ectx = new EmailContext();
329             ectx.setIssue(newIssue);
330             ectx.setModule(newModule);
331             // placed in the context for the email to be able to access them
332
// from within the email template
333
ectx.put("reason", reason);
334             ectx.put("action", selectAction);
335             ectx.put("oldModule", oldModule);
336             ectx.put("oldIssueType", oldIssueType);
337             ectx.put("oldIssue", issue);
338             if (selectAction.equals("copy"))
339             {
340                 ectx.setDefaultTextKey("CopiedIssueEmailSubject");
341             }
342             else
343             {
344                 ectx.setDefaultTextKey("MovedIssueEmailSubject");
345             }
346
347             String JavaDoc[] replyToUser = newModule.getSystemEmail();
348             String JavaDoc template = Turbine.getConfiguration().
349                getString("scarab.email.moveissue.template",
350                          "MoveIssue.vm");
351             Set JavaDoc allToUsers =
352                 issue.getAllUsersToEmail(AttributePeer.EMAIL_TO);
353             HashSet JavaDoc toUsers = new HashSet JavaDoc();
354             Set JavaDoc allCCUsers = issue.getAllUsersToEmail(AttributePeer.CC_TO);
355             HashSet JavaDoc ccUsers = new HashSet JavaDoc();
356
357             for (Iterator JavaDoc iter = allToUsers.iterator(); iter.hasNext();)
358             {
359                 ScarabUser su = (ScarabUser)iter.next();
360                 if (su.hasPermission(ScarabSecurity.ISSUE__VIEW, newModule))
361                 {
362                     toUsers.add(su);
363                 }
364             }
365             for (Iterator JavaDoc iter = allCCUsers.iterator(); iter.hasNext();)
366             {
367                 ScarabUser su = (ScarabUser)iter.next();
368                 if (su.hasPermission(ScarabSecurity.ISSUE__VIEW, newModule))
369                 {
370                     ccUsers.add(su);
371                 }
372             }
373             try
374             {
375                 Email.sendEmail(ectx, newModule, user, replyToUser,
376                                 toUsers, ccUsers, template);
377             }
378             catch (Exception JavaDoc e)
379             {
380                 L10NMessage l10nMessage = new L10NMessage(EMAIL_ERROR,e);
381                  scarabR.setAlertMessage(l10nMessage);
382             }
383         }
384
385         // Redirect to moved or copied issue
386
if (issues.size() == 1)
387         {
388             data.getParameters().remove("id");
389             data.getParameters().add("id", newIssue.getUniqueId().toString());
390             setTarget(data, "ViewIssue.vm");
391         }
392         else
393         {
394             setTarget(data, "IssueList.vm");
395         }
396
397         scarabR.setConfirmMessage(DEFAULT_MSG);
398     }
399
400     /**
401      * This manages clicking the Back button on MoveIssue2.vm
402      */

403     public void doBacktoone(RunData data, TemplateContext context) throws Exception JavaDoc
404     {
405         setTarget(data, data.getParameters()
406             .getString(ScarabConstants.CANCEL_TEMPLATE, "MoveIssue.vm"));
407     }
408 }
409
Popular Tags