/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package mypackage;
import com.funambol.storage.NamedObjectStore;
import com.funambol.syncml.protocol.SyncML;
import com.funambol.syncml.spds.SourceConfig;
import com.funambol.syncml.spds.SyncConfig;
import com.funambol.syncml.spds.SyncManager;
import com.funambol.syncml.spds.SyncSource;
import com.funambol.util.Log;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.midlet.MIDlet;
/**
*
* @author ali hassan
*/
public class MainMIDlet extends MIDlet implements CommandListener, Runnable{
private SourceConfig sc;
private SyncConfig conf;
private ContactSync cs ;
private Display display;
private Form mainFrm;
private Command cmd_exit;
private Command cmd_sync;
public MainMIDlet() {
Log.setLogLevel(Log.DEBUG);
sc = new SourceConfig();
conf = new SyncConfig();
NamedObjectStore store = new NamedObjectStore();
try{
store.create("DB");
sc.setRemoteUri("card");
sc.setType("text/x-vcard");
sc.setEncoding(SyncSource.ENCODING_B64);
conf.syncUrl = "http://myserver/funambol/ds";
conf.userName="username";
conf.password="password";
conf.compress=true;
conf.userAgent="NokiaN70";
store.store(SyncConfig.NAME, conf);
if(store.lookup("card")==0)
store.store("card", sc);
else
store.retrieve("card", sc);
}catch(Exception ex){
ex.printStackTrace();
}
mainFrm = new Form("MyForm");
this.cmd_exit = new Command("Exit", Command.EXIT, 0);
this.cmd_sync = new Command("Sync", Command.OK, 1);
mainFrm.addCommand(cmd_exit);
mainFrm.addCommand(cmd_sync);
mainFrm.setCommandListener(this);
display = Display.getDisplay(this);
}
protected void startApp(){
display.setCurrent(mainFrm);
}
protected void pauseApp() {
}
protected void destroyApp(boolean arg0){
}
public void commandAction(Command c, Displayable d) {
if(c==cmd_exit)
notifyDestroyed();
else if(c==cmd_sync)
{
new Thread(this).start();
}
}
public void run()
{
NamedObjectStore os = new NamedObjectStore();
try{
os.open("DB");
os.retrieve("card", sc);
os.retrieve(SyncConfig.NAME, conf);
}catch(Exception ex){
ex.printStackTrace();
}
SyncManager sm = new SyncManager(conf);
cs = new ContactSync(sc);
//Log.info(cs.+ "------------------------------");
try{
Log.info("Last Anchor Before Sync: "+sc.getLastAnchor());
sm.sync(cs, SyncML.ALERT_CODE_FAST);
Log.info("Last Anchor After Sync: "+sc.getLastAnchor());
os.store("card", sc);
os.close();
}catch(Exception ex){
ex.printStackTrace();
}
show();
}
private void show()
{
/*MyContacts mc = new MyContacts();
SyncItem si = cs.getNextItem();
Log.info("--------------------"+si.getType());*/
}
}
second step is:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package vopium;
import com.funambol.syncml.client.BaseSyncSource;
import com.funambol.syncml.spds.SourceConfig;
import com.funambol.syncml.spds.SyncException;
import com.funambol.syncml.spds.SyncItem;
import com.funambol.util.Log;
/**
*
* @author ali hassan
*/
public class ContactSync extends BaseSyncSource{
public ContactSync(SourceConfig config) {
super(config);
}
public int addItem(SyncItem item) throws SyncException {
Log.info(item.getKey()+"--------------------------------------------"+new String(item.getContent()));
if (item.getType().equals(SourceConfig.VCARD_TYPE)) {
try{
MyContacts c = new MyContacts();
c.parse( new String(item.getContent()));
RMSContactManager cm = new RMSContactManager();
item.setKey(""+cm.addContactToRMS(c));
}catch(Exception ex)
{
ex.printStackTrace();
}
}
return 200;
}
public int updateItem(SyncItem item) throws SyncException {
Log.info("update");
return 200;
}
public int deleteItem(String key) throws SyncException {
Log.info("del");
return 200;
}
protected void initAllItems() throws SyncException {
Log.info("init All");
}
protected void initNewItems() throws SyncException {
Log.info("init New");
}
protected void initUpdItems() throws SyncException {
Log.info("init Update");
}
protected void initDelItems() throws SyncException {
Log.info("init Del");
}
protected SyncItem getItemContent(SyncItem item) throws SyncException {
Log.info("getItemContent");
return new SyncItem(item);
}
public void beginSync(int syncMode)
{
super.beginSync(syncMode);
}
}
Above class is responsible for the object serialization. Like hibernate.
we are using funambol J2ME API for this purpose.