File: eaf_n_cst_helperservice.sru
Size: 58067
Date: Tue, 22 Jan 2008 23:39:22 +0100
HA$PBExportHeader$eaf_n_cst_helperservice.sru
forward
global type eaf_n_cst_helperservice from n_cst_baseservice
end type
end forward

global type eaf_n_cst_helperservice from n_cst_baseservice
end type
global eaf_n_cst_helperservice eaf_n_cst_helperservice

type variables
Protected:

string               is_headersuffix = "_t"

end variables

forward prototypes
public function string getcolumnheadername (ref datastore ads_working, string as_column, string as_suffix)
public function string getcolumnheadername (ref datawindow adw_working, string as_column, string as_suffix)
public function string getkeyvalue (ref string as_keysandvalues, string as_keyword, string as_separator)
public function long getobjects (datastore ads_source, ref string as_objects[], string as_objecttype, string as_band, boolean ab_visibleonly)
public function long getobjects (datawindow adw_source, ref string as_objects[], string as_objecttype, string as_band, boolean ab_visibleonly)
public function long replaceparameter (ref string as_text, string as_parameters[], string as_parameterindetifier)
public function long parsestringtoarray (string as_original, string as_delimiter, ref string as_result[])
public function string globalreplace (string as_original, string as_lookingfor, string as_replacewith, boolean ab_ignorecase)
public function long countoccurrences (string as_original, string as_lookfor, boolean ab_ignorecase)
public function integer getarguments (powerobject apo, ref string as_argnames[], ref string as_argdatatypes[])
public function long serverdate (ref datetime adt_date)
public function long servertime (ref datetime adt_time)
public function string modifyselectsqlwhere (string as_selectsql, string as_where)
public function long replaceparameters (ref string as_text, string as_parameternames[], string as_parameters[])
public function long selectsql (string selectsql)
public function long selectsql (string selectsql, ref any returnvalue1, ref any returnvalue2)
public function long selectsql (string selectsql, ref any returnvalue1, ref any returnvalue2, ref any returnvalue3)
public function long selectsql (string selectsql, ref any returnvalue1, ref any returnvalue2, ref any returnvalue3, ref any returnvalue4)
public function long selectsql (string selectsql, ref any returnvalue1, ref any returnvalue2, ref any returnvalue3, ref any returnvalue4, ref any returnvalue5)
public function long selectsql (string selectsql, ref any returnvalues[])
public function long selectsql1 (string selectsql, ref any returnvalue)
public function long executesql (string as_sql)
public function long readfile (string as_filename, ref string as_content)
public function long readfile (string as_filename, ref blob ab_content)
public function long writefile (string as_filename, blob ab_content)
public function long writefile (string as_filename, string ab_content)
public function string syntaxfromsql (string as_sql, string as_presentation)
public function string syntaxfromview (string as_viewname, string as_createviewsql, string as_presentation)
public function any stringtoany (string as_coltype, string as_inputvalue)
public function long executesql (string as_sql, ref string as_error)
public function long executesql (string as_sql[])
public function long executesql (string as_sql[], ref long al_sqlnrows[], ref string as_error)
public function long executesql (string as_sql[], ref string as_error)
public function long datawindowfromsql (string as_sql, datawindow a_datawindow, long mode)
public function long datawindowfromsql (string as_sql, datawindow a_datawindow)
public function integer datastorefromsql (string as_sql, ref n_datastore a_datastore)
public function integer datastorefromsql (string as_sql, ref n_datastore a_datastore, integer mode)
public function boolean isregexmatch (string as_text, string as_expr)
public function integer setdropdownlistbox (powerobject adw_obj, string as_colname, string as_ddlbvalue, boolean ab_allowedit, ref string as_modifyexpression)
public function integer setdropdownlistbox (powerobject adw_obj, string as_colname, string as_ddlbvalue, boolean ab_allowedit)
end prototypes

public function string getcolumnheadername (ref datastore ads_working, string as_column, string as_suffix);////////////////////////////////////////////////////////////////
// Description:
//    Attempts to get the proper Header Name for the passed in 
//    Column using the passed in Suffix
//
//    Similar function found in datastoreservice
// Revisions
//    1.0   - Initial version
// Arguments:  
//    ads_working - The datawindow object
//    as_column - The column for which to get the header name
//    as_suffix - The suffix to use to get the header name
// Returns: 
//    The column header name or the Origina passed in column name value
//    if none was found
//    "!" - Failure
////////////////////////////////////////////////////////////////
// Copyright $$HEX1$$a900$$ENDHEX$$ 2000 - 2007 Youngsoft, Inc.  All rights reserved.
// Any distribution of the Youngsoft, Inc. Enterprise Application Framework
// source code in whole or part by other than Youngsoft, Inc. is prohibited.
//////////////////////////////////////////////////////////////// 

constant string METHOD_NAME = "getColumnHeaderName "

If ib_trace And ib_detailed Then
   inv_log.traceLog (METHOD_NAME, "(ads_working, as_column, as_suffix)   " + &
      "as_column="+as_column+" as_suffix="+as_suffix)
End If   

// Declare local variables
string   ls_header
string   ls_holdheader

// Validate arguments
If isNull(ads_working) Or Not isValid(ads_working) Then
   addError(METHOD_NAME, "Invalid ads_working argument")
   Return "!"
End If
If isNull(as_column) Or len(as_column) = 0 Then
   addError(METHOD_NAME, "Invalid as_column argument")
   Return "!"
End If   
If isNull(as_suffix) or len(as_suffix) = 0 Then
   as_suffix = is_headersuffix
End If   

// Attempt to get the column header name
ls_header = ads_working.describe ( as_column + as_suffix + ".Text" )
If isNull(ls_header) Or ls_header="" Or ls_header="?" Or ls_header = "!" Then
   // Failed on the attempt to get the column header name
   ls_header = as_column
End If   

