FileDocCategorySizeDatePackage
PIMDatabase.javaAPI DocphoneME MR2 API (J2ME)15058Wed May 02 18:00:28 BST 2007com.sun.kvem.midp.pim

PIMDatabase

public class PIMDatabase extends Object
Handles reading and writing PIM data.

Fields Summary
private static SecurityToken
classSecurityToken
Security token to allow access to implementation APIs
private String
dir
Name of root dir.
private Hashtable
categoriesMap
Hashtable of categories.
private final String
fileSep
File separator symbol.
private static final String[]
DIRECTORIES
Directory structure of lists.
String[]
LISTS
Default names of lists.
Constructors Summary
public PIMDatabase()
Default constructor.


           
      
    
public PIMDatabase(String inpDir)
Constructor for creating a database object for the given path.

param
inpDir path of root directory
throws
IOException if an error occurs accessing the file

	this.dir = inpDir;
	String fcPrefix = "file:";
	if (dir.startsWith(fcPrefix)) { // remove file prefix
	    dir = dir.substring(fcPrefix.length());
	}
	FileConnection tmpDir;
	boolean tmpCond;
	String tmpStr, tmpStr1;

	com.sun.midp.io.j2me.file.Protocol conn =
	    new com.sun.midp.io.j2me.file.Protocol();

	tmpDir = (FileConnection)conn.openPrim(classSecurityToken, dir);
	tmpCond = tmpDir.exists();
	tmpDir.close();

        if (!tmpCond) { // Create directories if need
	    synchronized (this) {
	        tmpDir = (FileConnection)conn.openPrim
		    (classSecurityToken, dir);
                tmpDir.mkdir();
	        tmpStr = tmpDir.getURL().substring(fcPrefix.length());
	        tmpDir.close();
                // create list type directories with default lists
                for (int i = 0; i < DIRECTORIES.length; i++) {
                    tmpDir =
		        (FileConnection)conn.openPrim(classSecurityToken,
			    tmpStr + DIRECTORIES[i]);
                    tmpDir.mkdir();
 	            tmpStr1 = tmpDir.getURL().substring(fcPrefix.length());
	            tmpDir.close();
                    for (int j = 0; j < LISTS[i].length; j++) {
                        String listName =
			    Configuration.getProperty(LISTS[i][j]);
		        tmpDir = (FileConnection)conn.openPrim
			    (classSecurityToken, tmpStr1 + listName);
		        tmpDir.mkdir();
	                tmpDir.close();
                    }
                }
	    }
        }
    
Methods Summary
private java.lang.StringcategoryKey(int listType, java.lang.String listName)
Create the key string by the given PIM list.

param
listType CONTACT_LIST, EVENT_LIST or TODO_LIST
param
listName name of list
return
key string

        return "" + listType + "." + listName;
    
public synchronized java.lang.StringcommitElement(int listType, java.lang.String listName, java.lang.String key, byte[] data)
Commits the PIM item by key.

param
listType CONTACT_LIST, EVENT_LIST or TODO_LIST
param
listName name of list
param
key key of PIM item
param
data data of PIM item
return
key of PIM item
throws
PIMException in case of I/O error

        String ret_v = null;
	if (key == null) {
            // make a new key
            Hashtable keySet = getKeys(listType, listName);
            String extension;
            switch (listType) {
                case PIMBridge.CONTACT_LIST: extension = ".vcf"; break;
                default: extension = ".vcs"; break;
            }
            key = 1 + extension;
	    int i = 1;
            while (keySet.containsKey(i + extension)) {
                key = (++i) + extension;
                // keep looking for a free file name
            }
        }
        String listDir = getListDir(listType, listName);
        FileConnection file = null;
	com.sun.midp.io.j2me.file.Protocol conn =
	    new com.sun.midp.io.j2me.file.Protocol();
        try {
            file = (FileConnection)conn.openPrim(classSecurityToken,
	        listDir + fileSep + key);
            if (data == null) {
	        if (file.exists()) {
                    file.delete();
		}
		file.close();
            } else {
	        if (!file.exists()) {
                    file.create();
		}
                DataOutputStream out = file.openDataOutputStream();
                out.write(data);
                out.close();
		file.close();
                ret_v = new String(key);
            }
        } catch (IOException e) {
            if (Logging.TRACE_ENABLED) {
                Logging.trace(e, "commitElement: FileConnection problem");
	    }
            throw new PIMException("commitElement: " + e.getMessage());
        } finally {
	    try {
	        if (file.isOpen()) {
	            file.close();
                }
	    } catch (IOException e) {
	        // do nothing
	    }
	}
	return ret_v;
    
