package org.labkey.wnprc_compliance.schema.tables;

import org.jetbrains.annotations.Nullable;
import org.jooq.Select;
import org.labkey.api.data.*;
import org.labkey.api.query.ExprColumn;
import org.labkey.api.query.UserSchema;
import org.labkey.dbutils.jooq.jOOQConnection;
import org.labkey.wnprc_compliance.WNPRC_ComplianceSchema;

import static org.labkey.wnprc_compliance.model.jooq.Tables.PROTOCOLS;
import static org.labkey.wnprc_compliance.model.jooq.Tables.PROTOCOL_REVISIONS;

/**
 * Created by jon on 3/27/17.
 */
public class MostRecentProtocolRevision extends AbstractTableInfo {
    public static String NAME = "MostRecentProtocolRevision";
    private UserSchema schema;

    public MostRecentProtocolRevision(@Nullable UserSchema userSchema) {
        super(DbSchema.get(WNPRC_ComplianceSchema.NAME), NAME);
        schema = userSchema;

        this.addColumn(new ExprColumn(this, PROTOCOL_REVISIONS.PROTOCOL_ID.getName(), new SQLFragment(ExprColumn.STR_TABLE_ALIAS + "." + PROTOCOL_REVISIONS.PROTOCOL_ID.getName().toString()), JdbcType.VARCHAR));
        this.addColumn(new ExprColumn(this, PROTOCOL_REVISIONS.APPROVAL_DATE.getName(), new SQLFragment(ExprColumn.STR_TABLE_ALIAS + "." + PROTOCOL_REVISIONS.APPROVAL_DATE.getName().toString()), JdbcType.DATE));
    }


    @Override
    public SQLFragment getFromSQL() {
        SQLFragment sql = new SQLFragment();


        try (jOOQConnection jOOQConnection = new jOOQConnection(WNPRC_ComplianceSchema.NAME)) {
            String query = jOOQConnection.create().select(PROTOCOL_REVISIONS.PROTOCOL_ID, PROTOCOL_REVISIONS.APPROVAL_DATE)
                    .from(PROTOCOL_REVISIONS)
                    .getSQL();

            sql.append(query);
        }

        return sql;
    }

    @Nullable
    @Override
    public UserSchema getUserSchema() {
        return schema;
    }
}

@@ -108,84 +108,9 @@
 ooq.
-Select;%0Aimport org.labkey.api.data.*;%0Aimport org.labkey.api.query.ExprColumn
+*
 ;%0Aim
@@ -194,16 +194,62 @@
 ection;%0A
+import org.labkey.dbutils.jooq.jOOQTableInfo;%0A
 import o
@@ -542,16 +542,12 @@
 nds 
-Abstract
+jOOQ
 Tabl
@@ -619,39 +619,8 @@
 on%22;
-%0A    private UserSchema schema;
 %0A%0A  
@@ -708,21 +708,14 @@
 per(
-DbSchema.get(
+NAME, 
 WNPR
@@ -741,468 +741,20 @@
 NAME
-)
 , 
-NAME);%0A        schema = userSchema;%0A%0A        this.addColumn(new ExprColumn(this, PROTOCOL_REVISIONS.PROTOCOL_ID.getName(), new SQLFragment(ExprColumn.STR_TABLE_ALIAS + %22.%22 + PROTOCOL_REVISIONS.PROTOCOL_ID.getName().toString()), JdbcType.VARCHAR));%0A        this.addColumn(new ExprColumn(this, PROTOCOL_REVISIONS.APPROVAL_DATE.getName(), new SQLFragment(ExprColumn.STR_TABLE_ALIAS + %22.%22 + PROTOCOL_REVISIONS.APPROVAL_DATE.getName().toString()), JdbcType.DATE)
+userSchema
 );%0A 
@@ -759,17 +759,16 @@
 %0A    %7D%0A%0A
-%0A
     @Ove
@@ -788,81 +788,48 @@
 lic 
-SQLFragment getFromSQL() %7B%0A        SQLFragment sql = new SQLFragment();%0A%0A
+Table%3C? extends Record%3E getjOOQTable() %7B
 %0A   
@@ -937,22 +937,14 @@
     
-String query =
+return
  jOO
@@ -1035,16 +1035,57 @@
 VAL_DATE
+, PROTOCOL_REVISIONS.HAS_PHYSICAL_HAZARDS
 )%0A      
@@ -1149,178 +1149,28 @@
    .
-getSQL();%0A%0A            sql.append(query);%0A        %7D%0A%0A        return sql;%0A    %7D%0A%0A    @Nullable%0A    @Override%0A    public UserSchema getUserSchema() %7B%0A        return schema;
+asTable();%0A        %7D
 %0A   

Rework table to use jOOQTableInfo from DBUtils.