// Remove Special characters
ls_header = globalReplace ( ls_header, "~r", " ", true ) 
ls_header = globalReplace ( ls_header, "~n", " ", true ) 
ls_header = globalReplace ( ls_header, "_",  " ", true ) 
ls_header = globalReplace ( ls_header, "~"", "", true ) 
ls_header = globalReplace ( ls_header, "~'", "", true ) 
ls_header = globalReplace ( ls_header, "~~", "", true ) 
ls_header = globalReplace ( ls_header, ":", "", true ) 

// Hold the string prior to the last step (just in case it fails)
ls_holdheader = ls_header

// Capitalize the Column Header string
ls_header = ads_working.describe ( "evaluate('wordCap(~"" + ls_header + "~")',0)" )
If isNull(ls_header) Or ls_header="" Or ls_header="?" Or ls_header = "!" Then
   // Failed on the attempt to Capitalize string
   ls_header = ls_holdheader
End If   

Return ls_header
end function

public function string getcolumnheadername (ref datawindow adw_working, string as_column, string as_suffix);////////////////////////////////////////////////////////////////
// Description:
//    Attempts to get the proper Header Name for the passed in 
//    Column using the passed in Suffix
//
//    Similar function found in datastoreservice
// Revisions
//    1.0   - Initial version
// Arguments:  
//    adw_working - The datawindow object
//    as_column - The column for which to get the header name
//    as_suffix - The suffix to use to get the header name
// Returns: 
//    The column header name or the Origina passed in column name value
//    if none was found
//    "!" - Failure
////////////////////////////////////////////////////////////////
// Copyright $$HEX1$$a900$$ENDHEX$$ 2000 - 2007 Youngsoft, Inc.  All rights reserved.
// Any distribution of the Youngsoft, Inc. Enterprise Application Framework
// source code in whole or part by other than Youngsoft, Inc. is prohibited.
//////////////////////////////////////////////////////////////// 

constant string METHOD_NAME = "getColumnHeaderName "

If ib_trace And ib_detailed Then
   inv_log.traceLog (METHOD_NAME, "(adw_working, as_column, as_suffix)   " + &
      "as_column="+as_column+" as_suffix="+as_suffix)
End If   

// Declare local variables
string   ls_header
string   ls_holdheader

// Validate arguments
If isNull(adw_working) Or Not isValid(adw_working) Then
   addError(METHOD_NAME, "Invalid adw_working argument")
   Return "!"
End If
If isNull(as_column) Or len(as_column) = 0 Then
   addError(METHOD_NAME, "Invalid as_column argument")
   Return "!"
End If   
If isNull(as_suffix) or len(as_suffix) = 0 Then
   as_suffix = is_headersuffix
End If   

// Attempt to get the column header name
ls_header = adw_working.describe ( as_column + as_suffix + ".Text" )
If isNull(ls_header) Or ls_header="" Or ls_header="?" Or ls_header = "!" Then
   // Failed on the attempt to get the column header name
   ls_header = as_column
End If   

// Remove Special characters
ls_header = globalReplace ( ls_header, "~r", " ", true ) 
ls_header = globalReplace ( ls_header, "~n", " ", true ) 
ls_header = globalReplace ( ls_header, "_",  " ", true ) 
ls_header = globalReplace ( ls_header, "~"", "", true ) 
ls_header = globalReplace ( ls_header, "~'", "", true )  
ls_header = globalReplace ( ls_header, "~~", "", true )
ls_header = globalReplace ( ls_header, ":", "", true )

// Hold the string prior to the last step (just in case it fails)
ls_holdheader = ls_header

// Capitalize the Column Header string
ls_header = adw_working.describe ( "evaluate('wordCap(~"" + ls_header + "~")',0)" )
If isNull(ls_header) Or ls_header="" Or ls_header="?" Or ls_header = "!" Then
   // Failed on the attempt to Capitalize string
   ls_header = ls_holdheader
End If   

Return ls_header
end function

public function string getkeyvalue (ref string as_keysandvalues, string as_keyword, string as_separator);////////////////////////////////////////////////////////////////
// Description:
//    Receives a string with 0 to Many "Key=Value;" entries 
//    where ";" is a common separator for the possible Many 
//    "Key=Value" combinations.
//    Returns the Value on the first matching Key encountered.
//    Returns the original passed in string minus the entry returned.
///   The method is Not case sensitive.
//
//    Similar function found in stringservice
// Revisions
//    1.0   - Initial version
// Arguments:  
//    as_keysandvalues  - The original and changed string containing 
//                        0 to Many "Keys and Values"
//    as_keyword        - The keyword being searched for in the Keys
//    as_separator      - The separator being used to separate multiple key/values
// Returns: 
//    Returns the Value on the first matching Key encountered
//    "!" - Failure
////////////////////////////////////////////////////////////////
// Copyright $$HEX1$$a900$$ENDHEX$$ 2000 - 2007 Youngsoft, Inc.  All rights reserved.
// Any distribution of the Youngsoft, Inc. Enterprise Application Framework
// source code in whole or part by other than Youngsoft, Inc. is prohibited.
//////////////////////////////////////////////////////////////// 

constant string METHOD_NAME = "getKeyValue "

If ib_trace And ib_detailed Then
   inv_log.traceLog (METHOD_NAME, "(as_keysandvalues, as_keyword, as_separator) ~r~n" + &
      "   as_keysandvalues="+as_keysandvalues+ "~r~n" + &
      "   as_keyword="+as_keyword+ "~r~n" + &
      "   as_separator="+as_separator)
End If   

// Declare local variables
long     ll_pos_start =1
long     ll_pos_keyword
long     ll_pos_separator
long     ll_pos_point
long     ll_len_keyword
long     ll_len_separator
long     ll_len_keysandvalues
long     ll_equalsigndistance
long     ll_equal
string   ls_value 
string   ls_possibleequalsign
string   ls_source
string   ls_exact
string   ls_newkeysandvalues
boolean  lb_valuefound = false
boolean  lb_done = false

// Validate arguments
If isNull(as_keysandvalues) Then
   addError(METHOD_NAME, "Invalid as_keysandvalues argument")
   Return "!"
End If
If isNull(as_keyword) Then
   addError(METHOD_NAME, "Invalid as_keyword argument")
   Return "!"
End If
If isNull(as_separator) or len(trim(as_separator)) = 0 Then
   addError(METHOD_NAME, "Invalid as_separator argument")
   Return "!"
End If

// Determine the lengths
ll_len_keyword = len(as_keyword)
ll_len_separator = len(as_separator)
ll_len_keysandvalues = len(as_keysandvalues)

// Loop until the right Key/Value is encountered or
// no matching Key is encountered
Do While (Not lb_valuefound And Not lb_done)
   // Find a possible occurrence of the Key
   ll_pos_keyword = pos (lower(as_keysandvalues), lower(as_keyword), ll_pos_start)
   
   // If no possible key is found, then we are done
   lb_done = (ll_pos_keyword <= 0)
   If Not lb_done Then
      // A possible occurrence of the Key has been located
      // Check if the equal sign is next 
      ls_possibleequalsign = mid(as_keysandvalues, ll_pos_keyword + ll_len_keyword)
      If left(trim(ls_possibleequalsign), 1) = "=" Then
         // Yes, the Key/Value wanted has been found
         lb_valuefound = true
         
         // Find the distance of the "=" sign
         ll_equalsigndistance = pos(ls_possibleequalsign, "=") - 1
         
         // Define the position point
         ll_pos_point = ll_pos_keyword + ll_len_keyword + ll_equalsigndistance + 1        
         
         // Find the position of the separator
         ll_pos_separator = pos (as_keysandvalues, as_separator, ll_pos_point)
         If ll_pos_separator = 0 Then
            // There are no more separators (end of string)
            ll_pos_separator = ll_len_keysandvalues + 1
         End If
         
         // Get the value
         ls_value = mid(as_keysandvalues, &
                         ll_pos_point, ll_pos_separator - ll_pos_point)
         
         // Take out the Key/Value from the original string
         ls_newkeysandvalues = rightTrim(left(as_keysandvalues, ll_pos_keyword - 1))
         ls_newkeysandvalues += leftTrim(right(as_keysandvalues, &
            ll_len_keysandvalues - (ll_pos_point + len(ls_value))))
         as_keysandvalues = ls_newkeysandvalues
      End If
      
      // Define the next starting position to base the next search on
      ll_pos_start = ll_pos_keyword + ll_len_keyword

   End If
Loop

Return ls_value
end function

public function long getobjects (datastore ads_source, ref string as_objects[], string as_objecttype, string as_band, boolean ab_visibleonly);////////////////////////////////////////////////////////////////
// Description:
//    Returns the Object Names that match the Search Criteria
//    passed in
//
//    Similar function found in stringservice
// Revisions
//    1.0   - Initial version
// Arguments:
//    ads_source     - The datastore on which to get the objects
//    as_objects[]   - Place holder for the objects
//    as_objecttype  - The object type to search for (* for all)
//    as_band        - The band to search in (* for all)
//    ab_visibleonly - Set to true if only visible objects are desired
//                      otherwise set to False
// Returns: 
//     1 - Success
//     0 - No objects
//    -1 - Failure
////////////////////////////////////////////////////////////////
// Copyright $$HEX1$$a900$$ENDHEX$$ 2000 - 2007 Youngsoft, Inc.  All rights reserved.
// Any distribution of the Youngsoft, Inc. Enterprise Application Framework
// source code in whole or part by other than Youngsoft, Inc. is prohibited.
//////////////////////////////////////////////////////////////// 

constant string METHOD_NAME = "getObjects "

If ib_trace and ib_detailed Then
   inv_log.traceLog (METHOD_NAME, "(ref as_objects[], as_objecttype, " + &
      "as_band, ab_visibleonly)~r~n" + &
      "as_objecttype="+as_objecttype + "~r~n" + &
      "as_band="+as_band + "~r~n" + &
      "ab_visibleonly="+string(ab_visibleonly))
End If   

// Declare local variables
constant string   TAB = "~t"
string   ls_objectsyntax
string   ls_objectname
boolean  lb_validtype
boolean  lb_validband
boolean  lb_validvisibility
string   ls_empty[]
boolean  lb_done = false
long     ll_pos_namestart
long     ll_counter=0
long     ll_pos_nexttab

// Perform cleanup
as_objects = ls_empty
as_objecttype = trim(as_objecttype)
as_band = trim(as_band)

// Validate arguments
If isNull(ads_source) Or Not isValid(ads_source) Then 
   addError(METHOD_NAME, "Invalid ads_source argument")  
   Return -1
End If
If isNull(as_objecttype) Or Len(as_objecttype) = 0 Then 
   addError(METHOD_NAME, "Invalid as_objecttype argument")  
   Return -1
End If
If isNull(as_band) Or Len(as_band) = 0 Then 
   addError(METHOD_NAME, "Invalid as_band argument")  
   Return -1
End If
If isNull(ab_visibleonly)  Then
   addError(METHOD_NAME, "Invalid ab_visibleonly argument") 
   Return -1
End If

// Get the Object Syntax
ls_objectsyntax = ads_source.describe("datawindow.objects")
If isNull(ls_objectsyntax) Or ls_objectsyntax = "!" Then 
   addError(METHOD_NAME, "Invalid call for 'datawindow.Objects'") 
   Return -1
End If
If len(ls_objectsyntax) = 0 Then
   // No objects found
   Return 0
End If

// Loop around all the objects which are separated by TABs (with last one exception)
ll_pos_namestart = 1
ll_pos_nexttab =  pos(ls_objectsyntax, TAB, ll_pos_namestart)
Do While (Not lb_done)
   // Account for the last entry
   If ll_pos_nexttab = 0 Then
      ll_pos_nexttab = len(ls_objectsyntax) + 1
   End If
   
   // Get the next object name
   ls_objectname = mid(ls_objectsyntax, ll_pos_namestart, &
      (ll_pos_nexttab - ll_pos_namestart))

   // As long as there is a name, there is an entry to check

   If len(ls_objectname) > 0 Then
      // Determine if object name was requested
      lb_validtype = (as_objecttype = "*" Or &
                      ads_source.describe(ls_objectname + ".type") = as_objecttype)
      If lb_validtype Then
         lb_validband = (as_band = "*" Or &
                         ads_source.describe(ls_objectname + ".band") = as_band)
         If lb_validband Then
            lb_validvisibility = (Not ab_visibleonly Or &
                                  (ab_visibleonly And &
                                   ads_source.describe(ls_objectname + ".visible") = "1"))
         End If
      End If
      If lb_validtype And lb_validband And lb_validvisibility Then
         ll_counter ++
         as_objects[ll_counter] = ls_objectname
      End if
   
      // Locate the next start of a name and it next tab position
      ll_pos_namestart = ll_pos_nexttab + 1
      ll_pos_nexttab =  pos(ls_objectsyntax, TAB, ll_pos_namestart)
   Else
      lb_done = True
   End If
Loop 

// Determine the appropriate return code
If ll_counter = 0 Then
   Return 0
End If
Return 1
end function

public function long getobjects (datawindow adw_source, ref string as_objects[], string as_objecttype, string as_band, boolean ab_visibleonly);////////////////////////////////////////////////////////////////
// Description:
//    Returns the Object Names that match the Search Criteria
//    passed in
//
//    Similar function found in stringservice
// Revisions
//    1.0   - Initial version
// Arguments:
//    adw_source     - The datawindow on which to get the objects
//    as_objects[]   - Place holder for the objects
//    as_objecttype  - The object type to search for (* for all)
//    as_band        - The band to search in (* for all)
//    ab_visibleonly - Set to true if only visible objects are desired
//                      otherwise set to False
// Returns: 
//     1 - Success
//     0 - No objects
//    -1 - Failure
////////////////////////////////////////////////////////////////
// Copyright $$HEX1$$a900$$ENDHEX$$ 2000 - 2007 Youngsoft, Inc.  All rights reserved.
// Any distribution of the Youngsoft, Inc. Enterprise Application Framework
// source code in whole or part by other than Youngsoft, Inc. is prohibited.
//////////////////////////////////////////////////////////////// 

constant string METHOD_NAME = "getObjects "

If ib_trace and ib_detailed Then
   inv_log.traceLog (METHOD_NAME, "(ref as_objects[], as_objecttype, " + &
      "as_band, ab_visibleonly)~r~n" + &
      "as_objecttype="+as_objecttype + "~r~n" + &
      "as_band="+as_band + "~r~n" + &
      "ab_visibleonly="+string(ab_visibleonly))
End If   

// Declare local variables
constant string   TAB = "~t"
string   ls_objectsyntax
string   ls_objectname
boolean  lb_validtype
boolean  lb_validband
boolean  lb_validvisibility
string   ls_empty[]
boolean  lb_done = false
long     ll_pos_namestart
long     ll_counter=0
long     ll_pos_nexttab

// Perform cleanup
as_objects = ls_empty
as_objecttype = trim(as_objecttype)
as_band = trim(as_band)

// Validate arguments
If isNull(adw_source) Or Not isValid(adw_source) Then 
   addError(METHOD_NAME, "Invalid adw_source argument")  
   Return -1
End If
If isNull(as_objecttype) Or Len(as_objecttype) = 0 Then 
   addError(METHOD_NAME, "Invalid as_objecttype argument")  
   Return -1
End If
If isNull(as_band) Or Len(as_band) = 0 Then 
   addError(METHOD_NAME, "Invalid as_band argument")  
   Return -1
End If
If isNull(ab_visibleonly)  Then
   addError(METHOD_NAME, "Invalid ab_visibleonly argument") 
   Return -1
End If

// Get the Object Syntax
ls_objectsyntax = adw_source.describe("datawindow.objects")
If isNull(ls_objectsyntax) Or ls_objectsyntax = "!" Then 
   addError(METHOD_NAME, "Invalid call for 'datawindow.Objects'") 
   Return -1
End If
If len(ls_objectsyntax) = 0 Then
   // No objects found
   Return 0
End If

// Loop around all the objects which are separated by TABs (with last one exception)
ll_pos_namestart = 1
ll_pos_nexttab =  pos(ls_objectsyntax, TAB, ll_pos_namestart)
Do While (Not lb_done)
   // Account for the last entry
   If ll_pos_nexttab = 0 Then
      ll_pos_nexttab = len(ls_objectsyntax) + 1
   End If
   
   // Get the next object name
   ls_objectname = mid(ls_objectsyntax, ll_pos_namestart, &
      (ll_pos_nexttab - ll_pos_namestart))

   // As long as there is a name, there is an entry to check
   If len(ls_objectname) > 0 Then
      // Determine if object name was requested
      lb_validtype = (as_objecttype = "*" Or &
                      adw_source.describe(ls_objectname + ".type") = as_objecttype)
      If lb_validtype Then
         lb_validband = (as_band = "*" Or &
                         adw_source.describe(ls_objectname + ".band") = as_band)
         If lb_validband Then
            lb_validvisibility = (Not ab_visibleonly Or &
                                  (ab_visibleonly And &
                                   adw_source.describe(ls_objectname + ".visible") = "1"))
         End If
      End If
      If lb_validtype And lb_validband And lb_validvisibility Then
         ll_counter ++
         as_objects[ll_counter] = ls_objectname
      End if
   
      // Locate the next start of a name and it next tab position
      ll_pos_namestart = ll_pos_nexttab + 1
      ll_pos_nexttab =  pos(ls_objectsyntax, TAB, ll_pos_namestart)
   Else
      lb_done = True
   End If
Loop 

// Determine the appropriate return code
If ll_counter = 0 Then
   Return 0
End If
Return 1
end function

public function long replaceparameter (ref string as_text, string as_parameters[], string as_parameterindetifier);////////////////////////////////////////////////////////////////
// Description:
//    Replace the "Parameter place holders" inside the passed in 
//    "text" with the information in the "parameters[]"
//
//    Similar function found in stringservice
// Revisions
//    1.0   - Initial version
// Arguments:  
//    as_text     - Place holder to receive and contain the replaced text
//    as_parameters[]   - The parameters to be replaced into the text
//    as_parameterindetifier  - The parameter indentifier to be located
//                         and replaced with parameter values
// Returns:
//     1 - Success
//    -1 - Failure
////////////////////////////////////////////////////////////////
// Copyright $$HEX1$$a900$$ENDHEX$$ 2000 - 2007 Youngsoft, Inc.  All rights reserved.
// Any distribution of the Youngsoft, Inc. Enterprise Application Framework
// source code in whole or part by other than Youngsoft, Inc. is prohibited.
//////////////////////////////////////////////////////////////// 

constant string METHOD_NAME = "replaceParameter "

If ib_trace Then
   inv_log.traceLog (METHOD_NAME, "(ref as_text, as_parameters[], as_parameterindetifier)~r~n" + &
      "   as_text="+as_text+"~r~n" + &
      "   as_parameterindetifier="+as_parameterindetifier)
End If   

// Declare local variables
long  ll_idx
long  ll_pos_start = 1
long  ll_parameterupper
long  ll_parameterplaceholdercount
long  ll_pos_parameterindentifier

// Validate arguments
If isNull(as_text) Then
   addError (METHOD_NAME, "Invalid as_text argument")
   Return -1
End If
If isNull(as_parameters) Then
   addError (METHOD_NAME, "Invalid as_parameters argument")
   Return -1
End If
If isNull(as_parameterindetifier) Then
   addError (METHOD_NAME, "Invalid as_parameterindetifier argument")
   Return -1
End If

// Get the number of parameter entries
ll_parameterupper = upperBound(as_parameters)

// Get number of parameter place holders in the passed in text
If len(as_parameterindetifier) > 0 Then
   ll_parameterplaceholdercount = countOccurrences(as_text, as_parameterindetifier, true)
End If

// Quick check for no action needed
If ll_parameterupper = 0 And ll_parameterplaceholdercount = 0 Then
   Return 1
End If

// Make sure there are not any nulls in parameters
For ll_idx = 1 to ll_parameterupper
   If isNull(as_parameters[ll_idx]) Then
      as_parameters[ll_idx] = "null"
   End If
Next

// Quick check for improper number of parameters
If ll_parameterupper <> ll_parameterplaceholdercount Then
   // -- Problem encountered
   // Make the parameters passed in show up in the text
   If len(trim(as_text)) = 0 Then
      as_text = "'Blank Message' and "
   End If
   as_text += " Parameters("
   For ll_idx = 1 to ll_parameterupper
      If ll_idx >= 2 Then as_text += ", "
      as_text += as_parameters[ll_idx]
   Next
   as_text += ")" 
   addError (METHOD_NAME, &
      "Parameter count and parameters waiting to be replaced do not match:~r~n"+as_text)
   Return -1
End If

// Replace the "Parameter Place Holders" with the "Parameter Values"
For ll_idx = 1 to ll_parameterplaceholdercount
   ll_pos_parameterindentifier = pos(as_text, as_parameterindetifier, ll_pos_start)
   If ll_pos_parameterindentifier <=0 Then
      addError (METHOD_NAME, "Unexpected error (look into countOccurrences)")    
      Return 1
   End If
   as_text = replace (as_text, ll_pos_parameterindentifier, len(as_parameterindetifier), &
      as_parameters[ll_idx])
      
   // Define the next start point
   ll_pos_start = ll_pos_parameterindentifier + len(as_parameters[ll_idx])
Next
Return 1
end function

public function long parsestringtoarray (string as_original, string as_delimiter, ref string as_result[]);////////////////////////////////////////////////////////////////
// Description:
//    Parses the delimeted passed in string into an array
//    Create an array of strings containing the contents of the
//    passed in original string.  Each entry on to be placed on the
//    array is denoted on the original string via the Delimeter
//    string.
//
//    Similar function found in stringservice
// Revisions
//    1.0   - Initial version
// Arguments:  
//    as_original    - The original string
//    as_delimiter   - The delimiter separating the entries
//    as_result[]    - Place holder for the parsed out entries
// Returns:
//     1 - Success
//    -1 - Failure
////////////////////////////////////////////////////////////////
// Copyright $$HEX1$$a900$$ENDHEX$$ 2000 - 2007 Youngsoft, Inc.  All rights reserved.
// Any distribution of the Youngsoft, Inc. Enterprise Application Framework
// source code in whole or part by other than Youngsoft, Inc. is prohibited.
//////////////////////////////////////////////////////////////// 

constant string METHOD_NAME = "parseStringToArray "

If ib_trace And ib_detailed Then
   inv_log.traceLog (METHOD_NAME, "(as_original, as_delimiter, ref as_result[])~r~n" + &
      "   as_original="+as_original + "~r~n" + &
      "   as_delimiter="+as_delimiter)
End If   

// Declare local variables
long     ll_len_delimeter
long     ll_pos_delimeter
long     ll_pos_start = 1
long     ll_count = 0
string   ls_possibleentry
string   ls_empty[]

// Perform cleanup
as_result = ls_empty

// Validate arguments
If isNull(as_original) Then
   addError(METHOD_NAME, "Invalid as_original argument")
   Return -1
End If
If isNull(as_delimiter) Then
   addError(METHOD_NAME, "Invalid as_delimiter argument")
   Return -1
End If

// Perform special checks
If len(as_original) = 0 Then
   // Successfully found no entries
   Return 1
End If
If as_original = as_delimiter Then
   // Successfully found no entries
   Return 1
End If
If len(trim(as_original)) = 0 Then
   // Successfully found no entries
   Return 1
End If

// Delimeter length
ll_len_delimeter = Len(as_delimiter)

// Find the first occurrence of the delimeter
ll_pos_delimeter =  pos(lower(as_original), lower(as_delimiter))

// Loop until no more entries are found
Do While ll_pos_delimeter > 0
   // An entry has been found
   ll_count ++ 
   
   // Get the parsed entry
   as_result[ll_count] =  &
      mid (as_original, ll_pos_start, (ll_pos_delimeter - ll_pos_start))
   
   // Determine the new start position
   ll_pos_start = ll_pos_delimeter + ll_len_delimeter

   // Determine the position of the next delimeter
   ll_pos_delimeter =  pos(lower(as_original), lower(as_delimiter), ll_pos_start)
Loop

// Check if the end of the source string is also an entry Or
// Check to catch for the entire string being the one and only entry
ls_possibleentry = mid (as_original, ll_pos_start, Len (as_original))
If len (ls_possibleentry) > 0 Then
   // An entry has been found
   ll_count++
   as_result[ll_count] = ls_possibleentry
End If

Return 1

end function

public function string globalreplace (string as_original, string as_lookingfor, string as_replacewith, boolean ab_ignorecase);////////////////////////////////////////////////////////////////
// Description:
//    Replaces all occurrences of "as_lookingfor" with "as_replaceWith"
//    found in the source string
//
//    Similar function found in stringservice
// Revisions
//    1.0   - Initial version
// Arguments:  
//    as_original       - The original string
//    as_lookingfor     - The string to search for replacement
//    as_replacewith    - The string to replace
//    ab_ignorecase     - Should Case be ignored?
// Returns:
//    The replaced string
//    If as_original is Null, then NULL is returned
////////////////////////////////////////////////////////////////
// Copyright $$HEX1$$a900$$ENDHEX$$ 2000 - 2007 Youngsoft, Inc.  All rights reserved.
// Any distribution of the Youngsoft, Inc. Enterprise Application Framework
// source code in whole or part by other than Youngsoft, Inc. is prohibited.
//////////////////////////////////////////////////////////////// 

constant string METHOD_NAME = "globalReplace "

If ib_trace And ib_detailed Then
   inv_log.traceLog (METHOD_NAME, "(as_original, as_lookingfor, as_replacewith, ab_ignorecase)~r~n" + &
      "   as_original="+as_original + "~r~n" + &
      "   as_lookingfor="+as_lookingfor + "~r~n" + &
      "   as_replacewith="+as_replacewith + "~r~n" + &      
      "   ab_ignorecase="+string(ab_ignorecase))
End If   

// Declare local variables
long     ll_pos_found
long     ll_len_tobereplaced
long     ll_len_replacewith
string   ls_case_source
string   ls_ignorecase_source

// Validate arguments
If isNull(as_original) Or len(trim(as_original)) = 0 Then
   addError(METHOD_NAME, "Invalid as_original argument")
   Return as_original
End If
If isNull(as_lookingfor) Then
   addError(METHOD_NAME, "Invalid as_lookingfor argument")
   Return as_original
End If
If isNull(as_replacewith) Then
   addError(METHOD_NAME, "Invalid as_replacewith argument")
   Return as_original
End If
If isNull(ab_ignorecase) Then
   addError(METHOD_NAME, "Invalid ab_ignorecase argument")
   Return as_original
End If

// The working source strings
ls_case_source = as_original
ls_ignorecase_source = lower(as_original)

// Determine the length of the "lookingFor" and "replaceWith" strings
ll_len_tobereplaced = len(as_lookingfor)
ll_len_replacewith = len(as_replacewith)

// Is case being ignored
If ab_ignorecase Then
   // Yes, case will be ignored
   as_lookingfor = lower(as_lookingfor)
End If

// Search for the first occurrence of "toBeReplaced"
If ab_ignorecase Then
   ll_pos_found = pos(ls_ignorecase_source, as_lookingfor)
Else  
   ll_pos_found = pos(ls_case_source, as_lookingfor)  
End If

Do While ll_pos_found > 0
   // Replace "lookingFor" with "replaceWith"
   ls_case_source = replace(ls_case_source, ll_pos_found, ll_len_tobereplaced, as_replacewith)
   
   // Reset the string which ignores case
   ls_ignorecase_source = lower(ls_case_source)
   
   // Search for the next occurrence of "lookingFor"
   If ab_ignorecase Then
      ll_pos_found = pos(ls_ignorecase_source, as_lookingfor, &
         ll_pos_found + ll_len_replacewith)
   Else  
      ll_pos_found = pos(ls_case_source, as_lookingfor, &
         ll_pos_found + ll_len_replacewith)
   End If   
Loop

Return ls_case_source
end function

public function long countoccurrences (string as_original, string as_lookfor, boolean ab_ignorecase);////////////////////////////////////////////////////////////////
// Description:
//    Counts the number of occurrences of the LookFor string
//    within the Original string
//
//    Similar function found in stringservice
// Revisions
//    1.0   - Initial version
// Arguments:  
//    as_original - The string to be searched
//    as_lookfor - the string to be searched for
//    ab_ignorecase - case sensitive search or not
// Returns:
//    Number of occurrences found
//    -1 - Failure
////////////////////////////////////////////////////////////////
// Copyright $$HEX1$$a900$$ENDHEX$$ 2000 - 2007 Youngsoft, Inc.  All rights reserved.
// Any distribution of the Youngsoft, Inc. Enterprise Application Framework
// source code in whole or part by other than Youngsoft, Inc. is prohibited.
//////////////////////////////////////////////////////////////// 

constant string METHOD_NAME = "countOccurrences "

If ib_trace Then
   inv_log.traceLog (METHOD_NAME, "(as_original, as_lookfor, ab_ignorecase)~r~n" + &
      "   as_original="+string(as_original)+"~r~n" + &
      "   as_lookfor="+as_lookfor+ "~r~n" + &
      "   ab_ignorecase="+string(ab_ignorecase))
End If   

// Declare local variables
long  ll_pos_found
long  ll_len_lookfor
long  ll_count

// Validate arguments
If isNull(as_original) Then
   addError (METHOD_NAME, "Invalid as_original argument")
   Return -1
End If
If isNull(as_lookfor) Or len(as_lookfor) = 0 Then
   addError (METHOD_NAME, "Invalid as_lookfor argument")
   Return -1
End If
If isNull(ab_ignorecase) Then
   addError (METHOD_NAME, "Invalid ab_ignorecase argument")
   Return -1
End If

// Quick check
If len(as_original) = 0 Then
   Return 0
End If

// Determine the length of the string being searched for
ll_len_lookfor = len(as_lookfor)

// Should case be ignored
If ab_ignorecase Then
   as_original = lower(as_original)
   as_lookfor = lower(as_lookfor)
End If

// Loop around the original string finding all the occurrences
ll_pos_found = pos(as_original, as_lookfor, 1)
Do While ll_pos_found > 0
   // Found an occurrence
   ll_count ++
   
   // Attempt to find another occurrence
   ll_pos_found = pos(as_original, as_lookfor, (ll_pos_found + ll_len_lookfor))
Loop

// Return the number of occurrences found
Return ll_count
end function

public function integer getarguments (powerobject apo, ref string as_argnames[], ref string as_argdatatypes[]);////////////////////////////////////////////////////////////////
// Description:
//    Determines if a DataWindow has arguments and what they are
//    Similar function found in stringservice
// Revisions
//    1.0   - Initial version
// Arguments:  
//    apo - datawindow or datastore
//    as_argnames[]:  A string array (by reference) to hold the argument names
//    as_argdatatypes[]:  A string array (by reference) to hold argument datatypes
// Returns:
//    The number of arguments found
//    -1 - Failure
////////////////////////////////////////////////////////////////
// Copyright $$HEX1$$a900$$ENDHEX$$ 2000 - 2007 Youngsoft, Inc.  All rights reserved.
// Any distribution of the Youngsoft, Inc. Enterprise Application Framework
// source code in whole or part by other than Youngsoft, Inc. is prohibited.
//////////////////////////////////////////////////////////////// 

constant string METHOD_NAME = "GetArguments"

If ib_trace Then
   inv_log.traceLog (METHOD_NAME, "")
End If   

string ls_dwargs, ls_dwargswithtype[], ls_args[], ls_types[]
long ll_a, ll_args, ll_pos
n_cst_stringservice lnvo_string

ls_dwargs = apo.Dynamic Describe ( "DataWindow.Table.Arguments" ) 

ll_args = lnvo_string.ParseStringToArray ( ls_dwargs, "~n", ls_dwargswithtype ) 

For ll_a = 1 to ll_args
   ll_pos = Pos ( ls_dwargswithtype[ll_a], "~t", 1 )
   If ll_pos > 0 Then
      as_argnames[UpperBound(as_argnames)+1] = Left ( ls_dwargswithtype[ll_a], ll_pos - 1 ) 
      as_argdatatypes[UpperBound(as_argdatatypes)+1] = Mid ( ls_dwargswithtype[ll_a], ll_pos + 1 ) 
   End If
Next

Return UpperBound ( as_argnames )
end function

public function long serverdate (ref datetime adt_date);////////////////////////////////////////////////////////////////
// Description:
//    Get the today date from database
// Revisions
//    1.0   - Initial version
// Arguments:  
//    adt_date:  date returned
// Returns:
//     1 - Success
//    -1 - Failure
////////////////////////////////////////////////////////////////
// Copyright $$HEX1$$a900$$ENDHEX$$ 2000 - 2007 Youngsoft, Inc.  All rights reserved.
// Any distribution of the Youngsoft, Inc. Enterprise Application Framework
// source code in whole or part by other than Youngsoft, Inc. is prohibited.
//////////////////////////////////////////////////////////////// 

constant string METHOD_NAME = "ServerDate "

DateTime ldt_date

If ib_trace Then
   inv_log.traceLog (METHOD_NAME, "")
End If

if ServerTime(ldt_date) = 1 then
   adt_date = DateTime(Date(ldt_date))
   return 1
else
   return -1
end if
end function

public function long servertime (ref datetime adt_time);////////////////////////////////////////////////////////////////
// Description:
//    Get the time now from database
// Revisions
//    1.0   - Initial version
// Arguments:  
//    adt_date:  datetime returned
// Returns:
//     1 - Success
//    -1 - Failure
////////////////////////////////////////////////////////////////
// Copyright $$HEX1$$a900$$ENDHEX$$ 2000 - 2007 Youngsoft, Inc.  All rights reserved.
// Any distribution of the Youngsoft, Inc. Enterprise Application Framework
// source code in whole or part by other than Youngsoft, Inc. is prohibited.
//////////////////////////////////////////////////////////////// 

constant string METHOD_NAME = "ServerTime"

If ib_trace Then
   inv_log.traceLog (METHOD_NAME, "")
End If   

DateTime ldt_time
String ls_sql, ls_error, ls_dbms
Long ll_errorcode
n_datastore lds_temp

ls_dbms = Upper(Left(sqlca.dbms, 3))

if ls_dbms = "" then
   addError (METHOD_NAME, "Invalid SQLCA.dbms value, it is empty")
   return -1
end if

choose case ls_dbms
   case "INF"
      ls_sql = "select current from informix.systables where tabname = 'systables'"
   case "O10", "O90", "O84"
      ls_sql = "select sysdate from dual"
   case else
      ls_sql = "select getdate()"
end choose

if ls_sql <> "" then
   lds_temp = create n_datastore
   if datastorefromsql(ls_sql, lds_temp) = -1 then
      lds_temp.getlasterror(ll_errorcode, ls_error)
      addError (METHOD_NAME, "Select time error, errorcode: " + String(ll_errorcode) + " error message: " + ls_error)
      return -1
   end if
   
   ldt_time = lds_temp.GetItemDateTime(1, 1)

   destroy lds_temp
else
   ldt_time = DateTime(Today(), Now())
end if

adt_time = ldt_time

return 1

end function

public function string modifyselectsqlwhere (string as_selectsql, string as_where);integer  li_pos, li_mpos
string ls_sql = "", ls_orderby = "", ls_groupby = ""
string ls_source

ls_source = Upper(as_selectsql)

if Trim(as_where) = "" then   // $$HEX2$$a16c0967$$ENDHEX$$ WHERE $$HEX2$$6167f64e$$ENDHEX$$
   return as_selectsql
else
   li_pos = Pos(ls_source, "GROUP BY")
   if li_pos <> 0 then
      ls_groupby = Right(as_selectsql, Len(as_selectsql) - li_pos + 1)
      as_selectsql = Left(as_selectsql, li_pos -1)
      ls_source = Upper(as_selectsql)
   else
      li_pos = Pos(ls_source, "ORDER BY")
      if li_pos <> 0 then
         ls_orderby = Right(as_selectsql, Len(as_selectsql) - li_pos + 1)
         as_selectsql = Left(as_selectsql, li_pos -1)
         ls_source = Upper(as_selectsql)
      end if
   end if
   li_pos = Pos(ls_source, "FROM")
   do while li_pos > 0
      li_mpos = li_pos
      li_pos = Pos(ls_source, "FROM", li_pos + 5)
   loop
   
   if li_mpos = 0 then li_mpos = 1
   li_pos = Pos(ls_source, "WHERE", li_mpos)
   if li_pos = 0 then
      ls_sql = as_selectsql + " WHERE " + as_where 
   else
      ls_sql = as_selectsql + " AND (" + as_where + ")"
   end if
   if ls_groupby <> "" then
      ls_sql = ls_sql + " " + ls_groupby     
   else
      ls_sql = ls_sql + " " + ls_orderby
   end if
   return ls_sql
end if
end function

public function long replaceparameters (ref string as_text, string as_parameternames[], string as_parameters[]);////////////////////////////////////////////////////////////////
// Description:
//    Replace the "Parameter place holders" inside the passed in 
//    "text" with the information in the "parameters[]"
// Revisions
//    1.0   - Initial version
// Arguments:  
//    as_text     - Place holder to receive and contain the replaced text
//    as_parameters[]   - The parameters to be replaced into the text
//    as_parameternames[]  - The parameter names to be located
//                         and replaced with parameter values
// Returns:
//     1 - Success
//    -1 - Failure
////////////////////////////////////////////////////////////////
// Copyright $$HEX1$$a900$$ENDHEX$$ 2000 - 2007 Youngsoft, Inc.  All rights reserved.
// Any distribution of the Youngsoft, Inc. Enterprise Application Framework
// source code in whole or part by other than Youngsoft, Inc. is prohibited.
//////////////////////////////////////////////////////////////// 

constant string METHOD_NAME = "replaceParameter "

If ib_trace Then
   inv_log.traceLog (METHOD_NAME, "(ref as_text, as_parameters[], as_parameternames[])~r~n" + &
      "   as_text="+as_text+"~r~n" )
End If   

// Declare local variables
long  ll_parameterupper, ll_idx
n_cst_stringservice lnvo_string

// Validate arguments
If isNull(as_text) Then
   addError (METHOD_NAME, "Invalid as_text argument")
   Return -1
End If
If isNull(as_parameters) Then
   addError (METHOD_NAME, "Invalid as_parameters argument")
   Return -1
End If
If isNull(as_parameternames) Then
   addError (METHOD_NAME, "Invalid as_parameterindetifier argument")
   Return -1
End If

// Get the number of parameter entries
ll_parameterupper = upperBound(as_parameters)

// Quick check for no action needed
If ll_parameterupper = 0 Then
   Return 1
End If

lnvo_string = create n_cst_stringservice

// Replace action
For ll_idx = 1 to ll_parameterupper
   if Pos(Lower(as_text), Lower(as_parameternames[ll_idx])) > 0 then
      If isNull(as_parameters[ll_idx]) or Trim(as_parameters[ll_idx]) = "" Then
         as_parameters[ll_idx] = "''"
      End If
      as_text = lnvo_string.globalreplace(as_text, as_parameternames[ll_idx], as_parameters[ll_idx], true)
   end if
Next

destroy lnvo_string

Return 1
end function

public function long selectsql (string selectsql);Any la_returnvalues[]
Long ll_ret

ll_ret = selectsql(selectsql, la_returnvalues)

return ll_ret
end function

public function long selectsql (string selectsql, ref any returnvalue1, ref any returnvalue2);Any la_returnvalues[]
Long ll_ret

ll_ret = selectsql(selectsql, la_returnvalues)

returnvalue1 = la_returnvalues[1]
returnvalue2 = la_returnvalues[2]

return ll_ret
end function

public function long selectsql (string selectsql, ref any returnvalue1, ref any returnvalue2, ref any returnvalue3);Any la_returnvalues[]
Long ll_ret

ll_ret = selectsql(selectsql, la_returnvalues)

returnvalue1 = la_returnvalues[1]
returnvalue2 = la_returnvalues[2]
returnvalue3 = la_returnvalues[3]

return ll_ret
end function

public function long selectsql (string selectsql, ref any returnvalue1, ref any returnvalue2, ref any returnvalue3, ref any returnvalue4);Any la_returnvalues[]
Long ll_ret

ll_ret = selectsql(selectsql, la_returnvalues)

returnvalue1 = la_returnvalues[1]
returnvalue2 = la_returnvalues[2]
returnvalue3 = la_returnvalues[3]
returnvalue4 = la_returnvalues[4]

return ll_ret
end function

public function long selectsql (string selectsql, ref any returnvalue1, ref any returnvalue2, ref any returnvalue3, ref any returnvalue4, ref any returnvalue5);Any la_returnvalues[]
Long ll_ret

ll_ret = selectsql(selectsql, la_returnvalues)

returnvalue1 = la_returnvalues[1]
returnvalue2 = la_returnvalues[2]
returnvalue3 = la_returnvalues[3]
returnvalue4 = la_returnvalues[4]
returnvalue5 = la_returnvalues[5]

return ll_ret
end function

public function long selectsql (string selectsql, ref any returnvalues[]);n_datastore ldsobj
Long ll_ret, ll_row

ldsobj = create n_datastore
ll_ret = datastorefromsql(selectsql, ldsobj)
if ll_ret = -1 then 
   destroy ldsobj
   return -1
end if
if ll_ret > 0 then
   for ll_row = 1 to Integer(ldsobj.Describe("DataWindow.Column.Count"))
      returnvalues[ll_row] = ldsobj.GetItemAny(1, ll_row)
   next
end if
destroy ldsobj 

return ll_ret
end function

public function long selectsql1 (string selectsql, ref any returnvalue);Any la_returnvalues[]
Long ll_ret

ll_ret = selectsql(selectsql, la_returnvalues)

if UpperBound(la_returnvalues) >= 1 then
   returnvalue = la_returnvalues[1]
end if

return ll_ret
end function

public function long executesql (string as_sql);String ls_error

return executesql(as_sql, ls_error)
end function

public function long readfile (string as_filename, ref string as_content);constant string METHOD_NAME = "readfile "

Long ll_FileNum,ll_ret
String ls_filetext

ll_FileNum = FileOpen(as_filename, TextMode!, Read!, LockReadWrite!,Replace!,EncodingUTF16LE!)
if ll_FileNum = -1 then
   ll_FileNum = FileOpen(as_filename, TextMode!, Read!, LockReadWrite!,Replace!,EncodingANSI!)
   if ll_FileNum = -1 then
      addError(METHOD_NAME, "Open file:" + as_filename + " failed")
      return -1
   end if
end if

ll_ret = FileReadEx(ll_FileNum, ls_filetext)
FileClose(ll_FileNum)

if ll_ret < 0 then
   addError(METHOD_NAME, "Close file:" + as_filename + " failed")
   return -1
end if

as_content = ls_filetext

return 1
end function

public function long readfile (string as_filename, ref blob ab_content);constant string METHOD_NAME = "readfile "

Long ll_FileNum,ll_ret
Blob lb_filetext

ll_FileNum = FileOpen(as_filename, StreamMode!, Read!, LockReadWrite!,Replace!,EncodingUTF16LE!)
if ll_FileNum = -1 then
   ll_FileNum = FileOpen(as_filename, StreamMode!, Read!, LockReadWrite!,Replace!,EncodingANSI!)
   if ll_FileNum = -1 then
      addError(METHOD_NAME, "Open file:" + as_filename + " failed")
      return -1
   end if
end if

ll_ret = FileReadEx(ll_FileNum, lb_filetext)
FileClose(ll_FileNum)

if ll_ret < 0 then
   addError(METHOD_NAME,  "Close file:" + as_filename + " failed")
   return -1
end if

ab_content = lb_filetext

return 1
end function

public function long writefile (string as_filename, blob ab_content);constant string METHOD_NAME = "writefile "

Long ll_filenum, ll_ret

if FileExists(as_filename) then FileDelete(as_filename)
ll_filenum = FileOpen(as_filename, StreamMode!, Write!, LockWrite!,Replace!,EncodingUTF16LE!)
if ll_filenum = -1 then return -1

ll_ret = FileWriteEx(ll_filenum, ab_content)
FileClose(ll_filenum)

if ll_ret = -1 then  
   addError(METHOD_NAME, "Close file: " + as_filename + " failed")
   return -1
end if

return 1
end function

public function long writefile (string as_filename, string ab_content);constant string METHOD_NAME = "writefile "

Long ll_filenum, ll_ret

if FileExists(as_filename) then FileDelete(as_filename)
ll_filenum = FileOpen(as_filename, LineMode!, Write!, LockWrite!,Replace!,EncodingUTF16LE!)
if ll_filenum = -1 then return -1

ll_ret = FileWriteEx(ll_filenum, ab_content)
FileClose(ll_FileNum)

if ll_ret = -1 then 
   addError(METHOD_NAME, "Close file: "  + as_filename + " failed")
   return -1
end if

return 1
end function

public function string syntaxfromsql (string as_sql, string as_presentation);constant string METHOD_NAME = "syntaxfromsql "

String ls_syntax, ls_errtext

if IsNull(as_presentation) or Trim(as_presentation) = "" then
   as_presentation = "style(type=grid)"
end if
      
ls_syntax = sqlca.SyntaxFromSQL(as_sql, as_presentation, ls_errtext)
if Len(ls_errtext) > 0 then
   addError("", ls_errtext)
end if

return ls_syntax
end function

public function string syntaxfromview (string as_viewname, string as_createviewsql, string as_presentation);constant string METHOD_NAME = "syntaxfromview "

String ls_syntax, ls_errtext, ls_sql, ls_dropviewsql
Long ll_count

// Create View
ll_count = 0
select count(*) into :ll_count
   from sys.views 
   where name = :as_viewname;
if ll_count > 0 then
   // Drop View
   ls_dropviewsql = "drop view " + as_viewname
   execute immediate :ls_dropviewsql using sqlca;
end if

execute immediate :as_createviewsql using sqlca;
if sqlca.sqlcode <> 0 then
   addError(METHOD_NAME, "Execute sql:" + as_createviewsql + " failed, " + sqlca.sqlerrtext)
   return ""
end if

// Create Datawindow Syntax
ls_sql = "select * from " + as_viewname
ls_syntax = sqlca.SyntaxFromSQL(ls_sql, as_presentation, ls_errtext)
if Len(ls_errtext) > 0 then
   rollback using sqlca;
   if Len(ls_errtext) > 0 then
      addError(METHOD_NAME, "Syntaxfromsql failed, " + ls_errtext)
   end if
   return ""
else
   commit using sqlca;
   return ls_syntax
end if
   

end function

public function any stringtoany (string as_coltype, string as_inputvalue);constant string METHOD_NAME = "stringtoany "

Any la_ret

as_coltype = Lower(as_coltype)

if Left(as_coltype, 4) = "char" then
   as_coltype = "char"
elseif Left(as_coltype, 3) = "dec" then
   as_coltype = "dec"
end if

choose case as_coltype
   case "char", "string"
      la_ret = String(as_inputvalue)
      
   case "date"
      la_ret = Date(as_inputvalue)
      
   case "datetime"
      la_ret = DateTime(as_inputvalue)
      
   case "time","timestamp"
      la_ret = Time(as_inputvalue)
      
   case "dec","real","double"
      la_ret = Dec(as_inputvalue)
      
   case "int","long","number","ulong"
      la_ret = Long(as_inputvalue)
      
   case "longlong"
      la_ret = LongLong(as_inputvalue)
   case else
      addError(METHOD_NAME, "Invalid date type:" + as_coltype)
      
end choose

return la_ret
end function

public function long executesql (string as_sql, ref string as_error);String ls_sql[]
String ls_error

ls_sql[1] = as_sql

return executesql(ls_sql, ls_error)
end function

public function long executesql (string as_sql[]);String ls_error

return executesql(as_sql, ls_error)
end function

public function long executesql (string as_sql[], ref long al_sqlnrows[], ref string as_error);Long ll_ret, ll_row

ll_ret = 1
for ll_row = 1 to UpperBound(as_sql)
   if  Lower(Left(Trim(as_sql[ll_row]), 7)) = "select " then continue
   
   execute immediate :as_sql[ll_row] using sqlca;
   
   if sqlca.sqlcode <> 0 then
      addError("", "Execute sql:" + as_sql[ll_row] + " failed, " + sqlca.sqlerrtext)
      ll_ret = -1
      exit
   end if
   
   al_sqlnrows[ll_row] = sqlca.sqlnrows
next

if ll_ret = 1 then
   commit using sqlca;
else
   rollback using sqlca;
end if

return ll_ret
end function

public function long executesql (string as_sql[], ref string as_error);Long ll_sqlnrows[]

return executesql(as_sql, ll_sqlnrows, as_error)
end function

public function long datawindowfromsql (string as_sql, datawindow a_datawindow, long mode);n_datastore ldsobj
Long ll_ret

ldsobj = create n_datastore

ll_ret = datastorefromsql(as_sql, ldsobj)

if ll_ret = 1 then
   a_datawindow.Create(ldsobj.Object.DataWindow.Syntax)
   a_datawindow.ImportString(ldsobj.Object.DataWindow.Data)
   a_datawindow.ResetUpdate()
end if

destroy ldsobj


return ll_ret
end function

public function long datawindowfromsql (string as_sql, datawindow a_datawindow);return datawindowfromsql(as_sql, a_datawindow, 1)
end function

public function integer datastorefromsql (string as_sql, ref n_datastore a_datastore);////////////////////////////////////////////////////////////////
// Description:
//    Get a datastore from select sql
// Revisions
//    1.0   - Initial version
// Arguments:  
//    as_sql - The sql string to be created by
//    a_datastore - the datastore to be created
// Returns: 
//    Number of record retrieved
//    -1 - Failure 
////////////////////////////////////////////////////////////////
// Copyright $$HEX1$$a900$$ENDHEX$$ 2000 - 2007 Youngsoft, Inc.  All rights reserved.
// Any distribution of the Youngsoft, Inc. Enterprise Application Framework
// source code in whole or part by other than Youngsoft, Inc. is prohibited.
//////////////////////////////////////////////////////////////// 

constant string METHOD_NAME = "DataStoreFromSql "

return datastorefromsql(as_sql, a_datastore, 1)
end function

public function integer datastorefromsql (string as_sql, ref n_datastore a_datastore, integer mode);////////////////////////////////////////////////////////////////
// Description:
//    Get a datastore from select sql
// Revisions
//    1.0   - Initial version
// Arguments:  
//    as_sql - The sql string to be created by
//    a_datastore - the datastore to be created
//    mode - retrieve flag (1 retrieve, <> 1 not retrieve)
// Returns: 
//    Number of record retrieved
//    -1 - Failure 
////////////////////////////////////////////////////////////////
// Copyright $$HEX1$$a900$$ENDHEX$$ 2000 - 2007 Youngsoft, Inc.  All rights reserved.
// Any distribution of the Youngsoft, Inc. Enterprise Application Framework
// source code in whole or part by other than Youngsoft, Inc. is prohibited.
//////////////////////////////////////////////////////////////// 

constant string METHOD_NAME = "DataStoreFromSql "

String ls_syntax, ls_errtext, ls_data
Long ll_row, ll_ret, ll_errorcode
Boolean lb_retrieveflag
n_cst_stringservice lnvo_string

If ib_trace Then
   inv_log.traceLog (METHOD_NAME, "")
End If   

IF IsNull(as_sql) OR Trim(as_sql) = "" THEN
   addError (METHOD_NAME, "Invalid as_sql argument")
   Return -1
END IF

IF IsValid(a_datastore) = False Then
   addError (METHOD_NAME, "Invalid a_datastore argument")
   Return -1
END IF

if not SQLCA.istransactionconnected() then
   addError (METHOD_NAME, "Datebase transaction is not connected")
   return -1
end if

// Set dialect of DBMS
if Left(Lower(sqlca.dbms), 3) = "odb" then // Supposed as ASA
   lnvo_string = create n_cst_stringservice
   as_sql = lnvo_string.globalreplace(as_sql, "dbo.", "dba.", true)
   destroy lnvo_string
end if

if mode = 1 then
   lb_retrieveflag = true
else
   lb_retrieveflag = false
end if

// Report sql
inv_log.debuglog(METHOD_NAME, inv_constants.MESSAGE_TYPE_EAF_DEBUG, "as_sql is ~r~n " + as_sql)

ls_syntax = sqlca.SyntaxFromSQL(as_sql, "style(type=grid)", ls_errtext)
IF Len(ls_errtext)>0 Then
   addError (METHOD_NAME, "SyntaxFromSql failed, " + ls_errtext)
   Return -1
END IF

// Report syntax
inv_log.debuglog(METHOD_NAME, inv_constants.MESSAGE_TYPE_EAF_DEBUG, "DataWindow syntax is ~r~n " + ls_syntax)

a_datastore.Create(ls_syntax, ls_errtext)
IF Len(ls_errtext) > 0 Then
   addError (METHOD_NAME, "Create datawindow with syntax failed, " + ls_errtext)
   Return -1
END IF

a_datastore.SetTransObject(sqlca)
if lb_retrieveflag then
   ll_ret = a_datastore.Retrieve()
else
   ll_ret = 0
end if

if ll_ret = -1 then
   a_datastore.getlastdberror(ll_errorcode, ls_errtext)
   addError(METHOD_NAME, "Retrieve error code: " + String(ll_errorcode) + " , error message: " + ls_errtext)
end if

// Report retrieve result
inv_log.debuglog(METHOD_NAME, inv_constants.MESSAGE_TYPE_EAF_DEBUG, "Retrieve result, rowcount() = " + String(ll_ret))

return ll_ret
end function

public function boolean isregexmatch (string as_text, string as_expr);//??
//#if DEFINED PBDOTNET then
//return System.Text.RegularExpressions.Regex.IsMatch(as_text, as_expr)
//#end if

return true

end function

public function integer setdropdownlistbox (powerobject adw_obj, string as_colname, string as_ddlbvalue, boolean ab_allowedit, ref string as_modifyexpression);constant string METHOD_NAME = "setdropdownlistbox"

If ib_trace Then
   inv_log.traceLog (METHOD_NAME, "")
End If   

String   ls_ret
String   ls_exp

if adw_obj.Dynamic describe(as_colname + ".Edit.Style") <> 'ddlb' then
   ls_exp = as_colname + ".ddlb.Case='Any' "
   if ab_allowedit then
      ls_exp += as_colname + ".ddlb.AllowEdit='Yes' "
   end if
   ls_exp += as_colname + ".ddlb.Limit=0 "
   ls_exp += as_colname + ".ddlb.VScrollBar='Yes' "
   ls_exp += as_colname + ".ddlb.AutoHScroll='Yes' "
end if

if Pos(as_ddlbvalue, as_colname) = 0 then
   ls_exp += as_colname + as_ddlbvalue
else
   ls_exp += as_ddlbvalue
end if

ls_ret = adw_obj.Dynamic Modify(ls_exp)

if Len(ls_ret) > 0 then
   addError(METHOD_NAME, "Set Dropdownlistbox Error:" + ls_ret + " Class:" + ClassName(adw_obj) + " Column:" + as_colname + "~r~nValue:" + as_ddlbvalue)
end if

as_modifyexpression = ls_exp

return 1
end function

public function integer setdropdownlistbox (powerobject adw_obj, string as_colname, string as_ddlbvalue, boolean ab_allowedit);String ls_ret

return setdropdownlistbox(adw_obj, as_colname, as_ddlbvalue, ab_allowedit, ls_ret)
end function

on eaf_n_cst_helperservice.create
call super::create
end on

on eaf_n_cst_helperservice.destroy
call super::destroy
end on