The Oracle Lite Blog

The Rekounas Blog about Oracle's Mobile Enterprise Application Platform

The Oracle Lite Blog header image 2

Creating publication items using the Oracle Lite API

September 19th, 2008 · 3 Comments

If you have been doing Oracle Lite development, chances are you have been either using the Packaging Wizard or the Mobile Workbench to create your applications. In some cases, you have had to use the APIs to do partition mapping or altering the publication items (I will show those in another post). I on the other hand use the APIs. Why do I do this? I started doing this because the new application I was working on was estimated to have 150+ tables. In my previous experience of using the packaging wizard, publishing over a VPN line was time consuming and prone to problems. So, when I started a new project where I knew there would be a lot of tables with a very good probability that the database model would be changing quite frequently, I decided to create my own code using the APIs to manage the repository. That repository today is over 360 tables. I am now able to use that code that I wrote for one application, and use it to maintain other applications as well. It is reusable and reliable. Reliable enough that I can have a 3rd party deploy the application without the use of the Oracle Lite SDK. The only information I need to know is “what tables are changing and what are the details of the change.” That little information requires 2 minutes or less of work from my schedule.

Here is the sample of how to create a publication item and add it to your publication. In the sample, it will also show you how to add a regular index to the publication item.


import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import oracle.lite.sync.ConsolidatorException;
import oracle.lite.sync.ConsolidatorManager;
public class Sample {
    public static void main(String arg[]) throws ConsolidatorException, 
                                                 SQLException {
        Connection con = null;                                                 
        ConsolidatorManager cm = new ConsolidatorManager();
        String MOBILE_SCHEMA   = "mobileadmin";
        String MOBILE_PASSWORD = "password";
        String JDBC_URL        = "jdbc:oracle:thin:@localhost:1521:XE";
        String PUBLICATION     = "LITE";                         
        String PUB_ITEM        = "WTGPI_MYTABLE";
        String STORE_NAME      = "MYTABLE";
        String OWNER           = "MASTER";
        try {
            DriverManager.registerDriver((Driver)(Class.forName("oracle.jdbc.OracleDriver").newInstance()));
            try {
                con = DriverManager.getConnection(JDBC_URL, MOBILE_SCHEMA, MOBILE_PASSWORD);
            } catch (SQLException sqle) {
                sqle.printStackTrace();
            }
        } catch (Exception e) {
            e.printStackTrace();
            return;
        }
        cm.openConnection(con);
        PreparedStatement stmt = null;
        // If you are using a multilingual environment, this will need to be set.
        stmt = con.prepareStatement("ALTER SESSION SET NLS_LENGTH_SEMANTICS='BYTE'"); 
        stmt.execute();
        // attempt to drop the publication item in the case that it already exists
        try {
            cm.dropPublicationItem(PUB_ITEM);
        } catch (ConsolidatorException ce){
            System.out.println("Drop failed: Publication Item does not exist!");
        }
        cm.createPublicationItem(
           PUB_ITEM,                // publication item name
           OWNER,                   // owner
           STORE_NAME,              // store
           "F",                     // refresh mode (F - Fast Refresh, C - Complete Refresh)
           "SELECT USER_ID, COL1, COL2, COL3 FROM MASTER.MYTABLE WHERE USER_ID = :USER_ID",  // select stmt
           null,                    // callback package owner
           null);                   // callback package name
        cm.addPublicationItem(
            PUBLICATION,
            PUB_ITEM,
            null, 

            "",                     // To make read-only, this value will be IUD
            "S",                    // Conflict rule (S - Server wins, C - Client Wins)
            null, 
            1,
            null,
            true, 
            false,                  // Shared map.  Set to true for shared map and make pub item read only hint: IUD"
            null);
        try {                                
            cm.createPublicationItemIndex(  "COL123_IDX",       // index name
                                            "WTGPI_MYTABLE",    // Publication item
                                            "I",                // I - Regular Index, U - Unique, P - Primary Key
                                            "COL1, COL2, COL3"  // Columns included in the index.
                                        );
            // Note that you should not have to create a primary key index as the
            // createPublicationItem method will handle this.                                        
        } catch (ConsolidatorException ce){
            System.out.println("Add publication item index failed: " + ce.getMessage());
        }
        cm.resetCache();
    }
}

To compile the code, use the following:
javac -classpath %ORACLE_HOME%/mobile/classes/consolidator.jar;%ORACLE_HOME%/jdbc/lib/ojdbc14.jar;. Sample.java

Run the code using the following:
java -classpath %ORACLE_HOME%/mobile/classes/consolidator.jar;%ORACLE_HOME%/jdbc/lib/ojdbc14.jar;. Sample

Assumptions:
You have the Oracle Lite SDK installed and you have an Java JDK 1.4 or higher installed.

Share

Tags: Java · Oracle Lite

3 responses so far ↓

  • 1 Kapil wrote on Jan 29, 2009 at 4:34 am

    Greg, Your publication program is a great help for us in creating publication through API.

    But, now we need to work on creating Synchronization program through the API. Please post some good article on Sycnchronization API also.

    thanks
    Kapil


  • 2 Marius wrote on Apr 19, 2011 at 11:53 pm

    Nice article, Greg it helped me a lot to get started. One question though: If I start adding pub items through code, will I still be able to open the project and maintain it using the Mobile Workbench? The items would not exist in the .mpj xml project file?

    Cheers,

    Marius


  • 3 rekounas wrote on Apr 20, 2011 at 4:39 am

    Hi Marius,

    I guess the point of the APIs is to better maintain your code. I only use the Workbench if I am doing something quick and not for production systems. You can mix and match, but I wouldn’t recommend it. I would also have to check if the workbench could import publication items… but I think it is able to.


Leave a Comment

*