/*___INFO__MARK_BEGIN__*/
/*************************************************************************
 *
 *  The Contents of this file are made available subject to the terms of
 *  the Sun Industry Standards Source License Version 1.2
 *
 *  Sun Microsystems Inc., March, 2001
 *
 *
 *  Sun Industry Standards Source License Version 1.2
 *  =================================================
 *  The contents of this file are subject to the Sun Industry Standards
 *  Source License Version 1.2 (the "License"); You may not use this file
 *  except in compliance with the License. You may obtain a copy of the
 *  License at http://gridengine.sunsource.net/Gridengine_SISSL_license.html
 *
 *  Software provided under this License is provided on an "AS IS" basis,
 *  WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
 *  WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
 *  MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
 *  See the License for the specific provisions governing your rights and
 *  obligations concerning the Software.
 *
 *   The Initial Developer of the Original Code is: Sun Microsystems, Inc.
 *
 *   Copyright: 2001 by Sun Microsystems, Inc.
 *
 *   All Rights Reserved.
 *
 ************************************************************************/
/*___INFO__MARK_END__*/
/**
 *  Generated from java_jni_jgdi.jsp
 *  !!! DO NOT EDIT THIS FILE !!!
 */
<%
  final com.sun.grid.cull.JavaHelper jh = (com.sun.grid.cull.JavaHelper)params.get("javaHelper");
  final com.sun.grid.cull.CullDefinition cullDef = (com.sun.grid.cull.CullDefinition)params.get("cullDef");
  
  class JGDIJniGenerator extends com.sun.grid.cull.AbstractGDIGenerator {

     private String cullname;
     public JGDIJniGenerator(com.sun.grid.cull.CullObject cullObject) {
        super(cullObject.getIdlName(), jh.getClassName(cullObject), cullObject);
        this.cullname = cullObject.getName();
        addPrimaryKeys(cullObject, jh);
     }

     public void genImport() {
        if(!(cullObject.getType() == cullObject.TYPE_PRIMITIVE ||
             cullObject.getType() == cullObject.TYPE_MAPPED)) {
%>// for <%=name%>
import com.sun.grid.jgdi.configuration.<%=classname%>;
import com.sun.grid.jgdi.configuration.<%=classname%>Impl;        
<%        
        }
     } // end of genImport
     private boolean hasFillMethod;
     private void genFillMethod() {
        if(!hasFillMethod) {
%>        
   private native void fill<%=name%>ListWithAnswer(List< <%=classname%> > list, JGDIFilter filter, List<JGDIAnswer> answers) throws JGDIException;
   
   private void fill<%=name%>List(List< <%=classname%> > list, JGDIFilter filter) throws JGDIException {
      fill<%=name%>ListWithAnswer(list, filter, null);
   }
   
   private void fill<%=name%>List(List< <%=classname%> > list) throws JGDIException {
      fill<%=name%>ListWithAnswer(list, null, null);
   }
   
   private void fill<%=name%>ListWithAnswer(List< <%=classname%> > list, List<JGDIAnswer> answers) throws JGDIException {
      fill<%=name%>ListWithAnswer(list, null, answers);
   }

   
<%        
           hasFillMethod = true;
        } 
     } // end of genFillMethod
     
     protected void genGetMethod() {
        genFillMethod();
%>
   /**
    *   Get the <code><%=name%></code> object
    *   @return the <code><%=name%></code> object
    *   @throws JGDIException on any error on the GDI layer
    */
   public <%=classname%> get<%=name%>() throws JGDIException {
      List< <%=classname%> > ret = new LinkedList< <%=classname%> >();
      fill<%=name%>List(ret);
      switch(ret.size()) {
         case 0: throw new IllegalStateException("static  <%=name%> object not found");
         case 1: return (<%=classname%>)ret.get(0);
         default:
           throw new IllegalStateException("Got more than 1 static <%=name%> objects");
      }
   }

   /**
    *   Get the <code><%=name%></code> object.
    *   @param  answers the <code>answer list</code> object
    *   @return the <code><%=name%></code> object.
    *   @throws JGDIException on any error on the GDI layer
    */
   public <%=classname%> get<%=name%>WithAnswer(List<JGDIAnswer> answers) throws JGDIException {
      List< <%=classname%> > ret = new LinkedList< <%=classname%> >();
      fill<%=name%>ListWithAnswer(ret, answers);
      switch(ret.size()) {
         case 0: throw new IllegalStateException("static  <%=name%> object not found");
         case 1: return (<%=classname%>)ret.get(0);
         default:
           throw new IllegalStateException("Got more than 1 static <%=name%> objects");
      }
   }
<%
     } // end of genGetMethod

     public void genGetListMethod() {
        genFillMethod();
%>        
   /**
    *   Get the list of all defined <code><%=name%></code> objects.
    *   @return list of <code><%=name%></code> objects
    *   @throws JGDIException on any error on the GDI layer
    */
   public List get<%=name%>List() throws JGDIException {
      List< <%=classname%> > ret = new LinkedList< <%=classname%> >();
      fill<%=name%>List(ret);
      return ret;
   }
   /**
    *   Get the list of all defined <code><%=name%></code> objects.
    *   @param  answers the <code>answer list</code> object
    *   @return list of <code><%=name%></code> objects
    *   @throws JGDIException on any error on the GDI layer
    */
   public List get<%=name%>ListWithAnswer(List<JGDIAnswer> answers) throws JGDIException {
      List< <%=classname%> > ret = new LinkedList< <%=classname%> >();
      fill<%=name%>ListWithAnswer(ret, answers);
      return ret;
   }

<%        
     } // end of genGetMethod
     
     public void genGetByPrimaryKeyMethod() {
        genFillMethod();
%>        
   /**
    *  Get a <%=name%> by its primary key
    *
    *  @return the <%=name%>
<%
    for (java.util.Map.Entry<String, String> entry: primaryKeys.entrySet()) {
       String pkName = entry.getKey();
       String pkType = entry.getValue();
%>    *  @param <%=pkName%>   the <%=pkName%> of the <code><%=name%></code> object
<%
    }    
%>    *  @return the found <code><%=name%></code> object of <code>null</code>    
    *  @throws JGDIException on any error on the GDI layer
    */
   public <%=classname%> get<%=name%>(<%
      boolean first = true;
      for (java.util.Map.Entry<String, String> entry: primaryKeys.entrySet()) {
         String pkName = entry.getKey();
         String pkType = entry.getValue();
         if (first) {
            first = false;
         } else {
             %>, <%
         }
         %> <%=pkType%> <%=pkName%> <%
    }
   %>) throws JGDIException {
    
        List< <%=classname%> > ret = new LinkedList< <%=classname%> >();
        PrimaryKeyFilter filter = new PrimaryKeyFilter("<%=cullname%>");
<%
    for (java.util.Map.Entry<String, String> entry: primaryKeys.entrySet()) {
       String pkName = entry.getKey();
       String pkType = entry.getValue();
%>
        filter.include(CullConstants.<%=pkName%>, <%=pkName%>);
<% 
    }
%>
        fill<%=name%>List(ret, filter);

        switch(ret.size()) {
           case 0:  return null;
           case 1:  return (<%=classname%>)ret.get(0);
           default:
                throw new IllegalStateException("Duplicate primary key");
        }
   }
   
   /**
    *  Get a <%=name%> by its primary key
    *
    *  @return the <%=name%>
<%
    for (java.util.Map.Entry<String, String> entry: primaryKeys.entrySet()) {
       String pkName = entry.getKey();
       String pkType = entry.getValue();
%>    *  @param <%=pkName%>   the <%=pkName%> of the <code><%=name%></code> object
<%
    }
%>   *  @param  answers the <code>answer list</code> object
    *  @return the found <code><%=name%></code> object of <code>null</code>    
    *  @throws JGDIException on any error on the GDI layer
    */
   public <%=classname%> get<%=name%>WithAnswer(<%
    first = true;
    for (java.util.Map.Entry<String, String> entry: primaryKeys.entrySet()) {
       String pkName = entry.getKey();
       String pkType = entry.getValue();
       if (first) {
          first = false;
       } else {
         %>, <%
       }
       %> <%=pkType%> <%=pkName%> <%
    }
       %>, List<JGDIAnswer> answers) throws JGDIException {
    
        List< <%=classname%> >  ret = new LinkedList< <%=classname%> >();
        
        PrimaryKeyFilter filter = new PrimaryKeyFilter("<%=cullname%>");<%
    for (java.util.Map.Entry<String, String> entry: primaryKeys.entrySet()) {
       String pkName = entry.getKey();
       String pkType = entry.getValue();%>
        filter.include(CullConstants.<%=pkName%>, <%=pkName%>); <% 
    }
%>
        fill<%=name%>ListWithAnswer(ret, filter, answers);

        switch(ret.size()) {
           case 0:  return null;
           case 1:  return (<%=classname%>)ret.get(0);
           default:
                throw new IllegalStateException("Duplicate primary key");
        }
   }
   
<%
    } // genGetByPrimaryKeyMethod
  
    public void genAddMethod() {
%>
   /**
    *   Add a new <code><%=name%></code> object.
    *   @param  obj the new <code><%=name%></code> object
    *   @throws JGDIException on any error on the GDI layer
    */
   public native void add<%=name%>(<%=classname%> obj) throws JGDIException;

   /**
    *   Add a new <code><%=name%></code> object.
    *   @param  obj the new <code><%=name%></code> object
    *   @param  answers the <code>answer list</code> object
    *   @throws JGDIException on any error on the GDI layer
    */
   public native void add<%=name%>WithAnswer(<%=classname%> obj, List<JGDIAnswer> answers) throws JGDIException;

<% if ((name.equals("Manager")) || 
       (name.equals("Operator")) ||
       (name.equals("AdminHost")) || 
       (name.equals("SubmitHost"))) { %>
   /**
    *   Add a new <code><%=name%></code> object.
    *   @param  name the new <code>String</code> object
    *   @throws JGDIException on any error on the GDI layer
    */
   public void add<%=name%>(String name) throws JGDIException {
      <%=name%> obj = new <%=name%>Impl(name);
      add<%=name%>(obj);
   }

   /**
    *   Add a new <code><%=name%></code> object.
    *   @param  name the new <code>String</code> object
    *   @param  answers the <code>answer list</code> object
    *   @throws JGDIException on any error on the GDI layer
    */
   public void add<%=name%>WithAnswer(String name, List<JGDIAnswer> answers) throws JGDIException {
      <%=name%> obj = new <%=name%>Impl(name);
       add<%=name%>WithAnswer(obj, answers);
   }
   
<%   
       } //end if classname     
     } // end of genAddMethod
     
     public void genDeleteMethod() {
%>        
   /**
    *   Delete a <code><%=name%></code> object.
    *   @param obj  <code><%=name%></code> object with the primary key information
    *   @throws JGDIException on any error on the GDI layer
    */
   public native void delete<%=name%>(<%=classname%> obj) throws JGDIException;

   /**
    *   Delete a <code><%=name%></code> object.
    *   @param obj  <code><%=name%></code> object with the primary key information
    *   @param answers  <code>answer list</code> object
    *   @throws JGDIException on any error on the GDI layer
    */
   public native void delete<%=name%>WithAnswer(<%=classname%> obj, List<JGDIAnswer> answers) throws JGDIException;
   
   /**
    *   Delete several <code><%=name%></code> objects.
    *   @param objs  <code><%=name%></code> object array with the primary key information
    *   @param force  <code>force</code> delete flag
    *   @param userFilter  delete object for users contained in userFilter
    *   @param answers  <code>answer list</code> object
    *   @throws JGDIException on any error on the GDI layer
    */
   public native void delete<%=name%>sWithAnswer(Object[] objs, boolean force, UserFilter userFilter, List<JGDIAnswer> answers) throws JGDIException;
   
<%
     } // end of genDeleteMethod

     public void genDeleteByPrimaryKeyMethod() {
%>

   /**
    *   Delete a <code><%=name%></code> object by its primary key
<%
    for (java.util.Map.Entry<String, String> entry: primaryKeys.entrySet()) {
       String pkName = entry.getKey();
       String pkType = entry.getValue();
%>    *   @param <%=pkName%>   the <%=pkName%> of the <code><%=name%></code> object
<%
    }    
%>    *   @throws JGDIException on any error on the GDI layer
    */
   public void delete<%=name%>(<%
    boolean first = true;
    for (java.util.Map.Entry<String, String> entry: primaryKeys.entrySet()) {
       String pkName = entry.getKey();
       String pkType = entry.getValue();
       if(first) {
         first = false;
       } else {
            %>, <%
       }
       %><%=pkType%> <%=pkName%><%
    }
    %>) throws JGDIException {
       <%=classname%> obj = new <%=classname%>Impl(<%
      first = true;
      for (java.util.Map.Entry<String, String> entry: primaryKeys.entrySet()) {
         String pkName = entry.getKey();
         String pkType = entry.getValue();
         if (first) {
            first = false;
         } else {
            %> , <%           
         }
         %><%=pkName%><%
      }
    %>);
       delete<%=name%>(obj);
    }

   /**
    *   Delete a <code><%=name%></code> object by its primary key
<%
    for (java.util.Map.Entry<String, String> entry: primaryKeys.entrySet()) {
       String pkName = entry.getKey();
       String pkType = entry.getValue();
%>    *   @param <%=pkName%>   the <%=pkName%> of the <code><%=name%></code> object

<%
    }    
%>       *   @param answers  <code>answer list</code> object
    *   @throws JGDIException on any error on the GDI layer
    */
   public void delete<%=name%>WithAnswer(<%
    first = true;  
    for (java.util.Map.Entry<String, String> entry: primaryKeys.entrySet()) {
       String pkName = entry.getKey();
       String pkType = entry.getValue();
       if(first) {
         first = false;
       } else {
            %> , <%           
       }
       %><%=pkType%> <%=pkName%><%
    } // end of for  
    %>, List<JGDIAnswer> answers) throws JGDIException {
       <%=classname%> obj = new <%=classname%>Impl(<%
    first = true;  
    for (java.util.Map.Entry<String, String> entry: primaryKeys.entrySet()) {
       String pkName = entry.getKey();
       String pkType = entry.getValue();
       if(first) {
         first = false;
       } else {
            %> , <%           
       }
       %><%=pkName%><%
    } // end of for
    %>);
       delete<%=name%>WithAnswer(obj, answers);
    }

   /**
    *   Delete several <code><%=name%></code> objects by its primary key
<%
    for (java.util.Map.Entry<String, String> entry: primaryKeys.entrySet()) {
       String pkName = entry.getKey();
       String pkType = entry.getValue();
%>    *   @param <%=pkName%>s   the <%=pkName%> of the <code><%=name%></code> object
<%
    }
if (name.equals("Job") || name.equals("AdvanceReservation")) {%>    
    *   @param forced  <code>forced</code> delete flag
    *   @param userFilter  delete objects owned by users contained in userFilter 
<%}%>    *   @param answers  <code>answer list</code> object  
    *   @throws JGDIException on any error on the GDI layer
    */
   public void delete<%=name%>sWithAnswer(<%
    first = true;  
    for (java.util.Map.Entry<String, String> entry: primaryKeys.entrySet()) {
       String pkName = entry.getKey();
       // String pkType = entry.getValue();
       if(first) {
         first = false;
       } else {
            %> , <%           
       }
       %>String[] <%=pkName%>s<%
    } // end of for
    if (name.equals("Job") || name.equals("AdvanceReservation")) {%>, boolean forced, UserFilter userFilter
    <%}%>, List<JGDIAnswer> answers) throws JGDIException {

    <%
    if (name.equals("Job") || name.equals("AdvanceReservation")) {
       first = true;
       for (java.util.Map.Entry<String, String> entry: primaryKeys.entrySet()) {
          String pkName = entry.getKey();
    %>
         delete<%=name%>sWithAnswer(  
         <% if (first) {
            first = false;%>
            (Object[])<%=pkName%>s
         <%} else {%>
            , (Object[])<%=pkName%>s
         <%}%>
       <%
        } // end of for 
       %>, forced, userFilter, answers);
<%} else {
      first = true;  
    for (java.util.Map.Entry<String, String> entry: primaryKeys.entrySet()) {
       String pkName = entry.getKey();
       if (first) {%>
       // Other objects
       List< <%=classname%> > list = new LinkedList< <%=classname%> >();

       for (int i=0; <%=pkName%>s != null && i < <%=pkName%>s.length; i++) {
             <%=classname%> obj = new <%=classname%>Impl(
       <%} else {%> , <%}%>
       <%=pkName%>s[i]
  <%} // end of for 
         %>);
         list.add(obj);
       }
       delete<%=name%>sWithAnswer(list.toArray(), false, null, answers);      
<%}%>    
  }
<%
    } // end of genDeleteByPrimaryKeyMethod
     public void genUpdateMethod() { 
%>       
   /**
    *   Update a <code><%=name%></code> object.
    *   @param obj   the <code><%=name%></code> object with the new values
    *   @throws JGDIException on any error on the GDI layer
    */
   public native void update<%=name%>(<%=classname%> obj) throws JGDIException;

   /**
    *   Update a <code><%=name%></code> object.
    *   @param obj      the <code><%=name%></code> object with the new values
    *   @param answers  the <code>answer list</code> object
    *   @throws JGDIException on any error on the GDI layer
    */
   public native void update<%=name%>WithAnswer(<%=classname%> obj, List<JGDIAnswer> answers) throws JGDIException;
<%   
     } // end of getUpdateMethod 
  } // end of JGDIGenerator  
  // ---------------------------------------------------------------------------
  // Build generators
  // ---------------------------------------------------------------------------
  java.util.List<JGDIJniGenerator> generators = new java.util.LinkedList<JGDIJniGenerator>();
  for (String name : cullDef.getObjectNames()) {
      com.sun.grid.cull.CullObject cullObj = cullDef.getCullObject(name);
      generators.add(new JGDIJniGenerator(cullObj));
  } // end of for
