/*
  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.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.opengroupware.jope.appserver.core.WOApplication;
import org.opengroupware.jope.appserver.core.WOContext;
import org.opengroupware.jope.foundation.UString;
import org.opengroupware.jope.ofs.fs.IOFSFileInfo;
import org.opengroupware.jope.ofs.fs.IOFSFileManager;
import org.opengroupware.jope.ofs.fs.OFSHostFileManager;

/*
 * OFSApplication
 * 
 * An OFS application is a JOPE application which runs on top of a directory.
 */
public class OFSApplication extends WOApplication {
  protected static final Log ofslog = LogFactory.getLog("JoOFS");
  
  protected OFSRestorationFactory defaultRestorationFactory;
  
  /* setup */

  @Override
  public void init() {
    super.init();
    
    this.defaultRestorationFactory = new OFSRestorationFactory();

    /* load OFS product */
    this.joProductManager.loadProduct
      (null, OFSApplication.class.getPackage().getName());
  }
  
  /* factories */
  
  public OFSRestorationFactory defaultRestorationFactory() {
    return this.defaultRestorationFactory;
  }
  
  /* root object */

  @Override
  public Object rootObjectInContext(WOContext _ctx, String[] _path) {
    if (_path != null && _path.length > 0 && _path[0].startsWith("-")) {
      /* application code namespace */
      if (_path[0].startsWith("-ControlPanel"))
        return this;
    }
    
    /* directly pass on to document hierarchy */
    
    Object root = this.ofsRootObjectInContext(_ctx, _path);
    if (root != null)
      return root;
    
    log.error("got no OFS root object, using application as root" +
        "\n  path: " + UString.componentsJoinedByString(_path, " / ") +
        "\n  ctx:  " + _ctx);
    return this;
  }
  
  public String ofsDatabasePathInContext(WOContext _ctx, String[] _path) {
    return System.getProperty("user.dir");
  }
  
  public Object ofsRootObjectInContext(WOContext _ctx, String[] _path) {
    File file = new File(this.ofsDatabasePathInContext(_ctx, _path));
    
    IOFSFileManager fm       = new OFSHostFileManager(file);
    IOFSFileInfo    fileInfo = fm.fileInfoForPath(null /* root */);
    if (fm == null) {
      log().error("could not create filemanager for file: " + file);
      return null;
    }
    if (fileInfo == null) {
      log().error("got no info for root directory: " + file);
      return null;
    }
    
    return this.defaultRestorationFactory()
      .restoreObjectFromFileInContext(null, fm, fileInfo, _ctx);
  }
  
  /* logging */
  
  public Log log() {
    return ofslog;
  }
}
