📄 contactsyncsource.java
字号:
/*
* Funambol is a mobile platform developed by Funambol, Inc.
* Copyright (C) 2003 - 2007 Funambol, Inc.
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU Affero General Public License version 3 as published by
* the Free Software Foundation with the addition of the following permission
* added to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED
* WORK IN WHICH THE COPYRIGHT IS OWNED BY FUNAMBOL, FUNAMBOL DISCLAIMS THE
* WARRANTY OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program; if not, see http://www.gnu.org/licenses or write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301 USA.
*
* You can contact Funambol, Inc. headquarters at 643 Bair Island Road, Suite
* 305, Redwood City, CA 94063, USA, or at email address info@funambol.com.
*
* The interactive user interfaces in modified source and object code versions
* of this program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU Affero General Public License version 3.
*
* In accordance with Section 7(b) of the GNU Affero General Public License
* version 3, these Appropriate Legal Notices must retain the display of the
* "Powered by Funambol" logo. If the display of the logo is not reasonably
* feasible for technical reasons, the Appropriate Legal Notices must display
* the words "Powered by Funambol".
*/
package com.funambol.mailclient.syncml;
import com.funambol.mailclient.cm.rms.ContactEnumeration;
import com.funambol.mailclient.syncml.PimItem;
import com.funambol.mailclient.cm.Contact;
import com.funambol.mailclient.cm.ContactManager;
import com.funambol.mailclient.cm.ContactManagerFactory;
import com.funambol.mailclient.cm.ContactManagerException;
import com.funambol.mailclient.cm.ContactListFullException;
import com.funambol.mailclient.syncml.PimItemEnumeration;
import com.funambol.storage.ObjectEnumeration;
import com.funambol.storage.ObjectStore;
import com.funambol.syncml.client.BaseSyncSource;
import com.funambol.syncml.protocol.SyncML;
import com.funambol.syncml.protocol.SyncMLStatus;
import com.funambol.syncml.spds.SourceConfig;
import com.funambol.syncml.spds.SyncItem;
import com.funambol.syncml.spds.SyncException;
import com.funambol.util.Log;
import com.funambol.util.StringUtil;
import java.util.Enumeration;
import javax.microedition.rms.RecordStoreException;
/**
* An implementation of the <i>SyncSource</i> interface, used to send
* and receive email over the SyncML protocol.
*
*/
public class ContactSyncSource extends BaseSyncSource {
//--------------------------------------------------------------- Attributes
/** Reference to the contact manager */
private ContactManager cm;
/** Flag used to mark the beginning of the receiving phase */
//private boolean receiving;
//------------------------------------------------------------- Constructors
/**
* MailSyncSource constructor: initialize source config and
* init all the rest to null. The real initialization is done by
* the beginSync method.
*/
public ContactSyncSource(SourceConfig config) {
super(config);
try {
cm = ContactManagerFactory.getContactManager(ContactManager.RMS_TYPE);
} catch (RecordStoreException ex) {
ex.printStackTrace();
Log.error(this, "Cannot get RMSContactManager instance");
}
}
//----------------------------------------------------------- Public Methods
/*
* Called after SyncManager preparation and initialization just before start
* the synchronization of the SyncSource.
*
* @param syncMode the synchronization type: one of the values in
* sync4j.framework.core.AlertCode
*
* @throws SyncException in case of error. This will stop the sync process
*
public void beginSync(int syncMode) throws SyncException {
super.beginSync(syncMode); // call BaseSyncSource.beginSync()
receiving = false;
}
*/
/*
* Called just before committing the synchronization process by the
* SyncManager. The SyncSource can stop the commit phase raising an
* exception here.
*
* @throws SyncException in case of error, to stop the commit.
*
public void endSync() throws SyncException {
super.endSync(); // call BaseSyncSource.endSync()
}
*/
/**
* Add a new item from the server to the mail cm.
*/
public int addItem(SyncItem item) throws SyncException {
Log.info("New item " + item.getKey() + " from server.");
if(item.getType().equals(SourceConfig.VCARD_TYPE)) {
try {
Contact c = new Contact();
c.parse(new String(item.getContent()));
if ( StringUtil.isNullOrEmpty(c.getDefaultEmail()) ) {
Log.info("[addItem] no default email, discarding contact: " + c.getVisibleName());
globalStatus |= STATUS_RECV_ERROR;
return SyncMLStatus.GENERIC_ERROR;
}
// Add the new contact
int localkey = cm.addContact(c);
if (localkey != -1) {
String skey = Integer.toString(localkey);
item.setKey(skey);
item.setClientRepresentation(c);
return SyncMLStatus.SUCCESS;
} else {
Log.error(this, "addItem: error adding contact");
globalStatus |= STATUS_RECV_ERROR;
return SyncMLStatus.GENERIC_ERROR;
}
} catch (ContactListFullException cfe) {
// Error logged by ContactManager
throw new SyncException(SyncException.LIMIT_ERROR,
"Contact List full");
} catch (Exception ex) {
Log.error(this,"Error parsing contact: " + ex.toString());
globalStatus |= STATUS_RECV_ERROR;
return SyncMLStatus.GENERIC_ERROR;
}
} else {
Log.error("[ContactSyncSource.addItem] Unknown item type: "
+ item.getType());
return SyncMLStatus.GENERIC_ERROR;
}
}
/** Update a given SyncItem stored on the source backend */
public int updateItem(SyncItem item) {
Log.info("Updated item " + item.getKey() + " from server.");
if(item.getType().equals(SourceConfig.VCARD_TYPE)) {
// Parse the mail item
try {
Contact c = new Contact();
c.parse(new String(item.getContent()));
if (StringUtil.isNullOrEmpty(c.getDefaultEmail()) ) {
Log.info("no default email, discarding contact: " + c.getVisibleName());
globalStatus |= STATUS_RECV_ERROR;
return SyncMLStatus.GENERIC_ERROR;
}
c.setContactId(Integer.parseInt(item.getKey()));
cm.updateContact(c);
cm.removeSyncItem(item.getKey());
item.setClientRepresentation(c);
return SyncMLStatus.SUCCESS;
} catch (ContactManagerException ex) {
Log.error(this, "Cannot update item " + item.getKey());
globalStatus |= STATUS_RECV_ERROR;
return SyncMLStatus.GENERIC_ERROR;
}
} else {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -