1 package org.tigris.scarab.actions; 2 3 48 49 import java.util.ArrayList ; 50 import java.util.HashSet ; 51 import java.util.Iterator ; 52 import java.util.List ; 53 import java.util.Map ; 54 import java.util.Set ; 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 81 public class AssignIssue extends BaseModifyIssue 82 { 83 private static final String ADD_USER = "add_user"; 84 private static final String SELECTED_USER = "select_user"; 85 86 89 public void doAdd(RunData data, TemplateContext context) 90 throws Exception 91 { 92 ScarabUser user = (ScarabUser)data.getUser(); 93 ScarabRequestTool scarabR = getScarabRequestTool(context); 94 ScarabLocalizationTool l10n = getLocalizationTool(context); 95 Map userMap = user.getAssociatedUsersMap(); 96 ParameterParser params = data.getParameters(); 97 String [] userIds = params.getStrings(ADD_USER); 98 if (userIds != null && userIds.length > 0) 99 { 100 boolean isUserAttrRemoved = false; 101 StringBuffer msg = new StringBuffer (); 102 List removedUserAttrs = null; 103 for (int i =0; i<userIds.length; i++) 104 { 105 List item = new ArrayList (); 106 String userId = userIds[i]; 107 String attrId = params.get("user_attr_" + userId); 108 Attribute attribute = AttributeManager 109 .getInstance(new Integer (attrId)); 110 ScarabUser su = ScarabUserManager 111 .getInstance(new Integer (userId)); 112 item.add(attribute); 113 item.add(su); 114 List issues = scarabR.getAssignIssuesList(); 115 for (int j=0; j<issues.size(); j++) 116 { 117 Issue issue = (Issue)issues.get(j); 118 Long issueId = issue.getIssueId(); 119 Set userList = (Set ) userMap.get(issueId); 120 if (userList == null) 121 { 122 userList = new HashSet (); 123 } 124 List attributeList = scarabR.getCurrentModule() 125 .getUserAttributes(issue.getIssueType(), true); 126 if (!attributeList.contains(attribute)) 127 { 128 if (removedUserAttrs == null) 129 { 130 removedUserAttrs = new ArrayList (); 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 } 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 171 private void remove(RunData data, TemplateContext context, Long issueId) 172 throws Exception 173 { 174 ScarabUser user = (ScarabUser)data.getUser(); 175 ScarabRequestTool scarabR = getScarabRequestTool(context); 176 ScarabLocalizationTool l10n = getLocalizationTool(context); 177 Set userList = (Set ) user.getAssociatedUsersMap().get(issueId); 178 ParameterParser params = data.getParameters(); 179 String [] 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 item = new ArrayList (2); 185 String selectedUser = selectedUsers[i]; 186 String userId = selectedUser.substring(1, selectedUser.indexOf('_')-1); 187 String attrId = selectedUser.substring(selectedUser.indexOf('_')+1, selectedUser.length()); 188 Attribute attribute = AttributeManager 189 .getInstance(new Integer (attrId)); 190 ScarabUser su = ScarabUserManager 191 .getInstance(new Integer (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 207 private void update(RunData data, TemplateContext context, Long issueId) 208 throws Exception 209 { 210 ScarabUser user = (ScarabUser)data.getUser(); 211 ScarabRequestTool scarabR = getScarabRequestTool(context); 212 ScarabLocalizationTool l10n = getLocalizationTool(context); 213 Set userList = (Set ) user.getAssociatedUsersMap().get(issueId); 214 ParameterParser params = data.getParameters(); 215 String [] 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 selectedUser = selectedUsers[i]; 221 String userId = selectedUser.substring(1, selectedUser.indexOf('_')-1); 222 String attrId = selectedUser.substring(selectedUser.indexOf('_')+1, selectedUser.length()); 223 Attribute attribute = AttributeManager 224 .getInstance(new Integer (attrId)); 225 ScarabUser su = ScarabUserManager 226 .getInstance(new Integer (userId)); 227 List item = new ArrayList (2); 228 List newItem = new ArrayList (2); 229 item.add(attribute); 230 item.add(su); 231 userList.remove(item); 232 233 String newKey = "asso_user_{" + userId + "}_attr_{" + attrId + "}_issue_{" + issueId + '}'; 234 String newAttrId = params.get(newKey); 235 Attribute newAttribute = AttributeManager 236 .getInstance(new Integer (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 251 { 252 ScarabUser user = (ScarabUser)data.getUser(); 253 ScarabRequestTool scarabR = getScarabRequestTool(context); 254 ScarabLocalizationTool l10n = getLocalizationTool(context); 255 List issues = null; 256 String 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 (); 263 issues.add(issue); 264 } 265 } 266 else 267 { 268 issues = scarabR.getAssignIssuesList(); 269 } 270 271 Map userMap = user.getAssociatedUsersMap(); 272 ScarabUser assigner = (ScarabUser)data.getUser(); 273 String reason = data.getParameters().getString("reason", ""); 274 Attachment attachment = null; 275 ActivitySet activitySet = null; 276 boolean isUserAttrRemoved = false; 277 StringBuffer msg = new StringBuffer (); 278 List removedUserAttrs = null; 279 280 for (int i=0; i < issues.size(); i++) 281 { 282 Issue issue = (Issue)issues.get(i); 283 Set userList = (Set ) userMap.get(issue.getIssueId()); 284 List oldAssignees = issue.getUserAttributeValues(); 285 List attributeList = scarabR.getCurrentModule() 286 .getUserAttributes(issue.getIssueType(), true); 287 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 for (Iterator iter = userList.iterator(); iter.hasNext();) 300 { 301 List item = (List )iter.next(); 302 Attribute newAttr = (Attribute)item.get(0); 303 ScarabUser assignee = (ScarabUser)item.get(1); 304 Integer assigneeId = assignee.getUserId(); 305 boolean alreadyAssigned = false; 306 if (!attributeList.contains(newAttr)) 307 { 308 if (removedUserAttrs == null) 309 { 310 removedUserAttrs = new ArrayList (); 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 if (assigneeId.equals(oldAttVal.getUserId())) 330 { 331 if (!newAttr.getAttributeId().equals( 334 oldAttr.getAttributeId())) 335 { 336 List tmpItem = new ArrayList (); 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 (!alreadyAssigned) 356 { 357 activitySet = issue.assignUser(activitySet, assignee, 358 assigner, newAttr, attachment); 359 } 360 } 361 } 362 363 for (int m=0; m < oldAssignees.size(); m++) 366 { 367 boolean userStillAssigned = false; 368 AttributeValue oldAttVal = (AttributeValue)oldAssignees.get(m); 369 for (Iterator iter = userList.iterator(); iter.hasNext();) 370 { 371 List item = (List )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 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 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 alertMessage = scarabR.getAlertMessage(); 408 if (alertMessage == null || 409 alertMessage.toString().length() == 0) 410 { 411 scarabR.setConfirmMessage(DEFAULT_MSG); 412 } 413 } 414 415 423 private void emailNotify(ActivitySet activitySet, Issue issue) throws Exception 424 { 425 if (issue == null) 426 { 427 return; 428 } 429 430 String 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 exception; 437 activitySet.sendEmail(ectx, issue, template); 438 } 439 440 441 public void doPerform(RunData data, TemplateContext context) 442 throws Exception 443 { 444 445 Object [] keys = data.getParameters().getKeys(); 446 try 447 { 448 for (int i =0; i<keys.length; i++) 449 { 450 String key = keys[i].toString(); 451 if (key.startsWith("eventsubmit_doremove")) 452 { 453 String issueId = key.substring(21); 454 remove(data, context, new Long (issueId)); 455 } 456 else if (key.startsWith("eventsubmit_doupdate")) 457 { 458 String issueId = key.substring(21); 459 update(data, context, new Long (issueId)); 460 } 461 } 462 } 463 catch (NumberFormatException nfe) { 465 getScarabRequestTool(context).setAlertMessage(L10NKeySet.BadIntegerConversion); 466 } 467 } 468 469 } 470 | Popular Tags |