package org.opengroupware.jope.jsapp.adapter;

import org.mozilla.javascript.Context;
import org.mozilla.javascript.Scriptable;
import org.opengroupware.jope.appserver.core.WOSession;

public class JSSessionAdapter extends JSExtraVarAdapter {
  private static final long serialVersionUID = 1L;

  public JSSessionAdapter() {
  }

  public JSSessionAdapter(Scriptable _scope, Object object, Class _type) {
    super(_scope, object, _type);
  }

  public JSSessionAdapter
    (Scriptable _scope, Object object, Class _type, boolean adapter)
  {
    super(_scope, object, _type, adapter);
  }

  /* accessors */
  
  public String getClassName() {
    return "JSSessionAdapter"; 
  }
  
  /* remap some things to properties */

  @Override
  public boolean has(String _name, Scriptable _start) {
    int  len = _name.length();
    char c0  = len > 0 ? _name.charAt(0) : 0;
    
    switch (c0) {
      case 'i':
        if (len == 2 && "id".equals(_name)) return true;
        break;
    }
    
    return super.has(_name, _start);
  }
  
  @Override
  public Object get(String _name, Scriptable _start) {
    Object dp  = Scriptable.NOT_FOUND;
    int    len = _name.length();
    char   c0  = len > 0 ? _name.charAt(0) : 0;
    
    switch (c0) {
      case 'i':
        if (len == 2 && "id".equals(_name))
          dp = ((WOSession)this.javaObject).sessionID();
        break;
    }
    
    if (dp != Scriptable.NOT_FOUND) {
      Context cx = Context.getCurrentContext();
    
      return cx.getWrapFactory().wrap(cx,
          this /* scope? */,
          dp   /* Java object to be wrapped for JS */,
          null /* static type? */);
    }
    
    return super.get(_name, _start);
  }  
}
