package ${package};
import javaxt.json.*;
import java.sql.SQLException;
${includes}

//******************************************************************************
//**  ${modelName} Class
//******************************************************************************
/**
 *   Used to represent a ${modelName}
 *
 ******************************************************************************/

public class ${modelName} extends javaxt.sql.Model ${implements}{

    ${privateFields}


  //**************************************************************************
  //** Constructor
  //**************************************************************************
    public ${modelName}(){
        super("${tableName}", java.util.Map.ofEntries(
            ${fieldMap}
        ));
        ${initArrays}
    }


  //**************************************************************************
  //** Constructor
  //**************************************************************************
  /** Creates a new instance of this class using a record ID in the database.
   */
    public ${modelName}(long id) throws SQLException {
        this();
        init(id);
    }


  //**************************************************************************
  //** Constructor
  //**************************************************************************
  /** Creates a new instance of this class using a JSON representation of a
   *  ${modelName}.
   */
    public ${modelName}(JSONObject json){
        this();
        update(json);
    }


  //**************************************************************************
  //** update
  //**************************************************************************
  /** Used to update attributes using a record in the database.
   */
    protected void update(Object rs) throws SQLException {

        try{
            this.id = getValue(rs, "id").toLong();
${getValues}
${getModels}
        }
        catch(Exception e){
            if (e instanceof SQLException) throw (SQLException) e;
            else throw new SQLException(e.getMessage());
        }
    }


  //**************************************************************************
  //** update
  //**************************************************************************
  /** Used to update attributes with attributes from another ${modelName}.
   */
    public void update(JSONObject json){

        Long id = json.get("id").toLong();
        if (id!=null && id>0) this.id = id;
        ${getJson}
    }


    ${publicMembers}
    ${saveModel}
    ${toJson}


  //**************************************************************************
  //** get
  //**************************************************************************
  /** Used to find a ${modelName} using a given set of constraints. Example:
   *  ${modelName} obj = ${modelName}.get("${field[0]}=", ${field[0]});
   */
    public static ${modelName} get(Object...args) throws SQLException {
        Object obj = _get(${modelName}.class, args);
        return obj==null ? null : (${modelName}) obj;
    }


  //**************************************************************************
  //** find
  //**************************************************************************
  /** Used to find ${modelName}s using a given set of constraints.
   */
    public static ${modelName}[] find(Object...args) throws SQLException {
        Object[] obj = _find(${modelName}.class, args);
        ${modelName}[] arr = new ${modelName}[obj.length];
        for (int i=0; i<arr.length; i++){
            arr[i] = (${modelName}) obj[i];
        }
        return arr;
    }
}