1 package org.tigris.scarab.actions; 2 3 48 49 import java.util.HashMap ; 50 import java.util.Iterator ; 51 import java.util.List ; 52 53 import org.apache.commons.collections.MapIterator; 54 import org.apache.commons.collections.map.LinkedMap; 55 import org.apache.commons.lang.StringUtils; 56 import org.apache.fulcrum.intake.model.Group; 57 import org.apache.torque.om.NumberKey; 58 import org.apache.turbine.RunData; 59 import org.apache.turbine.TemplateContext; 60 import org.apache.turbine.tool.IntakeTool; 61 import org.tigris.scarab.actions.base.RequireLoginFirstAction; 62 import org.tigris.scarab.attribute.OptionAttribute; 63 import org.tigris.scarab.om.ActivitySet; 64 import org.tigris.scarab.om.ActivitySetManager; 65 import org.tigris.scarab.om.ActivitySetTypePeer; 66 import org.tigris.scarab.om.AttributeOption; 67 import org.tigris.scarab.om.AttributeOptionManager; 68 import org.tigris.scarab.om.AttributeValue; 69 import org.tigris.scarab.om.Issue; 70 import org.tigris.scarab.om.IssueManager; 71 import org.tigris.scarab.om.IssueTemplateInfo; 72 import org.tigris.scarab.om.IssueTemplateInfoPeer; 73 import org.tigris.scarab.om.IssueType; 74 import org.tigris.scarab.om.Module; 75 import org.tigris.scarab.om.ScarabUser; 76 import org.tigris.scarab.om.Scope; 77 import org.tigris.scarab.tools.ScarabLocalizationTool; 78 import org.tigris.scarab.tools.ScarabRequestTool; 79 import org.tigris.scarab.tools.localization.L10NKeySet; 80 import org.tigris.scarab.util.ScarabConstants; 81 import org.tigris.scarab.util.ScarabException; 82 83 84 91 public class TemplateList extends RequireLoginFirstAction 92 { 93 96 public void doCreatenew(RunData data, TemplateContext context) 97 throws Exception 98 { 99 IntakeTool intake = getIntakeTool(context); 100 ScarabRequestTool scarabR = getScarabRequestTool(context); 101 ScarabLocalizationTool l10n = getLocalizationTool(context); 102 ScarabUser user = (ScarabUser)data.getUser(); 103 Issue issue = scarabR.getIssueTemplate(); 104 if (issue == null) 105 { 106 scarabR.setAlertMessage(L10NKeySet.IssueTypeNotAvailable); 107 return; 108 } 109 110 LinkedMap avMap = issue.getModuleAttributeValuesMap(); 111 AttributeValue aval = null; 112 Group group = null; 113 114 IssueTemplateInfo info = scarabR.getIssueTemplateInfo(); 115 Group infoGroup = intake.get("IssueTemplateInfo", info.getQueryKey()); 116 Group issueGroup = intake.get("Issue", issue.getQueryKey()); 117 ActivitySet activitySet = null; 118 119 if (intake.isAllValid()) 120 { 121 issueGroup.setProperties(issue); 122 infoGroup.setProperties(info); 123 if (checkForDupes(info, infoGroup.get("Name").toString(), 124 user, issue)) 125 { 126 scarabR.setAlertMessage(L10NKeySet.DuplicateTemplateName); 127 } 128 else 129 { 130 boolean atLeastOne = false; 131 MapIterator iter = avMap.mapIterator(); 132 if (iter.hasNext()) 133 { 134 activitySet = ActivitySetManager 136 .getInstance(ActivitySetTypePeer.CREATE_ISSUE__PK, user); 137 activitySet.save(); 138 while (iter.hasNext()) 139 { 140 aval = (AttributeValue)avMap.get(iter.next()); 141 group = intake.get("AttributeValue", aval.getQueryKey(),false); 142 String value = null; 143 if (group != null) 144 { 145 if (aval instanceof OptionAttribute) 146 { 147 value = group.get("OptionId").toString(); 148 } 149 else 150 { 151 value = group.get("Value").toString(); 152 } 153 if (StringUtils.isNotEmpty(value)) 154 { 155 atLeastOne = true; 156 aval.startActivitySet(activitySet); 157 group.setProperties(aval); 158 } 159 } 160 } 161 } 162 if (atLeastOne) 163 { 164 issue.setCreatedTransId(activitySet.getActivitySetId()); 165 issue.save(); 166 info.setIssueId(issue.getIssueId()); 167 168 info.saveAndSendEmail(user, 170 scarabR.getCurrentModule(), 171 context); 172 173 if (info.canEdit(user)) 177 { 178 data.getParameters().add 179 ("templateId", issue.getIssueId().toString()); 180 scarabR.setConfirmMessage(L10NKeySet.NewTemplateCreated); 181 } 182 else 183 { 184 scarabR.setInfoMessage( 186 l10n.format("NotifyPendingApproval", 187 l10n.get("Template").toLowerCase())); 188 setTarget(data, "TemplateList.vm"); 189 doPerform(data, context); 190 } 191 } 192 else 193 { 194 scarabR.setAlertMessage( 195 L10NKeySet.AtLeastOneAttributeForTemplate); 196 } 197 } 198 } 199 else 200 { 201 scarabR.setAlertMessage(ERROR_MESSAGE); 202 } 203 } 204 205 208 public void doEditvalues(RunData data, TemplateContext context) 209 throws Exception 210 { 211 IntakeTool intake = getIntakeTool(context); 212 ScarabRequestTool scarabR = getScarabRequestTool(context); 213 ScarabLocalizationTool l10n = getLocalizationTool(context); 214 ScarabUser user = (ScarabUser)data.getUser(); 215 Issue issue = scarabR.getIssueTemplate(); 216 217 LinkedMap avMap = issue.getModuleAttributeValuesMap(); 218 AttributeValue aval = null; 219 Group group = null; 220 Group issueGroup = intake.get("Issue", issue.getQueryKey()); 221 issueGroup.setProperties(issue); 222 223 if (intake.isAllValid()) 224 { 225 AttributeValue aval2 = null; 226 HashMap newAttVals = new HashMap (); 227 boolean modifiedAttribute = false; 228 229 Iterator iter = avMap.mapIterator(); 230 while (iter.hasNext()) 231 { 232 aval = (AttributeValue)avMap.get(iter.next()); 233 aval2 = AttributeValue.getNewInstance(aval.getAttributeId(), 234 aval.getIssue()); 235 aval2.setProperties(aval); 236 group = intake.get("AttributeValue", aval.getQueryKey(),false); 237 if (group != null) 238 { 239 String newValue = ""; 240 String oldValue = ""; 241 if (aval instanceof OptionAttribute) 242 { 243 newValue = group.get("OptionId").toString(); 244 oldValue = aval.getOptionIdAsString(); 245 246 if (!newValue.equals("")) 247 { 248 AttributeOption newAttributeOption = 249 AttributeOptionManager 250 .getInstance(new Integer (newValue)); 251 newValue = newAttributeOption.getName(); 252 } 253 if (!oldValue.equals("")) 254 { 255 Integer oldOptionId = aval.getOptionId(); 256 AttributeOption oldAttributeOption = 257 AttributeOptionManager 258 .getInstance(oldOptionId); 259 oldValue = oldAttributeOption.getName(); 260 } 261 262 } 263 else 264 { 265 newValue = group.get("Value").toString(); 266 oldValue = aval.getValue(); 267 } 268 if (newValue.trim().length() > 0 && (oldValue == null || 272 !newValue.trim().equals(oldValue.trim()))) 273 { 274 group.setProperties(aval2); 275 newAttVals.put(aval.getAttributeId(), aval2); 276 modifiedAttribute = true; 277 } 278 else if (oldValue != null && newValue.trim().length() == 0 && 280 oldValue.trim().length() != 0) 281 { 282 aval2.setValue(null); 283 newAttVals.put(aval.getAttributeId(), aval2); 284 modifiedAttribute = true; 285 } 286 } 287 } 288 if (modifiedAttribute) 290 { 291 issue.setAttributeValues(null, newAttVals, null, user); 292 intake.removeAll(); 293 } 294 scarabR.setConfirmMessage(L10NKeySet.TemplateModified); 295 } 296 else 297 { 298 scarabR.setAlertMessage(ERROR_MESSAGE); 299 } 300 } 301 302 305 public boolean doEdittemplateinfo(RunData data, TemplateContext context) 306 throws Exception 307 { 308 IntakeTool intake = getIntakeTool(context); 309 ScarabRequestTool scarabR = getScarabRequestTool(context); 310 ScarabLocalizationTool l10n = getLocalizationTool(context); 311 ScarabUser user = (ScarabUser)data.getUser(); 312 Issue issue = scarabR.getIssueTemplate(); 313 boolean success = true; 314 315 IssueTemplateInfo info = scarabR.getIssueTemplateInfo(); 316 Group infoGroup = intake.get("IssueTemplateInfo", info.getQueryKey()); 317 318 if (intake.isAllValid()) 319 { 320 infoGroup.setProperties(info); 321 info.setIssueId(issue.getIssueId()); 322 if (checkForDupes(info, infoGroup.get("Name").toString(), 323 user, issue)) 324 { 325 success = false; 326 scarabR.setAlertMessage(L10NKeySet.DuplicateTemplateName); 327 } 328 else 329 { 330 info.saveAndSendEmail(user, scarabR.getCurrentModule(), context); 332 data.getParameters().add("templateId", issue.getIssueId().toString()); 333 if (info.canEdit(user)) 334 { 335 scarabR.setConfirmMessage(L10NKeySet.TemplateModified); 336 } 337 else 338 { 339 scarabR.setInfoMessage( 340 l10n.format("NotifyPendingApproval", 341 l10n.get("Template").toLowerCase())); 342 setTarget(data, data.getParameters().getString( 343 ScarabConstants.CANCEL_TEMPLATE)); 344 } 345 } 346 } 347 else 348 { 349 success = false; 350 scarabR.setAlertMessage(ERROR_MESSAGE); 351 } 352 return success; 353 } 354 355 public void doDeletetemplates(RunData data, TemplateContext context) 356 throws Exception 357 { 358 ScarabLocalizationTool l10n = getLocalizationTool(context); 359 ScarabRequestTool scarabR = getScarabRequestTool(context); 360 Object [] keys = data.getParameters().getKeys(); 361 String key; 362 String templateId; 363 ScarabUser user = (ScarabUser)data.getUser(); 364 boolean atLeastOne = false; 365 boolean success = true; 366 367 for (int i =0; i<keys.length; i++) 368 { 369 key = keys[i].toString(); 370 if (key.startsWith("delete_")) 371 { 372 atLeastOne = true; 373 templateId = key.substring(7); 374 try 375 { 376 Issue issue = IssueManager 377 .getInstance(new NumberKey(templateId), false); 378 if (issue == null) 379 { 380 throw new Exception ( 381 l10n.get("CouldNotLocateTemplateToDelete")); } 383 issue.delete(user); 384 } 385 catch (ScarabException e) 386 { 387 success = false; 388 scarabR.setAlertMessage(NO_PERMISSION_MESSAGE); 389 } 390 catch (Exception e) 391 { 392 success = false; 393 scarabR.setAlertMessage(l10n.getMessage(e)); 394 } 395 } 396 } 397 if (!atLeastOne) 398 { 399 scarabR.setAlertMessage(L10NKeySet.NoTemplateSelected); 400 } 401 else if (success) 402 { 403 scarabR.setConfirmMessage(L10NKeySet.TemplateDeleted); 404 } 405 } 406 407 public void doUsetemplate(RunData data, TemplateContext context) 408 throws Exception 409 { 410 IntakeTool intake = getIntakeTool(context); 411 intake.removeAll(); 412 ScarabRequestTool scarabR = getScarabRequestTool(context); 413 String templateId = data.getParameters().getString("templateId"); 414 if (templateId != null && templateId.length() > 0) 415 { 416 IssueType templateType = IssueManager.getInstance( 417 new Long (templateId)).getIssueType(); 418 setTarget(data, scarabR.getNextEntryTemplate( 419 templateType.getIssueTypeForTemplateType())); 420 ReportIssue.cleanOutStaleIssue(data, context); 421 } 422 } 423 424 public void doSave(RunData data, TemplateContext context) 425 throws Exception 426 { 427 doEditvalues(data, context); 428 doEdittemplateinfo(data, context); 429 } 430 431 private boolean checkForDupes(IssueTemplateInfo template, 432 String newName, ScarabUser user, 433 Issue issueTemplate) 434 throws Exception 435 { 436 boolean areThereDupes = false; 437 Module module = issueTemplate.getModule(); 438 List prevTemplates = IssueTemplateInfoPeer.getUserTemplates(user, 439 module, issueTemplate.getIssueType().getIssueTypeForTemplateType()); 440 if (template.getScopeId().equals(Scope.MODULE__PK)) 441 { 442 prevTemplates.addAll(IssueTemplateInfoPeer.getModuleTemplates(module)); 443 } 444 if (prevTemplates != null && !prevTemplates.isEmpty()) 445 { 446 Long pk = template.getIssueId(); 447 for (Iterator i = prevTemplates.iterator(); 448 i.hasNext() && !areThereDupes;) 449 { 450 IssueTemplateInfo t = (IssueTemplateInfo)i.next(); 451 areThereDupes = ((pk == null || !pk.equals(t.getIssueId())) && 452 newName.trim().toLowerCase().equals( 453 t.getName().trim().toLowerCase())); 454 } 455 } 456 return areThereDupes; 457 } 458 459 462 public void doDone(RunData data, TemplateContext context) 463 throws Exception 464 { 465 boolean success = doEdittemplateinfo(data, context); 466 if (success) 467 { 468 doEditvalues(data, context); 469 doCancel(data, context); 470 } 471 } 472 } 473 | Popular Tags |