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

📄 pimitemqueue.java

📁 moblie syncml mail javame
💻 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.storage.AbstractRecordStore;
import com.funambol.storage.ObjectEnumeration;
import com.funambol.storage.ObjectFilter;
import com.funambol.storage.ObjectStore;
import com.funambol.storage.Serialized;
import com.funambol.util.Log;
import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.util.Enumeration;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;

public class PimItemQueue {
    public static final String ITEM_LIST_NAME = "SyncItemList";

    //------------------------------------------------------------- Private Data
    private ObjectStore syncItemList;
    
    //------------------------------------------------------------- Constructors
    
    /**
     * Creates a new instance of PimItemQueue
     */
    public PimItemQueue() {
        syncItemList = new ObjectStore();
        try {
            syncItemList.create(ITEM_LIST_NAME);
        } catch (RecordStoreException ex) {
            Log.error(this, ITEM_LIST_NAME + " cannot be open nor created: ");
            Log.error(ex.toString());
        }
    }
    
    //----------------------------------------------------------- Public Methods
    /**
     * Add the given sync item info to the store
     */
    public void addItemInfo(String key, char state) {
        try {
            PimItem ii = exists(key);
            
            PimItem rii = new PimItem(key, state);
            
            if (ii==null) {
                syncItemList.store(rii);
            } else {
                if (state=='D') {
                    syncItemList.store(ii.getId(), rii);
                }
            }
        } catch (RecordStoreException ex) {
            Log.error(this, ex.toString());
            ex.printStackTrace();
        } catch (IOException ex) {
            Log.error(this, ex.toString());
            ex.printStackTrace();
        }
    } 
    
    /**
     * Initialize item list
     */
    public void resetPimItemQueue() {
        try {
            syncItemList.close();
            AbstractRecordStore.deleteRecordStore(ITEM_LIST_NAME);
            syncItemList.create(ITEM_LIST_NAME);
            syncItemList.open(ITEM_LIST_NAME);
        } catch (RecordStoreException ex) {
            ex.printStackTrace();
            Log.error(this, "Recordstore Exception in resetPimItemQueue() method");
        }
    }
    
    /**
     * remove the given sync item info from the store
     */
    public void removeItemInfo(String key) {
        try {
            PimItem sii = exists(key);
            if (sii!=null) {
                syncItemList.remove(sii.getId());
            }
        } catch (RecordStoreException ex) {
            Log.error("Item " + key + " cannot be removed");
            Log.error(ex.toString());
        } 
    } 
    
    /**
     * get sync item key and state
     */
    public PimItem getItemInfo(String key) {
        PimItem sii = exists(key);
    
        //avoid nullpointer 
        if (sii == null)  {
            return null;
        }
        
        try {
            return (PimItem) 
                         syncItemList.retrieve(sii.getId(), new PimItem());
        } catch (IOException ex) {
            ex.printStackTrace();
            Log.error("Cannot retrieve item " + sii.getKey() + ":");
        } catch (RecordStoreException ex) {
            Log.error(ex.toString());
            ex.printStackTrace();
        }
        return null;
    }
    
    /**
     * get items of a certain state
     */
    public Enumeration getItemInfos(char state) {
        syncItemList.setObjectFilter(new ItemFilter(" ", state));
        PimItemEnumeration sie = new PimItemEnumeration(
                   (ObjectEnumeration) syncItemList.getObjects(new PimItem()));
        syncItemList.removeObjectFilter();
        return sie;
    }
    
    
    
    private PimItem exists(String key) {
        syncItemList.setObjectFilter(
                              new ItemFilter(key, ' '));
        Enumeration e = syncItemList.getObjects(new PimItem());
        PimItemEnumeration sie = 
                                 new PimItemEnumeration((ObjectEnumeration) e);
        if (sie.hasMoreElements()) {
            PimItem rii = (PimItem) sie.nextElement();
            syncItemList.removeObjectFilter();
            return rii; 
        }
        syncItemList.removeObjectFilter();
        return null;
    }
    
    class ItemFilter implements ObjectFilter {
        char state;
        String key;
        public ItemFilter(String key, char state) {
            this.state = state;
            this.key = key;
        } 
        public boolean matches(byte[] b) {
            char found = ' ';
            ByteArrayInputStream bais = new ByteArrayInputStream(b);
            DataInputStream dis = new DataInputStream(bais);
            boolean ret = false;
            if (state!=found) {
                try {
                    dis.readUTF();
                    found = dis.readChar();
                    ret = (found==state);
                } catch (IOException ex) {
                    Log.error(this, "Cannot read record:");
                    Log.error(this, ex.toString());
                    ex.printStackTrace();
                }
            } else {
                try {
                    String actualKey = dis.readUTF();
                    ret = (actualKey.equals(key));
                } catch (IOException ex) {
                    ex.printStackTrace();
                    Log.error(this, "Cannot read record:");
                    Log.error(this, ex.toString());
                }
            }
            return ret;
        }
    }
}

⌨️ 快捷键说明

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