⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 basesyncsource.java

📁 j2me java syncml moblie
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 * 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.syncml.client;

import com.funambol.syncml.spds.SourceConfig;
import com.funambol.syncml.spds.SyncItem;
import com.funambol.syncml.spds.SyncSource;
import com.funambol.syncml.spds.SyncException;

import com.funambol.syncml.protocol.SyncFilter;
import com.funambol.syncml.protocol.SyncML;

import com.funambol.util.Log;
import com.funambol.util.SyncListener;

/**
 * An abstract implementation of the <i>SyncSource</i> interface, providing
 * the basic framework each SyncSource has to implement. 
 * A developer can choose to extends BaseSyncSource or to implements
 * SyncSource directly if needed.
 *
 * The class BaseSyncSource uses the SyncConfig to store the source 
 * configuration data. With this class is possible to alter the source
 * configuration, which is not permitted by the SyncSource interface.
 */
public abstract class BaseSyncSource implements SyncSource {
    
    //--------------------------------------------------------------- Attributes

    /** SyncSource configuration */
    protected SourceConfig config;

    /** Synchronization filter */
    protected SyncFilter filter;

    /** SyncMode, set by beginSync */
    protected int syncMode;

    // Item lists
    protected SyncItem[] allItems, newItems, updItems, delItems;

    // Lists counters
    protected int allIndex, newIndex, updIndex, delIndex;

    /** The number of items to be sent to the server in the session */
    private int clientItemsNumber;
    
    /** The number of items that the server announced to send in the session */
    private int serverItemsNumber;

    /** The number of new items to be sent to the server in the session */
    private int clientAddItemsNumber;

    /** The number of replaced items to be sent to the server in the session */
    private int clientReplaceItemsNumber;

    /** The number of deleted items to be sent to the server in the session */
    private int clientDeleteItemsNumber;

    /** Status of the sync source summarized in an integer value. See constants
     * defined in SyncSource */
    protected int globalStatus;

    /** Listener of the sync process */
    private SyncListener listener;

    //------------------------------------------------------------- Constructors

    /**
     * BaseSyncSource constructor: initialize source config
     */
    public BaseSyncSource(SourceConfig config) {
        this.config = config;
        
        syncMode = 0;
        
        // Init lists (empty)
        allItems = null;
        newItems = null;
        updItems = null;
        delItems = null;

        // Init counters
        allIndex = newIndex = updIndex = delIndex = 0;

        // Init number of chages counters
        clientItemsNumber = serverItemsNumber = -1;
        clientAddItemsNumber = -1;
        clientReplaceItemsNumber = -1;
        clientDeleteItemsNumber = -1;

        filter = null;
    }

    //----------------------------------------------------------- Public Methods

    /**
     * Returns the config of the source. The client can use this method
     * to obtain the config object and change some parameter. A setConfig()
     * must be called to actually change the source configuration.
     *
     * @return the config of the source
     */
    public SourceConfig getConfig() {
        return config;
    }

    /**
     * Sets the config of the source. The client can use this method
     * to change the config of the source configuration.
     * This operation should not be done while the sync is in progress.
     *
     */
    public void setConfig(SourceConfig config) {
        this.config = config;
    }
    
    //------------------------------------------------ SyncSource implementation

    /**
     * Returns the name of the source
     *
     * @return the name of the source
     */
    public String getName() {
        return config.getName();
    }

    /**
     * Returns the source URI
     *
     * @return the absolute URI of the source
     */
    public String getSourceUri() {
        return config.getRemoteUri();
    }

    /**
     * Returns the type of the source.
     * The types are defined as mime-types, for instance * text/x-vcard).
     * @return the type of the source
     */
    public String getType() {
        return config.getType();
    }

    /**
     * Returns the encoding of the source.
     * The encoding can be 'b64' or 'none' only. The standard defines
     * also 'des' and '3des' but they are not implemented in this version
     * of the APIs.
     *
     * @return the encoding of the source
     */
    public String getEncoding() {
        return config.getEncoding();
    }

