////////////////////////////////////////////////////////////////////////////////
// Hook for dbs | invoice
// => Java-Functions
// File-Encoding: UTF-8 without BOM( Test: ü,ä,ö )
//------------------------------------------------------------------------------
// Created/Edited       | Description
//------------------------------------------------------------------------------
// 16.01.2017           | dbs | invoice workflow 1.0.0.0
// 06.11.2017	tott	| Write State mit Langem Usernamen
////////////////////////////////////////////////////////////////////////////////

global gDBS_RET_CODE_SERVLET_JAVA	   = ""
global gDBS_RET_MSG_SERVLET_JAVA	   = ""

//---------------------------------------------------------------------------------------------------------------------------------------------------
//  write current state of workflow to erp interface via java class
//---------------------------------------------------------------------------------------------------------------------------------------------------
proc write_state( pDocId, wfUser, wfState, wfStep, wfStepLvl, wfWebServiceURL )
{
   vars lReturnCode, lReturnValue    
   vars lCurrentTimestamp =  getCurrentTimestamp()

	vars lSplittedUsers // Number of returned values of function string_split
	vars lUsersString = wfUser // string to be splitted
	vars lCharSplitter = ";" // split character
	vars lCount, h_ReturnValue
	vars lUsers
   
   gDBS_RET_CODE_SERVLET_JAVA = ""
   gDBS_RET_MSG_SERVLET_JAVA = ""
   
  call api_log_info ("Start Function Write State")
  
	lSplittedUsers = api_function ("string_split", lUsersString, lCharSplitter)
	// Ergebnisausgabe
	if( lSplittedUsers > 0 )
	{
		call api_log_info ("String wfUsers was splitted in :lSplittedUsers Strings.")
		
		for lCount = 1 while( lCount <= lSplittedUsers ) step 1
		{	
			// Username anhand ; Splitten und Langnamen ermitteln
			HIDE_API_ERROR_ONCE = "1"
			h_ReturnValue = api_function ("user_get_data", api_splitted_string[ lCount ])
			
			if ( h_ReturnValue == 0 )
			{
				// das Komma erst setzen wenn ein User durch
				if( lCount != 1 )
				{
					lUsers = lUsers ## "; " 
				}

				// Umlaute umbennnen, da es sonst zu UTF8 Problemen kommt
				h_ReturnValue = api_function ("string_replace", api_user_realname, "ä", "ae" )
				h_ReturnValue = api_function ("string_replace", api_converted_string, "ü", "ue" )
				h_ReturnValue = api_function ("string_replace", api_converted_string, "ö", "oe" )
				h_ReturnValue = api_function ("string_replace", api_converted_string, "Ä", "Ae" )
				h_ReturnValue = api_function ("string_replace", api_converted_string, "Ü", "Ue" )
				h_ReturnValue = api_function ("string_replace", api_converted_string, "Ö", "Oe" )
				
				// Username Lang speichern
				lUsers = lUsers ## api_converted_string 
			}
			
		}
	}
	
   call api_clear_document_context()
   lReturnValue = api_function ("attribute_get_all", pDocId, "")
   

   if( lReturnValue == 0 )
   {
		// -----------------------------------------------------------
		// add request params
		lReturnValue = api_function ("java_add_param", wfWebServiceURL) // wfWebServiceURL
		lReturnValue = api_function ("java_add_param", gGMF_CONFIG_INTERFACE)
		lReturnValue = api_function ("java_add_param", "writeState")
		lReturnValue = api_function ("java_add_param", "ERPID" ## "|" ## pDocId)   // pDocId     api_doc_field[gDDF_ERP_ID])
		lReturnValue = api_function ("java_add_param", "SYSID" ## "|" ## api_doc_field[gDDF_SYSTEM_ID])
		lReturnValue = api_function ("java_add_param", "CLIENT" ## "|" ## api_doc_field[gDDF_CLIENT])
		lReturnValue = api_function ("java_add_param", "COMPANY_CODE" ## "|" ## api_doc_field[gDDF_COMPANY_CODE])
		lReturnValue = api_function ("java_add_param", "TIME_STAMP" ## "|" ## lCurrentTimestamp)
		lReturnValue = api_function ("java_add_param", "USER" ## "|" ## wfUser)
		lReturnValue = api_function ("java_add_param", "STATE" ## "|" ## wfState)
		lReturnValue = api_function ("java_add_param", "STEP" ## "|" ## wfStep)
		lReturnValue = api_function ("java_add_param", "STEPLVL" ## "|" ## wfStepLvl)

		lReturnCode = api_function ("java_call_static", "com/dvelop/decs/erp/servlet/javacall/DecsERPServletConnector", "executePost")

		if( lReturnCode != 0 || gDBS_RET_CODE_SERVLET_JAVA != 0 )
		{
			call api_log_info( "Error while creating xml request: " ## lReturnCode )
			call api_log_info( "Servlet returned error code: " ## gDBS_RET_CODE_SERVLET_JAVA )
			call api_log_info( "Servlet returned error message: " ## gDBS_RET_MSG_SERVLET_JAVA )
			return gERR_ERP_SVLT_WRITE_STATE
		}
		else
		{
			call api_log_info( "Created xml request for write_state(): " ## lReturnCode )
			return 0
		}
   }
   return 0
} // end of write_state

//---------------------------------------------------------------------------------------------------------------------------------------------------
// write current users of workflow to erp interface via java class
//---------------------------------------------------------------------------------------------------------------------------------------------------
proc write_user( pDocId, wfUsers, wfState, wfStep, wfStepLvl, wfWebServiceURL )
{
	vars lReturnCode, lReturnValue 
	gDBS_RET_CODE_SERVLET_JAVA = ""
    gDBS_RET_MSG_SERVLET_JAVA = ""

	call api_log_info ("START write_user.")
	
	call api_clear_document_context()
	lReturnValue = api_function ("attribute_get_all", pDocId, "")

	if( lReturnValue == 0 )
	{
		// Variablendeklaration
		vars lSplittedUsers // Number of returned values of function string_split
		vars lUsersString = wfUsers // string to be splitted
		vars lCharSplitter = ";" // split character

		// call string_split
		lSplittedUsers = api_function ("string_split", lUsersString, lCharSplitter)
		// Ergebnisausgabe
		if( lSplittedUsers > 0 )
		{
			call api_log_info ("String wfUsers was splitted in :lSplittedUsers Strings.")

			// -----------------------------------------------------------
			// add request params
			lReturnValue = api_function ("java_add_param", wfWebServiceURL) // wfWebServiceURL
			lReturnValue = api_function ("java_add_param", gGMF_CONFIG_INTERFACE)
			lReturnValue = api_function ("java_add_param", "writeUser")
			lReturnValue = api_function ("java_add_param", "ERPID" ## "|" ## api_doc_field[gDDF_ERP_ID])
			lReturnValue = api_function ("java_add_param", "SYSID" ## "|" ## api_doc_field[gDDF_SYSTEM_ID])
			lReturnValue = api_function ("java_add_param", "CLIENT" ## "|" ## api_doc_field[gDDF_CLIENT])
			lReturnValue = api_function ("java_add_param", "COMPANY_CODE" ## "|" ## api_doc_field[gDDF_COMPANY_CODE])		
			lReturnValue = api_function ("java_add_param", "STEP" ## "|" ## wfStep)
			lReturnValue = api_function ("java_add_param", "STEPLVL" ## "|" ## wfStepLvl)

			// start of for-loop --------------------------------------------------------------
			vars lCount // counter for loop
			vars lCurrentTimestamp =  getCurrentTimestamp()
			lReturnValue = api_function ("java_add_param", "USERS_TIME_STAMP" ## "|" ## lCurrentTimestamp)	
			lReturnValue = api_function ("java_add_param", "USERS_STATE" ## "|" ## wfState)

			for lCount = 1 while( lCount <= lSplittedUsers ) step 1
			{								
				lReturnValue = api_function ("java_add_param", "USERS_USER" ## "|" ## api_splitted_string[ lCount ])
			}
			// end of for-loop ---------------------------------------------------------------

			lReturnCode = api_function ("java_call_static", "com/dvelop/decs/erp/servlet/javacall/DecsERPServletConnector", "executePost")


			if( lReturnCode != 0 || gDBS_RET_CODE_SERVLET_JAVA != 0 )
			{
				call api_log_info( "Error while creating xml request: " ## lReturnCode )
				call api_log_info( "Servlet returned error code: " ## gDBS_RET_CODE_SERVLET_JAVA )
				call api_log_info( "Servlet returned error message: " ## gDBS_RET_MSG_SERVLET_JAVA )
				return gERR_ERP_SVLT_WRITE_USER
			}
			else
			{
				call api_log_info ( "Created xml request for write_user(): " ## lReturnCode )
			}	
		}
		else
		{
			if( lSplittedUsers < 0 )
			{
			call api_log_error ("No users could be determined with string_split in write_user()! Errorcode was :lSplittedUsers.")			
			}
		}
		return 0
	}
	else
	{
		call api_log_error ("Errorcode :lReturnValue was returned!")
	}
}//end of write_user