/*
  Copyright (C) 2007 Helge Hess

  This file is part of JOPE.

  JOPE is free software; you can redistribute it and/or modify it under
  the terms of the GNU Lesser General Public License as published by the
  Free Software Foundation; either version 2, or (at your option) any
  later version.

  JOPE 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 Lesser General Public
  License for more details.

  You should have received a copy of the GNU Lesser General Public
  License along with JOPE; see the file COPYING.  If not, write to the
  Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
  02111-1307, USA.
*/
package org.opengroupware.jope.ofs;

import java.io.File;

import org.opengroupware.jope.appserver.publisher.IJoObject;
import org.opengroupware.jope.appserver.publisher.JoClass;
import org.opengroupware.jope.appserver.publisher.JoContext;
import org.opengroupware.jope.eocontrol.EODataSource;

public class OFSFolder extends OFSBaseObject implements IJoFolderish {
  
  protected OFSFileContainerChildInfo childInfo;
  
  public OFSFolder(Object _container, String _name, File _file) {
    super(_container, _name, _file);
  }
  
  /* directory contents */
  
  public OFSFileContainerChildInfo childInfo() {
    if (this.childInfo == null && this.file != null)
      this.childInfo = OFSFileContainerChildInfo.infoForFile(this.file);
    return this.childInfo;
  }

  /* container */
  
  public boolean isFolderish() {
    return true; /* not strictly necessary, but this is static info anyways */
  }
  
  /* IJoFolderish */
  
  public EODataSource folderDataSource() {
    // TODO: implement me
    return null;
  }
  
  /* stored keys */
  
  public Object lookupStoredName(String _name, JoContext _ctx) {
    // TODO: implement me
    return null;
  }
  
  /* IJoObject */
  
  public Object lookupName(String _name, JoContext _ctx, boolean _acquire) {
    /* lookup using JoClass */
    
    JoClass cls = this.joClassInContext(_ctx);
    if (cls != null) {
      Object o = cls.lookupName(this, _name, _ctx);
      if (o != null) return o;
    }
    
    // TODO: check children
    OFSFileContainerChildInfo ci = this.childInfo();
    if (ci != null && ci.hasKey(_name))
      return this.lookupStoredName(_name, _ctx);
    
    /* if we shall acquire, continue at parent */
    
    if (_acquire && this.container != null)
      return ((IJoObject)this.container).lookupName(_name, _ctx, true /* aq */);
    
    return null;
  }
  
  /* description */

  @Override
  public void appendAttributesToDescription(StringBuffer _d) {
    super.appendAttributesToDescription(_d);
    
    if (this.childInfo != null)
      _d.append(" has-childinfo");
  }
}