public synchronized java.lang.StringgetCategories(int listType, java.lang.String listName)
Gets the string of supported categories.

param
listType CONTACT_LIST, EVENT_LIST or TODO_LIST
param
listName name of list
return
string of supported categories
throws
PIMException in case of I/O error
see
#setCategories

        String key = categoryKey(listType, listName);
        String categories = (String) categoriesMap.get(key);

	com.sun.midp.io.j2me.file.Protocol conn =
	    new com.sun.midp.io.j2me.file.Protocol();

	if (categories == null) {
            FileConnection file = null;
	    try {
                file =  (FileConnection)conn.openPrim(classSecurityToken,
		    getListDir(listType, listName) + fileSep +
		    "categories.txt");
                if (!file.exists()) {
                    file.create();
		}
                long catSize = file.fileSize();
                if (catSize <= 0) { // no categories
                    categories = "";
                } else { // read categories
                    byte[] buffer = new byte[(int)catSize];
                    DataInputStream in = file.openDataInputStream();
                    in.readFully(buffer);
                    in.close();
                    categories = new String(buffer);
                }
                file.close();
                categoriesMap.put(key, categories);
            } catch (IOException e) {
		throw new PIMException("getCategories: " + e.getMessage());
            } finally {
	        try {
	            if (file.isOpen()) {
	                file.close();
                    }
	        } catch (IOException e) {
		    throw new PIMException("getCategories: " +
		        e.getMessage());
	        }
	    }
        }
        return categories;
    
public java.lang.StringgetDefaultListName(int listType)
Gets the default list name for given list type.

param
listType - CONTACT_LIST, EVENT_LIST or TODO_LIST
return
default name

        return Configuration.getProperty(LISTS[listType - 1][0]);
    
public byte[]getElement(int listType, java.lang.String listName, java.lang.String key)
Gets the element by key.

param
listType - CONTACT_LIST, EVENT_LIST or TODO_LIST
param
listName name of list
param
key key value
return
data by key
throws
PIMException in case of I/O error

	FileConnection file = null;
	byte[] ret_v = null;

	com.sun.midp.io.j2me.file.Protocol conn =
	    new com.sun.midp.io.j2me.file.Protocol();

	try {
            file = (FileConnection)conn.openPrim(classSecurityToken,
	        getListDir(listType, listName) + fileSep + key,
		Connector.READ);
            long elemSize = file.fileSize();
            if (elemSize <= 0) { // no categories
                ret_v = new byte[0];
            } else { // read categories
                byte[] buffer = new byte[(int)elemSize];
                DataInputStream in = file.openDataInputStream();
                in.readFully(buffer);
                in.close();
                ret_v = buffer;
            }
            file.close();
        } catch (IOException e) {
            if (Logging.TRACE_ENABLED) {
                Logging.trace(e, "getElement: FileConnection problem");
	    }
            throw new PIMException("getElement: " + e.getMessage());
        } finally {
	    try {
	        if (file.isOpen()) {
	            file.close();
                }
	    } catch (IOException e) {
	        // do nothing
	    }
	}
	return ret_v;
    
public java.util.HashtablegetKeys(int listType, java.lang.String listName)
Gets the key table for given list name.

