/**
 * JOnAS: Java(TM) Open Application Server
 * Copyright (C) 1999-2008 Bull S.A.
 * Contact: jonas-team@objectweb.org
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
 * USA
 */

package org.objectweb.jonas.webapp.jonasadmin.joramplatform;

import java.util.ArrayList;
import java.util.Iterator;

import javax.management.ObjectName;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.objectweb.jonas.jmx.JonasManagementRepr;
import org.objectweb.jonas.jmx.JoramObjectName;
import org.objectweb.jonas.webapp.jonasadmin.WhereAreYou;

/**
 * @author Adriana Danes
 * @author Frederic MAISTRE
 */

public class EditJoramPlatformAction extends EditJoramBaseAction {

// --------------------------------------------------------- Public Methods

    public ActionForward executeAction(ActionMapping pMapping, ActionForm pForm
        , HttpServletRequest pRequest, HttpServletResponse pResponse) {

        // Current JOnAS server
        String jonasServerName =  m_WhereAreYou.getCurrentJonasServerName();

        // Force the node selected in tree
        m_WhereAreYou.selectNameNode(getTreeBranchName(DEPTH_DOMAIN) + WhereAreYou.NODE_SEPARATOR
            + "joramplatform", true);

        // Form used
        JoramPlatformForm oForm = new JoramPlatformForm();
        try {
            // Object name used
            //ObjectName joramPlatformON = JoramObjectName.joramPlatform();
            ObjectName joramAdapterON = JoramObjectName.joramAdapter();
            boolean isAdapterLoaded = JonasManagementRepr.isRegistered(joramAdapterON, jonasServerName);
            // local server Id is given by ServerId in the JoramAdapter MBean ?
            Short localServerIdValue = (Short) JonasManagementRepr.getAttribute(joramAdapterON, "ServerId", jonasServerName);
            // TODO ask new joramAdapter MBean version to Joram team.
            // the serverIds list is empty
            //Iterator serverIdsList = ((List) JonasManagementRepr.getAttribute(joramPlatformON, "ServersIds", jonasServerName)).iterator();
            Iterator serverIdsList = (new ArrayList()).iterator();
            ArrayList serverIds = new ArrayList();
            int nbRemoteServers = 0;
            for (; serverIdsList.hasNext();) {
                Integer serverIdValue = (Integer) serverIdsList.next();
                if (!serverIdValue.equals(localServerIdValue)) {
                    serverIds.add(serverIdValue.toString());
                    nbRemoteServers++;
                }
            }
            oForm.setAdapterLoaded(isAdapterLoaded);
            oForm.setLocalServer(localServerIdValue.toString());
            String[] remoteServers = new String[nbRemoteServers];
            for (int i = 0; i < nbRemoteServers; i++) {
                remoteServers[i] = (String) serverIds.get(i);
            }
            oForm.setRemoteServers(remoteServers);
            // TODO ask new joramAdapter MBean version to Joram team.
            // 
            //oForm.setConfiguration((String) JonasManagementRepr.getAttribute(joramPlatformON, "Configuration", jonasServerName));
            oForm.setConfiguration("");
            //oForm.setLocalPort(getIntegerAttribute(joramPlatformON, "LocalPort"));
            oForm.setLocalPort(getIntegerAttribute(joramAdapterON, "ServerPort"));
            //oForm.setLocalHost(getStringAttribute(joramPlatformON, "LocalHost"));
            oForm.setLocalHost(getStringAttribute(joramAdapterON, "HostName"));
            oForm.setDefaultThreshold(getIntegerAttribute(joramAdapterON, "DefaultThreshold"));
            if (isAdapterLoaded) {
                // Get configuration info from RA
                //oForm.setVersion(joramAdapterON.getKeyProperty("version"));
                oForm.setVersion(getStringAttribute(joramAdapterON, "ProviderVersion"));
                oForm.setCollocatedServer(getBooleanAttribute(joramAdapterON, "CollocatedServer"));
                //oForm.setConfigDir(getStringAttribute(joramAdapterON, "PlatformConfigDir"));
                // TODO this is null
                oForm.setConfigDir("");
                oForm.setAdminFile(getStringAttribute(joramAdapterON, "AdminFileXML"));
                oForm.setPersistentServer(getBooleanAttribute(joramAdapterON, "PersistentPlatform"));
                oForm.setServerIdTxt(toStringShortAttribute(joramAdapterON, "ServerId"));
                oForm.setServerName(getStringAttribute(joramAdapterON, "ServerName"));
                oForm.setHostName(getStringAttribute(joramAdapterON, "HostName"));
                oForm.setServerPortTxt(toStringIntegerAttribute(joramAdapterON, "ServerPort"));
                oForm.setCnxPendingTimerTxt(toStringIntegerAttribute(joramAdapterON, "CnxPendingTimer"));
                oForm.setConnectingTimerTxt(toStringIntegerAttribute(joramAdapterON, "ConnectingTimer"));
                oForm.setTxPendingTimerTxt(toStringIntegerAttribute(joramAdapterON, "TxPendingTimer"));
                oForm.setTimeOutToAbortRequest(getLongAttribute(joramAdapterON, "TimeOutToAbortRequest"));
                String defaultDmqId = getStringAttribute(joramAdapterON, "DefaultDMQ");
                if (defaultDmqId != null) {
                	ObjectName defaultDmqOn = getDestinationOn(defaultDmqId, jonasServerName);
                	String defaultDmqName = (String) JonasManagementRepr.getAttribute(defaultDmqOn, "AdminName", jonasServerName);
                    oForm.setDefaultDMQ(defaultDmqName);
                }
            } else {
                // TODO
                // return error
            }
            m_Session.setAttribute("joramPlatformForm", oForm);
            m_Session.setAttribute("localId", oForm.getLocalServer());
        } catch (Throwable t) {
            return (treatError(t, pMapping, pRequest));
        }

        // Forward to the jsp.
        return (pMapping.findForward("JoramPlatform"));
    }

}
