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

📄 contactsyncsource.java

📁 moblie syncml mail javame
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
            //Log.info("[ContactSyncSource.updateItem] Unexpected item type"+ item.getType());
            Log.error(this, "[updateItem] Unexpected item type"
                    + item.getType());
            
            globalStatus |= STATUS_RECV_ERROR;
            return SyncMLStatus.GENERIC_ERROR;
        }
    }
    
    /** Delete a SyncItem stored on the related Items list */
    public int deleteItem(String key) {
        Log.info("Delete from server for item " + key);
        
        try {
            cm.removeContact(Integer.parseInt(key));
            cm.removeSyncItem(key);
            return SyncMLStatus.SUCCESS;
        } catch (ContactManagerException cme) {
            Log.error(this, "Error removing contact: " + key + " " + cme.toString());
            globalStatus |= STATUS_RECV_ERROR;
            return SyncMLStatus.GENERIC_ERROR;
        }
    }
    
    /**
     * Tell the SyncSource the status returned by the server
     * for an Item previously sent.
     *
     * @param key the key of the item
     * @param status the status code received for that item
     *
     * @throws SyncException if the SyncSource wants to stop the sync
     */
    public void setItemStatus(String key, int status)
    throws SyncException {
        Log.info("Status " + status + " for item " + key + " from server.");
        if (SyncMLStatus.isSuccess(status)) {
            cm.removeSyncItem(key);
        }
    }
    
    /**
     * Send incomingMsgNumber event when the engine announce the number of
     * changes: it is the first Sync tag from server.
     *
     * public void setServerItemsNumber(int number) {
     * super.setServerItemsNumber(number);
     * receiving = true;
     * }
     */
    
    //------------------------------------------------------- Protected methods
    
    /**
     * Init the allItems list with contacts present in the client storage
     */
    protected void initAllItems() throws SyncException {
        Enumeration list = null;
        
        try {
            list =  cm.getContactList();
        } catch (ContactManagerException ex) {
            Log.error("Error retrieving contatc list");
            Log.error("Contact.initAllItems: " + ex.toString());
            // TODO: should this throws an exception? I guess so...
        }
        
        allItems = new SyncItem[cm.getContactCount()];
        int i=0;
        while (list.hasMoreElements()) {
            Contact c = (Contact) list.nextElement();
            allItems[i] = new SyncItem(
                    String.valueOf(c.getContactId()),
                    SourceConfig.VCARD_TYPE,
                    SyncItem.STATE_UPDATED,
                    null,
                    null
                    );
            i++;
        }
        
    }
    
    /**
     * Init the newItems list used in the fast sync.
     */
    protected void initNewItems() throws SyncException {
        PimItemEnumeration list =
                (PimItemEnumeration) cm.getSyncItems(SyncItem.STATE_NEW);
        
        newItems = new SyncItem[list.getSize()];
        
        // TODO: why not using a while loop?
        // yes refactor with while and then extract method for all the initXXXItems
        for (int i=0; i<list.getSize(); i++) {
            if (list.hasMoreElements()) {
                PimItem sii = (PimItem) list.nextElement();
                newItems[i] = new SyncItem(
                        sii.getKey(),
                        SourceConfig.VCARD_TYPE,
                        SyncItem.STATE_NEW,
                        null,
                        null
                        );
            }
        }
        
    }
    
    /**
     * Init the updItems list used in the fast sync.
     */
    protected void initUpdItems() throws SyncException {
        PimItemEnumeration list =
                (PimItemEnumeration) cm.getSyncItems(SyncItem.STATE_UPDATED);
        
        updItems = new SyncItem[list.getSize()];
        // TODO: why not using a while loop?
        for (int i=0; i<list.getSize(); i++) {
            if (list.hasMoreElements()) {
                PimItem sii = (PimItem) list.nextElement();
                updItems[i] = new SyncItem(
                        sii.getKey(),
                        SourceConfig.VCARD_TYPE,
                        SyncItem.STATE_UPDATED,
                        null,
                        null
                        );
            }
        }
        
    }
    
    /**
     * Init the delItems list used in the fast sync
     */
    protected void initDelItems() throws SyncException {
        PimItemEnumeration list =
                (PimItemEnumeration) cm.getSyncItems(SyncItem.STATE_DELETED);
        
        delItems = new SyncItem[list.getSize()];
        // TODO: why not using a while loop?
        for (int i=0; i<list.getSize(); i++) {
            if (list.hasMoreElements()) {
                PimItem sii = (PimItem) list.nextElement();
                delItems[i] = new SyncItem(
                        sii.getKey(),
                        SourceConfig.VCARD_TYPE,
                        SyncItem.STATE_DELETED,
                        null,
                        null
                        );
            }
        }
        
    }
    
    /**
     * This function gets the item content in the backend database and
     * returns a complete item. The parameter item is marked final because
     * should not be used for the filled item: it is a reference to the
     * array entry, and filling it would cause the array to keep all the
     * filled items at the end (the gc will not dispose them). <p>
     * The content of the item depends also from the encoding of this
     * SyncSource:
     * <li> if the encoding is <i>none</i>, it must be a String, converted
     *      with getBytes(), so the engine will send it unchanged.
     * <li> if the encoding is <i>b64</i>, the content can be binary, and the
     *      type should be set accordingly, so that the receiving source
     *      can handle it. In this way, the binary content is transferred
     *      encoded in the SyncML message. This encoding can be applied to
     *      a text item too, to avoid problems with charset or other, like
     *      what is done with the SIF format.
     */
    protected SyncItem getItemContent(final SyncItem item) throws SyncException {
        Log.debug("Current Item State: " + item.getState());
        SyncItem ret = new SyncItem(item);
        Contact c = null;
        try {
            c = cm.getContact(Integer.parseInt(item.getKey()));
            ret.setContent(c.format().getBytes());
        } catch (NumberFormatException ex) {
            Log.error("Error Accessing contact: " + c.getContactId());
            Log.error(ex.toString());
            ex.printStackTrace();
            return item;
        } catch (ContactManagerException ex) {
            Log.error("Error Accessing contact; " + c.getContactId());
            ex.printStackTrace();
            return item;
        }
        return ret;
    }
    
}

⌨️ 快捷键说明

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