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

package hu.nir.nmail.utils;

import hu.nir.nmail.dbm.DataManager;
import hu.nir.nmail.ejb.entity.EmailAddress;
import hu.nir.nmail.ejb.entity.NmailUser;
import hu.nir.nmail.ejb.stateless.NmailUserManager;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;

/**
 *
 * @author attila
 */
public class AddressStoreHelper {
    DataManager dataManager;
    Logger logger = Logger.getLogger(AddressStoreHelper.class.getName());
    
    public AddressStoreHelper(DataManager dataManager) {
        this.dataManager = dataManager;
    }
    
    public int storeAddresses(InputStream inputStream, NmailUser nmailUser) throws IOException {
        InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
        BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
        
        List<EmailAddress> newEmailAddressList = new ArrayList<EmailAddress>();
        String line = null;
        long lineIndex = 0;
        
        while ((line = bufferedReader.readLine()) != null) {
            
            processLine(line, newEmailAddressList, nmailUser);
            lineIndex++;
            
            if (lineIndex > 100) {
                updateAddressTable(nmailUser, newEmailAddressList);
                lineIndex = 0;
                newEmailAddressList = new ArrayList<EmailAddress>();
            }
        }
        
        updateAddressTable(nmailUser, newEmailAddressList);
        
        return newEmailAddressList.size();
    }
    
    private void updateAddressTable(NmailUser nmailUser, List<EmailAddress> emailAddressesList) {
        nmailUser.setEmailAddressCollection(emailAddressesList);
        NmailUserManager nmailUserManager = dataManager.getNmailUserManager();
        nmailUserManager.update(nmailUser);
        logger.log(Level.INFO, "EmailAddresses table updated.");
    }
    
    private void processLine(String line, List<EmailAddress> emailAddressList, NmailUser nmailUser) {
        StringTokenizer tokenizer = new StringTokenizer(line, ",");
        
        while (tokenizer.hasMoreTokens()) {
            String token = tokenizer.nextToken();
            token = token.trim();
            
            try {
                InternetAddress address = new InternetAddress(token);
                EmailAddress emailAddress = new EmailAddress();
                emailAddress.setEmailAddress(address.getAddress());
                emailAddress.setUserId(nmailUser);
                emailAddressList.add(emailAddress);
            } catch (AddressException ex) {
                //do nothing, because this is not an email address
                logger.log(Level.WARNING, token+" is a wong email address");
            }
        }
    }
}
