/*
 * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
 *
 * This file is part of Resin(R) Open Source
 *
 * Each copy or derived work must preserve the copyright notice and this
 * notice unmodified.
 *
 * Resin Open Source is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * Resin Open Source 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, or any warranty
 * of NON-INFRINGEMENT.  See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Resin Open Source; if not, write to the
 *   Free SoftwareFoundation, Inc.
 *   59 Temple Place, Suite 330
 *   Boston, MA 02111-1307  USA
 *
 * @author Scott Ferguson
 */

package com.caucho.ejb.doclet;

import java.io.*;
import java.util.*;
import java.util.logging.*;

import com.caucho.util.L10N;
import com.caucho.util.Alarm;
import com.caucho.util.QDate;

import com.caucho.log.Log;

import com.caucho.vfs.Path;
import com.caucho.vfs.Vfs;
import com.caucho.vfs.WriteStream;

import com.caucho.java.JavaWriter;

import com.caucho.config.ConfigException;
import com.caucho.config.LineConfigException;

import com.caucho.doclet.RootDocImpl;
import com.caucho.doclet.ClassDocImpl;
import com.caucho.doclet.MethodDocImpl;
import com.caucho.doclet.DocImpl;
import com.caucho.doclet.TagImpl;

/**
 * Generates the object interface.
 */
public class EjbObjectGenerator {
  private static final Logger log = Log.open(EjbObjectGenerator.class);
  private static final L10N L = new L10N(EjbObjectGenerator.class);

  private ClassDocImpl _classDoc;
  private boolean _isLocal = true;
  
  private String _fullClassName;
  private String _shortClassName;
  private String _package;

  private String _interfaceName;
  private String _primKeyClass;

  EjbObjectGenerator(ClassDocImpl classDoc)
  {
    _classDoc = classDoc;
  }

  /**
   * Sets true if the local home interface should be generated.
   */
  public void setLocal(boolean isLocal)
  {
    _isLocal = isLocal;
  }

  /**
   * Sets the class name.
   */
  public void setClassName(String className)
  {
    _fullClassName = className;
    int p = className.lastIndexOf('.');
    if (p > 0) {
      _shortClassName = className.substring(p + 1);
      _package = className.substring(0, p);
    }
    else
      _shortClassName = className;
  }

  public void generate(Path path)
    throws IOException, ConfigException
  {
    _interfaceName = _classDoc.getAttribute("@ejb.home", "local-class");
    _primKeyClass =  _classDoc.getAttribute("@ejb.pk", "class");
    if (_primKeyClass == null)
      _primKeyClass = "java.lang.String";
    
    path.getParent().mkdirs();
    
    WriteStream os = path.openWrite();

    try {
      JavaWriter out = new JavaWriter(os);

      generate(out);
    } finally {
      os.close();
    }
  }

  /**
   * Generates the top-level data for the home.
   */
  public void generate(JavaWriter out)
    throws IOException, ConfigException
  {
    out.println("/**");
    out.println(" * object interface generated by Resin-EE doclet.");
    out.println(" * on " + QDate.formatLocal(Alarm.getCurrentTime()));
    out.println(" */");

    if (_package != null) {
      out.println();
      out.println("package " + _package + ";");
    }

    out.println();
    out.println("public interface " + _shortClassName);
    if (_isLocal) {
      String localExtends = _classDoc.getAttribute("@ejb.interface",
						   "local-extends");

      if (localExtends == null)
	localExtends = "javax.ejb.EJBLocalObject";

      out.println("  extends " + localExtends + " {");
    }
    else {
      String remoteExtends = _classDoc.getAttribute("@ejb.interface",
						    "extends");

      if (remoteExtends == null)
	remoteExtends = "javax.ejb.EJBObject";

      out.println("  extends " + remoteExtends + " {");
    }

    out.pushDepth();

    generateMethods(out);
    
    out.popDepth();
    out.println("}");
  }

  /**
   * Generates the methods the object.
   */
  public void generateMethods(JavaWriter out)
    throws IOException, ConfigException
  {
    ArrayList<MethodDocImpl> methodList = _classDoc.getMethods();

    for (int i = 0; i < methodList.size(); i++) {
      MethodDocImpl method = methodList.get(i);

      if (method.getName().startsWith("ejb"))
	continue;

      ArrayList<TagImpl> tagList = method.getTagList("@ejb.interface-method");

      if (tagList != null) {
        out.println();
        out.print("public " + method.getReturnType() + " ");
        out.print(method.getName() + "(");

        ArrayList<MethodDocImpl.Param> paramList = method.getParameters();
        for (int j = 0; j < paramList.size(); j++) {
          MethodDocImpl.Param param = paramList.get(j);

          if (j > 0)
            out.print(", ");
          out.print(param.getType() + " " + param.getName());
        }

        out.println(");");
      }
    }
  }

  private ConfigException error(DocImpl loc, String msg)
  {
    if (loc == null)
      return new ConfigException(msg);
    else if (loc.getPosition() == null)
      return new ConfigException(loc.getName() + ": " + msg);
    else
      return new LineConfigException(loc.getPosition() + msg);
  }
}
