From: <mb...@re...> - 2004-11-05 23:49:15
|
Author: mbooth Date: 2004-11-06 00:42:14 +0100 (Sat, 06 Nov 2004) New Revision: 86 Modified: ccm-core/trunk/src/com/arsdigita/formbuilder/actions/SimpleEmailListener.java Log: Only output relevant (directly user entered) values in simple email. Format it nicely and handle array values. Modified: ccm-core/trunk/src/com/arsdigita/formbuilder/actions/SimpleEmailListener.java =================================================================== --- ccm-core/trunk/src/com/arsdigita/formbuilder/actions/SimpleEmailListener.java 2004-11-05 23:41:13 UTC (rev 85) +++ ccm-core/trunk/src/com/arsdigita/formbuilder/actions/SimpleEmailListener.java 2004-11-05 23:42:14 UTC (rev 86) @@ -18,33 +18,43 @@ */ package com.arsdigita.formbuilder.actions; +import com.arsdigita.formbuilder.PersistentComponent; +import com.arsdigita.formbuilder.PersistentFormSection; +import com.arsdigita.formbuilder.PersistentProcessListener; +import com.arsdigita.formbuilder.PersistentSubmit; +import com.arsdigita.formbuilder.PersistentWidget; import com.arsdigita.formbuilder.util.Placeholders; +import com.arsdigita.bebop.FormData; +import com.arsdigita.bebop.FormProcessException; +import com.arsdigita.bebop.event.FormProcessListener; +import com.arsdigita.bebop.event.FormSectionEvent; +import com.arsdigita.bebop.parameters.ParameterData; +import com.arsdigita.kernel.Kernel; +import com.arsdigita.kernel.PersonName; +import com.arsdigita.kernel.User; +import com.arsdigita.mail.Mail; import com.arsdigita.persistence.OID; import com.arsdigita.persistence.DataObject; import com.arsdigita.persistence.metadata.ObjectType; import com.arsdigita.domain.DataObjectNotFoundException; +import com.arsdigita.util.UncheckedWrapperException; +import com.arsdigita.web.Web; -import com.arsdigita.bebop.FormData; -import com.arsdigita.bebop.FormProcessException; -import com.arsdigita.bebop.event.FormSectionEvent; - -import com.arsdigita.mail.Mail; - -import com.arsdigita.formbuilder.PersistentProcessListener; -import com.arsdigita.formbuilder.util.Placeholders; - import java.math.BigDecimal; import java.util.Iterator; import javax.mail.MessagingException; -import com.arsdigita.util.UncheckedWrapperException; -import com.arsdigita.bebop.event.FormProcessListener; +import org.apache.log4j.Logger; + public class SimpleEmailListener extends PersistentProcessListener { public static final String BASE_DATA_OBJECT_TYPE = "com.arsdigita.formbuilder.actions.SimpleEmailListener"; + private static final Logger s_log = + Logger.getLogger( SimpleEmailListener.class ); + public static final String TO = "recipient"; public static final String SUBJECT = "subject"; @@ -138,29 +148,74 @@ public void process(FormSectionEvent e) throws FormProcessException { - Placeholders p = new Placeholders(e.getPageState()); + PersistentFormSection form = getForm(); + Placeholders p = new Placeholders(e.getPageState(), e.getFormData()); - String to = p.interpolate(m_to); String subject = p.interpolate(m_subject); - //String from = p.interpolate("::user.givenname:: ::user.familyname:: <::user.email::>"); - String from = p.interpolate("::user.email::"); - StringBuffer body = new StringBuffer(p.interpolate("::user.givenname:: ::user.familyname:: submittted the following form values:\n\n")); + User user = (User) Kernel.getContext().getParty(); + String from; + if( null == user ) { + from = "notloggedinuser@" + + Web.getConfig().getServer().getName(); + } else { + from = user.getPrimaryEmail().getEmailAddress(); + } + StringBuffer body = new StringBuffer(); + + if( null != user ) { + PersonName name = user.getPersonName(); + + body.append( name.getGivenName() ).append( ' ' ); + body.append( name.getFamilyName() ); + body.append( " submitted the following:\n\n" ); + } + + String submit = null; + FormData data = e.getFormData(); - Iterator keys = data.keySet().iterator(); - while (keys.hasNext()) { - String key = (String)keys.next(); - Object value = data.get(key); + Iterator components = form.getComponents().iterator(); + while( components.hasNext() ) { + PersistentComponent c = (PersistentComponent) components.next(); - body.append(key + ": " + (value == null ? "(null)" : value.toString()) + "\n"); + if( c instanceof PersistentWidget ) { + PersistentWidget w = (PersistentWidget) c; + + if( c instanceof PersistentSubmit ) { + submit = c.getDescription(); + } else { + body.append( c.getDescription() ).append( '\n' ); + + Object value = data.get( w.getParameterName() ); + if( null != value ) { + if( value.getClass().isArray() ) { + Object[] values = (Object[]) value; + + for( int i = 0; i < values.length; i++ ) { + body.append( values[i].toString() ); + body.append( '\n' ); + } + } else { + body.append( value.toString() ); + body.append( '\n' ); + } + + body.append( '\n' ); + } + } + } } + if( null != submit ) { + body.append( "User clicked " ).append( submit ); + } + try { - Mail message = new Mail(to, from, subject, body.toString()); + Mail message = new Mail(m_to, from, subject, body.toString()); message.send(); } catch (MessagingException ex) { - throw new UncheckedWrapperException("cannot send message", ex); + throw new FormProcessException( "cannot send message", ex ); } } } |