Saturday 19 April 2008

Connecting Sage with JAVA

Connecting Sage with JAVA

It is possible to connect Sage with Java via Sage Data Objects, and a Java-COM bridge "EZ-JCom". You will need to download the SageDataObjects library from http://www.sagedataobjects.com/ and the SageForJava Library from (http://www.sagedataobjects.com/javasdo/sageforjava.zip)

After downloading and installing the SageDataObjects library, and unzipping the SageForJava Library, you will need to select the version of Sage you want to connect to, for example, version 14, Copy the File, JSDOENG140.DLL from the SDOENG140Java Folder., and place it in your Windows\System32 Folder.

Creating your Java Application

Using your preferred IDE, add a reference to the JAR file, for the selected version of sage, for example, with JCreator, Press Project > Project Settings > Required Libraries > New > Add > Add Archive. Then Select JSDOENG140.JAR

Coding:
The following code will connect to sage, and list all current customers on the screen. the code is available as part of the Java SDO download shown above.

import ezjcom.*;

public class JSage {

public static void main(String[] args)
{
SageDataObjects50.SDOEngine SDO = null;
SageDataObjects50.IWorkSpace IWS = null;
SageDataObjects50.ISalesRecord ISR = null;

try
{
// This should be the Installation location of the Sage software
String strACCData = "c:\\line50\\accdata\\";
// The default login to Sage is MANAGER
String strUsername = "MANAGER";
String strPassword = "";

// Create a new Sage Data Objects Engine object
SDO = new SageDataObjects50.SDOEngine();
SageDataObjects50.ISDOEngine ISDO = SDO.getISDOEngine();
SageDataObjects50.Workspaces WSS = ISDO.getWorkspaces();
SageDataObjects50.IWorkspaces IWSS = WSS.getIWorkspaces();

// Create a Workspace from the Engine
IWS = (SageDataObjects50.IWorkSpace)IWSS.Add("MyConnection");

// Connect to Sage
IWS.Connect(strACCData, strUsername, strPassword, "MyConnection");

// Have the Workspace get the first Sales Record
SageDataObjects50.ISDORecord ISDOR = (SageDataObjects50.ISDORecord)IWS.CreateObject("SalesRecord");

// ISDORecord is too generic, use CoerceObject to perform a 'dirty' cast.
ISR = (SageDataObjects50.ISalesRecord)ISDOR.JComCoerceObjectToAnotherType(SageDataObjects50.ISalesRecord.class);

System.out.println("Customers:");
while(true)
{
// Get the Acount_Ref field fr the customer
JComVariant jcvAccountRef = new JComVariant("Account_Ref");
SageDataObjects50.IFields ifsCustomer = ISR.getFields().getIFields();
SageDataObjects50.IField ifCustomer = ifsCustomer.Item(jcvAccountRef).getIField();
String strCustomer = ifCustomer.get_Value().getString();

// Output the Customers' Reference
System.out.println(strCustomer);

// Move to the next Customer
ISR.MoveNext();

// If end of customers, break out.
if (ISR.IsEOF()) break;
}
// Disconnect from Sage
IWS.Disconnect();
}
catch(ezjcom.JComException eException)
{
System.out.println("Exception:" + eException.toString());
}
}
}


Licensing

Code written using the SageDataObjects library is subject to a 14 day trial license, and will cease to operate after those 14 days. More details on this can be read at www.sagedataobjects.com

The Java-Com bridge is subject to a seperate license, which can be obtained at www.ezjcom.com

Thursday 17 April 2008

Sage on your website


Connecting Sage with your website


Sage Data Objects can run within an ASP.NET web page, which will allow Internet visitors interact with your Sage accountancy software.

Sage requires high levels of permissions to run from a ASP.NET website, which may lead to some security implications. Your web-server should be dedicated, and you will need either physical or remote desktop access to it.

Configuring the Administrator Account

If your Administrator account has a blank password – that is, you are not asked to enter one, when logging onto the machine. Then you will need to configure the account to allow log-ons from non console applications.

Press Start > Run > gpedit.msc

Press Computer Configuration > Windows Settings > Security Settings > Local Policies > Security Options

Click on “Account: Limit local account use of blank password to console log on only”
Select Disable

Configuring IISAdmin Service
IIS will need to be able to interact with the desktop, in order to process Sage instructions. This is achieved thus:

Press Start > Control Panel > Administrative Tools > Services
Right Click on IISAdmin, Select properties

Press Log on
Check “Allow service interact with desktop”

Restart the service

Configuring Machine.config
The ASP.NET worker process (aspnet_wp.exe) will have to run under the Administrator account also, to configure this, you will need to edit the machine.config file

Open C:\windows\Microsoft.net\Framework\v1.1.4322\Config\Machine.config in notepad

Edit the ProcessModel section to add the following
username=”Administrator”
password=””
Then Restart IIS.

Creating a Web Project

Create a new Visual Basic .NET web application project in Visual Studio,
Right Click on References > Add Reference
Select SageDataObjectComponent

Add a Button to the page, named btnConnect, and a DataGrid named dgCustomers

Click the button, and add this code:

Dim sdo50 As SageDataObject50.SageDataObjects
Dim ws50 As SageDataObject50.WorkSpace
sdo50 = New SageDataObject50.SageDataObjects
ws50 = sdo50.GetWorkSpace()
Dim strACCData As String = ConfigurationSettings.AppSettings("ACCDATA")
Dim strUsername As String = ConfigurationSettings.AppSettings("USERNAME")
Dim strPassword As String = ConfigurationSettings.AppSettings("PASSWORD")
ws50.Connect(strACCData, strUsername, strPassword, Guid.NewGuid().ToString())
Dim alCustomers As ArrayList = New ArrayList
Dim srCustomer50 As SageDataObject50.SalesRecord = _ DirectCast(ws50.CreateObject("SalesRecord"), SageDataObject50.SalesRecord)
srCustomer50.MoveFirst()
While True
alCustomers.Add(srCustomer50.Fields.Item("Account_Ref").Value())
srCustomer50.MoveNext()
If srCustomer50.IsEOF() Then Exit While
End While
dgCustomers.DataSource = alCustomers
dgCustomers.DataBind()
ws50.Disconnect()


Your Web.config will need the following values

<appsettings>
<add key="ACCDATA" value="C:\LINE50\ACCDATA\"></add>
<add key="USERNAME" value="MANAGER"></add>
<add key="PASSWORD" value=""></add>
</appsettings>