Subversion Repositories bacoAlunos

Rev

Rev 1426 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

package pt.estgp.estgweb.services.email;

import org.apache.log4j.Logger;
import pt.estgp.estgweb.Globals;
import pt.estgp.estgweb.services.expceptions.ServiceException;
import pt.estgp.estgweb.services.jobs.JobHandler;
import pt.estgp.estgweb.utils.Email;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * @author Jorge Machado
 * @date 11/Jul/2008
 * @see pt.estgp.estgweb.services.sms
 */

public class EMAILJob implements JobHandler, 1.5.0/docs/api/java/io/Serializable.html">Serializable
{
    private static final 1.5.0/docs/api/java/util/logging/Logger.html">Logger logger = 1.5.0/docs/api/java/util/logging/Logger.html">Logger.getLogger(EMAILJob.class);

    private Email email;




    public EMAILJob(Email email)
    {
        this.email = email;
    }




    //TODO WHEN BACK TO BCC REMOVE this to 20 and remove Thread
    private static final int NUMBER_EMAILS_TO = 1;
    private static final int pauseTime = 1000;


    public void runJob()
    {
        final Email cloned = email.cloneEmailForJob();
        logger.info("Opening new Thread to send emails");
        new 1.5.0/docs/api/java/lang/Thread.html">Thread(
                new 1.5.0/docs/api/java/lang/Runnable.html">Runnable() {
                    @1.5.0/docs/api/java/lang/Override.html">Override
                    public void run() {
                        if(Globals.TEST_ENVIRONEMENT && !cloned.isSystemAssumingTestEmail())
                        {
                            1.5.0/docs/api/java/lang/String.html">String warn = "System in TEST Environement - will not send EMAIL";
                            logger.warn(warn);
                            return;
                        }
                        try
                        {
                            List<String> recipientsOriginal = new ArrayList<String>();
                            List<String> recipients = new ArrayList<String>();
                            recipients.addAll(cloned.getRecipients());
                            recipientsOriginal.addAll(cloned.getRecipients());
                            while(recipients.size() > 0)
                            {
                                List<String> recipientsToSend = new ArrayList<String>();

                                int contador = 0;
                                Iterator<String> iter = recipients.iterator();
                                while(iter.hasNext() && contador < NUMBER_EMAILS_TO)
                                {
                                    recipientsToSend.add(iter.next());
                                    iter.remove();
                                    contador++;
                                }
                                cloned.setRecipients(recipientsToSend);
                                new SendEmailService().sendEmail(cloned);
                                try {
                                    1.5.0/docs/api/java/lang/Thread.html">Thread.sleep(pauseTime);
                                } catch (1.5.0/docs/api/java/lang/InterruptedException.html">InterruptedException e) {
                                    logger.error(e,e);
                                }
                            }
                            try {
                                1.5.0/docs/api/java/lang/Thread.html">Thread.sleep(1000);
                            } catch (1.5.0/docs/api/java/lang/InterruptedException.html">InterruptedException e) {
                                logger.error(e,e);
                            }
                            cloned.setRecipients(recipientsOriginal);
                        }
                        catch (ServiceException e)
                        {
                            logger.error("Sending EMAIL:" + e.toString(),e);
                        }
                    }
                }
        ).start();
    }


}