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

No comments: