KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.tigris.scarab.actions;
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 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.Map JavaDoc;
54 import java.util.Set JavaDoc;
55
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.tigris.scarab.actions.base.BaseModifyIssue;
61 import org.tigris.scarab.om.ActivitySet;
62 import org.tigris.scarab.om.Attachment;
63 import org.tigris.scarab.om.Attribute;
64 import org.tigris.scarab.om.AttributeManager;
65 import org.tigris.scarab.om.AttributeValue;
66 import org.tigris.scarab.om.Issue;
67 import org.tigris.scarab.om.ScarabUser;
68 import org.tigris.scarab.om.ScarabUserManager;
69 import org.tigris.scarab.tools.ScarabLocalizationTool;
70 import org.tigris.scarab.tools.ScarabRequestTool;
71 import org.tigris.scarab.tools.localization.L10NKeySet;
72 import org.tigris.scarab.tools.localization.L10NMessage;
73 import org.tigris.scarab.util.EmailContext;
74
75 /**
76  * This class is responsible for assigning users to attributes.
77  *
78  * @author <a HREF="mailto:jmcnally@collab.net">John D. McNally</a>
79  * @version $Id: AssignIssue.java 9255 2004-11-14 21:07:04Z dep4b $
80  */

81 public class AssignIssue extends BaseModifyIssue
82 {
83     private static final String JavaDoc ADD_USER = "add_user";
84     private static final String JavaDoc SELECTED_USER = "select_user";
85         
86     /**
87      * Adds users to temporary working list.
88      */

89     public void doAdd(RunData data, TemplateContext context)
90         throws Exception JavaDoc
91     {
92         ScarabUser user = (ScarabUser)data.getUser();
93         ScarabRequestTool scarabR = getScarabRequestTool(context);
94         ScarabLocalizationTool l10n = getLocalizationTool(context);
95         Map JavaDoc userMap = user.getAssociatedUsersMap();
96         ParameterParser params = data.getParameters();
97         String JavaDoc[] userIds = params.getStrings(ADD_USER);
98         if (userIds != null && userIds.length > 0)
99         {
100             boolean isUserAttrRemoved = false;
101             StringBuffer JavaDoc msg = new StringBuffer JavaDoc();
102             List JavaDoc removedUserAttrs = null;
103             for (int i =0; i<userIds.length; i++)
104             {
105                 List JavaDoc item = new ArrayList JavaDoc();
106                 String JavaDoc userId = userIds[i];
107                 String JavaDoc attrId = params.get("user_attr_" + userId);
108                 Attribute attribute = AttributeManager
109                     .getInstance(new Integer JavaDoc(attrId));
110                 ScarabUser su = ScarabUserManager
111                     .getInstance(new Integer JavaDoc(userId));
112                 item.add(attribute);
113                 item.add(su);
114                 List JavaDoc issues = scarabR.getAssignIssuesList();
115                 for (int j=0; j<issues.size(); j++)
116                 {
117                     Issue issue = (Issue)issues.get(j);
118                     Long JavaDoc issueId = issue.getIssueId();
119                     Set JavaDoc userList = (Set JavaDoc) userMap.get(issueId);
120                     if (userList == null)
121                     {
122                         userList = new HashSet JavaDoc();
123                     }
124                     List JavaDoc attributeList = scarabR.getCurrentModule()
125                         .getUserAttributes(issue.getIssueType(), true);
126                     if (!attributeList.contains(attribute))
127                     {
128                         if (removedUserAttrs == null)
129                         {
130                             removedUserAttrs = new ArrayList JavaDoc();
131                             removedUserAttrs.add(attribute);
132                             msg.append("'").append(attribute.getName())
133                                 .append("'");
134                         }
135                         else if (!removedUserAttrs.contains(attribute))
136                         {
137                             removedUserAttrs.add(attribute);
138                             msg.append(", '").append(attribute.getName())
139                                 .append("'");
140                         }
141                         isUserAttrRemoved = true;
142                     }
143                     else
144                     {
145                         userList.add(item);
146                         // userMap.put(issueId, userList);
147
// user.setAssociatedUsersMap(userMap);
148
}
149                 }
150             }
151             if (!isUserAttrRemoved)
152             {
153                 scarabR.setConfirmMessage(L10NKeySet.SelectedUsersWereAdded);
154             }
155             else
156             {
157                 L10NMessage l10nMsg = new L10NMessage(L10NKeySet.UserAttributeRemoved,
158                                                   msg.toString());
159                 scarabR.setAlertMessage(l10nMsg);
160             }
161         }
162         else
163         {
164             scarabR.setAlertMessage(L10NKeySet.NoUsersSelected);
165         }
166     }
167         
168     /**
169      * Removes users from temporary working list.
170      */

171     private void remove(RunData data, TemplateContext context, Long JavaDoc issueId)
172         throws Exception JavaDoc
173     {
174         ScarabUser user = (ScarabUser)data.getUser();
175         ScarabRequestTool scarabR = getScarabRequestTool(context);
176         ScarabLocalizationTool l10n = getLocalizationTool(context);
177         Set JavaDoc userList = (Set JavaDoc) user.getAssociatedUsersMap().get(issueId);
178         ParameterParser params = data.getParameters();
179         String JavaDoc[] selectedUsers = params.getStrings(SELECTED_USER);
180         if (selectedUsers != null && selectedUsers.length > 0)
181         {
182             for (int i =0; i<selectedUsers.length; i++)
183             {
184                 List JavaDoc item = new ArrayList JavaDoc(2);
185                 String JavaDoc selectedUser = selectedUsers[i];
186                 String JavaDoc userId = selectedUser.substring(1, selectedUser.indexOf('_')-1);
187                 String JavaDoc attrId = selectedUser.substring(selectedUser.indexOf('_')+1, selectedUser.length());
188                 Attribute attribute = AttributeManager
189                     .getInstance(new Integer JavaDoc(attrId));
190                 ScarabUser su = ScarabUserManager
191                     .getInstance(new Integer JavaDoc(userId));
192                 item.add(attribute);
193                 item.add(su);
194                 userList.remove(item);
195             }
196             scarabR.setConfirmMessage(L10NKeySet.SelectedUsersWereRemoved);
197         }
198         else
199         {
200             scarabR.setAlertMessage(L10NKeySet.NoUsersSelected);
201         }
202     }
203
204     /**
205      * Changes the user attribute a user is associated with.
206      */

207     private void update(RunData data, TemplateContext context, Long JavaDoc issueId)
208         throws Exception JavaDoc
209     {
210         ScarabUser user = (ScarabUser)data.getUser();
211         ScarabRequestTool scarabR = getScarabRequestTool(context);
212         ScarabLocalizationTool l10n = getLocalizationTool(context);
213         Set JavaDoc userList = (Set JavaDoc) user.getAssociatedUsersMap().get(issueId);
214         ParameterParser params = data.getParameters();
215         String JavaDoc[] selectedUsers = params.getStrings(SELECTED_USER);
216         if (selectedUsers != null && selectedUsers.length > 0)
217         {
218             for (int i =0; i < selectedUsers.length; i++)
219             {
220                 String JavaDoc selectedUser = selectedUsers[i];
221                 String JavaDoc userId = selectedUser.substring(1, selectedUser.indexOf('_')-1);
222                 String JavaDoc attrId = selectedUser.substring(selectedUser.indexOf('_')+1, selectedUser.length());
223                 Attribute attribute = AttributeManager
224                     .getInstance(new Integer JavaDoc(attrId));
225                 ScarabUser su = ScarabUserManager
226                     .getInstance(new Integer JavaDoc(userId));
227                 List JavaDoc item = new ArrayList JavaDoc(2);
228                 List JavaDoc newItem = new ArrayList JavaDoc(2);
229                 item.add(attribute);
230                 item.add(su);
231                 userList.remove(item);
232
233                 String JavaDoc newKey = "asso_user_{" + userId + "}_attr_{" + attrId + "}_issue_{" + issueId + '}';
234                 String JavaDoc newAttrId = params.get(newKey);
235                 Attribute newAttribute = AttributeManager
236                      .getInstance(new Integer JavaDoc(newAttrId));
237                 newItem.add(newAttribute);
238                 newItem.add(su);
239                 userList.add(newItem);
240             }
241             scarabR.setConfirmMessage(L10NKeySet.SelectedUsersWereModified);
242         }
243         else
244         {
245             scarabR.setAlertMessage(L10NKeySet.NoUsersSelected);
246         }
247     }
248
249     public void doSave(RunData data, TemplateContext context)
250         throws Exception JavaDoc
251     {
252         ScarabUser user = (ScarabUser)data.getUser();
253         ScarabRequestTool scarabR = getScarabRequestTool(context);
254         ScarabLocalizationTool l10n = getLocalizationTool(context);
255         List JavaDoc issues = null;
256         String JavaDoc singleIssueId = data.getParameters().getString("id");
257         if (singleIssueId != null)
258         {
259             Issue issue = scarabR.getIssue(singleIssueId);
260             if (issue != null)
261             {
262                 issues = new ArrayList JavaDoc();
263                 issues.add(issue);
264             }
265         }
266         else
267         {
268             issues = scarabR.getAssignIssuesList();
269         }
270
271         Map JavaDoc userMap = user.getAssociatedUsersMap();
272         ScarabUser assigner = (ScarabUser)data.getUser();
273         String JavaDoc reason = data.getParameters().getString("reason", "");
274         Attachment attachment = null;
275         ActivitySet activitySet = null;
276         boolean isUserAttrRemoved = false;
277         StringBuffer JavaDoc msg = new StringBuffer JavaDoc();
278         List JavaDoc removedUserAttrs = null;
279
280         for (int i=0; i < issues.size(); i++)
281         {
282             Issue issue = (Issue)issues.get(i);
283             Set JavaDoc userList = (Set JavaDoc) userMap.get(issue.getIssueId());
284             List JavaDoc oldAssignees = issue.getUserAttributeValues();
285             List JavaDoc attributeList = scarabR.getCurrentModule()
286                                 .getUserAttributes(issue.getIssueType(), true);
287             // save attachment with user-provided reason
288
if (reason != null && reason.length() > 0)
289             {
290                 attachment = new Attachment();
291                 attachment.setData(reason);
292                 attachment.setName("comment");
293                 attachment.setTextFields(assigner, issue,
294                                          Attachment.MODIFICATION__PK);
295                 attachment.save();
296             }
297
298             // loops through users in temporary working list
299
for (Iterator JavaDoc iter = userList.iterator(); iter.hasNext();)
300             {
301                 List JavaDoc item = (List JavaDoc)iter.next();
302                 Attribute newAttr = (Attribute)item.get(0);
303                 ScarabUser assignee = (ScarabUser)item.get(1);
304                 Integer JavaDoc assigneeId = assignee.getUserId();
305                 boolean alreadyAssigned = false;
306                 if (!attributeList.contains(newAttr))
307                 {
308                     if (removedUserAttrs == null)
309                     {
310                         removedUserAttrs = new ArrayList JavaDoc();
311                         removedUserAttrs.add(newAttr);
312                         msg.append("'").append(newAttr.getName()).append("'");
313                     }
314                     else if (!removedUserAttrs.contains(newAttr))
315                     {
316                         removedUserAttrs.add(newAttr);
317                         msg.append(", '").append(newAttr.getName()).append("'");
318                     }
319                     isUserAttrRemoved = true;
320                 }
321                 else
322                 {
323                     for (int k=0; k < oldAssignees.size(); k++)
324                     {
325                             AttributeValue oldAttVal = (AttributeValue)
326                                                         oldAssignees.get(k);
327                         Attribute oldAttr = oldAttVal.getAttribute();
328                         // ignore already assigned users
329
if (assigneeId.equals(oldAttVal.getUserId()))
330                         {
331                             // unless user has different attribute id, then
332
// switch their user attribute
333
if (!newAttr.getAttributeId().equals(
334                                                 oldAttr.getAttributeId()))
335                             {
336                                 List JavaDoc tmpItem = new ArrayList JavaDoc();
337                                 tmpItem.add(newAttr);
338                                 tmpItem.add(assignee);
339                                 if (!userList.contains(tmpItem))
340                                 {
341                                     alreadyAssigned = true;
342                                     activitySet = issue.changeUserAttributeValue(
343                                                           activitySet, assignee,
344                                                           assigner, oldAttVal,
345                                                           newAttr, attachment);
346                                 }
347                             }
348                             else
349                             {
350                                 alreadyAssigned = true;
351                             }
352                         }
353                     }
354                     // if user was not already assigned, assign them
355
if (!alreadyAssigned)
356                     {
357                             activitySet = issue.assignUser(activitySet, assignee,
358                                         assigner, newAttr, attachment);
359                     }
360                 }
361             }
362
363             // loops thru previously assigned users to find ones that
364
// have been removed
365
for (int m=0; m < oldAssignees.size(); m++)
366             {
367                 boolean userStillAssigned = false;
368                 AttributeValue oldAttVal = (AttributeValue)oldAssignees.get(m);
369                 for (Iterator JavaDoc iter = userList.iterator(); iter.hasNext();)
370                 {
371                     List JavaDoc item = (List JavaDoc)iter.next();
372                     Attribute attr = (Attribute)item.get(0);
373                     ScarabUser su = (ScarabUser)item.get(1);
374                     if (su.getUserId().equals(oldAttVal.getUserId())
375                         && attr.getAttributeId().equals(oldAttVal.getAttributeId()))
376                     {
377                          userStillAssigned = true;
378                     }
379                 }
380                 if (!userStillAssigned)
381                 {
382                     ScarabUser assignee = scarabR.getUser(oldAttVal.getUserId());
383                     // delete the user
384
activitySet = issue.deleteUser(activitySet, assignee,
385                                                    assigner, oldAttVal, attachment);
386                 }
387             }
388             if (activitySet != null) {
389                 try
390                 {
391                     emailNotify(activitySet, issue);
392                 }
393                 catch(Exception JavaDoc e)
394                 {
395                     L10NMessage l10nMessage = new L10NMessage(EMAIL_ERROR,e);
396                     scarabR.setAlertMessage(l10nMessage);
397                 }
398             }
399         }
400         if (isUserAttrRemoved)
401         {
402             L10NMessage l10nMsg = new L10NMessage(L10NKeySet.UserAttributeRemoved,
403                                                   msg.toString());
404             scarabR.setAlertMessage(l10nMsg);
405         }
406         
407         Object JavaDoc alertMessage = scarabR.getAlertMessage();
408         if (alertMessage == null ||
409             alertMessage.toString().length() == 0)
410         {
411             scarabR.setConfirmMessage(DEFAULT_MSG);
412         }
413     }
414
415     /**
416      * Takes care of giving an email notice about an issue to a list of users
417      * with a comment. If an exception occured, the Exception instance is
418      * returned. Otherwise null is returned.
419      *
420      * @param issue a <code>Issue</code> to notify users about being assigned to.
421      * @param action <code>String</code> text to email to others.
422      */

423     private void emailNotify(ActivitySet activitySet, Issue issue) throws Exception JavaDoc
424     {
425         if (issue == null)
426         {
427             return;
428         }
429
430         String JavaDoc template = Turbine.getConfiguration().
431            getString("scarab.email.assignissue.template",
432                      "ModifyIssue.vm");
433
434         EmailContext ectx = new EmailContext();
435         ectx.setSubjectTemplate("AssignIssueModifyIssueSubject.vm");
436         Exception JavaDoc exception;
437         activitySet.sendEmail(ectx, issue, template);
438     }
439
440
441     public void doPerform(RunData data, TemplateContext context)
442         throws Exception JavaDoc
443     {
444         
445         Object JavaDoc[] keys = data.getParameters().getKeys();
446         try
447         {
448             for (int i =0; i<keys.length; i++)
449             {
450                 String JavaDoc key = keys[i].toString();
451                 if (key.startsWith("eventsubmit_doremove"))
452                 {
453                     String JavaDoc issueId = key.substring(21);
454                     remove(data, context, new Long JavaDoc(issueId));
455                 }
456                 else if (key.startsWith("eventsubmit_doupdate"))
457                 {
458                     String JavaDoc issueId = key.substring(21);
459                     update(data, context, new Long JavaDoc(issueId));
460                 }
461             }
462         }
463         catch (NumberFormatException JavaDoc nfe) // new Integer(issueId) above could fail
464
{
465             getScarabRequestTool(context).setAlertMessage(L10NKeySet.BadIntegerConversion);
466         }
467     }
468
469 }
470
Popular Tags