* @throws Exception
*/
public ActionForward createAndSubscribe(ActionMapping mapping, ActionForm lf, HttpServletRequest request, HttpServletResponse response)
throws Exception {
MailingListsSubscribeForm form = (MailingListsSubscribeForm) lf;
String[] inodes = form.getMailingListsInodes();
User user = null;
try {
user = APILocator.getUserAPI().loadByUserByEmail(form.getEmailAddress(), APILocator.getUserAPI().getSystemUser(), false);
} catch (NoSuchUserException e) {
Logger.debug(this,"No User Found");
}
User defaultUser = APILocator.getUserAPI().getDefaultUser();
Date today = new Date();
String referrer = null;
if (request.getParameter("referrer") != null && !request.getParameter("referrer").toString().equalsIgnoreCase("")) {
referrer = (String)request.getParameter("referrer");
}
ActionErrors errors = form.validateCreateAndSubscribe(mapping, request);
if(errors.size() > 0) {
saveMessages(request.getSession(), errors);
ActionForward af = mapping.findForward("subscribePage");
if(UtilMethods.isSet(referrer) && !referrer.startsWith(af.getPath())) {
af = new ActionForward(SecurityUtils.stripReferer(request, referrer));
af.setRedirect(true);
}
return af;
}
HibernateUtil.startTransaction();
if(user == null) {
//### CREATE USER ###
user = APILocator.getUserAPI().createUser(null, null);
user.setEmailAddress(form.getEmailAddress());
user.setFirstName(form.getName() == null ? "" : form.getName());
user.setLastName(form.getLastName() == null ? "" : form.getLastName());
user.setPasswordEncrypted(true);
user.setPassword(PublicEncryptionFactory.getRandomEncryptedPassword());
user.setComments("");
user.setGreeting("Welcome, " + user.getFullName() + "!");
user.setCreateDate(today);
user.setActive(true);
APILocator.getUserAPI().save(user,APILocator.getUserAPI().getSystemUser(),false);
}
subscribeToMailingLists(user, inodes, form.isUnsubscribeFromAll());
HibernateUtil.commitTransaction();
ActionMessages message = new ActionMessages();
message.add("message", new ActionMessage("message.myaccount.mailinglists.updated"));
saveMessages(request.getSession(), message);