phis-クライアントキャッシュテクノロジー分析

10856 ワード

/** phis ! html5, phis\EAPDomain\unieap\ria3.3\unieap\clientCache\localStorage.js ! : phis\EAPDomain\unieap\ria3.3\unieap\clientCache\activexSQLite.js */
dojo.require("unieap.global");
dojo.require("unieap.util.util");
dojo.provide("unieap.cache");
(function(){
    var isHTML5Available = ('localStorage' in window) && window['localStorage'] !== null && window['localStorage'] !== undefined;
    if(isHTML5Available){
        dojo.require("unieap.clientCache.localStorage");
    }else{
        dojo.require("unieap.clientCache.activexSQLite");
    }
    unieap.cache = new unieap.clientCache();
})();
/**
   phis   IE8/9             js      ActiveX    SQLite  !
   ,         else  !          
     :C:/phis_local/db/   ,new     :phislocalcache+ gsLoginInfo.getOrgId()(4028c8813a002456013a00a4c6ca001f)+.db  SQLite   
 
 phis\EAPDomain\unieap\ria3.3\unieap\clientCache\activexSQLite.js 
*/
dojo.require("unieap.global");
dojo.require("unieap.util.util");
dojo.provide("unieap.clientCache.activexSQLite");
/**
 * @declaredClass:
 *      unieap.cache
 * @summary:
 *              ,    google gears
 * @example:
 * |
 * |    //  google gears    
 * |    var isCacheUsable=unieap.cache.isAvailable();
 * |    alert(isCacheUsable);
 * |
 * @example:
 * |
 * |    //     google gears      
 * |    var totalCount=unieap.cache.getCount();
 * |
 * @example:
 * |
 * |    //  google gears        
 * |    unieap.cache.clear();
 * |
 */
