/*
 * Copyright 
 *   2005 axYus - www.axyus.com
 *   2005 C.Marchand - christophe.marchand@axyus.com
 * 
 * This file is part of XEMELIOS.
 * 
 * XEMELIOS is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * XEMELIOS 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 General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with XEMELIOS; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package fr.gouv.finances.dgfip.xemelios.common;

import fr.gouv.finances.dgfip.xemelios.common.config.DocumentsModel;
import fr.gouv.finances.dgfip.xemelios.common.config.EtatModel.Marker;
import java.util.Enumeration;
import java.util.Hashtable;

/**
 * Permet de stocker le nombre d'etats et d'elements contenus dans un document
 * 
 * @author chm
 */
public class FileInfo {

    private Hashtable<Marker,Long> elementsCount;
    private long globalCount = 0L;
    /**
     * 
     */
    public FileInfo() {
        super();
        elementsCount = new Hashtable<Marker,Long>();
    }
    public void merge(FileInfo other) {
        if(other==null) return;
        for(Enumeration<Marker> enumer=other.elementsCount.keys(); enumer.hasMoreElements();) {
            Marker marker = enumer.nextElement();
            Long otherCount = other.elementsCount.get(marker);
            Long thisCount = this.elementsCount.get(marker);
            if(thisCount!=null) otherCount = new Long(otherCount.longValue() + thisCount.longValue());
            this.elementsCount.put(marker,otherCount);
            other.elementsCount.remove(marker);
        }
        if(!other.elementsCount.isEmpty()) this.elementsCount.putAll(other.elementsCount);
        globalCount+=other.getGlobalCount();
    }
    public void incElement(Marker marker) {
        Long ret = elementsCount.get(marker);
        if(ret==null) ret = new Long(0);
        elementsCount.put(marker,new Long(ret.longValue()+1));
        globalCount++;
    }

    public String toString(DocumentsModel dm) {
        StringBuffer ret = new StringBuffer();
        Hashtable<String,StringBuffer> docs = new Hashtable<String,StringBuffer>();
        for(Enumeration<Marker> enumer=elementsCount.keys(); enumer.hasMoreElements();) {
            Marker marker = enumer.nextElement();
            StringBuffer doc = docs.get(marker.getDocId());
            if(doc==null) {
                doc = new StringBuffer();
                doc.append(dm.getDocumentById(marker.getDocId()).getTitre()).append("\n");
                docs.put(marker.getDocId(), doc);
            }
            doc.append("   ").append(dm.getDocumentById(marker.getDocId()).getEtatById(marker.getEtatId()).getTitre()).append("\t-> ").append(elementsCount.get(marker)).append(" éléments importés\n");
        }
        for(Enumeration<StringBuffer> buffs=docs.elements(); buffs.hasMoreElements();) {
            StringBuffer buff = buffs.nextElement();
            ret.append(buff);
        }
        ret.append("\n");
        return ret.toString();
    }

    public long getGlobalCount() { return globalCount; }

    @Override
    @Deprecated
    public String toString() {
        return super.toString();
    }


}
