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

PIMBridge

public class PIMBridge extends PIMHandler
Implementations of shared PIM code.

Fields Summary
private static Hashtable
contactListFields
Contact table.
private static Hashtable
eventListFields
Event table.
private static Hashtable
todoListFields
Todo table.
private static PIMDatabase
database
PIMItem data storage.
public static final int
CONTACT_LIST
Constant representing a Contact List.
public static final int
EVENT_LIST
Constant representing an Event List.
public static final int
TODO_LIST
Constant representing a ToDo List.
private static boolean
initialized
Initialization flag.
Constructors Summary
Methods Summary
public voidaddCategory(java.lang.Object listHandle, java.lang.String category)
Adds a category to the categories defined for a list.

param
listHandle handle of list
param
category category name
throws
PIMException If an error occurs or the list is no longer accessible or closed.
see
#getCategories

        initialize();
        String cats = database.getCategories(((List)listHandle).type,
            ((List)listHandle).name);
        // the implementation expects that '\n' is never escaped in categories
        String cat_add = "\n" + category;
        if (cats.indexOf(cat_add + '\n") != -1 ||
            cats.startsWith(category + '\n") ||
            cats.endsWith(cat_add) ||
            ((cats.length() > 0) && cats.equals(category))) {
            return;
        }
        if (cats.length() == 0 && category.length() > 0) {
            cats = category;
        } else {
            cats += cat_add;
        }
        database.setCategories(((List)listHandle).type,
            ((List)listHandle).name, cats);
    
public voidcloseList(java.lang.Object listHandle)
Closes list.

param
listHandle handle of list
throws
PIMException in case of I/O error.

    
public synchronized java.lang.ObjectcommitListElement(java.lang.Object listHandle, java.lang.Object elementKey, byte[] element, java.lang.String[] categories)
Commit a list element.

param
listHandle handle of the list
param
elementKey the key of the element to be stored, or null if this is a new element.
param
element element data in a form that can be interpreted by getListElement()
param
categories list of categories which the list element belongs to
return
a non-null key for this element, to be used in future calls to commitListElement() and getListElement()
throws
PIMException in case of I/O error.

        initialize();
        elementKey = database.commitElement(((List)listHandle).type,
            ((List)listHandle).name, (String)elementKey, element);
        return elementKey;
    
public voiddeleteCategory(java.lang.Object listHandle, java.lang.String category)
Deletes a category from the categories defined for a list.

param
listHandle handle of list
param
category category name
throws
PIMException If an error occurs or the list is no longer accessible or closed.
see
#getCategories

        initialize();
        String cats = database.getCategories(((List)listHandle).type,
            ((List)listHandle).name);
        // the implementation expects that '\n' is never escaped in categories
        String cat_add = "\n" + category;
        int pos;
        if ((pos = cats.indexOf(cat_add + '\n")) != -1) {
            cats = cats.substring(0, pos)
                + cats.substring(pos + cat_add.length());
        } else if (cats.startsWith(category + '\n")) {
            cats = cats.substring(cat_add.length());
        } else if (cats.endsWith(cat_add)) {
            cats = cats.substring(0, cats.length() - cat_add.length());
        } else if (cats.equals(category)) {
            cats = "";
        } else {
            return;
        }
        database.setCategories(((List)listHandle).type,
            ((List)listHandle).name, cats);
    
public java.lang.StringgetArrayElementLabel(java.lang.Object listHandle, int field, int arrayElement)
Gets the array element label.

param
listHandle handle of the list
param
field the field number
param
arrayElement the element identifier
return
label fro the array element

        initialize();
        return getFieldDescriptor(((List)listHandle).type, field)
            .getElementlabel(arrayElement);
    
public java.lang.StringgetAttributeLabel(java.lang.Object listHandle, int attribute)
Gets attribute label for the given field attribute.

param
listHandle handle of the list
param
attribute identifier of attribute
return
attribute label

        initialize();
        StringBuffer tag = new StringBuffer("PIM.Attributes.");
        if (attribute == 0) {
            tag.append("None");
        } else {
            switch (((List)listHandle).type) {
                case CONTACT_LIST: tag.append("ContactList."); break;
                case EVENT_LIST: tag.append("EventList."); break;
                case TODO_LIST: tag.append("ToDoList."); break;
                default: return null;
            }
            int index = 0;
            while (attribute > 1) {
                index++;
                attribute = attribute >> 1;
            }
            tag.append(index);
        }
        String tagString = tag.toString();
        String returnValue = Configuration.getPropertyDefault(tagString,
            "Label_" + tagString);
        return returnValue;
    
public java.lang.String[]getCategories(java.lang.Object listHandle)
Gets the set of categories defined for a list.

param
listHandle handle of the list
return
the set of defined categories
throws
PIMException If an error occurs or the list is no longer accessible or closed.

        initialize();
        String result = database.getCategories(((List)listHandle).type,
            ((List)listHandle).name);
        return FormatSupport.split(result, '\n", 0);
    
public byte[]getDefaultBinaryValue(java.lang.Object listHandle, int field)
Gets the default byte[] value for the given field. This will only return a valid value if hasDefaultValue(listType, field) returns true.

param
listHandle handle of the list
param
field identifier of field
return
default value of the field

        return null;
    
public booleangetDefaultBooleanValue(java.lang.Object listHandle, int field)
Gets the default boolean value for the given field. This will only return a valid value if hasDefaultValue(listType, field) returns true.

param
listHandle handle of the list
param
field identifier of field
return
default value of the field

        return false;
    
public longgetDefaultDateValue(java.lang.Object listHandle, int field)
Gets the default date value for the given field. This will only return a valid value if hasDefaultValue(listType, field) returns true.

param
listHandle handle of the list
param
field identifier of field
return
default value of the field

        return 0;
    
public intgetDefaultIntValue(java.lang.Object listHandle, int field)
Gets the default integer value for the given field. This will only return a valid value if hasDefaultValue(listType, field) returns true.

param
listHandle handle of the list
param
field identifier of field
return
default value of the field

        initialize();
        PIMFieldDescriptor descriptor =
            getFieldDescriptor(((List)listHandle).type, field);
        return ((Integer) descriptor.getDefaultValue()).intValue();
    
public java.lang.StringgetDefaultListName(int listType)
Get the name of the default list for the given type.

param
listType the type of the list
return
the name of the default list, or null if no list of this type is supported.

        initialize();
        return database.getDefaultListName(listType);
    
public java.lang.String[]getDefaultStringArrayValue(java.lang.Object listHandle, int field)
Gets the default String[] value for the given field. This will only return a valid value if hasDefaultValue(listType, field) returns true.

param
listHandle handle of the list
param
field identifier of field
return
default value of the field

        int length = getStringArraySize(listHandle, field);
        return new String[length];
    
public java.lang.StringgetDefaultStringValue(java.lang.Object listHandle, int field)
Gets the default string value for the given field. This will only return a valid value if hasDefaultValue(listType, field) returns true.

param
listHandle handle of the list
param
field identifier of field
return
default value of the field

        return null;
    
public intgetFieldDataType(java.lang.Object listHandle, int field)
Gets the data type of the field.

param
listHandle handle of the list
param
field identifier of field
return
data type identifier

        initialize();
        try {
            return getFieldDescriptor(((List)listHandle).type, field)
                    .getDataType();
        } catch (NullPointerException npe) {
            return -1;
        }
    
private PIMFieldDescriptorgetFieldDescriptor(int listType, int field)
Gets the descriptor for given field.

param
listType CONTACT_LIST, EVENT_LIST or TODO_LIST
param
field the field ID
return
field descriptor

        return (PIMFieldDescriptor)getFields(listType).get(new Integer(field));
    
public java.lang.StringgetFieldLabel(java.lang.Object listHandle, int field)
Gets the label of the field.

param
listHandle handle of the list
param
field identifier of field
return
label of the field

        initialize();
        try {
            return getFieldDescriptor(((List)listHandle).type, field)
                    .getLabel();
        } catch (NullPointerException npe) {
            return null;
        }
    
private java.util.HashtablegetFields(int listType)
Gets the table of fields for given list type.

param
listType CONTACT_LIST, EVENT_LIST or TODO_LIST
return
hashtable of fields

        Hashtable map;
        switch (listType) {
            case CONTACT_LIST:
                map = contactListFields;
                break;
            case EVENT_LIST:
                map = eventListFields;
                break;
            case TODO_LIST:
                map = todoListFields;
                break;
            default:
                throw new IllegalArgumentException("List type " + listType
                    + " is not valid");
        }
        return map;
    
public byte[]getListElement(java.lang.Object listHandle, java.lang.Object elementKey)
Get the data for a list element.

param
listHandle handle of the list
param
elementKey the key of the requested element
return
a byte array containing the element data in a supported format
throws
PIMException in case of I/O error.

        initialize();
        return database.getElement(((List)listHandle).type,
            ((List)listHandle).name, (String)elementKey);

    
public java.lang.String[]getListElementCategories(java.lang.Object listHandle, java.lang.Object elementKey)
Get categories for the specified list element.

param
listHandle handle of list
param
elementKey the key of the requested element
return
an array of categories names
throws
PIMException in case of I/O error.


        return new String[0];
    
public synchronized java.lang.Object[]getListKeys(java.lang.Object listHandle)
Get list element keys.

param
listHandle handle of the list
return
an array of objects representing PIM element keys. These keys are to be passed to getListElement() and commitListElement().
throws
PIMException in case of I/O error.

        initialize();

        Hashtable hash_keys =
            database.getKeys(((List)listHandle).type, ((List)listHandle).name);
        int len = hash_keys.size();
        Object[] keys = new Object[len];
        Enumeration en_keys = hash_keys.keys();

        for (int i = 0; i < len; i++) {
            keys[i] = en_keys.nextElement().toString();
        }

        return keys;
    
public synchronized java.lang.String[]getListNames(int listType)
Get the supported list names for the given list type. All list elements must be unique within the list.

param
listType the type of the list
return
a non-null array of supported list names. A copy of this array is returned by PIM.listPIMLists()

        initialize();
        int length = database.getListNames(listType).length;
        String[] names = new String[length];
        for (int i = 0; i < length; i++) {
            names[i] = database.getListNames(listType)[i];
        }
        return names;
    
public intgetMaximumValues(java.lang.Object listHandle, int field)
Get the maximum number of values that can be stored in the given field.

param
listHandle handle of the list
param
field the field type
return
the maximum value

        initialize();
        return getFieldDescriptor(((List)listHandle).type, field)
            .getMaximumValues();
    
public intgetStringArraySize(java.lang.Object listHandle, int field)
Checks if size of the string array.

param
listHandle handle of the list
param
field the field number
return
size of the string array

        initialize();
        try {
            return getFieldDescriptor(((List)listHandle).type, field)
                .getStringArraySize();
        } catch (NullPointerException npe) {
            // debug.exception(Debug.LIGHT, npe);
            return 0;
        }
    
public int[]getSupportedArrayElements(java.lang.Object listHandle, int field)
Gets the array of supported elements.

param
listHandle handle of the list
param
field the field number
return
array of supported elements

        int size = getStringArraySize(listHandle, field);
        int[] result = new int[size];
        for (int i = 0; i < size; i++) {
            result[i] = i;
        }
        return result;
    
public int[]getSupportedAttributes(java.lang.Object listHandle, int field)
Gets the supported attributes for the given field.

param
listHandle handle of the list
param
field identifier of field
return
array of supported attributes of the field

        initialize();
        int listType = ((List)listHandle).type;
        long attributes = getFieldDescriptor(listType, field)
            .getSupportedAttributes();
        int elementCount = listType == PIM.CONTACT_LIST ? 1 : 0;
        for (long a = attributes; a > 0; a >>= 1) {
            if ((a & 1) == 1) {
                elementCount++;
            }
        }
        int[] result = new int[elementCount];
        if (elementCount > 0) {
            int a = 1;
            int i;
            if (listType == PIM.CONTACT_LIST) {
                result[0] = PIMItem.ATTR_NONE;
                i = 1;
            } else {
                i = 0;
            }
            for (; i < elementCount; i++) {
                while ((attributes & a) == 0) a <<= 1;
                result[i] = a;
                a <<= 1;
            }
        }
        return result;
    
public intgetSupportedAttributesMask(java.lang.Object listHandle, int field)
Gets a mask containing all possible attributes for the given field.

param
listHandle handle of the list
param
field the field number
return
supported attribute mask

        initialize();
        return (int)getFieldDescriptor(((List)listHandle).type, field)
                .getSupportedAttributes();
    
public int[]getSupportedFields(java.lang.Object listHandle)
Gets all fields that are supported in this list. All fields supported by this list, including both standard and extended, are returned in this array.

param
listHandle handle of list
return
an int array containing all fields supported by this list. The order of the fields returned is unspecified. If there are no supported fields, a zero-length array is returned.

        initialize();

        Hashtable map = getFields(((List)listHandle).type);
        Enumeration fieldNumbers = map.keys();
        int len = map.size();
        int[] result = new int[len];

        for (int i = 0; i < len; i++) {
            result[i] = (fieldNumbers.nextElement()).hashCode();
        }

        return result;
    
public booleanhasDefaultValue(java.lang.Object listHandle, int field)
Checks if field has default value.

param
listHandle handle of the list
param
field identifier of field
return
true if field supported

        initialize();
        return getFieldDescriptor(((List)listHandle).type, field)
                .hasDefaultValue();
    
public synchronized voidinitialize()
Set up data structures.

        if (!initialized) {
            initializeMap(contactListFields,
            SupportedPIMFields.CONTACT_LIST_FIELDS);
            initializeMap(eventListFields,
            SupportedPIMFields.EVENT_LIST_FIELDS);
            initializeMap(todoListFields,
            SupportedPIMFields.TODO_LIST_FIELDS);
        try {
                database = new PIMDatabase(
            Configuration.getProperty("PIMRootDir") + "pim");
                initialized = true;
        } catch (IOException e) {
            if (Logging.TRACE_ENABLED) {
                    Logging.trace(e, "Unable to create PIMDatabase");
        }
            }
    }
    
private voidinitializeMap(java.util.Hashtable map, PIMFieldDescriptor[] descriptors)
Initialization one structure.

param
map Hashtable for initialization
param
descriptors array of the field descriptors

        for (int i = 0; i < descriptors.length; i++) {
            map.put(new Integer(descriptors[i].getField()), descriptors[i]);
        }
    
public booleanisSupportedArrayElement(java.lang.Object listHandle, int field, int arrayElement)
Checks if the array element is supported.

param
listHandle handle of the list
param
field the field number
param
arrayElement the element identifier
return
true if attribute element is supported

        return arrayElement >= 0 &&
            arrayElement < getStringArraySize(listHandle, field);
    
public booleanisSupportedAttribute(java.lang.Object listHandle, int field, int attribute)
Checks if attribute is supported.

param
listHandle handle of the list
param
field the field number
param
attribute identifier of attribute
return
true if attribute is supported

        initialize();
        if (attribute == PIMItem.ATTR_NONE) {
            return true;
        } else {
            long attributes = getFieldDescriptor(((List)listHandle).type, field)
                .getSupportedAttributes();
            return (attributes & attribute) != 0;
        }
    
public booleanisSupportedField(java.lang.Object listHandle, int field)
Checks if field is supported in list.

param
listHandle handle of the list
param
field identifier of field
return
true if field supported

        initialize();
        return getFieldDescriptor(((List)listHandle).type, field) != null;
    
public java.lang.ObjectopenList(int listType, java.lang.String listName, int openMode)
Opens list.

param
listType the type of the list
param
listName the name of the list
param
openMode open mode
return
list handle that will be used to access this list
throws
PIMException in case of I/O error.

        return new List(listType, listName);
    
public voidrenameCategory(java.lang.Object listHandle, java.lang.String currentCategory, java.lang.String newCategory)
Rename a category.

param
listHandle handle of list
param
currentCategory current category name
param
newCategory new category name
throws
PIMException If an error occurs or the list is no longer accessible or closed.
see
#getCategories

        deleteCategory(listHandle, currentCategory);
        addCategory(listHandle, newCategory);