%>
package com.sun.grid.jgdi.jni;

import com.sun.grid.jgdi.JGDIException;
import com.sun.grid.jgdi.filter.*;
import java.util.List;
import java.util.LinkedList;
import com.sun.grid.jgdi.CullConstants;
import com.sun.grid.jgdi.configuration.JGDIAnswer;
import com.sun.grid.jgdi.monitoring.filter.UserFilter;

<%  
    for (JGDIJniGenerator gen : generators) {
      gen.genImport();
    }
%>
/**
 *  JNI implementation of {@link com.sun.grid.jgdi.JGDI}
 * 
 */
public class JGDIImpl extends JGDIBaseImpl implements com.sun.grid.jgdi.JGDI {

<%
    //  We can not move the getRealExecHostList into JGDIBase, because
    //  it uses fillExecHostList.
%>   
    /**
     * Get the list of real exec hosts (excludes "template" and "global").
     *
     * @throws com.sun.grid.jgdi.JGDIException 
     * @return the list of real exec hosts
     */
   public List getRealExecHostList() throws com.sun.grid.jgdi.JGDIException {
        List<ExecHost> ret = new LinkedList<ExecHost>();
        PrimaryKeyFilter filter = new PrimaryKeyFilter("EH_Type");
        filter.exclude(CullConstants.EH_name, "template" ); 
        filter.exclude(CullConstants.EH_name, "global" );
        fillExecHostList(ret, filter);
        return ret;
   }

<%  
    for (JGDIJniGenerator gen : generators) {
      gen.genMethods();
    }
%>

}