param
listType - CONTACT_LIST, EVENT_LIST or TODO_LIST
param
listName name of list
return
hashkey of keys
throws
PIMException in case of I/O error

        Hashtable keys = new Hashtable();
	Enumeration enListDir = null;
	FileConnection tmpDir;

	com.sun.midp.io.j2me.file.Protocol conn =
	    new com.sun.midp.io.j2me.file.Protocol();

	try {
	    tmpDir = (FileConnection)conn.openPrim(classSecurityToken,
	        getTypeDir(listType) + "/" + listName);
	    enListDir = tmpDir.list();
            tmpDir.close();
        } catch (IOException e) {
            if (Logging.TRACE_ENABLED) {
                Logging.trace(e, "getKeys: FileConnection problem");
	    }
            throw new PIMException("getKeys: " + e.getMessage());
	}
	while (enListDir.hasMoreElements()) {
	    String curr_name = (enListDir.nextElement()).toString();
	    curr_name = curr_name.toLowerCase();
	    if (curr_name.endsWith(".vcf") || curr_name.endsWith(".vcs")) {
	        keys.put(curr_name, curr_name);
	    }
	}
        return keys;
    
private java.lang.StringgetListDir(int listType, java.lang.String listName)
Gets the path for given list name.

param
listType - CONTACT_LIST, EVENT_LIST or TODO_LIST
param
listName name of list
return
the path

        return getTypeDir(listType) + fileSep + listName;
    
public java.lang.String[]getListNames(int listType)
Gets the list of names for given list type.

param
listType - CONTACT_LIST, EVENT_LIST or TODO_LIST
return
array of names

	Vector vect_names = new Vector();
	Enumeration enListType = null;
	FileConnection tmpDir;

	com.sun.midp.io.j2me.file.Protocol conn =
	    new com.sun.midp.io.j2me.file.Protocol();

	try {
	    tmpDir = (FileConnection)conn.openPrim(classSecurityToken,
	        getTypeDir(listType));
	    enListType = tmpDir.list();
            tmpDir.close();
        } catch (IOException e) {
	    if (Logging.TRACE_ENABLED) {
                Logging.trace(e, "getListNames: FileConnection problem");
	    }
	}
	while (enListType.hasMoreElements()) {
	    String curr_name = (enListType.nextElement()).toString();
	    if (curr_name.endsWith(fileSep)) { // The last symbol is "/"
	        vect_names.addElement(curr_name.substring(0,
		    curr_name.length() - 1)); // save the list name
	    }
	}
        String[] names = new String[vect_names.size()];
	Enumeration dir_names = vect_names.elements();
	int i;
	for (i = 0; i < names.length; i++) {
	    names[i] = (dir_names.nextElement()).toString();
	}
        return names;
    
private java.lang.StringgetTypeDir(int listType)
Gets the path for given list type.

param
listType - CONTACT_LIST, EVENT_LIST or TODO_LIST
return
the path

        return dir + fileSep + DIRECTORIES[listType - 1];
    
public synchronized voidsetCategories(int listType, java.lang.String listName, java.lang.String categories)
Sets the new categories.

param
listType CONTACT_LIST, EVENT_LIST or TODO_LIST
param
listName name of list
param
categories string of new categories
throws
PIMException in case of I/O error
see
#getCategories

        FileConnection file = null;

	com.sun.midp.io.j2me.file.Protocol conn =
	    new com.sun.midp.io.j2me.file.Protocol();

	try {
            file = (FileConnection)conn.openPrim(classSecurityToken,
	        getListDir(listType, listName) + fileSep + "categories.txt");
	    if (!file.exists()) {
	        file.create();
	    } else {
                file.truncate(0);
            }
            DataOutputStream out = file.openDataOutputStream();
            out.write(categories.getBytes());
            out.close();
            file.close();
            categoriesMap.put(categoryKey(listType, listName), categories);
        } catch (IOException e) {
            if (Logging.TRACE_ENABLED) {
                Logging.trace(e, "setCategories: FileConnection problem");
	    }
            throw new PIMException("setCategories: " + e.getMessage());
        } finally {
	    try {
	        if (file.isOpen()) {
	            file.close();
                }
	    } catch (IOException e) {
	        // do nothing
	    }
	}