// Global d.3 libraries -------------------------------------------------------
import com.dvelop.d3.server.core.D3;
import com.dvelop.d3.server.core.D3Interface;
import com.dvelop.d3.server.Document;
import com.dvelop.d3.server.User;
import com.dvelop.d3.server.DocumentType;
// Libraries to handle the diferent hook types ---------------------------------------------------
import com.dvelop.d3.server.Entrypoint;
import com.dvelop.d3.server.Condition;
// Libraries to handle the diferent hook types --------------------------------
import com.dvelop.d3.server.Validation; 
import com.dvelop.d3.server.RepositoryField;
import com.dvelop.d3.server.ValueSet;
import com.dvelop.d3.server.Translation
import com.dvelop.d3.server.ValueSetTranslation

// Special libraries -----------------------------------------------------------------------------
import groovy.sql.Sql; 
import java.lang.annotation.*;


public class D3ValueSets {

@ValueSet( entrypoint = "dsClient" )
  def getClient(D3Interface d3, RepositoryField reposField, User d3User, DocumentType docTypeShort, int row_no, int validate, Document docObj) {
    d3.log.info( "[DBS] START getClient");
	
    def sqlQuery = """SELECT kuliname + ' | ' + strasse + ' | ' + plz + ' | ' + ort + ' | ' + land + ' | ' + kulinr
	FROM [D3ServiceT].[dbo].[dv_kulistamm] order by kuliname""";
	
	def sql = groovy.sql.Sql.newInstance( "jdbc:sqlserver://d3sql;databaseName=D3ServiceT", 'd3DBAdminT', 'Dvelop1!', 'com.microsoft.sqlserver.jdbc.SQLServerDriver' );
	
    def resultRows = sql.rows( sqlQuery );
    def clientIDPList = [];
	def erg, erg2;
	
    resultRows.each { 
		erg = it[ 0 ].split( " \\| " );
		//d3.log.error( " getClient: <" + erg[ 0 ] + "> <" + erg[ 5 ] + ">" );
		if ( erg[ 5 ].substring( 0,3 ) == "000" )
		{
			erg2 = erg[ 0 ] + ' | ' + erg[ 1 ] + ' | ' + erg[ 2 ] + ' | ' + erg[ 3 ] + ' | ' + erg[ 4 ] + ' | ' + erg[ 5 ].substring( 3 );
		}
		else
		{
			erg2 = erg[ 0 ] + ' | ' + erg[ 1 ] + ' | ' + erg[ 2 ] + ' | ' + erg[ 3 ] + ' | ' + erg[ 4 ] + ' | ' + erg[ 5 ];
		}
		clientIDPList.add( erg2 );
	}
	
    if ( clientIDPList.size() > 0 ) {
      reposField.provideValuesForValueSet( clientIDPList );
    }
  }

@ValueSet( entrypoint = "dsClientNr" )
  def getClientNr(D3Interface d3, RepositoryField reposField, User d3User, DocumentType docTypeShort, int row_no, int validate, Document docObj) {
    d3.log.info( "[DBS] START getClientNr");
	
    def sqlQuery = """SELECT kuliname + ' | ' + strasse + ' | ' + plz + ' | ' + ort + ' | ' + land + ' | ' + kulinr
	FROM [D3ServiceT].[dbo].[dv_kulistamm] order by kulinr""";
	
	def sql = groovy.sql.Sql.newInstance( "jdbc:sqlserver://d3sql;databaseName=D3ServiceT", 'd3DBAdminT', 'Dvelop1!', 'com.microsoft.sqlserver.jdbc.SQLServerDriver' );
	
    def resultRows = sql.rows( sqlQuery );
    def clientIDPList = [];
	def erg, erg2;
	
    resultRows.each { 
		erg = it[ 0 ].split( " \\| " );
		//d3.log.error( " getClient: <" + erg[ 0 ] + "> <" + erg[ 5 ] + ">" );
		if ( erg[ 5 ].substring( 0,3 ) == "000" )
		{
			erg2 = erg[ 0 ] + ' | ' + erg[ 1 ] + ' | ' + erg[ 2 ] + ' | ' + erg[ 3 ] + ' | ' + erg[ 4 ] + ' | ' + erg[ 5 ].substring( 3 );
		}
		else
		{
			erg2 = erg[ 0 ] + ' | ' + erg[ 1 ] + ' | ' + erg[ 2 ] + ' | ' + erg[ 3 ] + ' | ' + erg[ 4 ] + ' | ' + erg[ 5 ];
		}
		clientIDPList.add( erg2 );
	}
	
    if ( clientIDPList.size() > 0 ) {
      reposField.provideValuesForValueSet( clientIDPList );
    }
  }

@ValueSet( entrypoint = "dsMaterialNr" )
  def getMaterialNr(D3Interface d3, RepositoryField reposField, User d3User, DocumentType docTypeShort, int row_no, int validate, Document docObj) {
    d3.log.info( "[DBS] START getMaterialNr");
	
    def sqlQuery = """SELECT matnr + ' | ' + bez + ' | ' + fremdart + ' | ' + mat_such
	FROM [D3ServiceT].[dbo].[dv_artikelstamm] order by matnr""";
	
	def sql = groovy.sql.Sql.newInstance( "jdbc:sqlserver://d3sql;databaseName=D3ServiceT", 'd3DBAdminT', 'Dvelop1!', 'com.microsoft.sqlserver.jdbc.SQLServerDriver' );
	
    def resultRows = sql.rows( sqlQuery );
    def clientIDPList = [];
    resultRows.each { clientIDPList.add(it[0] ); }
	
    if ( clientIDPList.size() > 0 ) {
      reposField.provideValuesForValueSet( clientIDPList );
    }
  }

@ValueSet( entrypoint = "dsMandantName" )
  def getMandantName(D3Interface d3, RepositoryField reposField, User d3User, DocumentType docTypeShort, int row_no, int validate, Document docObj) {
    d3.log.info( "[DBS] START getMandantName");
	
    def sqlQuery = """SELECT mandant_nr + ' | ' + mandant_name
	FROM [D3ServiceT].[dbo].[dv_mandant] order by mandant_nr""";
	
	def sql = groovy.sql.Sql.newInstance( "jdbc:sqlserver://d3sql;databaseName=D3ServiceT", 'd3DBAdminT', 'Dvelop1!', 'com.microsoft.sqlserver.jdbc.SQLServerDriver' );
	
    def resultRows = sql.rows( sqlQuery );
    def clientIDPList = [];
    resultRows.each { clientIDPList.add(it[0] ); }
	
    if ( clientIDPList.size() > 0 ) {
      reposField.provideValuesForValueSet( clientIDPList );
    }
  }


@ValueSet( entrypoint = "dsMandantNr" )
  def getMandantNr(D3Interface d3, RepositoryField reposField, User d3User, DocumentType docTypeShort, int row_no, int validate, Document docObj) {
    d3.log.info( "[DBS] START getMandantNr");
	
    def sqlQuery = """SELECT mandant_nr + ' | ' + mandant_name
	FROM [D3ServiceT].[dbo].[dv_mandant] order by mandant_nr""";
	
	def sql = groovy.sql.Sql.newInstance( "jdbc:sqlserver://d3sql;databaseName=D3ServiceT", 'd3DBAdminT', 'Dvelop1!', 'com.microsoft.sqlserver.jdbc.SQLServerDriver' );
	
    def resultRows = sql.rows( sqlQuery );
    def clientIDPList = [];
    resultRows.each { clientIDPList.add(it[0] ); }
	
    if ( clientIDPList.size() > 0 ) {
      reposField.provideValuesForValueSet( clientIDPList );
    }
  }




 // ------------------------------------------------------------------------------------------------
  
  
} // end of D3ValueSets 


