/*
* ApiSubscriberSample.java
*
* This is a sample program for usage of API for Management Console.
* To use the sample program, change the parameters in run() method, put
* the agent name list on the command line:
*
* java -cp .;api.jar ApiSubscriberSample [agentname1 [agentname2...]]
*
* The output file names will be each agent name.
*
*/
import com.datamirror.ea.api.*;
import com.datamirror.ea.api.subscriber.*;
public class ApiSubscriberSample
{
java.io.PrintStream out;
String tab;
public static void main(String[] args)
{
ApiSubscriberSample sample = new ApiSubscriberSample();
sample.run(args);
System.out.println(Done!
);
}
/*
* Connect to the Access Server and get the Agent list.
*/
private void run(String[] args)
{
try
{
/*
* User, Password, Hostname and Port are parameters needed to connect
* the Access Server. You should change them to your correct parameters.
*/
DefaultContext c = new DefaultContext();
c.setString(DataSource.User, gsmith
);
c.setString(DataSource.Password, cricket
);
c.setString(DataSource.Hostname, localhost
);
c.setInt(DataSource.Port, 10101);
DataSource dataSource = Toolkit.getDefaultToolkit().createDataSource();
dataSource.connect(c);
if (dataSource.isOpen())
{
for (int i = 0; i < args.length; i ++)
{
Subscriber subscriber = matchSubscriber(dataSource, args[i]);
if (subscriber != null)
{
out = new java.io.PrintStream(new java.io.FileOutputStream(subscriber.getName()));
testSubscriber(subscriber);
System.out.println(\nThe output filename is:
+ subscriber.getName() + \n
);
}
}
}
dataSource.close();
}
catch(Exception e)
{
e.printStackTrace(System.out);
}
}
/*
* Find the Subscriber according to its name.
*/
private Subscriber matchSubscriber(DataSource ds, String name)
{
Subscriber[] subscribers = ds.getSubscribers();
for (int i = 0; i < subscribers.length; i ++)
{
if (name.equals(subscribers[i].getName()))
{
return subscribers[i];
}
}
return null;
}
/*
* Do Subscriber level test.
*/
private void testSubscriber(Subscriber subscriber) throws ApiException
{
String[] pubNames;
Publication publication;
subscriber.connect();
System.out.println(Test Subscriber:
+ subscriber.getName());
out.println(--Agent--
);
out.println(subscriber.getName()+ (
+ subscriber.getDBPlatform() +
+ subscriber.getVersion() + on
+ subscriber.getHostName() +
+ subscriber.getOSPlatform() + )
);
/* Create target table SAMPLETABLE in master database under owner dbo. The
* table has columns: COL1, COL2, COL3. Column COL2 is the key. Change the
* parameters to suit your need.
DBColumn[] columns = new DBColumn[3];
columns[0] = new DefaultDBColumn(COL1
, decimal
, false, 10, 2, true);
columns[1] = new DefaultDBColumn(COL2
, decimal
, true, 4, 0, false);
columns[2] = new DefaultDBColumn(COL3
, varchar
, false, 24, 0, true);
String[] keys = {COL2
};
subscriber.createTable(master
, dbo
, SAMPLETABLE
, columns, keys, COL2
, );
*/
pubNames = subscriber.getPublicationNames();
for(int i = 0; i < pubNames.length; i++)
{
publication = subscriber.getPublication(pubNames[i]);
testPublication(subscriber, publication);
}
return;
}
/*
* Do Publication level test.
*/
private void testPublication(Subscriber subscriber,
Publication publication) throws ApiException
{
String[] pTableNames;
DBPath[] paths;
PublishedTable pTable;
System.out.println( Test Publication:
+ publication.getName());
tab =
;
out.println();
out.println(tab + --Publication--
);
out.println(tab + publication.getName() + (
+
publication.getDescription() + )
);
paths = publication.getDBPaths();
tab =
;
for(int i = 0; i < paths.length; i++)
{
out.println(tab + --Owner--
);
out.println(tab + paths[i].getFullName());
pTableNames = publication.getPublishedTableNames(paths[i]);
for(int j = 0; j < pTableNames.length; j++)
{
pTable = publication.getPublishedTable(paths[i], pTableNames[j]);
testTable(publication, pTable);
}
}
return;
}
/*
* Do table level test including Row Identifier and Member Identifier.
*/
private void testTable(Publication publication,
PublishedTable pTable) throws ApiException
{
TableAssignment ta;
DBTable destinationTable;
DBColumn column;
ColumnAssignment ca;
System.out.println( Test table:
+ pTable.getName());
tab =
;
if(pTable.isAssigned())
{
ta = pTable.getTableAssignment();
destinationTable = ta.getDestinedTable();
out.println(tab + --Table--
);
out.println(tab + pTable.getName() + ==>
+
destinationTable.getName());
/* Set Row ID to REPNO < 600
for the published table
CUSTOMER
under the
* publication API
. Change the parameters to suit
your configuration.
if(CUSTOMER
.equals(pTable.getName()) &&
API4
.equals(publication.getName()))
{
ta.setRowIDExpression(REPNO < 600
);
}
*/
tab =
;
String rowID = ta.getRowIDExpression();
if(rowID != null && rowID.length() != 0)
{
out.println(tab + --RowID--
);
out.println(tab + (rowID = + rowID +
)
);
}
// Member Id
String[] memberNames = ta.getMemberNames();
for(int i = 0; i < memberNames.length; i++)
{
out.println(tab + --MemberID--
);
out.println(tab + (member = +
memberNames[i] +
:
+ ta.getMemberIdentifier(memberNames[i]) + )
);
}
// UserExit
testUserExit(publication, pTable, ta);
// Column mapping
String[] dColNames = ta.getDestinedColumnNames();
out.println(tab + --Column--
);
for(int i = 0; i < dColNames.length; i++)
{
ca = ta.getColumnAssignment(dColNames[i]);
testColumnAssignment(publication, pTable, ca);
}
}
else // Table is not assigned
{
String[] pColNames = pTable.getColumnNames();
tab =
;
out.println(tab + --Table--
);
out.println(tab + pTable.getName() + ==> ?
);
tab =
;
out.println(tab + --Column--
);
for(int i = 0; i < pColNames.length; i++)
{
column = pTable.getColumn(pColNames[i]);
out.println(tab + column.getName());
}
/* Assign the published table PRODUCT
under
publication API4
to destination
* table CUST10K
owned by dbo
in the master
database. Change the parameters
* to suit your configuration.
if(PRODUCT
.equals(pTable.getName()) &&
API4
.equals(publication.getName()))
{
pTable.assign(CUST10K
, dbo
, master
);
}
*/
}
return;
}
/*
* Do column level mapping test.
*/
private void testColumnAssignment(Publication publication,
PublishedTable pTable, ColumnAssignment ca)
throws ApiException
{
String temp = null;
String[] valueTrans;
PublishedDBColumn pColumn = ca.getPublishedColumn();
/* Do column mapping and set value translation for the
published table PRODUCT
that
* has been assigned to a destination table.
* Map the first column of destination table to a
constant APPLE
.
* Map the second column of the destination table to
a published column named PRICE
.
* Map the third column of the destination table to a
derived expression.
* Add a value translation to the mapping of the
fourth column.
* Change the parameters to suit your configuration.
if(PRODUCT
.equals(pTable.getName()) &&
API4
.equals(publication.getName()))
{
for(int i = 0; i < colNames.length; i++)
{
ca = ta.getColumnAssignment(colNames[i]);
if (i == 1)
ca.mapTo(ColumnAssignment.MAP_CONSTANT, APPLE
);
if (i == 2)
ca.mapTo(ColumnAssignment.MAP_PUBLISHED_COLUMN, PRICE
);
if (i == 3)
ca.mapTo(ColumnAssignment.MAP_DERIVED_EXPRESSION,
UNITPRICE + 10
);
if (i == 4)
ca.addValueTranslation(ON
, Ontario
);
}
}
*/
switch(ca.getAssignmentType())
{
case ColumnAssignment.MAP_PUBLISHED_COLUMN:
if (pColumn.getDescription() != null &&
!pColumn.getDescription().equals(pColumn.getName()))
temp = pColumn.getName() + (
+ pColumn.getDescription() + )
;
else
temp = pColumn.getName() ;
break;
case ColumnAssignment.MAP_CONSTANT:
temp = (Constant)
+ ca.getConstantValue();
break;
case ColumnAssignment.MAP_ZERO:
temp = (zero)
;
break;
case ColumnAssignment.MAP_DERIVED_EXPRESSION:
temp = (Derived_Expression)
+ ca.getDerivedExpression();
break;
case ColumnAssignment.MAP_BLANK:
temp = (blank)
;
break;
case ColumnAssignment.MAP_NULL:
temp = (null)
;
break;
case ColumnAssignment.MAP_CURRENT_DATE:
temp = (curDate)
;
break;
case ColumnAssignment.MAP_DATABASE_DEFAULT:
temp = (DBdefault)
;
}
tab =
;
out.println(tab + temp + ->
+ ca.getDestinedColumn().getName());
if(ca.hasValueTranslation())
{
tab =
;
out.println(tab + --Value_Translation--
);
valueTrans = ca.getValueTranslationNames();
for( int i = 0; i < valueTrans.length; i++)
out.println(tab + valueTrans[i] + .x.
+ ca.getValueTranslationValue(valueTrans[i]));
}
return;
}
/*
* Test User Exit.
*/
private void testUserExit(Publication publication,
PublishedTable pTable, TableAssignment ta)
throws ApiException
{
/* Set User Exit for the published table CUSTOMER
under publication API4
.
* Create a C/C++ DLL user exit, the program is located at /gsmith/home/lib/
,
* Add operation function3
at BEFORE_DELETE entry point, and clearme
at
* AFTER_CLEAR entry point. Disable STANDARD_DELETE and STANDARD_UPDATE TS
* operation. Change the parameters to suit your configuration.
if(CUSTOMER
.equals(pTable.getName()) && API4
.equals(publication.getName()))
{
UserExit nue = ta.createNewUserExit(UserExit.C_CPP_DLL);
nue.setFunctionPath(/gsmith/home/lib/
);
nue.addOperation(UserExit.BEFORE_DELETE, function3
);
nue.addOperation(UserExit.AFTER_CLEAR, clearme
);
nue.setStdOperation(UserExit.STANDARD_DELETE, UserExit.DISABLE);
nue.setStdOperation(UserExit.STANDARD_UPDATE, UserExit.DISABLE);
ta.setUserExit(nue);
}
*/
UserExit ue = ta.getUserExit();
if(ue != null)
{
String type = null;
switch(ue.getFunctionType())
{
case UserExit.STANDARD_FUNCTION:
type = Standard Function
;
break;
case UserExit.C_CPP_DLL:
type = C/C++ Dll
;
break;
case UserExit.IDISPATCH_COM_DLL:
type = COM
;
break;
case UserExit.STORED_PROCEDURE:
type = Stored procedure
;
break;
case UserExit.JAVA_CLASS:
type = Java class
;
}
out.println(tab + --User Exit--
);
out.print(tab + (type:
+ type + path:
+ ue.getFunctionPath() +
entry_point:<
);
int[] ep = ue.getOperationEntryPoints();
for(int i = 0; i < ep.length; i++)
out.print(matchEntryPoint(ep[i]) + :
+
ue.getOperation(ep[i]) +
);
out.println(>)
);
}
return;
}
private String matchEntryPoint(int ep)
{
String result = ;
switch(ep)
{
case UserExit.BEFORE_CLEAR:
result = BEFORE_CLEAR
;
break;
case UserExit.AFTER_CLEAR:
result = AFTER_CLEAR
;
break;
case UserExit.BEFORE_DELETE:
result = BEFORE_DELETE
;
break;
case UserExit.AFTER_DELETE:
result = AFTER_DELETE
;
break;
case UserExit.BEFORE_INSERT:
result = BEFORE_INSERT
;
break;
case UserExit.AFTER_INSERT:
result = AFTER_INSERT
;
break;
case UserExit.BEFORE_UPDATE:
result = BEFORE_UPDATE
;
break;
case UserExit.AFTER_UPDATE:
result = AFTER_UPDATE
;
break;
case UserExit.BEFORE_REFRESH:
result = BEFORE_REFRESH
;
break;
case UserExit.AFTER_REFRESH:
result = AFTER_REFRESH
;
break;
case UserExit.BEFORE_CONNECT_DISCONNECT:
result = BEFORE_CONNECT_DISCONNECT
;
break;
case UserExit.AFTER_CONNECT_DISCONNECT:
result = AFTER_CONNECT_DISCONNECT
;
break;
}
return result;
}
}