/* JFox, the OpenSource J2EE Application Server
 *
 * Distributable under GNU LGPL license by gun.org
 * more details please visit http://www.huihoo.org/jfox
 */

package org.jfox.framework.management.http;

import java.util.Properties;

import org.jfox.framework.Component;
import org.jfox.framework.ComponentMeta;
import org.jfox.framework.ComponentName;
import org.jfox.framework.Registry;

/**
 * @author <a href="mailto:young_yy@hotmail.com">Young Yang</a>
 */

public class InstanceComponentCommand implements Command {

    public String doGet(Registry registry, Properties queryMap) throws Exception {
        String compName = queryMap.getProperty("compname");
        ComponentName componentName = ComponentName.parseString(compName, registry.getClassLoaderRepository());

        ComponentMeta meta = registry.getComponentMeta(componentName);
        Component component = registry.getComponentInstance(componentName);

        StringBuffer sb = new StringBuffer();
        sb.append(PageUtil.head("result view"));
        sb.append(PageUtil.banner("Result View"));
        sb.append(mxOperInfoTable(meta, component));
        sb.append(linkTable(compName));
        sb.append(PageUtil.foot());
        return sb.toString();

    }

    private static String mxOperInfoTable(ComponentMeta meta, Component component) {

        StringBuffer sb = new StringBuffer();
        sb.append("<table width=\"760\" align=\"center\">\n" +
                "  <tr>\n" +
                "    <td width=\"180\" align=\"right\"><b>Component Name: </b></td><td>")
                .append(meta.getComponentName().toString())
                .append("</td>\n" +
                "  </tr>\n" +

                "  <tr>\n" +
                "    <td width=\"180\" align=\"right\"><b>Component Description: </b></td><td>")
                .append(meta.getDescription())
                .append("</td>\n" +
                "  </tr>\n" +
                "  <tr>\n" +
                "    <td width=\"180\" align=\"right\"><b>Component Class: </b></td><td>")
                .append(meta.getComponentImplementation().getName())
                .append("</td>\n" +
                "  </tr>\n" +
                "  <tr>\n" +
                "    <td width=\"180\" align=\"right\" valign=\"top\"><b>Component Instance: </b></td><td>")
                .append(component.toString())
                .append("</td>\n" +
                "  </tr>\n" +

                "  </tr>\n" +
                "\n" +
                "</table><br>\n" +
                "");
        return sb.toString();

    }

    private static String linkTable(String compName) {
        StringBuffer sb = new StringBuffer();
        sb.append("<table width=\"760\" align=\"center\">\n" +
                "  <tr>\n" +
                "    <td>")
                .append("<a href=\"?action=ViewComponent&compname=")
                .append(compName).append("\"> &lt;&lt;Return To Component View</a>")
                .append("</td>\n" +
                "  </tr>\n" +
                "</table>\n");
        return sb.toString();
    }

    public static void main(String[] args) {

    }
}

