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.Set ; 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 88 public class MoveIssue extends BaseModifyIssue 89 { 90 91 95 public void doValidate(RunData data, TemplateContext context) 96 throws Exception 97 { 98 boolean collisionOccurred = isCollision(data, context); 99 context.put("collisionDetectedOnMoveAttempt", 100 collisionOccurred ? Boolean.TRUE : Boolean.FALSE); 101 if (collisionOccurred) 102 { 103 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 [] issueIds = data.getParameters().getStrings("issue_ids"); 117 List issues = new ArrayList (); 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 [] modIssueTypes = 137 data.getParameters().getStrings("mod_issuetype"); 138 String modIssueType = null; 139 if (modIssueTypes != null) 140 { 141 for (int i=0; i<modIssueTypes.length; i++) 142 { 143 String 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 newModuleId = null; 167 Integer newIssueTypeId = null; 168 Module newModule = null; 169 try 170 { 171 newModuleId = new Integer (modIssueType. 172 substring(0, modIssueType.indexOf('_'))); 173 newIssueTypeId = new Integer (modIssueType. 174 substring(modIssueType.indexOf('_')+1, modIssueType.length())); 175 newModule = ModuleManager 176 .getInstance(newModuleId); 177 178 } 179 catch (Exception e) 180 { 181 scarabR.setAlertMessage(L10NKeySet.SelectModuleAndIssueType); 182 return; 183 } 184 185 String 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 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 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 nextTemplate = getNextTemplate(data); 222 setTarget(data, nextTemplate); 223 } 224 225 229 public void doSaveissue(RunData data, TemplateContext context) 230 throws Exception 231 { 232 boolean collisionOccurred = isCollision(data, context); 233 context.put("collisionDetectedOnMoveAttempt", collisionOccurred ? Boolean.TRUE : Boolean.FALSE); 234 if (collisionOccurred) 235 { 236 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 [] issueIds = data.getParameters().getStrings("issue_ids"); 250 List issues = new ArrayList (); 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 newModuleId = ((Integer ) moveIssue.get("ModuleId"). 271 getValue()); 272 Integer newIssueTypeId = ((Integer ) moveIssue.get("IssueTypeId"). 273 getValue()); 274 Module newModule = ModuleManager 275 .getInstance(newModuleId); 276 IssueType newIssueType = IssueTypeManager 277 .getInstance(newIssueTypeId); 278 String selectAction = moveIssue.get("Action").toString(); 279 ScarabUser user = (ScarabUser)data.getUser(); 280 281 List commentAttrs = new ArrayList (); 283 List commentUserValues = new ArrayList (); 284 ParameterParser params = data.getParameters(); 285 Object [] keys = params.getKeys(); 286 for (int i=0; i<keys.length; i++) 287 { 288 String key = (String ) keys[i]; 289 if (key.startsWith("comment_attr_ids_")) 290 { 291 commentAttrs.add(scarabR 292 .getAttribute(new Integer (key.substring(17)))); 293 } 294 if (key.startsWith("comment_user_attval_")) 295 { 296 Long valueId = new Long (key.substring(20)); 297 commentUserValues.add(AttributeValuePeer.retrieveByPK(valueId)); 298 } 299 } 300 String 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 try 313 { 314 newIssue = issue.move(newModule, newIssueType, 315 selectAction, user, 316 reason, commentAttrs, commentUserValues); 317 } 318 catch (Exception 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 EmailContext ectx = new EmailContext(); 329 ectx.setIssue(newIssue); 330 ectx.setModule(newModule); 331 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 [] replyToUser = newModule.getSystemEmail(); 348 String template = Turbine.getConfiguration(). 349 getString("scarab.email.moveissue.template", 350 "MoveIssue.vm"); 351 Set allToUsers = 352 issue.getAllUsersToEmail(AttributePeer.EMAIL_TO); 353 HashSet toUsers = new HashSet (); 354 Set allCCUsers = issue.getAllUsersToEmail(AttributePeer.CC_TO); 355 HashSet ccUsers = new HashSet (); 356 357 for (Iterator 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 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 e) 379 { 380 L10NMessage l10nMessage = new L10NMessage(EMAIL_ERROR,e); 381 scarabR.setAlertMessage(l10nMessage); 382 } 383 } 384 385 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 403 public void doBacktoone(RunData data, TemplateContext context) throws Exception 404 { 405 setTarget(data, data.getParameters() 406 .getString(ScarabConstants.CANCEL_TEMPLATE, "MoveIssue.vm")); 407 } 408 } 409 | Popular Tags |