001package org.apache.wiki.tasks.auth;
002
003import org.apache.log4j.Logger;
004import org.apache.wiki.api.core.Context;
005import org.apache.wiki.api.core.ContextEnum;
006import org.apache.wiki.api.exceptions.WikiException;
007import org.apache.wiki.auth.UserManager;
008import org.apache.wiki.auth.user.UserProfile;
009import org.apache.wiki.i18n.InternationalizationManager;
010import org.apache.wiki.tasks.TasksManager;
011import org.apache.wiki.util.MailUtil;
012import org.apache.wiki.workflow.Outcome;
013import org.apache.wiki.workflow.Task;
014import org.apache.wiki.workflow.WorkflowManager;
015
016import javax.mail.MessagingException;
017import javax.mail.internet.AddressException;
018import java.util.Locale;
019
020
021/**
022 * Handles the actual profile save action. 
023 */
024public class SaveUserProfileTask extends Task {
025
026    private static final long serialVersionUID = 6994297086560480285L;
027    private static final Logger LOG = Logger.getLogger( SaveUserProfileTask.class );
028    private final Locale m_loc;
029
030    /**
031     * Constructs a new Task for saving a user profile.
032     */
033    public SaveUserProfileTask( final Locale loc ) {
034        super( TasksManager.USER_PROFILE_SAVE_TASK_MESSAGE_KEY );
035        m_loc = loc;
036    }
037
038    /**
039     * Saves the user profile to the user database.
040     *
041     * @return {@link org.apache.wiki.workflow.Outcome#STEP_COMPLETE} if the task completed successfully
042     * @throws WikiException if the save did not complete for some reason
043     */
044    @Override
045    public Outcome execute( final Context context ) throws WikiException {
046        // Retrieve user profile
047        final UserProfile profile = ( UserProfile )getWorkflowContext().get( WorkflowManager.WF_UP_CREATE_SAVE_ATTR_SAVED_PROFILE );
048
049        // Save the profile (userdatabase will take care of timestamps for us)
050        context.getEngine().getManager( UserManager.class ).getUserDatabase().save( profile );
051
052        // Send e-mail if user supplied an e-mail address
053        if ( profile != null && profile.getEmail() != null ) {
054            try {
055                final InternationalizationManager i18n = context.getEngine().getManager( InternationalizationManager.class );
056                final String app = context.getEngine().getApplicationName();
057                final String to = profile.getEmail();
058                final String subject = i18n.get( InternationalizationManager.DEF_TEMPLATE, m_loc,
059                                                 "notification.createUserProfile.accept.subject", app );
060
061                final String loginUrl = context.getEngine().getURL( ContextEnum.WIKI_LOGIN.getRequestContext(), null, null );
062                final String content = i18n.get( InternationalizationManager.DEF_TEMPLATE, m_loc,
063                                                 "notification.createUserProfile.accept.content", app,
064                                                 profile.getLoginName(),
065                                                 profile.getFullname(),
066                                                 profile.getEmail(),
067                                                 loginUrl );
068                MailUtil.sendMessage( context.getEngine().getWikiProperties(), to, subject, content );
069            } catch ( final AddressException e) {
070                LOG.debug( e.getMessage(), e );
071            } catch ( final MessagingException me ) {
072                LOG.error( "Could not send registration confirmation e-mail. Is the e-mail server running?", me );
073            }
074        }
075
076        return Outcome.STEP_COMPLETE;
077    }
078
079}