/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package hu.nir.nmail.threads;

import hu.nir.nmail.dbm.DataManager;
import hu.nir.nmail.ejb.entity.NmailUser;
import hu.nir.nmail.utils.AddressStoreHelper;
import java.io.IOException;
import java.io.InputStream;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author attila
 */
public class ImporterThread implements Runnable {
    private DataManager dataManager;
    private Thread thread;
    private InputStream inputStream;
    private NmailUser nmailUser;
    
    public ImporterThread(DataManager dataManager, InputStream inputStream, NmailUser nmailUser) {
        this.dataManager = dataManager;
        this.inputStream = inputStream;
        this.nmailUser = nmailUser;
    }
    
    public void start() {
        if (thread == null) {
            thread = new Thread(this);
            thread.start();
        }
    }
    
    public void stop() {
        thread = null;
    }
    
    public void run() {
        try {
            AddressStoreHelper addressStoreHelper = new AddressStoreHelper(dataManager);
            addressStoreHelper.storeAddresses(inputStream, nmailUser);
        } catch (IOException ex) {
            Logger.getLogger(ImporterThread.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

}
