/Users/richardallenbair/Documents/Source/Projects/nonsense/swingx/src/java/org/jdesktop/swingx/error/EmailErrorReporter.java
/*
 * $Id: EmailErrorReporter_IncompleteDocs.html 1302 2006-07-31 17:58:46Z rbair $
 *
 * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
 * Santa Clara, California 95054, U.S.A. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

package org.jdesktop.swingx.error;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.jdesktop.swingx.*;
import org.jdesktop.swingx.util.SwingWorker;


/**
 * <p>A convenient base class for email based {@link ErrorReporter} implementations.
 * <code>EmailErrorReporter</code> extends {@link JavaBean}, and thus contains
 * all the infrastructure for firing property change events.</p>
 *
 * <p><code>EmailErrorReporter</code> allows developers to specify an array of
 * "to" addresses, "cc" addresses, and "bcc" addresses. It also supports an
 * arbitrary number of attachments. Typically, the subject and message parts of
 * the email are constructed automatically by subclasses. Hence, the
 * {@link #constructMessageBody(IncidentInfo)} and 
 * {@link constructSubject(IncidentInfo)} protected methods. Subclasses
 * should override these methods if they want to change the default message or
 * subject generated by this class.</p>
 *
 * <p>Subclasses must implement the {@link sendMessage()} method. This is the
 * method that performs the actual send task. Implementations of this method
 * need not worry about blocking the method. This class will ensure that
 * {@link sendMessage} is called on a background thread.</p>
 *
 * @author Alexander Zuev
 * @author rbair
 * @version 1.1
 */
public abstract class EmailErrorReporter extends JavaBean implements ErrorReporter {
    private List<String> toList = new ArrayList<String>();
    private List<String> ccList = new ArrayList<String>();
    private List<String> bccList = new ArrayList<String>();
    private List<Object> attachments = new ArrayList<Object>();
    
    /**
     * Creates a new <code>EmailErrorReporter</code>.
     */
    public EmailErrorReporter() {
        //...
    }
    
    public void setToAddresses(int index, String to) {}
    public String getToAddresses(int index) {return null;}
    public void setToAddresses(String[] addresses) {
        toList.clear();
        toList.addAll(Arrays.asList(addresses));
    }
    public String[] getToAddresses() {return null;}
    
    public void setCcAddresses(int index, String to) {}
    public String getCcAddresses(int index) {return null;}
    public void setCcAddresses(String[] addresses) {}
    public String[] getCcAddresses() {return null;}

    public void setBccAddresses(int index, String to) {}
    public String getBccAddresses(int index) {return null;}
    public void setBccAddresses(String[] addresses) {}
    public String[] getBccAddresses() {return null;}
    
    public void setAttachments(int index, String to) {}
    public String getAttachments(int index) {return null;}
    public void setAttachments(String[] addresses) {}
    public String[] getAttachments() {return null;}
    
    /**
     * @inheritDoc
     */
    public void reportIncident(IncidentInfo info) {
        SendWorker thread = new SendWorker(this, info);
        thread.execute();
    }

    /**
     * Gets the message
     * This method is used to extract text message from the provided <code>IncidentInfo</code>.
     * Override this method to change text formatting or contents.
     *
     * @param incident - Incapsulates all the information about error
     * @return String to be used as a body message in report.
     */
    protected String constructMessageBody(IncidentInfo incident) {
        String body = incident.getBasicErrorMessage();
        if(incident.getDetailedErrorMessage() != null) {
            body.concat("\n"+incident.getDetailedErrorMessage());
        }
        if(incident.getErrorException() != null) {
            StringWriter sw = new StringWriter();
            PrintWriter pw = new PrintWriter(sw);
            incident.getErrorException().printStackTrace(pw);
            body = body + "\n ----- " + sw.toString();
        }
        return body;
    }
    
    protected String constructSubject(IncidentInfo incident) {
        return incident.getHeader();
    }

    /**
     * Compose and send message
     * @param info
     */
    protected abstract void sendMessage(IncidentInfo info);
    
    /**
     * A very simple SwingWorker for performing the "send" operation on a background
     * thread. This may be expanded in the future to allow a GUI showing the 
     * process of the send operation.
     */
    private static final class SendWorker extends SwingWorker {
        private EmailErrorReporter reporter;
        private IncidentInfo info;
        
        private SendWorker(EmailErrorReporter r, IncidentInfo i) {
            if (r == null || i == null) {
                throw new NullPointerException("Both EmailErrorReporter and " +
                        "IncidentInfo must be specified");
            }
            this.reporter = r;
            this.info = i;
        }
        
        @Override
        protected Object doInBackground() throws Exception {
            reporter.sendMessage(info);
            return null;
        }
    }
}