import com.dvelop.d3.server.Document
import com.dvelop.d3.server.core.D3Interface
import com.dvelop.d3.server.exceptions.SQLException
import com.dvelop.d3.server.exceptions.D3Exception
import groovy.transform.Field

D3Interface d3 = getProperty("d3");

@Field prg              = "migration_nachverknuepfen.groovy";
@Field version          = "0.1";
@Field kunde            = "Dr. Kleeberg & Partner GmbH";

/////////////////////////////////////////////////////////////////////////


// debug = 0 => es finden Änderungen am Datenbestand statt
// debug = 1 => es finden keine Änderungen am Datenbestand statt
@Field debug = 0;

@Field zieltabelle = "mig_multi_link_work";


@Field doku_id_ende = "0";

/////////////////////////////////////////////////////////////////////////

def debugtext;

switch (debug)
{
        case 0:
                debugtext = "ausgeschaltet";
                break;
        default:
                debugtext = "eingeschaltet (keine Aenderungen am Datenbestand)";
                break;
}



xlog( d3, 4, "************************************************" );
xlog( d3, 4, "* Programm         : " + prg );
xlog( d3, 4, "* Version          : " + version );
xlog( d3, 4, "* Kunde            : " + kunde );
xlog( d3, 4, "* geschrieben von  : calb / d.velop AG" );
xlog( d3, 4, "* (c) d.velop AG" );
xlog( d3, 4, "************************************************" );


xlog( d3, 4, "Parameter: " );
xlog( d3, 4, "* zieltabelle        : " + zieltabelle );
xlog( d3, 4, "* doku_id_ende       : " + doku_id_ende );
xlog( d3, 4, "* DEBUG              : " + debugtext );




xlog( d3, 4, "************************************************" );



def sqlQuery = "SELECT TOP (200) id, doku_id_father, doku_id_child, fehler FROM " + zieltabelle + " where doku_id_father like '%" + doku_id_ende + "' and fehler is null order by id";

xlog( d3, 5, sqlQuery );

resultRows = d3.sql.executeAndGet( (String) sqlQuery );
def meldung, ergebnis, xdebug;

xlog( d3, 4, " Es wurden " + resultRows.size() + " Treffer gefunden." );
xlog( d3, 4, "************************************************" );


if ( resultRows.size() > 0 )
{
        def i = 0;
        
        resultRows.each
		{
			i++;
			if ( debug == 0 )
			{
				ergebnis = d3.call.link_documents( it.doku_id_father, it.doku_id_child, "Master", false, false );
			}
			else
			{
				ergebnis = 0
			}
			
			
			
			if ( ergebnis == 0 )
			{
				xdebug = "";
				
				if ( debug != 0 )
				{
					xdebug = " (DEBUG)";
				}
				
				meldung = "OK" + xdebug;
			}
			else
			{
				meldung = "Fehler: RC [" + ergebnis + "]";
			}
			
			xlog( d3, 4, "Nr. " + i + " / ID " + it.id + ": (C) [" + it.doku_id_child + "] => (F) [" + it.doku_id_father + "] => " + meldung );
			
			if ( debug == 0 )
			{
				fehler( d3, meldung, (int) it.id );
			}
		}
        
        
}




























def xlog( D3Interface d3, int loglevel, String logtext )
{
        def logdatei            = "d:\\d3\\apps\\" + prg + "_" + doku_id_ende + ".txt";
        def g_loglevel          = 5;
        def g_logdirect         = "BEIDE";  // BEIDE oder D3LOG oder DATEI
        def date                = new Date();
        def zeitstempel         = date.format( "yyyy.MM.dd HH:mm:ss", TimeZone.getTimeZone('CET') );
        def loglevel_lang       = "[INFO ]";
        
        
        
        
        
        if ( loglevel <= g_loglevel )
        {
                switch ( loglevel )
                {
                        case 1:
                                loglevel_lang   = "[FATAL]";
                                
                                if ( ( g_logdirect == "D3LOG" ) || ( g_logdirect == "BEIDE" ) )
                                {
                                        d3.log.critical( logtext );
                                }
                                break;
                        case 2:
                                loglevel_lang   = "[ERROR]";
                                
                                if ( ( g_logdirect == "D3LOG" ) || ( g_logdirect == "BEIDE" ) )
                                {
                                        d3.log.error( logtext );
                                }
                                break;
                        case 3:
                                loglevel_lang   = "[WARN ]";
                                
                                if ( ( g_logdirect == "D3LOG" ) || ( g_logdirect == "BEIDE" ) )
                                {
                                        d3.log.warn( logtext );
                                }
                                break;
                        case 4:
                                loglevel_lang   = "[INFO ]";
                                
                                if ( ( g_logdirect == "D3LOG" ) || ( g_logdirect == "BEIDE" ) )
                                {
                                        d3.log.info( logtext );
                                }
                                break;
                        case 5:
                                loglevel_lang   = "[DEBUG]";
                                // Da die d.3 Server i.d.R. nicht im Debug-Modus laufen, erscheinen DEBUG-Meldungen nicht im Log.
                                if ( ( g_logdirect == "D3LOG" ) || ( g_logdirect == "BEIDE" ) )
                                {
                                        d3.log.debug( logtext );
                                }
                                break;
                }
                
        }
        
        if ( ( g_logdirect == "DATEI" ) || ( g_logdirect == "BEIDE" ) )
        {
                new File( logdatei ).append( zeitstempel + " " + loglevel_lang + " " + logtext + "\n", "UTF8" );
        }
        
        
        return 0;
}






def fehler( D3Interface d3, String meldung, int id )
{
	def meldung_db;
	
	if ( meldung.length() > 250 )
	{
		meldung_db = meldung.substring( 0, 249 );
	}
	else
	{
		meldung_db = meldung;
	}
	
	if ( debug == 0 )
	{
		sql = "update " + zieltabelle + " set fehler = '" + meldung_db + "' where id = " + id;
		d3.sql.execute( sql );
	}
	
	//xlog( d3, 2, meldung );
}





