/**
 * JOnAS: Java(TM) Open Application Server
 * Copyright (C) 1999 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 java.util.List;
import java.util.Set;

import javax.management.InstanceNotFoundException;
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 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();
            Set adapterOns = JonasManagementRepr.queryNames(JoramObjectName.joramAdapter(), jonasServerName);
            if (adapterOns.isEmpty()) {
                throw new InstanceNotFoundException();
            }
            ObjectName joramAdapterON = (ObjectName) adapterOns.iterator().next();
            boolean isAdapterLoaded = JonasManagementRepr.isRegistered(joramAdapterON, jonasServerName);
            Integer localServerIdValue = (Integer) JonasManagementRepr.getAttribute(joramPlatformON, "LocalServerId", jonasServerName);
            Iterator serverIdsList = ((List) JonasManagementRepr.getAttribute(joramPlatformON, "ServersIds", jonasServerName)).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);
            oForm.setConfiguration((String) JonasManagementRepr.getAttribute(joramPlatformON, "Configuration", jonasServerName));
            oForm.setLocalPort(getIntegerAttribute(joramPlatformON, "LocalPort"));
            oForm.setLocalHost(getStringAttribute(joramPlatformON, "LocalHost"));
            oForm.setDefaultThreshold(getIntegerAttribute(joramPlatformON, "DefaultThreshold"));
            if (isAdapterLoaded) {
                // Get configuration info from RA
                oForm.setVersion(joramAdapterON.getKeyProperty("version"));
                oForm.setCollocatedServer(getBooleanAttribute(joramAdapterON, "CollocatedServer"));
                oForm.setConfigDir(getStringAttribute(joramAdapterON, "PlatformConfigDir"));
                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"));
                Object defaultDMQinstance = JonasManagementRepr.getAttribute(joramAdapterON, "DefaultDMQ", jonasServerName);
                if (defaultDMQinstance != null) {
                    oForm.setDefaultDMQ(defaultDMQinstance.toString());
                }
            } else {
                ObjectName joramAdminON = JoramObjectName.joramAdmin();
                oForm.setTimeOutToAbortRequest(getLongAttribute(joramAdminON, "TimeOutToAbortRequest"));
                oForm.setDefaultDMQ(JonasManagementRepr.getAttribute(joramAdminON, "DefaultDMQ", jonasServerName).toString());
            }
            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"));
    }

}