dojo.declare("unieap.clientCache", null, {
    //          
    DB_PATH : "C:/phis_local/db/",
    
    // SQLite     database   
    DB_NAME : "phislocalcache",
    
    //        
    _cache_dbPath : "",
    
    // SQLite     table   
    CODELIST_TABLE_NAME : "unieap_codelist_table",
    
    constructor: function(param) {
        dojo.mixin(this, param);
    },
    
    _cache_db : null,
    
    _initActiveX:function(){
        var _db = null;
        
        if(document.getElementById('sqliteObj')){
            _db = document.getElementById('sqliteObj');
        } else {
            if (navigator.appVersion.indexOf("MSIE") != -1) {
                // IE
                var obj = document.createElement("object");
                obj.setAttribute("id","sqliteObj");
                obj.setAttribute("style","visibility:hidden;width:0px; height:0px;");
                obj.setAttribute("classid","clsid:3E22694D-7B92-42A1-89A7-668E2F7AA107");
                obj.setAttribute("progid","LiteX.LiteConnection");
                document.appendChild(obj);
            } else {
                alert("   IE   ");
                return null;
            }
            _db = document.getElementById('sqliteObj');
        }
        if (!_db) {
            return null;
        }
        return _db;
    },
    
    /**
     *         
     * 
     * @return      ,          nul
     */
    _initDB:function(){
        if(typeof(gsLoginInfo) != 'undefined' && typeof(gsLoginInfo.getOrgId) != 'undefined'){
            if(unieap.isEmpty(this._cache_dbPath)){
                this._cache_dbPath += this.DB_PATH + + this.DB_NAME + gsLoginInfo.getOrgId() + ".db";
            }
        } else if(typeof(getCurrentOrgID) != 'undefined'){
            if(unieap.isEmpty(this._cache_dbPath)){
                this._cache_dbPath += this.DB_PATH + + this.DB_NAME + getCurrentOrgID() + ".db";
            }
        }
        
        if (this._cache_db) {
            this._cache_db.path = this._cache_dbPath;
            //     ,           Close()    
            try{
                this._cache_db.Open();
            }catch(e){
                this._cache_db.Close();
                this._cache_db.Open();
            }
            return this._cache_db;
        }
        if(unieap.global.isUseClientCache == false){
            return null;
        }   
        try {
            //  db  
            this._cache_db = this._initActiveX();
            if(this._cache_db){
                this._cache_db.path = this._cache_dbPath;
                //     ,           Close()    
                this._cache_db.Open();
            }
        }catch(e){
            MessageBox.alert({
                title:"  ",
                message:"         : "+e.message
            });
            this._cache_db = null;
        }
        return this._cache_db;
    },
    
    //       DB   
    /**
     *@summary:
     *       SQLite   codelist_table 
     * @example:
     * |
     * |    unieap.cache.creatCodeListTable();
     * |   
     */
    creatCodeListTable : function(){
        var db = this._initDB();
        if (db == null) {
            return null;
        } 
        
        db.execute("CREATE TABLE IF NOT EXISTS "
        + unieap.cache.CODELIST_TABLE_NAME + "(key TEXT,value TEXT,timestamp INT)");
        db.execute("CREATE UNIQUE INDEX IF NOT EXISTS key_index"
            + " ON " + unieap.cache.CODELIST_TABLE_NAME + " (key)");
        
        try{
            db.Close();
        } catch(ex){}
    },
    
    /**
     *@summary:
     *        ActiveX    
     * @description:
     *        ActiveX         gloabal.js   unieap.global.isUseClientCache false,
     *             false
     */
    isAvailable : function(){
        return unieap.global.isUseClientCache 
                && this._initActiveX()!= null;
    },
    
    //  gears codelist_table      
    /**
     *@summary:
     *        gears codelist_table      
     * @example:
     * |
     * |    unieap.cache.clear();
     * |   
     *   unieap debug               
     */
    clear : function() {
        var db = this._initDB();
        if (db == null) {
            return;
        }
        db.execute("DELETE FROM " + this.CODELIST_TABLE_NAME);
        
        try{
            db.Close();
        }catch(ex){
        }
    },
    
    // codelist_table      
    //unieap.cache.put('name','neusoft');
    //     timestamp,timestamp     1
    /**
     *@summary:
     *       codelist_table      
     * @param:
     *      {string} key  
     * @param:
     *      {string} value 
     * @param:
     *      {string} timestamp   
     * @example:
     * |
     * |    var value = "[
     * |        {CODEVALUE:1,CODENAME:'  '},
     * |        {CODEVALUE:2,CODENAME:'  '},
     * |        {CODEVALUE:3,CODENAME:'  '}
     * |    ]";
     * |    unieap.cache.put("dept",value,String(new Date().getTime()));
     * | 
     *   unieap debug             
     */
    put : function(key, value,timestamp) {
        var db = this._initDB();
        if (db == null) {
            return;
        }
        db.execute("REPLACE INTO " + this.CODELIST_TABLE_NAME
                + " VALUES (?,?,?)", key, value,timestamp||1);
        
        try{
            db.Close();
        }catch(ex){
        }
    },
    
    // codelist_table      
    //unieap.cache.putMultiple(['name','age'],['neusoft',20]);
    //     timestamp,timestamp     1
    putMultiple : function(keys, values,timestamps) {
        var db = this._initDB();
        if (db == null) {
            return;
        }
        db.execute("BEGIN TRANSACTION");
        var _stmt = "REPLACE INTO " + this.CODELIST_TABLE_NAME
                + " VALUES (?, ?,?)";
        for (var i = 0,j = 0,k=0, key, value,timestamp; (key = keys[i++]) && (value = values[j++])&&(timestamp=timestamps&&timestamps[k++]||1);) {
        
            db.execute(_stmt, key, value, timestamp);
        }
        db.execute("COMMIT TRANSACTION");
        try{
            db.Close();
        }catch(ex){
        }
    },
    
    // codelist_table      
    ////unieap.cache.get('name')=>'neusoft'
    get : function(key) {
        var db = this._initDB();
        var value = null;
        if (db == null) {
            return value;
        }
        var rs = db.Prepare("SELECT value FROM " + unieap.cache.CODELIST_TABLE_NAME + " WHERE key = '"+key+"'");
        while (!rs.Step()) {
            value = rs.ColumnValue(0);
        }
        
        this._setRsDsClose(rs, ds);
        return value;
    },
    
    //  codelist_table       
    //unieap.cache.remove('name')=>key name       
    remove : function(key) {
        var db = this._initDB();
        if (db == null) {
            return;
        }
        db.execute("DELETE FROM " + this.CODELIST_TABLE_NAME
                + " WHERE key = ?", key);
        
        try{
            db.Close();
        }catch(ex){
        }
    },
    
    //  codelist_table     keys 
    getKeys : function(){
        var db = this._initDB();
        var value = [];
        if (db == null) {
            return null;
        }
        var rs = db.Prepare("SELECT key FROM " + this.CODELIST_TABLE_NAME);
        while (!rs.Step()) {
            value.push(rs.ColumnValue(0));
        }
        
        this._setRsDsClose(rs, ds);
        
        return value;
    },
    
    //  codelist_table       
    getCount : function() {
        var db = this._initDB();
        if (db == null) {
            return null;
        }
        var rs = db.Prepare("SELECT count(*) FROM " + this.CODELIST_TABLE_NAME);
        var result = rs.ColumnValue(0);
        
        this._setRsDsClose(rs, ds);
        
        return result;
    },
    
    //  codelist_table       timestamp
    //      object,   obj[key]=timestamp
    //        2649600000,     1970 1 1 
    getAllTimeStamps : function(){
        var db = this._initDB();
        if (db == null) {
            return null;
        }
        var rs = db.Prepare("SELECT key,timestamp FROM " + this.CODELIST_TABLE_NAME+' WHERE timestamp>2649600000');
        var value={}
        while(!rs.Step()){
            value[rs.ColumnValue(0)]=rs.ColumnValue(1);
        }
        
        this._setRsDsClose(rs, ds);
        
        return value;
    },
    
    //   rs,ds
    _setRsDsClose: function(rs, ds){
        try{
            rs.Close();
        }catch(ex){
        }
        try{
            db.Close();
        }catch(ex){
        }
    }
    
});