1 19 20 package za.org.coefficient.modules.project; 21 22 import org.apache.commons.lang.StringUtils; 23 24 import net.sf.hibernate.Hibernate; 25 import net.sf.hibernate.HibernateException; 26 import net.sf.hibernate.StaleObjectStateException; 27 28 import za.co.csir.icomtek.workflow.WorkflowData; 29 import za.co.csir.icomtek.workflow.WorkflowEvaluator; 30 import za.co.csir.icomtek.workflow.model.State; 31 import za.co.csir.icomtek.workflow.model.StateTransition; 32 import za.co.csir.icomtek.workflow.model.Workflow; 33 34 import za.org.coefficient.authentication.CoefficientUser; 35 import za.org.coefficient.authentication.ProjectMember; 36 import za.org.coefficient.authentication.Role; 37 import za.org.coefficient.core.Category; 38 import za.org.coefficient.core.Constants; 39 import za.org.coefficient.core.Project; 40 import za.org.coefficient.core.ProjectAttribute; 41 import za.org.coefficient.core.ProjectAttributeData; 42 import za.org.coefficient.core.ProjectVersion; 43 import za.org.coefficient.core.ProjectComponent; 44 import za.org.coefficient.events.*; 45 import za.org.coefficient.events.CoefficientEvent; 46 import za.org.coefficient.events.CoefficientEvents; 47 import za.org.coefficient.interfaces.CoefficientContext; 48 import za.org.coefficient.modules.BaseModule; 49 import za.org.coefficient.modules.attribute.*; 50 import za.org.coefficient.statistics.data.ProjectStatistics; 51 import za.org.coefficient.util.common.HardURLConverterChainUtil; 52 import za.org.coefficient.util.common.MailUtil; 53 import za.org.coefficient.util.common.InvokerFactory; 54 import net.sf.hibernate.util.HibernateUtil; 55 import za.org.coefficient.util.ejb.SecurityUtil; 56 import za.org.coefficient.util.ejb.VelocityScreenUtil; 57 import za.org.coefficient.html.Theme; 58 import za.org.coefficient.html.ThemeSupport; 59 60 import java.io.Serializable ; 61 import java.io.UnsupportedEncodingException ; 62 63 import java.net.URLEncoder ; 64 65 import java.util.ArrayList ; 66 import java.util.Date ; 67 import java.util.Enumeration ; 68 import java.util.HashMap ; 69 import java.util.Iterator ; 70 import java.util.List ; 71 import java.util.Vector ; 72 73 import javax.naming.Context ; 74 import javax.naming.NameClassPair ; 75 import javax.naming.NamingEnumeration ; 76 77 91 public class Projects extends BaseModule { 92 94 private static class TestEventHandler implements CoefficientEventHandler, Serializable { 95 public void handleEvent(CoefficientEvent event){ 96 System.out.println("EVENT :: " + event.getDescription()); 97 } 98 } 99 100 static { 101 HardURLConverterChainUtil.addConverter(new ProjectHardURLConverter()); 103 CoefficientEvents.registerEventHandler(CoefficientEvent.class, new Projects.TestEventHandler()); 104 } 105 106 private static final String ATTR_NOT_SELECTED_LIST = 107 "__not_selected_attribute_names__list"; 108 private static final String ATTR_SELECTED_LIST = 109 "__selected_attribute_names__list"; 110 private static final String MOD_NOT_SELECTED_LIST = 111 "__not_selected_module_names__list"; 112 private static final String MOD_SELECTED_LIST = 113 "__selected_module_names__list"; 114 private static final String CAT_NOT_SELECTED_LIST = 115 "__not_selected_categories__list"; 116 private static final String CAT_SELECTED_LIST = 117 "__selected_categories__list"; 118 private static final String NAME_SPACE = "/project"; 119 private static final String NAME_SPACE2 = "/nav-project"; 120 private static final String MESSAGE_1 = 121 "Thank you for registering a " + ProjectConstants.PROJECT_NAME + " on the "; 122 private static final String MESSAGE_11 = 123 " web site. Your " + ProjectConstants.PROJECT_NAME +" with name '"; 124 private static final String MESSAGE_2 = 125 "' has been created for you. You can view your " 126 + ProjectConstants.PROJECT_NAME + " at: \n\n"; 127 private static final String MESSAGE_3 = 128 "' has been rejected by the site admistrator. If you feel this is in error please contact the site administrator by email.\n"; 129 private static final String SUBJECT = 130 StringUtils.capitalise(ProjectConstants.PROJECT_NAME) +" Approval"; 131 private static final String SUBJECT_2 = 132 StringUtils.capitalise(ProjectConstants.PROJECT_NAME) + " Rejection"; 133 134 136 public String displayUsersData(CoefficientUser user) { 137 HashMap map = new HashMap (); 138 try { 139 List projects = 140 HibernateUtil.find("select distinct prj from " 141 + Project.class.getName() 142 +" as prj left outer join prj.members as " 143 +" prj_mems where " 144 +" prj_mems.coefficientUser.id = ?", 145 user.getId(), Hibernate.LONG); 146 map.put("projects", projects); 147 map.put("user", user); 148 map.put("project_name", ProjectConstants.PROJECT_NAME); 149 map.put("project_name_cap", 150 StringUtils.capitalise(ProjectConstants.PROJECT_NAME)); 151 if(user != null && user.getSystemRole().getRoleValue() 152 <= SecurityUtil.SITE_MODERATOR_ROLE_VAL) { 153 map.put("userIsAdmin", new Boolean (true)); 154 } 155 } catch (HibernateException he) { 156 he.printStackTrace(); 157 } 158 StringBuffer sb = 159 VelocityScreenUtil.getProcessedScreen("usersDataSummary.vm", map); 160 return sb.toString(); 161 } 162 163 public String getFirstChampion(CoefficientContext ctx) { 164 List champs = ctx.getProject() 165 .getChampionsAsUsers(); 166 CoefficientUser user; 167 168 if (0 < champs.size()) { 169 user = (CoefficientUser) (champs.get(0)); 170 String username = user.getUserName(); 171 172 return username; 173 } 174 175 return "admin"; 176 } 177 178 public String getMainMethod() { 179 return "viewProject"; 180 } 181 182 public String getModuleDescription() { 183 return "This is the main " + ProjectConstants.PROJECT_NAME 184 + " module that will allow " + ProjectConstants.PROJECT_NAME 185 + " related activites"; 186 } 187 188 public String getModuleDisplayName() { 189 return StringUtils.capitalise(ProjectConstants.PROJECT_NAME); 190 } 191 192 public Project getProject(CoefficientContext ctx) { 193 return ctx.getProject(); 194 } 195 196 public String canExecuteForRole(CoefficientContext ctx, String methodName, 197 Role usersHighestRole) { 198 199 if(ctx.getProject() != null && !ctx.getProject().getIsPublic() && 200 !ctx.getProject().containsUser(ctx.getCurrentUser()) && 201 usersHighestRole.getRoleValue() > SecurityUtil.SITE_MODERATOR_ROLE_VAL) { 202 return "You must be a member of the " + ProjectConstants.PROJECT_NAME 203 + " before you can view it."; 204 } 205 206 if ((usersHighestRole.getRoleValue() == SecurityUtil.GUEST_ROLE_VAL) 207 && (methodName.equals("chooseProjectType") 208 || methodName.equals("newProject") 209 || methodName.equals("createProject"))) { 210 return "You must be logged in to create a project"; 211 } else if ((usersHighestRole.getRoleValue() > SecurityUtil.PROJECT_CHAMPION_ROLE_VAL) 212 && (methodName.equals("manuallyTransition") 213 || methodName.equals("transition") 214 || methodName.equals("editNameDesc") 215 || methodName.equals("saveNameDesc") 216 || methodName.equals("editOptions") 217 || methodName.equals("saveModules") 218 || methodName.equals("editModulesSelect") 219 || methodName.equals("saveAttributes") 220 || methodName.equals("editAttributeData") 221 || methodName.equals("editAttributesSelect") 222 || methodName.equals("saveCategories") 223 || methodName.equals("editCategoriesSelect") 224 || methodName.equals("changeToWorkflow") 225 || methodName.equals("changeToWorkflowSelect") 226 || methodName.equals("manuallyTransition") 227 || methodName.equals("transition") 228 || methodName.equals("toggleVisibility") 229 || methodName.equals("editTheme") 230 || methodName.equals("saveTheme") 231 || methodName.equals("editNameDesc"))) { 232 return "To edit project details you must be at least a project champion"; 233 } else if ((usersHighestRole.getRoleValue() != SecurityUtil.SITE_ADMIN_ROLE_VAL) 234 && (methodName.equals("deleteProject") 235 || methodName.equals("confirm"))) { 236 return "Only a site administrator can delete or approve a " 237 + ProjectConstants.PROJECT_NAME; 238 } else { 239 return null; 240 } 241 } 242 243 public CoefficientContext changeToWorkflow(CoefficientContext ctx) throws Exception { 244 Project prj = ctx.getProject(); 245 if (prj != null) { 246 String workflowName = ctx.getParameter("workflowName"); 247 if ((workflowName != null) && !workflowName.trim() 248 .equals("")) { 249 Workflow pw = 250 WorkflowEvaluator.getWorkflow(ctx.getParameter( 251 "workflowName")); 252 WorkflowData pwd = new WorkflowData(); 253 pwd.setLinkId(prj.getId()); 254 pwd.setWorkflowName(workflowName); 255 State state = 256 WorkflowEvaluator.getDefaultStateForWorkflow(workflowName); 257 ArrayList newMods = new ArrayList (); 258 for (Iterator it = pw.getModuleAsReference().iterator(); it.hasNext();) { 259 za.co.csir.icomtek.workflow.model.Module mod = 260 (za.co.csir.icomtek.workflow.model.Module) it.next(); 261 newMods.add(mod.getName()); 262 } 263 264 cleanUpModules(newMods, prj, ctx); 266 prj.setIsWorkflow(true); 267 HashMap forwardMap = new HashMap (); 268 forwardMap.put("clearProject", "true"); 269 forwardMap.put("projectId", prj.getId()); 270 ctx.setForward("Project", "viewProject", forwardMap); 271 HibernateUtil.saveOrUpdate(pwd); 272 HibernateUtil.saveOrUpdate(prj); 273 } else { 274 ctx.setModuleContent("You must select a workflow", 275 getModuleDisplayName()); 276 } 277 } else { 278 ctx.setModuleContent("You must select a " 279 + ProjectConstants.PROJECT_NAME, 280 getModuleDisplayName()); 281 } 282 return ctx; 283 } 284 285 public CoefficientContext changeToWorkflowSelect(CoefficientContext ctx) throws Exception { 286 HashMap map = new HashMap (); 287 if (ctx.getProject() != null) { 288 map.put("project", ctx.getProject()); 289 ArrayList mods = getModules(); 290 map.put("workflows", WorkflowEvaluator.getAllRegisteredWorkflows()); 291 map.put("project_name", ProjectConstants.PROJECT_NAME); 292 map.put("project_name_cap", StringUtils.capitalise(ProjectConstants.PROJECT_NAME)); 293 if(ctx.getProject().getIsWorkflow()) { 294 WorkflowData wd = (WorkflowData)ctx 295 .getSessionAttribute("__current_workflow_data__"); 296 map.put("workflowData", wd); 297 } 298 StringBuffer sb = 299 VelocityScreenUtil.getProcessedScreen("changeToWorkflow.vm", map); 300 ctx.setModuleContent(sb.toString(), getModuleDisplayName()); 301 } else { 302 ctx.setModuleContent("You can not change a " 303 + ProjectConstants.PROJECT_NAME 304 + " type without a " 305 + ProjectConstants.PROJECT_NAME, 306 getModuleDisplayName()); 307 } 308 return ctx; 309 } 310 311 public CoefficientContext chooseProjectType(CoefficientContext ctx) { 312 HashMap map = new HashMap (); 313 314 if(WorkflowEvaluator.getAllRegisteredWorkflows().size() > 0) { 315 map.put("project_name", ProjectConstants.PROJECT_NAME); 316 map.put("project_name_cap", 317 StringUtils.capitalise(ProjectConstants.PROJECT_NAME)); 318 319 StringBuffer sb = 320 VelocityScreenUtil.getProcessedScreen("choose.vm", map); 321 322 ctx.setModuleContent(sb.toString(), getModuleDisplayName()); 324 } else { 325 HashMap params = new HashMap (); 327 params.put("choice", new Integer (1)); 328 ctx.setForward("Project", "newProject", params); 329 } 330 return ctx; 331 } 332 333 public CoefficientContext confirm(CoefficientContext ctx) throws HibernateException { 334 Long prjId = ctx.getParameterAsLong("projectId", -1); 335 long version = ctx.getParameterAsLongPrimitive("version", -1); 336 Project prj = null; 337 if (prjId.longValue() > 0) { 338 prj = (Project) HibernateUtil.load(Project.class, prjId); 339 if (prj != null) { 340 if (prj.getVersion() == version) { 341 prj.setActive(true); 342 343 ProjectStatistics stats = new ProjectStatistics(); 345 stats.setStatsDate(new Date (System.currentTimeMillis())); 346 stats.setProjectId(prj.getId()); 347 348 HibernateUtil.saveOrUpdate(stats); 349 prj.setStatistics(stats); 350 351 HibernateUtil.saveOrUpdate(prj); 352 ctx.setModuleContent("The " + ProjectConstants.PROJECT_NAME 353 + " is now active", 354 getModuleDisplayName()); 355 } else { 356 throw new StaleObjectStateException(prj.getClass(), 357 prj.getId()); 358 } 359 } else { 360 ctx.setError("No such " + ProjectConstants.PROJECT_NAME + " found"); 361 } 362 } 363 364 if (!ctx.isError()) { 365 for (Iterator it = prj.getMembers() 366 .iterator(); it.hasNext();) { 367 ProjectMember pm = (ProjectMember) it.next(); 368 if (pm.getProjectRole() 369 .getRoleValue() == SecurityUtil.PROJECT_CHAMPION_ROLE_VAL) { 370 String link = 371 ctx.getRequestURL() + "?module=project&projectId=" 372 + prj.getId(); 373 374 MailUtil.sendEmail(MESSAGE_1 + Constants.CFG_SITENAME+ MESSAGE_11 + prj.getName() + MESSAGE_2 376 + "<a target='_blank' HREF='" + link + "'>" + link 377 + "</a>", Constants.CFG_SITENAME + SUBJECT, 378 pm.getCoefficientUser().getEmail(), null); 379 } 380 } 381 } 382 return ctx; 383 } 384 385 public CoefficientContext createProject(CoefficientContext ctx) 386 throws HibernateException 387 { 388 Project prj = new Project(); 389 390 String shortName = ctx.getParameter("shortName"); 391 List projects = HibernateUtil 392 .find("select from " + Project.class.getName() + " as prj where" 393 + " prj.shortName = ?", shortName, Hibernate.STRING); 394 if(projects.size() == 0) { 395 prj.setShortName(shortName); 396 prj.setName(ctx.getParameter("name")); 397 prj.setDescription(ctx.getParameter("description")); 398 prj.setHomePage(ctx.getParameter("homePage")); 399 prj.setIsPublic(!"on".equals(ctx.getParameter("isPrivate"))); 400 String workflowName = ctx.getParameter("workflowName"); 401 Workflow pw = null; 402 if(workflowName != null && !workflowName.trim().equals("")) { 403 pw = WorkflowEvaluator.getWorkflow(ctx.getParameter("workflowName")); 404 } 405 WorkflowData pwd = null; 406 if (pw != null) { 407 pwd = new WorkflowData(); 408 pwd.setWorkflowName(pw.getName()); 409 State state = 410 WorkflowEvaluator.getDefaultStateForWorkflow(pw.getName()); 411 412 for (Enumeration e = pw.enumerateModule(); e.hasMoreElements();) { 414 za.co.csir.icomtek.workflow.model.Module mod = 415 (za.co.csir.icomtek.workflow.model.Module) e.nextElement(); 416 prj.addModule(mod.getName()); 417 } 418 prj.setIsWorkflow(true); 419 } else { 420 String [] modules = ctx.getParameterValues("modules"); 422 for (int i = 0; i < modules.length; i++) { 423 prj.addModule(modules[i]); 424 } 425 } 426 427 try { 429 String [] categories = ctx.getParameterValues("categories"); 430 for (int i = 0; i < categories.length; i++) { 431 prj.addCategory((Category)InvokerFactory.getInvoker() 432 .invokeMethodOnModule("CategoryManagement", 433 "getCategoryByName", 434 new Object []{categories[i]})); 435 } 436 } catch (Exception e) { 437 e.printStackTrace(); 439 } 440 441 try { 445 int i = 0; 446 List existing = (List )InvokerFactory.getInvoker() 447 .invokeMethodOnModule("AttributeDataManagement", 448 "getExistingAttributes", 449 new Object [0]); 450 for (i = 0; i < existing.size(); i++) { 451 String attributeId = ctx.getParameter("attribute" + i); 452 if (attributeId != null) { 453 Long id; 454 id = new Long (attributeId); 455 ProjectAttribute attr = (ProjectAttribute) 456 InvokerFactory.getInvoker() 457 .invokeMethodOnModule("AttributeDataManagement", 458 "getAttributeByNumber", 459 new Object []{id}); 460 prj.addAttribute(attr); 461 String [] values = ctx.getParameterValues("data" + i); 462 int j; 463 List pads = (List )InvokerFactory.getInvoker() 464 .invokeMethodOnModule("AttributeDataManagement", 465 "getExistingData", 466 new Object []{id}); 467 ProjectAttributeData pad; 468 int k; 469 for (j = 0; j < values.length; j++) { 470 for (k = 0; k < pads.size(); k++) { 471 pad = (ProjectAttributeData) (pads.get(k)); 472 if (values[j].equals("" + pad.getId())) { 473 prj.addAttributeData(pad); 474 } 475 } 476 } 477 } 478 } 479 } catch (Throwable t) { 480 } 481 482 Role project_champ = 484 SecurityUtil.getRoleForDescription(SecurityUtil.PROJECT_CHAMPION_ROLE_DESC); 485 prj.addMember(new ProjectMember(ctx.getCurrentUser(), project_champ)); 486 487 prj.setActive(false); 490 491 try { 492 HibernateUtil.save(prj); 493 if(pwd != null) { 494 pwd.setLinkId(prj.getId()); 495 HibernateUtil.save(pwd); 496 } 497 } catch (HibernateException he) { 498 he.printStackTrace(); 499 ctx.setError("FAILED !!!"); 500 } 501 if (!ctx.isError()) { 502 HashMap params = new HashMap (); 503 params.put("projectId", prj.getId()); 504 ctx.setModuleContent(StringUtils.capitalise(ProjectConstants 505 .PROJECT_NAME) 506 + " has been submitted for approval, you will be emailed when the site administrator has done so.", 507 getModuleDisplayName()); 508 509 } 513 } else { 514 ctx.setError("The short name you provided is not unique in the system. Please reselect modules, workflows, attributes, and categories."); 516 newProject(ctx); 517 } 518 return ctx; 519 } 520 521 524 public CoefficientContext deleteProject(CoefficientContext ctx) { 525 System.out.println("deleteProject executing"); 526 try { 527 for (Iterator it = ctx.getProject() 529 .getModules() 530 .iterator(); it.hasNext();) { 531 String modName = (String ) it.next(); 532 System.out.println("moduleName is " + modName); 533 InvokerFactory.getInvoker() 534 .invokeOpOnModule(modName, "deleteAllForProject", ctx); 535 } 536 System.out.println("loop finished"); 537 538 WorkflowData wd = (WorkflowData)ctx 540 .getSessionAttribute("__current_workflow_data__"); 541 if(wd != null) { 542 HibernateUtil.delete(wd); 543 } 544 545 HibernateUtil.delete(ctx.getProject()); 546 System.out.println("hibernate finished"); 547 } catch (HibernateException he) { 548 he.printStackTrace(); 549 ctx.setError("unable to delete the " + ProjectConstants.PROJECT_NAME ); 550 } catch (Exception e) { 551 e.printStackTrace(); 552 ctx.setError("unable to delete a modules content for this " 553 + ProjectConstants.PROJECT_NAME ); 554 } 555 ctx.setForward("DeleteProjects"); 556 return ctx; 557 } 558 559 public CoefficientContext deleteVersion(CoefficientContext ctx) throws Exception { 560 if (ctx.getProject() != null) { 561 Long id = ctx.getParameterAsLong("id", -1); 562 if (id.longValue() > -1) { 563 ProjectVersion pv = new ProjectVersion(); 564 pv.setId(id); 565 ctx.getProject().deleteVersion(pv); 566 HibernateUtil.saveOrUpdate(ctx.getProject()); 567 HashMap forwardMap = new HashMap (); 568 forwardMap.put("clearProject", "true"); 569 forwardMap.put("projectId", ctx.getProject().getId()); 570 ctx.setForward("Project", "editVersions", forwardMap); 571 } else { 572 ctx.setError("You must supply a version to delete"); 573 } 574 } else { 575 ctx.setError(StringUtils.capitalise(ProjectConstants.PROJECT_NAME) 576 + " must be selected to perform this action"); 577 } 578 return ctx; 579 } 580 581 public CoefficientContext deleteComponent(CoefficientContext ctx) throws Exception { 582 Long id = ctx.getParameterAsLong("id", -1); 583 if (id.longValue() > -1) { 584 ProjectComponent pv = new ProjectComponent(); 585 pv.setId(id); 586 ctx.getProject().deleteComponent(pv); 587 HibernateUtil.saveOrUpdate(ctx.getProject()); 588 HashMap forwardMap = new HashMap (); 589 forwardMap.put("clearProject", "true"); 590 forwardMap.put("projectId", ctx.getProject().getId()); 591 ctx.setForward("Project", "editComponents", forwardMap); 592 } else { 593 ctx.setError("You must supply a component to delete"); 594 } 595 return ctx; 596 } 597 598 public CoefficientContext editAttributeData(CoefficientContext ctx) { 599 Project project = ctx.getProject(); 600 if (project != null) { 601 project.setAttributeData(new ArrayList ()); 602 project.setAttributes(new ArrayList ()); 603 604 try { 608 int i = 0; 609 List existing = (List )InvokerFactory.getInvoker() 610 .invokeMethodOnModule("AttributeDataManagement", 611 "getExistingAttributes", 612 new Object [0]); 613 614 for (i = 0; i < existing.size(); i++) { 615 String attributeId = ctx.getParameter("attribute" + i); 616 if (attributeId != null) { 617 Long id; 618 id = new Long (attributeId); 619 ProjectAttribute attr = (ProjectAttribute) 620 InvokerFactory.getInvoker() 621 .invokeMethodOnModule("AttributeDataManagement", 622 "getAttributeByNumber", 623 new Object []{id}); 624 project.addAttribute(attr); 625 String [] values = ctx.getParameterValues("data" + i); 626 int j; 627 List pads = (List )InvokerFactory.getInvoker() 628 .invokeMethodOnModule("AttributeDataManagement", 629 "getExistingData", 630 new Object []{id}); 631 ProjectAttributeData pad; 632 int k; 633 634 for (j = 0; j < values.length; j++) { 635 for (k = 0; k < pads.size(); k++) { 636 pad = (ProjectAttributeData) (pads.get(k)); 637 if (values[j].equals("" + pad.getId())) { 638 project.addAttributeData(pad); 639 } 640 } 641 } 642 } 643 } 644 HibernateUtil.saveOrUpdate(project); 645 } catch (Throwable t) { 646 } 647 } 648 HashMap forwardMap = new HashMap (); 649 forwardMap.put("clearProject", "true"); 650 forwardMap.put("projectId", project.getId()); 651 ctx.setForward("Project", "viewProject", forwardMap); 652 return ctx; 653 } 654 655 public CoefficientContext editAttributeDataSelect(CoefficientContext ctx) { 656 Project project = ctx.getProject(); 657 if (project != null) { 658 HashMap map = new HashMap (); 659 map.put("attributes", getAttributes()); 660 map.put("prjattributes", getAttributes(ctx)); 661 map.put("project_name", ProjectConstants.PROJECT_NAME); 662 map.put("project_name_cap", 663 StringUtils.capitalise(ProjectConstants.PROJECT_NAME)); 664 StringBuffer sb = 665 VelocityScreenUtil.getProcessedScreen("editAttributeData.vm", 666 map); 667 ctx.setModuleContent(sb.toString(), getModuleDisplayName()); 668 } else { 669 ctx.setModuleContent("You must select a " 670 + ProjectConstants.PROJECT_NAME + " to edit", 671 getModuleDisplayName()); 672 } 673 return ctx; 674 } 675 676 public CoefficientContext editAttributesSelect(CoefficientContext ctx) throws Exception { 677 Project prj = ctx.getProject(); 678 if (prj != null) { 679 HashMap map = new HashMap (); 680 map.put("project", prj); 681 String listAction = ctx.getParameter("list_action"); 682 List attrs = null; 683 List prjattrs = null; 684 685 attrs = (List ) ctx.getSessionAttribute(ATTR_NOT_SELECTED_LIST); 687 prjattrs = (List ) ctx.getSessionAttribute(ATTR_SELECTED_LIST); 688 689 if ((listAction == null) || ((attrs == null) && (prjattrs == null))) { 690 attrs = new ArrayList (); 691 attrs.addAll(getAttributeNames()); 692 prjattrs = new ArrayList (); 693 prjattrs.addAll(getAttributeNames(ctx)); 694 attrs.removeAll(prjattrs); 695 } else if ("add".equals(listAction)) { 696 String attrName = ctx.getParameter("not_selected"); 697 if ((attrName != null) && !attrName.trim() 698 .equals("")) { 699 prjattrs.add(attrName); 700 attrs.remove(attrName); 701 } 702 } else if ("delete".equals(listAction)) { 703 String attrName = ctx.getParameter("selected"); 704 if ((attrName != null) && !attrName.trim() 705 .equals("")) { 706 attrs.add(attrName); 707 prjattrs.remove(attrName); 708 } 709 } 710 map.put("notSelected", attrs); 711 map.put("selected", prjattrs); 712 map.put("project_name", ProjectConstants.PROJECT_NAME); 713 map.put("project_name_cap", StringUtils.capitalise(ProjectConstants.PROJECT_NAME)); 714 ctx.setSessionAttribute(ATTR_NOT_SELECTED_LIST, attrs); 715 ctx.setSessionAttribute(ATTR_SELECTED_LIST, prjattrs); 716 StringBuffer sb = 717 VelocityScreenUtil.getProcessedScreen("editAttributes.vm", map); 718 ctx.setModuleContent(sb.toString(), getModuleDisplayName()); 719 } else { 720 ctx.setModuleContent("You must select a " 721 + ProjectConstants.PROJECT_NAME + " to edit", 722 getModuleDisplayName()); 723 } 724 return ctx; 725 } 726 727 public CoefficientContext editCategoriesSelect(CoefficientContext ctx) throws Exception { 728 Project prj = ctx.getProject(); 729 if (prj != null) { 730 HashMap map = new HashMap (); 731 map.put("project", prj); 732 String listAction = ctx.getParameter("list_action"); 733 List cats = null; 734 List prjCats = null; 735 736 cats = (List ) ctx.getSessionAttribute(CAT_NOT_SELECTED_LIST); 738 prjCats = (List ) ctx.getSessionAttribute(CAT_SELECTED_LIST); 739 740 if ((listAction == null) || ((cats == null) && (prjCats == null))) { 741 cats = (List ) InvokerFactory.getInvoker() 742 .invokeMethodOnModule("CategoryManagement", 743 "listAllCategories", new Object [0]); 744 prjCats = prj.getCategories(); 745 cats.removeAll(prjCats); 746 } else if ("add".equals(listAction)) { 747 String catName = ctx.getParameter("not_selected"); 748 if ((catName != null) && !catName.trim() 749 .equals("")) { 750 Category cat = 751 (Category)InvokerFactory.getInvoker() 752 .invokeMethodOnModule("CategoryManagement", 753 "getCategoryByName", 754 new Object []{catName}); 755 prjCats.add(cat); 756 cats.remove(cat); 757 } 758 } else if ("delete".equals(listAction)) { 759 String catName = ctx.getParameter("selected"); 760 if ((catName != null) && !catName.trim() 761 .equals("")) { 762 Category cat = 763 (Category)InvokerFactory.getInvoker() 764 .invokeMethodOnModule("CategoryManagement", 765 "getCategoryByName", 766 new Object []{catName}); 767 cats.add(cat); 768 prjCats.remove(cat); 769 } 770 } 771 map.put("notSelected", cats); 772 map.put("selected", prjCats); 773 map.put("project_name", ProjectConstants.PROJECT_NAME); 774 map.put("project_name_cap", StringUtils.capitalise(ProjectConstants.PROJECT_NAME)); 775 ctx.setSessionAttribute(CAT_NOT_SELECTED_LIST, cats); 776 ctx.setSessionAttribute(CAT_SELECTED_LIST, prjCats); 777 StringBuffer sb = 778 VelocityScreenUtil.getProcessedScreen("editCategories.vm", map); 779 ctx.setModuleContent(sb.toString(), getModuleDisplayName()); 780 } else { 781 ctx.setModuleContent("You must select a " 782 + ProjectConstants.PROJECT_NAME + " to edit", 783 getModuleDisplayName()); 784 } 785 return ctx; 786 } 787 788 public CoefficientContext editModulesSelect(CoefficientContext ctx) throws Exception { 789 Project prj = ctx.getProject(); 790 if (prj != null) { 791 HashMap map = new HashMap (); 792 map.put("project", prj); 793 String listAction = ctx.getParameter("list_action"); 794 List mods = null; 795 List prjMods = null; 796 797 mods = (List ) ctx.getSessionAttribute(MOD_NOT_SELECTED_LIST); 799 prjMods = (List ) ctx.getSessionAttribute(MOD_SELECTED_LIST); 800 801 if ((listAction == null) || ((mods == null) && (prjMods == null))) { 802 mods = new ArrayList (); 803 mods.addAll(getModules()); 804 prjMods = new ArrayList (); 805 prjMods.addAll(prj.getModules()); 806 mods.removeAll(prjMods); 807 } else if ("add".equals(listAction)) { 808 String modName = ctx.getParameter("not_selected"); 809 if ((modName != null) && !modName.trim() 810 .equals("")) { 811 prjMods.add(modName); 812 mods.remove(modName); 813 } 814 } else if ("delete".equals(listAction)) { 815 String modName = ctx.getParameter("selected"); 816 if ((modName != null) && !modName.trim() 817 .equals("")) { 818 mods.add(modName); 819 prjMods.remove(modName); 820 } 821 } 822 map.put("notSelected", mods); 823 map.put("selected", prjMods); 824 map.put("project_name", ProjectConstants.PROJECT_NAME); 825 map.put("project_name_cap", 826 StringUtils.capitalise(ProjectConstants.PROJECT_NAME)); 827 ctx.setSessionAttribute(MOD_NOT_SELECTED_LIST, mods); 828 ctx.setSessionAttribute(MOD_SELECTED_LIST, prjMods); 829 StringBuffer sb = 830 VelocityScreenUtil.getProcessedScreen("editModules.vm", map); 831 ctx.setModuleContent(sb.toString(), getModuleDisplayName()); 832 } else { 833 ctx.setModuleContent("You must select a " 834 + ProjectConstants.PROJECT_NAME + " to edit", 835 getModuleDisplayName()); 836 } 837 return ctx; 838 } 839 840 public CoefficientContext editNameDesc(CoefficientContext ctx) throws Exception { 841 setupSimpleScreen(ctx, "editNameDesc.vm"); 842 return ctx; 843 } 844 845 public CoefficientContext editOptions(CoefficientContext ctx) throws Exception { 846 setupSimpleScreen(ctx, "editProject.vm"); 847 return ctx; 848 } 849 850 public CoefficientContext editComponent(CoefficientContext ctx) throws Exception { 851 HashMap map = new HashMap (); 852 map.put("project", ctx.getProject()); 853 Long id = ctx.getParameterAsLong("id", -1); 854 if (id.longValue() > -1) { 855 map.put("component", HibernateUtil.load(ProjectComponent.class, 856 ctx.getParameterAsLong("id"))); 857 } 858 StringBuffer sb = 859 VelocityScreenUtil.getProcessedScreen("saveComponent.vm", map); 860 ctx.setModuleContent(sb.toString(), getModuleDisplayName()); 861 return ctx; 862 } 863 864 public CoefficientContext editComponents(CoefficientContext ctx) throws Exception { 865 setupSimpleScreen(ctx, "editComponents.vm"); 866 return ctx; 867 } 868 869 public CoefficientContext editVersion(CoefficientContext ctx) throws Exception { 870 HashMap map = new HashMap (); 871 map.put("project", ctx.getProject()); 872 Long id = ctx.getParameterAsLong("id", -1); 873 if (id.longValue() > -1) { 874 map.put("version", HibernateUtil.load(ProjectVersion.class, 875 ctx.getParameterAsLong("id"))); 876 } 877 StringBuffer sb = 878 VelocityScreenUtil.getProcessedScreen("saveVersion.vm", map); 879 ctx.setModuleContent(sb.toString(), getModuleDisplayName()); 880 return ctx; 881 } 882 883 public CoefficientContext editVersions(CoefficientContext ctx) throws Exception { 884 setupSimpleScreen(ctx, "editVersions.vm"); 885 return ctx; 886 } 887 888 public Project findProjectByPK(Long projectId) { 889 Project prj = null; 890 try { 891 prj = (Project) HibernateUtil.load(Project.class, projectId); 892 } catch (HibernateException he) { 893 he.printStackTrace(); 894 } 895 896 return prj; 897 } 898 899 public Project findProjectByName(String projectName) { 900 List projects = null; 901 Project prj = null; 902 try { 903 projects = HibernateUtil.find("from " + Project.class.getName() + 904 " as prj where prj.name = ?", 905 projectName, Hibernate.STRING); 906 if(projects.size() == 1) { 907 prj = (Project)projects.get(0); 908 } 909 } catch (HibernateException he) { 910 he.printStackTrace(); 911 } 912 913 return prj; 914 } 915 916 public Project findProjectByShortName(String projectShortName) { 917 List projects = null; 918 Project prj = null; 919 try { 920 projects = HibernateUtil.find("from " + Project.class.getName() + 921 " as prj where prj.shortName = ?", 922 projectShortName, Hibernate.STRING); 923 if(projects.size() == 1) { 924 prj = (Project)projects.get(0); 925 } 926 } catch (HibernateException he) { 927 he.printStackTrace(); 928 } 929 930 return prj; 931 } 932 933 public CoefficientContext makeCurrentVersion(CoefficientContext ctx) throws Exception { 934 if (ctx.getProject() != null) { 935 Long id = ctx.getParameterAsLong("id", -1); 936 if (id.longValue() > -1) { 937 ProjectVersion pv = 938 (ProjectVersion)HibernateUtil.load(ProjectVersion.class, id); 939 HibernateUtil.evict(pv); 940 ctx.getProject().setCurrentVersion(pv); 941 HibernateUtil.saveOrUpdate(ctx.getProject()); 942 HashMap forwardMap = new HashMap (); 943 forwardMap.put("clearProject", "true"); 944 forwardMap.put("projectId", ctx.getProject().getId()); 945 ctx.setForward("Project", "viewProject", forwardMap); 946 } else { 947 ctx.setError("You must select a version to make current"); 948 } 949 } else { 950 ctx.setError("You must select a " + ProjectConstants.PROJECT_NAME 951 +" before you can make a current version"); 952 } 953 return ctx; 954 } 955 956 public CoefficientContext editTheme(CoefficientContext ctx) throws Exception { 957 Vector v = ThemeSupport.getThemes(); 958 HashMap map = new HashMap (); 959 map.put("project", ctx.getProject()); 960 map.put("themes", v); 961 StringBuffer sb = VelocityScreenUtil.getProcessedScreen("editTheme.vm", map); 962 ctx.setModuleContent(sb.toString(), getModuleDisplayName()); 963 return ctx; 964 } 965 966 public CoefficientContext saveTheme(CoefficientContext ctx) throws Exception { 967 try { 968 String selectedTheme = ctx.getParameter("theme"); 969 ctx.getProject().setThemeName(selectedTheme); 970 HibernateUtil.saveOrUpdate(ctx.getProject()); 971 } 972 catch ( Throwable t ) { 973 System.out.println("saveTheme throws " +t); 974 } 975 return ctx; 976 } 977 978 public CoefficientContext manuallyTransition(CoefficientContext ctx) throws Exception { 979 HashMap map = new HashMap (); 980 map.put("project", ctx.getProject()); 981 982 WorkflowData wd = (WorkflowData)ctx 983 .getSessionAttribute("__current_workflow_data__"); 984 if (wd != null) { 985 State currState = 987 WorkflowEvaluator.getState(wd.getWorkflowName(), 988 wd.getCurrentState()); 989 ArrayList transitions = new ArrayList (); 990 for (Enumeration enumer = currState.enumerateStateTransition(); 991 enumer.hasMoreElements();) { 992 StateTransition st = (StateTransition) enumer.nextElement(); 993 State state = 994 WorkflowEvaluator.getState(wd.getWorkflowName(), 995 st.getDestination()); 996 if (state != null) { 997 transitions.add(state); 998 } 999 } 1000 map.put("transitions", transitions); 1001 map.put("workflowData", wd); 1002 1003 StringBuffer sb = 1004 VelocityScreenUtil.getProcessedScreen("stateEdit.vm", map); 1005 1006 ctx.setModuleContent(sb.toString(), getModuleDisplayName()); 1008 } else { 1009 ctx.setModuleContent("Your " + ProjectConstants.PROJECT_NAME 1010 + " must be of type workflow to change state", 1011 getModuleDisplayName()); 1012 } 1013 return ctx; 1014 } 1015 1016 public CoefficientContext newProject(CoefficientContext ctx) { 1017 HashMap map = new HashMap (); 1018 map.put("module", this); 1019 int choice = ctx.getParameterAsInt("choice", -1); 1020 map.put("choice", new Integer (choice)); 1021 if (choice == 0) { 1022 map.put("workflows", WorkflowEvaluator.getAllRegisteredWorkflows()); 1023 } else if (choice == 1) { 1024 ArrayList mods = getModules(); 1025 map.put("modules", mods); 1026 } 1027 try { 1028 map.put("categories", InvokerFactory.getInvoker() 1029 .invokeMethodOnModule("CategoryManagement", "listAll", 1030 new Object [0])); 1031 } catch (Exception e) { 1032 e.printStackTrace(); 1033 System.err.println("<< error loading the categories"); 1034 } 1035 1036 map.put("ctx", ctx); 1037 map.put("attributes", getAttributes()); 1038 map.put("project_name", ProjectConstants.PROJECT_NAME); 1039 map.put("project_name_cap", 1040 StringUtils.capitalise(ProjectConstants.PROJECT_NAME)); 1041 map.put("allow_private", 1042 new Boolean (ProjectConstants.ALLOW_PRIVATE_PROJECTS)); 1043 1044 StringBuffer sb = VelocityScreenUtil.getProcessedScreen("new.vm", map); 1045 1046 ctx.setModuleContent(sb.toString(), getModuleDisplayName()); 1048 return ctx; 1049 } 1050 1051 public CoefficientContext reject(CoefficientContext ctx) throws HibernateException { 1052 Long prjId = ctx.getParameterAsLong("projectId", -1); 1053 long version = ctx.getParameterAsLongPrimitive("version", -1); 1054 Project prj = null; 1055 String email = null; 1056 if (prjId.longValue() > 0) { 1057 prj = (Project) HibernateUtil.load(Project.class, prjId); 1058 if (prj != null) { 1059 if (prj.getVersion() == version) { 1060 for (Iterator it = prj.getMembers() 1062 .iterator(); it.hasNext();) { 1063 ProjectMember pm = (ProjectMember) it.next(); 1064 if (pm.getProjectRole() 1065 .getRoleValue() == SecurityUtil.PROJECT_CHAMPION_ROLE_VAL) { 1066 email = pm.getCoefficientUser() 1067 .getEmail(); 1068 } 1069 } 1070 HibernateUtil.delete(prj); 1071 ctx.setModuleContent("The " + ProjectConstants.PROJECT_NAME 1072 +" has been rejected", 1073 getModuleDisplayName()); 1074 } else { 1075 throw new StaleObjectStateException(prj.getClass(), 1076 prj.getId()); 1077 } 1078 } else { 1079 ctx.setError("No such " + ProjectConstants.PROJECT_NAME 1080 + " found"); 1081 } 1082 } 1083 1084 if (!ctx.isError()) { 1085 if (email != null) { 1087 MailUtil.sendEmail(MESSAGE_1 + Constants.CFG_SITENAME + MESSAGE_11 + prj.getName() + MESSAGE_3, 1088 Constants.CFG_SITENAME + SUBJECT_2, email, null); 1089 } 1090 } 1091 return ctx; 1092 } 1093 1094 public CoefficientContext saveAttributes(CoefficientContext ctx) throws Exception { 1095 Project prj = ctx.getProject(); 1096 if (prj != null) { 1097 HashMap map = new HashMap (); 1098 map.put("project", prj); 1099 List prjAttributeNames = 1100 (List ) ctx.getSessionAttribute(ATTR_SELECTED_LIST); 1101 1102 ctx.removeSessionAttribute(ATTR_NOT_SELECTED_LIST); 1104 ctx.removeSessionAttribute(ATTR_SELECTED_LIST); 1105 List allAttributes = getAttributes(); 1106 List newAttributes = new ArrayList (); 1107 List newAttributeData = new ArrayList (); 1108 List oldAttributeData = prj.getAttributeData(); 1109 AttributeDisplayData data; 1110 ProjectAttributeData pad; 1111 int i; 1112 int j; 1113 int k; 1114 for (i = 0; i < allAttributes.size(); i++) { 1115 for (j = 0; j < prjAttributeNames.size(); j++) { 1116 data = (AttributeDisplayData) (allAttributes.get(i)); 1117 if (data.getAttributeName() 1118 .equals((String ) (prjAttributeNames.get(j)))) { 1119 newAttributes.add(data.getAttributeId()); 1120 for (k = 0; k < oldAttributeData.size(); k++) { 1121 pad = (ProjectAttributeData) oldAttributeData.get(k); 1122 if (pad.getAttributeId() 1123 .compareTo(data.getAttributeId()) == 0) { 1124 newAttributeData.add(pad); 1125 } 1126 } 1127 } 1128 } 1129 } 1130 prj.setAttributes(newAttributes); 1131 prj.setAttributeData(newAttributeData); 1132 HibernateUtil.saveOrUpdate(prj); 1133 HashMap forwardMap = new HashMap (); 1134 forwardMap.put("clearProject", "true"); 1135 forwardMap.put("projectId", prj.getId()); 1136 ctx.setForward("Project", "viewProject", forwardMap); 1137 } else { 1138 ctx.setModuleContent("You must select a " 1139 + ProjectConstants.PROJECT_NAME + " to edit", 1140 getModuleDisplayName()); 1141 } 1142 return ctx; 1143 } 1144 1145 public CoefficientContext saveCategories(CoefficientContext ctx) throws Exception { 1146 Project prj = ctx.getProject(); 1147 if (prj != null) { 1148 HashMap map = new HashMap (); 1149 map.put("project", prj); 1150 List prjCats = (List ) ctx.getSessionAttribute(CAT_SELECTED_LIST); 1151 1152 ctx.removeSessionAttribute(CAT_NOT_SELECTED_LIST); 1154 ctx.removeSessionAttribute(CAT_SELECTED_LIST); 1155 prj.setCategories(prjCats); 1156 HibernateUtil.saveOrUpdate(prj); 1157 HashMap forwardMap = new HashMap (); 1158 forwardMap.put("clearProject", "true"); 1159 forwardMap.put("projectId", prj.getId()); 1160 ctx.setForward("Project", "viewProject", forwardMap); 1161 } else { 1162 ctx.setModuleContent("You must select a " 1163 + ProjectConstants.PROJECT_NAME + " to edit", 1164 getModuleDisplayName()); 1165 } 1166 return ctx; 1167 } 1168 1169 public CoefficientContext saveModules(CoefficientContext ctx) throws Exception { 1170 Project prj = ctx.getProject(); 1171 if (prj != null) { 1172 HashMap map = new HashMap (); 1173 map.put("project", prj); 1174 List prjMods = (List ) ctx.getSessionAttribute(MOD_SELECTED_LIST); 1175 1176 ctx.removeSessionAttribute(MOD_NOT_SELECTED_LIST); 1178 ctx.removeSessionAttribute(MOD_SELECTED_LIST); 1179 1180 cleanUpModules(prjMods, prj, ctx); 1182 1183 if(prj.getIsWorkflow()) { 1185 prj.setIsWorkflow(false); 1186 WorkflowData wd = (WorkflowData)ctx 1187 .getSessionAttribute("__current_workflow_data__"); 1188 if(wd != null) { 1189 HibernateUtil.delete(wd); 1190 } 1191 } 1192 HibernateUtil.saveOrUpdate(prj); 1193 HashMap forwardMap = new HashMap (); 1194 forwardMap.put("clearProject", "true"); 1195 forwardMap.put("projectId", prj.getId()); 1196 ctx.setForward("Project", "viewProject", forwardMap); 1197 } else { 1198 ctx.setModuleContent("You must select a " 1199 + ProjectConstants.PROJECT_NAME + " to edit", 1200 getModuleDisplayName()); 1201 } 1202 return ctx; 1203 } 1204 1205 public CoefficientContext saveNameDesc(CoefficientContext ctx) throws Exception { 1206 Project prj = ctx.getProject(); 1207 if (prj != null) { 1208 ctx.setProperties(prj); 1209 HibernateUtil.saveOrUpdate(prj); 1210 HashMap forwardMap = new HashMap (); 1211 forwardMap.put("clearProject", "true"); 1212 forwardMap.put("projectId", prj.getId()); 1213 ctx.setForward("Project", "viewProject", forwardMap); 1214 } else { 1215 ctx.setModuleContent("You must select a " 1216 + ProjectConstants.PROJECT_NAME + " to edit", 1217 getModuleDisplayName()); 1218 } 1219 return ctx; 1220 } 1221 1222 public CoefficientContext saveComponent(CoefficientContext ctx) throws Exception { 1223 if (ctx.getProject() != null) { 1224 ProjectComponent pv = new ProjectComponent(); 1225 ctx.setProperties(pv); 1226 if (ctx.getParameter("id") == null) { 1227 ctx.getProject().addComponent(pv); 1228 } else { 1229 ctx.getProject().updateComponent(pv); 1230 } 1231 HibernateUtil.saveOrUpdate(ctx.getProject()); 1232 HashMap forwardMap = new HashMap (); 1233 forwardMap.put("clearProject", "true"); 1234 forwardMap.put("projectId", ctx.getProject().getId()); 1235 ctx.setForward("Project", "editComponents", forwardMap); 1236 } else { 1237 ctx.setError("You must select a " + ProjectConstants.PROJECT_NAME 1238 + " to save a " + ProjectConstants.PROJECT_NAME 1239 + " component"); 1240 } 1241 return ctx; 1242 } 1243 1244 public CoefficientContext saveVersion(CoefficientContext ctx) throws Exception { 1245 if (ctx.getProject() != null) { 1246 ProjectVersion pv = new ProjectVersion(); 1247 ctx.setProperties(pv); 1248 if (ctx.getParameter("id") == null) { 1249 ctx.getProject().addVersion(pv); 1250 } else { 1251 ctx.getProject().updateVersion(pv); 1252 } 1253 HibernateUtil.saveOrUpdate(ctx.getProject()); 1254 HashMap forwardMap = new HashMap (); 1255 forwardMap.put("clearProject", "true"); 1256 forwardMap.put("projectId", ctx.getProject().getId()); 1257 ctx.setForward("Project", "editVersions", forwardMap); 1258 } else { 1259 ctx.setError("You must select a " + ProjectConstants.PROJECT_NAME 1260 + " to save a " + ProjectConstants.PROJECT_NAME 1261 + " version"); 1262 } 1263 return ctx; 1264 } 1265 1266 public CoefficientContext transition(CoefficientContext ctx) throws Exception { 1267 String tranName = ctx.getParameter("choice"); 1268 Project prj = ctx.getProject(); 1269 WorkflowData wd = null; 1270 if (prj != null) { 1271 wd = (WorkflowData)ctx 1272 .getSessionAttribute("__current_workflow_data__"); 1273 } 1274 if ((wd != null) && (tranName != null) && !tranName.trim().equals("")) { 1275 State currState = 1277 WorkflowEvaluator.getState(wd.getWorkflowName(), 1278 wd.getCurrentState()); 1279 StateTransition st = null; 1280 for (Enumeration enumer = currState.enumerateStateTransition(); 1281 enumer.hasMoreElements();) { 1282 st = (StateTransition) enumer.nextElement(); 1283 if (st.getDestination().equals(tranName)) { 1284 break; 1285 } 1286 } 1287 if (st != null) { 1288 String performActions = ctx.getParameter("performActions"); 1289 boolean pa = performActions == null; 1290 WorkflowEvaluator.transition(ctx, st, wd, pa); 1291 HashMap forwardMap = new HashMap (); 1292 forwardMap.put("clearProject", "true"); 1293 forwardMap.put("projectId", prj.getId()); 1294 ctx.setForward("Project", "viewProject", forwardMap); 1295 HibernateUtil.saveOrUpdate(wd); 1296 } 1297 } else { 1298 ctx.setModuleContent("Unable to change states", 1299 getModuleDisplayName()); 1300 } 1301 return ctx; 1302 } 1303 1304 public CoefficientContext viewProject(CoefficientContext ctx) throws Exception { 1305 publishEvent(new CoefficientEvent("Viewing project ... " + ((ctx.getProject() != null)?ctx.getProject().getName():"none"))); 1306 if (ctx.getProject() != null) { 1307 try { 1309 HibernateUtil.refresh(ctx.getProject()); 1310 HibernateUtil.evict(ctx.getProject()); 1311 } catch(HibernateException he) { 1312 he.printStackTrace(); 1314 } 1315 1316 HashMap map = new HashMap (); 1318 map.put("module", this); 1319 map.put("project", ctx.getProject()); 1320 if(ctx.getProject().getIsWorkflow()) { 1321 WorkflowData wd = (WorkflowData)ctx 1322 .getSessionAttribute("__current_workflow_data__"); 1323 map.put("workflowData", wd); 1324 } 1325 Role role = 1326 SecurityUtil.getHighestRoleForUser(ctx.getCurrentUser(), 1327 ctx.getProject()); 1328 if (role.getRoleValue() <= SecurityUtil.PROJECT_CHAMPION_ROLE_VAL) { 1329 map.put("admin", new Boolean (true)); 1330 } 1331 try { 1332 map.put("memberContent", InvokerFactory.getInvoker() 1333 .invokeOpOnModule("MemberAdmin", "viewMembers", ctx)); 1334 } catch (Exception e) { 1335 e.printStackTrace(); 1336 map.put("memberContent", "none"); 1337 } 1338 1339 map.put("attributes", getAttributes(ctx)); 1340 map.put("project_name", ProjectConstants.PROJECT_NAME); 1341 map.put("project_name_cap", 1342 StringUtils.capitalise(ProjectConstants.PROJECT_NAME)); 1343 1344 HashMap moduleSummaries = new HashMap (); 1345 1346 String modName = null; 1347 try { 1348 WorkflowData wd = (WorkflowData)ctx 1349 .getSessionAttribute("__current_workflow_data__"); 1350 1351 for (Iterator it = ctx.getProject().getModules().iterator(); 1353 it.hasNext();) { 1354 modName = (String ) it.next(); 1355 if (!ctx.getProject().getIsWorkflow() 1356 || WorkflowEvaluator.isModuleViewableForState(ctx, wd, modName)) { 1357 1358 String contentPosition = ctx.getPage() 1359 .getPositionForModule(modName); 1360 1361 if(Theme.CENTER_POSITION.equals(contentPosition) && 1362 ProjectConstants.SHOW_IN_LINE_MODULE_SUMMARIES) { 1363 1366 InvokerFactory.getInvoker() 1367 .invokeOpOnModule(modName, "showSummaryForProject", ctx); 1368 String displayName = (String )InvokerFactory.getInvoker() 1369 .invokeGetterOnModule(modName, "moduleDisplayName"); 1370 1371 String moduleContent = ctx.clearModuleContent(displayName); 1372 if(moduleContent != null && !"".equals(moduleContent.trim())) { 1373 moduleSummaries.put(displayName, moduleContent); 1374 } 1375 } else if(!Theme.CENTER_POSITION.equals(contentPosition) && 1376 !Theme.NULL_POSITION.equals(contentPosition)) { 1377 InvokerFactory.getInvoker() 1379 .invokeOpOnModule(modName, "showSummaryForProject", ctx); 1380 } 1381 } 1382 } 1383 1384 } catch (Exception e) { 1385 System.err.println("There was an error rendering the display of module: " 1386 + modName); 1387 } 1388 1389 map.put("moduleSummaries", moduleSummaries); 1390 1391 try { 1392 map.put("bookmarkLink", URLEncoder 1393 .encode(ProjectConstants.PROJECT_NAME + "/" 1394 + ctx.getProject().getShortName(), "UTF-8")); 1395 } catch (UnsupportedEncodingException uee) { 1396 } 1398 StringBuffer sb = 1399 VelocityScreenUtil.getProcessedScreen("view.vm", map); 1400 ctx.setModuleContent(sb.toString(), getModuleDisplayName()); 1401 1402 } else { 1403 ctx.setModuleContent("You must have selected a " 1404 + ProjectConstants.PROJECT_NAME 1405 + " before you can view it", 1406 getModuleDisplayName()); 1407 } 1408 return ctx; 1409 } 1410 1411 public CoefficientContext viewComponent(CoefficientContext ctx) throws Exception { 1412 HashMap map = new HashMap (); 1413 map.put("project", ctx.getProject()); 1414 Long id = ctx.getParameterAsLong("id", -1); 1415 if (id.longValue() > -1) { 1416 map.put("component", HibernateUtil.load(ProjectComponent.class, id)); 1417 } else { 1418 ctx.setError("You must choose a component to view"); 1419 } 1420 StringBuffer sb = 1421 VelocityScreenUtil.getProcessedScreen("viewComponent.vm", map); 1422 ctx.setModuleContent(sb.toString(), getModuleDisplayName()); 1423 return ctx; 1424 } 1425 1426 public CoefficientContext viewVersion(CoefficientContext ctx) throws Exception { 1427 HashMap map = new HashMap (); 1428 map.put("project", ctx.getProject()); 1429 Long id = ctx.getParameterAsLong("id", -1); 1430 if (id.longValue() > -1) { 1431 map.put("version", HibernateUtil.load(ProjectVersion.class, id)); 1432 } else { 1433 ctx.setError("You must choose a version to view"); 1434 } 1435 StringBuffer sb = 1436 VelocityScreenUtil.getProcessedScreen("viewVersion.vm", map); 1437 ctx.setModuleContent(sb.toString(), getModuleDisplayName()); 1438 return ctx; 1439 } 1440 1441 public CoefficientContext toggleVisibility(CoefficientContext ctx) throws Exception { 1442 Project project = ctx.getProject(); 1443 project.setIsPublic(!project.getIsPublic()); 1444 HibernateUtil.saveOrUpdate(project); 1445 ctx.setModuleContent("The " + ProjectConstants.PROJECT_NAME + "s visibility has been changed.", getModuleDisplayName()); 1446 return ctx; 1447 } 1448 1449 public WorkflowData findWorkflowDataByLinkId(Long linkId) throws Exception { 1450 WorkflowData data = null; 1451 List datas = HibernateUtil.find("FROM " + WorkflowData.class.getName() 1452 + " as wfdata where wfdata.linkId = ?", 1453 linkId, Hibernate.LONG); 1454 if(datas.size() == 1) { 1455 data = (WorkflowData)datas.get(0); 1456 } 1457 return data; 1458 } 1459 1460 private List getAttributeNames() { 1461 List list = getAttributes(); 1462 List newlist = new ArrayList (); 1463 int i; 1464 1465 for (i = 0; i < list.size(); i++) { 1466 newlist.add(((AttributeDisplayData) (list.get(i))).getAttributeName()); 1467 } 1468 1469 return newlist; 1470 } 1471 1472 private List getAttributeNames(CoefficientContext ctx) { 1473 List list = getAttributes(ctx); 1474 List newlist = new ArrayList (); 1475 int i; 1476 1477 for (i = 0; i < list.size(); i++) { 1478 newlist.add(((AttributeDisplayData) (list.get(i))).getAttributeName()); 1479 } 1480 1481 return newlist; 1482 } 1483 1484 private List getAttributes(CoefficientContext ctx) { 1485 List attributes = new ArrayList (); 1486 1487 try { 1491 List list; 1492 List data; 1493 ProjectAttributeData pad; 1494 int i; 1495 int k; 1496 Long idLong = new Long (0); 1497 ProjectAttribute attr; 1498 1499 list = ctx.getProject() 1500 .getAttributes(); 1501 data = ctx.getProject() 1502 .getAttributeData(); 1503 for (i = 0; i < list.size(); i++) { 1504 AttributeDisplayData displayData = new AttributeDisplayData(); 1505 idLong = (Long ) (list.get(i)); 1506 1507 attr = (ProjectAttribute)InvokerFactory.getInvoker() 1508 .invokeMethodOnModule("AttributeDataManagement", 1509 "getAttributeByNumber", 1510 new Object []{idLong}); 1511 displayData.setAttributeName(attr.getName()); 1512 1513 displayData.setAttributeId(idLong); 1514 for (k = 0; k < data.size(); k++) { 1515 pad = (ProjectAttributeData) (data.get(k)); 1516 1517 if (idLong.compareTo(pad.getAttributeId()) == 0) { 1518 displayData.getAttributeData() 1519 .add(pad.getDescription()); 1520 } 1521 } 1522 attributes.add(displayData); 1523 } 1524 } catch (Throwable t) { 1525 System.out.println("Attribute decyphering throws " + t); 1526 } 1527 1528 return attributes; 1529 } 1530 1531 private List getAttributes() { 1532 List attributeDisplayData = new ArrayList (); 1533 1534 try { 1538 AttributeDisplayData displayData = new AttributeDisplayData(); 1539 1540 List attributes = (List )InvokerFactory.getInvoker() 1541 .invokeMethodOnModule("AttributeDataManagement", 1542 "getExistingAttributes", 1543 new Object [0]); 1544 1545 int i; 1546 ProjectAttribute pa; 1547 for (i = 0; i < attributes.size(); i++) { 1548 displayData = new AttributeDisplayData(); 1549 pa = (ProjectAttribute) (attributes.get(i)); 1550 displayData.setAttributeName(pa.getName()); 1551 displayData.setAttributeId(pa.getId()); 1552 1553 List data = (List )InvokerFactory.getInvoker() 1554 .invokeMethodOnModule("AttributeDataManagement", 1555 "getExistingDataKeywords", 1556 new Object [] {pa.getId()}); 1557 displayData.setAttributeData(data); 1558 attributeDisplayData.add(displayData); 1559 } 1560 } catch (Throwable t) { 1561 System.err.println("<< error loading attributes"); 1562 } 1563 1564 return attributeDisplayData; 1565 } 1566 1567 private ArrayList getModules() { 1568 ArrayList mods = new ArrayList (); 1569 Context initialContext = null; 1570 try { 1571 initialContext = za.org.coefficient.util.common.ContextUtil.getContext(); 1572 for (NamingEnumeration ne = initialContext.list(Constants.JNDI_CONTEXT + NAME_SPACE); 1573 ne.hasMore();) { 1574 NameClassPair bind = (NameClassPair ) ne.next(); 1575 mods.add(bind.getName()); 1576 } 1577 for (NamingEnumeration ne = initialContext.list(Constants.JNDI_CONTEXT +NAME_SPACE2); 1578 ne.hasMore();) { 1579 NameClassPair bind = (NameClassPair ) ne.next(); 1580 mods.add(bind.getName()); 1581 } 1582 } catch (Exception e) { 1583 e.printStackTrace(); 1584 } finally { 1585 try { 1586 initialContext.close(); 1587 } catch (Exception e) { 1588 e.printStackTrace(); 1589 } 1590 } 1591 1592 return mods; 1593 } 1594 1595 private void cleanUpModules(List newModules, Project prj, 1596 CoefficientContext ctx) throws Exception { 1597 for (Iterator it = prj.getModules() 1598 .iterator(); it.hasNext();) { 1599 String modName = (String ) it.next(); 1600 if (!newModules.contains(modName)) { 1601 try { 1603 System.err.println("<< invoking delete for: " + modName); 1604 InvokerFactory.getInvoker() 1605 .invokeOpOnModule(modName, "deleteAllForProject", ctx); 1606 } catch(Exception e) { 1607 System.err.println("<< invoke on deleteAllForProject for module " 1608 + modName + " failed!"); 1609 } 1610 } 1611 } 1612 prj.setModules(newModules); 1613 } 1614 1615 private void setupSimpleScreen(CoefficientContext ctx, String screenName) { 1616 HashMap map = new HashMap (); 1617 map.put("project", ctx.getProject()); 1618 map.put("project_name", ProjectConstants.PROJECT_NAME); 1619 map.put("project_name_cap", 1620 StringUtils.capitalise(ProjectConstants.PROJECT_NAME)); 1621 map.put("workflow_number", new Integer (WorkflowEvaluator.getAllRegisteredWorkflows().size())); 1622 java.lang.reflect.Field [] fields = ProjectConstants.class.getFields(); 1623 for (int i = 0; i < fields.length; i++) { 1624 String name = fields[i].getName(); 1625 String value = null; 1626 try { 1627 value = fields[i].get(null).toString(); 1628 } catch (Exception e) { 1629 e.printStackTrace(); 1630 } 1631 map.put(name, value); 1632 } 1633 StringBuffer sb = 1634 VelocityScreenUtil.getProcessedScreen(screenName, map); 1635 ctx.setModuleContent(sb.toString(), getModuleDisplayName()); 1636 } 1637} 1638 | Popular Tags |