// WebView.java

/**
*    Copyright (C) 2008 10gen Inc.
*  
*    This program is free software: you can redistribute it and/or  modify
*    it under the terms of the GNU Affero General Public License, version 3,
*    as published by the Free Software Foundation.
*  
*    This program 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 Affero General Public License for more details.
*  
*    You should have received a copy of the GNU Affero General Public License
*    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

package ed.manager;

import java.util.*;

import ed.net.httpserver.*;

public class WebView extends HttpMonitor {
    
    
    static final String BASE_NAME = "appManager";

    public WebView( Manager manager ){
        super( BASE_NAME );
        _manager = manager;
        _detail = new Detail();
        _javaApps = new JavaApps();
    }
    
    public void handle( MonitorRequest request ){
        
        final String restartId = request.getRequest().getParameter( "restart" );
        if ( restartId != null ){
            Application app = _manager.findApplication( restartId );
            if ( app == null ){
                request.addMessage( "can't find application [" + restartId + "]" );
            }
            else {
                request.addMessage( "restarted [" + restartId + "]" );
                _manager.getRunning( app ).restart();
            }
            
        }
        
        request.startData( "applications" , "type" , "id" , "started" , "uptime" , "timesStarted" );
        
        for ( Application app : _manager.getApplications() ){
            RunningApplication ra = _manager.getRunning( app );
            request.addData( app.getType() + "." + app.getId() , 
                             app.getType() , app.getId() , new Date( ra.getLastStart() ) , ra.getUptimeMinutes() , ra.timesStarted() , 
                             "<a href='" + _detail.getURI() + "?id=" + app.getFullId() + "'>detail</a> | " + 
                             "<a href='" + getURI() + "?restart=" + app.getFullId() + "'>restart</a>"
                             );
        }

        request.endData();
    }
    
    void add(){
        HttpServer.addGlobalHandler( this );
        HttpServer.addGlobalHandler( _detail );
        HttpServer.addGlobalHandler( _javaApps );
    }
    
    abstract class ProcessViewer extends HttpMonitor {
        ProcessViewer( String name ){
            super( BASE_NAME + "-" + name );
        }
        
        public void handle( MonitorRequest request ){
            String fullId = request.getRequest().getParameter( "id" );
            if ( fullId == null ){
                printList( request );
            }
            else {
                printApp( request , fullId );
            }
        }
        
        void printApp( MonitorRequest request , String fullId ){
            Application app = _manager.findApplication( fullId );
            if ( app == null ){
                request.print( "can't find app [" + fullId + "]" );
                return;
            }
            
            RunningApplication ra = _manager.getRunning( app );
            if ( ra == null ){
                request.print( "can't find running app for [" + fullId + "]" );
                return;
            }
            
            print( request , app , ra );
        }

        void printList( MonitorRequest request ){
            request.print( "<ul>" );
            for ( Application app : getApps() ){
                request.print( "<li>" );
                request.print( "<a href='" + getURI() + "?id=" + app.getFullId() + "'>" + 
                               app.getType() + "." + app.getId() + 
                               "</a>" );
                request.print( "</li>" );
            }  
            request.print( "</ul>" );
        }
        
        abstract List<? extends Application> getApps();
        abstract void print( MonitorRequest request , Application app , RunningApplication ra );
    }
    
    class Detail extends ProcessViewer {
        Detail(){
            super( "detail" );
        }
        
        List<Application> getApps(){
            return _manager.getApplications();
        }
        
        void print( MonitorRequest request , Application app , RunningApplication ra ){
            request.print( "Command: <b>" + Arrays.toString( app.getCommand() ) + "</b><br>" );
            
            request.print( "<pre>\n" );
            for ( int i=0; i<ra._lastOutput.size(); i++ ){
                request.print( ra._lastOutput.get(i) );
                request.print( "\n" );
            }
            request.print( "\n</pre>\n" );            
        }
        
    }

    class JavaApps extends ProcessViewer {
        JavaApps(){
            super( "javaApps" );
        }

        void print( MonitorRequest request , Application app , RunningApplication ra ){
            JavaApplication j = (JavaApplication)app;
            request.print( "<pre>\n" );

            int size = j._gcs.size();
            for ( int i=0; i<size; i++ )
                request.print( j._gcs.get(i) + "\n" );

            request.print( "</pre>\n" );
        }

        List<JavaApplication> getApps(){
            List<JavaApplication> j = new ArrayList<JavaApplication>();
            for ( Application app : _manager.getApplications() ){
                if ( app instanceof JavaApplication )
                    j.add( (JavaApplication)app );
            }
            return j;
        }
    }

    final Manager _manager;
    final Detail _detail;
    final JavaApps _javaApps;
}

@@ -1022,28 +1022,109 @@
 ps();%0A    %7D%0A
+%0A
     
+protected boolean uriOK( String uri )%7B %0A        return uri.equals( %22/%22 );%0A    %7D%0A
 %0A    public 
@@ -2256,109 +2256,483 @@
     
-%22%3Ca href='%22 + getURI() + %22?restart=%22 + app.getFullId() + %22'%3Erestart%3C/a%3E%22%0A                            
+_action( %22restart%22 , app )%0A                             );%0A        %7D%0A        %0A        request.endData();%0A    %7D%0A%0A    String _action( String name , Application app )%7B%0A        StringBuilder buf = new StringBuilder( %22%3Cform action='%22 + getURI() + %22' method='POST'%3E%22 );%0A        buf.append( %22%3Cinput type='hidden' name='%22 + name + %22' value='%22 + app.getFullId() + %22' %3E%22 );%0A        buf.append( %22%3Cinput type='submit' name='action' value='%22 + name + %22'%3E%22 );%0A        buf.append( %22%3C/form%3E%22
  );%0A
@@ -2742,35 +2742,28 @@
     
- 
-%7D%0A%0A        request.endData
+ return buf.toString
 ();%0A

fix restart button