    /**
     * Returns the preferred sync mode of the source.
     * The preferred sync mode is the one that the SyncManager sends
     * to the server in the initialization phase. The server can respond
     * with a different alert code, to force, for instance, a slow.
     *
     * @return the preferred sync mode for this source
     */
    public int getSyncMode() {
        return config.getSyncMode();
    }

    /**
     * Returns the current filter for this SyncSource.
     */
    public SyncFilter getFilter() {
        return filter;
    }

    /**
     * Set a new filter for this SyncSource
     */
    public void setFilter(SyncFilter filter) {
        this.filter = filter;
    }

    /**
     * Add a new SyncItem to this source backend.
     * The item key after a successful add must contain the local UID,
     * that is used by the engine to send the mappings to the server.
     * The source must then change the item key accordingly before return.
     *
     * @param item the SyncItem to add, with the GUID sent by the server.
     *             The source is resposible to set it to the LUID before
     *             returning a successful status code.
     *
     * @return the status code of the operation. It will be returned to
     *         the server in the response for this item.
     *
     * @throws SyncException if an unrecoverable error occur, to stop the sync
     */
    public abstract int addItem(SyncItem item) throws SyncException ;
    
    /**
     * Update a given SyncItem stored in the source backend.
     *
     * @param item the SyncItem to update. The key of the item is already
     *             the LUID.
     *
     * @return the status code of the operation. It will be returned to
     *         the server in the response for this item.
     *
     * @throws SyncException if an unrecoverable error occur, to stop the sync
     */
    public abstract int updateItem(SyncItem item) throws SyncException ;
    
    /**
     * Delete a SyncItem stored in the source backend.
     *
     * @param key The key of the item to delete.
     *
     * @return the status code of the operation. It will be returned to
     *         the server in the response for this item.
     *
     * @throws SyncException if an unrecoverable error occur, to stop the sync
     */
    public abstract int deleteItem(String key) throws SyncException ;
    
    /** 
     * Returns the next item of the store.
     **/
    public SyncItem getNextItem() throws SyncException {
        if (allItems == null) {
            Log.info("Source "+getName()+": no items to send for slow sync");
            return null;
        }
        
        if (allIndex<allItems.length) {
            Log.info("Source "+getName()+": sending item "
                     +allItems[allIndex].getKey());
            return getItemContent(allItems[allIndex++]);
        }
        else {
            Log.info("Source "+getName()+": no more items to send for slow sync");
            // All Items sent, we can free memory
            allItems = null;
            allIndex = 0;
            return null;
        }
    }
    
    /**
     * Returns the next new item of the store
     * (not yet sent to the server)
     */
    public SyncItem getNextNewItem() throws SyncException {
        if (newItems == null) {
            Log.info("Source "+getName()+": no new items to send");
            return null;
        }
        
        if (newIndex<newItems.length) {
            Log.info("Source "+getName()+": sending item "
                     +newItems[newIndex].getKey());
            return getItemContent(newItems[newIndex++]);
        }
        else {
            Log.info("Source "+getName()+": no more new items to send");
            // All Items sent, we can free memory
            newItems = null;
            newIndex = 0;
            return null;
        }
    }
    
    
    /**
     * Returns the first/next updated item of the store
     * (changed from the last sync)
     */
    public SyncItem getNextUpdatedItem() throws SyncException {
        if (updItems == null) {
            Log.info("Source "+getName()+": no updated items to send");
            return null;
        }
        
        if (updIndex<updItems.length) {
            Log.info("Source "+getName()+": sending item "
                     +updItems[updIndex].getKey());
            return getItemContent(updItems[updIndex++]);
        }
        else {
            Log.info("Source "+getName()+": no more updated items to send");
            // All Items sent, we can free memory
            updItems = null;
            updIndex = 0;
            return null;
        }
    }
    
    /** 
     * Returns a SyncItem containing the key of the first/next
     * deleted item of the store (locally removed after the last sync,
     * but not yet deleted on server)
     */
    public SyncItem getNextDeletedItem() throws SyncException {
        if (delItems == null) {
            Log.info("Source "+getName()+": no deleted items to send");
            return null;
        }
        
        if (delIndex<delItems.length) {
            Log.info("Source "+getName()+": sending item "

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -