// mttml(maachang-template-html)入力処理.
//

// mttmlオブジェクト.
MTtml = function() {} ;

// テンプレートMHTML追加.
MTtml.put = function( path,value ) {
    if( useString( path ) == false || isNull( value ) ) {
        return -1 ;
    }
    create_base_model() ;
    if( script_model().isCreate() == false ) {
        return -1 ;
    }
    var mdbm = script_model().getMDbm() ;
    if( isNull( mdbm ) ) {
        return -1 ;
    }
    var key = org.maachang.comet.httpd.engine.script.scripts.TemplateScript.getBaseKey( path ) ;
    var update = org.maachang.comet.httpd.engine.script.scripts.TemplateScript.getUpdateKey( path ) ;
    var time = java.lang.System.currentTimeMillis() ;
    mdbm.put( key,value ) ;
    mdbm.put( update,org.maachang.util.ConvertParam.convertLong( time ) ) ;
    return 0 ;
} ;

// テンプレートMHTML削除.
MTtml.remove = function( path ) {
    if( useString( path ) == false ) {
        return -1 ;
    }
    create_base_model() ;
    if( script_model().isCreate() == false ) {
        return -1 ;
    }
    var mdbm = script_model().getMDbm() ;
    if( isNull( mdbm ) ) {
        return -1 ;
    }
    mdbm.remove( org.maachang.comet.httpd.engine.script.scripts.TemplateScript.getBaseKey( path ) ) ;
    mdbm.remove( org.maachang.comet.httpd.engine.script.scripts.TemplateScript.getUpdateKey( path ) ) ;
    return 0 ;
} ;

// テンプレート情報を取得.
MTtml.get = function( path ) {
    if( useString( path ) == false ) {
        return -1 ;
    }
    create_base_model() ;
    if( script_model().isCreate() == false ) {
        return -1 ;
    }
    var mdbm = script_model().getMDbm() ;
    if( isNull( mdbm ) ) {
        return -1 ;
    }
    return mdbm.get( org.maachang.comet.httpd.engine.script.scripts.TemplateScript.getBaseKey( path ) ) ;
}

// 指定MTTMLにforward.
MTtml.forward = function(name) {
    if( isNull( name ) ) {
        return -1 ;
    }
    if( isResponse() ) {
        if( getHttpdResponse().isOutputStream() == true ) {
            _exit( "forward" ) ;
        }
        if( endsWith( name,".ms" ) == true ||  endsWith( name,".mhtml" ) == true ) {
            return -1 ;
        }
        if( endsWith( name,".mttml" ) == false ) {
            name += ".mttml" ;
        }
        var factory = _$$maachang_comet_manager.get( "WEBAPP_FACTORY" ) ;
        if( factory.isApplication( name ) == false ) {
            _exit( "error404" ) ;
        }
        var exec = factory.getApplication( name ) ;
        exec.getScript().eval( _$script$context ) ;
        _exit( "forward" ) ;
    }
} ;

