/** * Takes a submitted contact and adds it to the database if it passes validation. * * @param request * @param response */ public void registerContact(ActionRequest request, ActionResponse response) throws Exception { _log.debug("registerContact"); Contact contact = ActionUtil.contactFromRequest(request); ArrayList<String> errors = new ArrayList(); ThemeDisplay themeDisplay = (ThemeDisplay) request.getAttribute(WebKeys.THEME_DISPLAY); String community = ""; try { community = themeDisplay.getScopeGroupName(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } boolean regValid = ContactMeRegValidator.validateRegistration(contact, errors); if (regValid) { long userId = themeDisplay.getUserId(); contact.setCreateDate(new Date(System.currentTimeMillis())); contact.setUserId(userId); contact.setStatus(Constants.CONTACT_RECEIVED); contact.setCommunity(community); ContactLocalServiceUtil.addContact(contact); SessionMessages.add(request, "thank-you-message"); PortletPreferences preferences = request.getPreferences(); long companyId = PortalUtil.getCompanyId(request); Message message = new Message(); message.put("from-name", preferences.getValue("fromName", "")); message.put("from-address", preferences.getValue("fromEMail", "")); message.put("subject", preferences.getValue("subject", "")); message.put("body", preferences.getValue("body", "")); message.put("signature", preferences.getValue("signature", "")); message.put("to-name", contact.getFullname()); message.put("to-address", contact.getEmail()); message.put("community", contact.getCommunity()); message.put("companyId", companyId); MessageBusUtil.sendMessage("liferay/contactme", message); response.setRenderParameter("jspPage", viewThankYouJSP); } else { for (String error : errors) { SessionErrors.add(request, error); } SessionErrors.add(request, "error-saving-contact"); response.setRenderParameter("jspPage", viewAddContactJSP); request.setAttribute("cmContact", contact); } }
public static boolean validateRegistration(Contact contact, List errors) throws Exception { boolean valid = true; _log.debug("contact.getFullname():" + contact.getFullname()); _log.debug("contact.getEmail():" + contact.getEmail()); _log.debug("contact.getPhone():" + contact.getPhone()); _log.debug("contact.getSubject():" + contact.getSubject()); _log.debug("contact.getCommunity():" + contact.getCommunity()); if (Validator.isNull(contact.getFullname())) { errors.add("fullname-required"); valid = false; } if (Validator.isNull(contact.getEmail())) { errors.add("email-required"); valid = false; } else if (!Validator.isEmailAddress(contact.getEmail())) { errors.add("email-invalid"); valid = false; } /*if (Validator.isNull(contact.getPhone())) { errors.add("phone-required"); valid = false; }*/ if (Validator.isNotNull(contact.getPhone())) { if (!Validator.isPhoneNumber(contact.getPhone())) { errors.add("phone-invalid"); valid = false; } } /* if (Validator.isNull(contact.getSubject())) { errors.add("subject-required"); valid = false; } */ _log.debug("valid:" + valid); return valid; }