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

package hu.nir.nmail.services;

import hu.nir.nmail.dbm.DataManager;
import hu.nir.nmail.ejb.entity.EmailSendingProces;
import hu.nir.nmail.ejb.entity.NmailUser;
import hu.nir.nmail.ejb.stateless.UserProcesManager;
import hu.nir.nmail.spring.beans.EmailProcesData;
import hu.nir.nmail.utils.NmailUtility;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 *
 * @author attila
 * 
 * Lekérdezi az utolsó MAX_PROCES mennyiségű adott felhasználóhoz tartozó
 * folyamatot, és visszaadja azt az információs oldal számára megjelenítésre.
 */
public class ProcesTableUpdaterService implements NmailService {
    private DataManager dataManager;
    private static final int MAX_PROCES = 10;
    
    public Map<String, Object> processCommand(Object... commands) {
        NmailUser nmailUser = null;
        for (Object command : commands) {
            if (command instanceof NmailUser) {
                nmailUser = (NmailUser) command;
                continue;
            }
        }
        
        if (nmailUser == null) {
            return null;
        }

        UserProcesManager userProcesManager = dataManager.getUserProcesManager();
        List<EmailSendingProces> allEmailSendingProces = userProcesManager.getAllEmailSendingProces(nmailUser);
        List procesList = new ArrayList();
        
        for (int i=allEmailSendingProces.size()-1, n=0; i>0; i--, n++) {
            
            if (n>=MAX_PROCES) {
                break;
            }
            
            EmailProcesData emailProcesData = new EmailProcesData(allEmailSendingProces.get(i));
            procesList.add(emailProcesData);
        }
        
        return NmailUtility.toMap("procesList", procesList);
    }

    public DataManager getDataManager() {
        return dataManager;
    }

    public void setDataManager(DataManager dataManager) {
        this.dataManager = dataManager;
    }

}
