/*
 * Copyright
 *   2008 axYus - www.axyus.com
 *   2008 C.Bosquet - charles.bosquet@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.web.gwt.server.service;

import fr.gouv.finances.dgfip.utils.Pair;
import fr.gouv.finances.dgfip.xemelios.common.config.DocumentModel;
import fr.gouv.finances.dgfip.xemelios.common.config.ElementModel;
import fr.gouv.finances.dgfip.xemelios.common.config.EtatModel;
import fr.gouv.finances.dgfip.xemelios.common.config.ListeResultatModel;
import fr.gouv.finances.dgfip.xemelios.data.DataResultSet;
import fr.gouv.finances.dgfip.xemelios.services.XemeliosHelper;
import fr.gouv.finances.dgfip.xemelios.web.gwt.client.dto.SearchSessionDTO;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpSession;

/**
 * Permet de gérer les données à conserver sur le serveur pour chaque fenêtre de recherche
 * @author CBO
 */
public class SearchSessionContainer {

    private int lastId = 0;
    private Map<String, SearchSession> map = new HashMap<String, SearchSession>();

    private SearchSessionContainer() {
        super();
    }

    public static SearchSessionContainer getInstance(HttpSession session) {
        SearchSessionContainer searchSessionContainer = (SearchSessionContainer) session.getAttribute("searchSessionContainer");
        if (searchSessionContainer == null) {
            searchSessionContainer = new SearchSessionContainer();
            session.setAttribute("searchSessionContainer", searchSessionContainer);
        }
        return searchSessionContainer;
    }

    public SearchSession createSearchSession(String documentId, String etatId, String elementId) {
        lastId++;
        SearchSession searchSession = new SearchSession("" + lastId, documentId, etatId, elementId);
        map.put("" + lastId, searchSession);
        return searchSession;
    }

    public void destroySearchSession(String id) {
        map.remove(id);
    }

    public SearchSession getSearchSession(String id) {
        return map.get(id);
    }

    /**
     * Contient l'état serveur d'une fenêtre de recherche (conserveée en session)
     */
    static public class SearchSession {

        // Identifiant de fenêtre :
        private String id;
        // Document, etat et element :
        private String documentId;
        private String etatId;
        private String elementId;
        // Pour la pagination :
        private DataResultSet dataResultSet;
        private ListeResultatModel lrm;
        private Pair collectivite;
        private Pair budget;

        SearchSession(String id, String documentId, String etatId, String elementId) {
            this.id = id;
            this.documentId = documentId;
            this.etatId = etatId;
            this.elementId = elementId;
        }

        public String getId() {
            return id;
        }

        public String getDocumentId() {
            return documentId;
        }

        public String getElementId() {
            return elementId;
        }

        public String getEtatId() {
            return etatId;
        }

        public DocumentModel getDocumentModel() {
            return XemeliosHelper.getDocumentsModel(documentId);
        }

        public EtatModel getEtatModel() {
            return getDocumentModel().getEtatById(etatId);
        }

        public ElementModel getElementModel() {
            return getEtatModel().getElementById(elementId);
        }

        public DataResultSet getDataResultSet() {
            return dataResultSet;
        }

        public void setDataResultSet(DataResultSet dataResultSet) {
            this.dataResultSet = dataResultSet;
        }

        public ListeResultatModel getLrm() {
            return lrm;
        }

        public void setLrm(ListeResultatModel lrm) {
            this.lrm = lrm;
        }

        public Pair getBudget() {
            return budget;
        }

        public void setBudget(Pair budget) {
            this.budget = budget;
        }

        public Pair getCollectivite() {
            return collectivite;
        }

        public void setCollectivite(Pair collectivite) {
            this.collectivite = collectivite;
        }

        public SearchSessionDTO getSearchSessionDTO() {
            return new SearchSessionDTO(id, documentId, etatId, elementId, collectivite.key, budget.key);
        }
    }
}
