GeneratePermissionTemplateController extends AbstractFormController { @Override protected ModelAndView processFormSubmission(HttpServletRequest aReq, HttpServletResponse aRes, Object aCmd, BindException aErrors) throws Exception { GeneratePermissionTemplateCommand tempCmd = (GeneratePermissionTemplateCommand) aCmd; User loggedInUser = AuthUtil.getRemoteUserObject(); ModelAndView mav = new ModelAndView(); if (tempCmd != null) { if (aErrors.hasErrors()) { mav.addObject(Constants.GBL_CMD_DATA, aErrors.getTarget()); mav.addObject(Constants.GBL_ERRORS, aErrors); mav.setViewName("generate-request"); } else if (GeneratePermissionTemplateCommand.ACTION_GENERATE_TEMPLATE.equals(tempCmd.getAction())) { Long templateOid = tempCmd.getTemplateOid(); Long permissionOid = tempCmd.getPermissionOid(); PermissionTemplate template = permissionTemplateManager.completeTemplate(templateOid, AuthUtil.getRemoteUserObject(),permissionOid); mav.addObject(GeneratePermissionTemplateCommand.MDL_TEMPLATE, template); mav.addObject(Constants.GBL_CMD_DATA, tempCmd); mav.setViewName("generate-request"); } else if (GeneratePermissionTemplateCommand.ACTION_PRINTIT.equals(tempCmd.getAction())) { Long permissionOid = tempCmd.getPermissionOid(); Permission perm = permissionTemplateManager.getPermission(permissionOid); perm.setStatus(Permission.STATUS_REQUESTED); perm.setPermissionSentDate(new Date()); perm.setDirty(true); perm.getSite().setPermissions(perm.getSite().getPermissions()); try { siteManager.save(perm.getSite()); } catch (Exception e) { } mav.addObject(Constants.GBL_MESSAGES, messageSource.getMessage("site.print.success", new Object[] {null}, Locale.getDefault())); mav.addObject("permissions", perm.getSite().getPermissions()); List templates = permissionTemplateManager.getTemplates(AuthUtil.getRemoteUserObject()); mav.addObject(GeneratePermissionTemplateCommand.MDL_TEMPLATES, templates); mav.setViewName("select-permission"); } else if (GeneratePermissionTemplateCommand.ACTION_SEND_EMAIL.equals(tempCmd.getAction())) { Long templateOid = tempCmd.getTemplateOid(); Long permissionOid = tempCmd.getPermissionOid(); Permission perm = permissionTemplateManager.getPermission(permissionOid); PermissionTemplate template = permissionTemplateManager.completeTemplate(templateOid, AuthUtil.getRemoteUserObject(),permissionOid); if (perm.getAuthorisingAgent().getEmail() != null ) { if (!"".equals(perm.getAuthorisingAgent().getEmail().trim())) { String recipientAddress = perm.getAuthorisingAgent().getEmail(); Mailable email = new Mailable(); if (template.getTemplateOverwriteFrom() && notNullOrEmpty(template.getTemplateFrom())) email.setSender(template.getTemplateFrom()); else email.setSender(loggedInUser.getEmail()); if (notNullOrEmpty(template.getTemplateCc())) email.setCcs(template.getTemplateCc()); if (notNullOrEmpty(template.getTemplateBcc())) email.setBccs(template.getTemplateBcc()); email.setRecipients(recipientAddress); email.setMessage(template.getParsedText()); String replyTo = template.getReplyTo(); if(notNullOrEmpty(replyTo)) { email.setReplyTo(replyTo); } if (notNullOrEmpty(template.getTemplateSubject())) email.setSubject(template.getTemplateSubject()); else email.setSubject("Web Curator Tool - Permission Request Email"); mailServer.send(email); perm.setStatus(Permission.STATUS_REQUESTED); perm.setPermissionSentDate(new Date()); perm.setDirty(true); perm.getSite().setPermissions(perm.getSite().getPermissions()); try { siteManager.save(perm.getSite()); } catch (Exception e) { } mav.addObject(Constants.GBL_MESSAGES, messageSource.getMessage("site.email.success", new Object[] {recipientAddress}, Locale.getDefault())); } else { mav.addObject(Constants.GBL_MESSAGES,messageSource.getMessage("site.invalid.email.for.template", new Object[] {template.getTemplateName() }, Locale.getDefault())); } } else { mav.addObject(Constants.GBL_MESSAGES,messageSource.getMessage("site.invalid.email.for.template", new Object[] {template.getTemplateName() }, Locale.getDefault())); } mav.addObject("permissions", perm.getSite().getPermissions()); List templates = permissionTemplateManager.getTemplates(AuthUtil.getRemoteUserObject()); mav.addObject(GeneratePermissionTemplateCommand.MDL_TEMPLATES, templates); mav.setViewName("select-permission"); } } return mav; } GeneratePermissionTemplateController(); void setSiteManager(SiteManager siteManager); void setPermissionTemplateManager(
            PermissionTemplateManager permissionTemplateManager); void setMailServer(MailServer mailServer); void setMessageSource(MessageSource messageSource);  }
@Test public void testProcessFormSubmission() { try { MockHttpServletRequest request = new MockHttpServletRequest(); MockHttpServletResponse response = new MockHttpServletResponse(); BindException aError = new BindException(new GeneratePermissionTemplateCommand(), GeneratePermissionTemplateCommand.ACTION_GENERATE_TEMPLATE); SiteManager sitemanager = new MockSiteManagerImpl(testFile); PermissionTemplateManager permissionTemplateManager = new MockPermissionTemplateManagerImpl(testFile); PermissionTemplate temp = permissionTemplateManager.getTemplate(1L); temp.setTemplate("test"); temp.setTemplateType("testType"); testInstance.setSiteManager(sitemanager); testInstance.setPermissionTemplateManager(permissionTemplateManager); testInstance.setMessageSource(new MockMessageSource()); GeneratePermissionTemplateCommand aCommand = null; ModelAndView mav = null; aCommand = new GeneratePermissionTemplateCommand(); aCommand.setAction(GeneratePermissionTemplateCommand.ACTION_GENERATE_TEMPLATE); aCommand.setTemplateOid(1L); aCommand.setPermissionOid(1L); mav = testInstance.processFormSubmission(request, response, aCommand, aError); assert(mav != null); assert(mav.getModel().values() != null); assert(mav.getViewName().equals("generate-request")); aCommand = new GeneratePermissionTemplateCommand(); aCommand.setAction(GeneratePermissionTemplateCommand.ACTION_PRINTIT); aCommand.setPermissionOid(1L); mav = testInstance.processFormSubmission(request, response, aCommand, aError); assert(mav != null); assert(mav.getModel().values() != null); assert(mav.getViewName().equals("select-permission")); aCommand = new GeneratePermissionTemplateCommand(); aCommand.setAction(GeneratePermissionTemplateCommand.ACTION_SEND_EMAIL); aCommand.setTemplateOid(1L); aCommand.setPermissionOid(1L); mav = testInstance.processFormSubmission(request, response, aCommand, aError); assert(mav != null); assert(mav.getModel().values() != null); assert(mav.getViewName().equals("select-permission")); } catch (Exception e) { String message = e.getClass().toString() + " - " + e.getMessage(); log.debug(message); fail(message); } }