StatusShoutData.java

/**
 * Copyright (c) 2013 Nokia Corporation. All rights reserved.
 * Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation. 
 * Oracle and Java are trademarks or registered trademarks of Oracle and/or its
 * affiliates. Other product and company names mentioned herein may be trademarks
 * or trade names of their respective owners. 
 * See LICENSE.TXT for license information.
 */

package com.nokia.example.statusshout.engine;

import java.util.Vector;

import com.nokia.mid.ui.FileSelectDetail;

/**
 * Manages the application specific data including the persistency.
 */
public class StatusShoutData {
    // Constants
    private static final String RECORD_STORE_NAME = "StatusShout";
    private static final char EQUALS_CHAR = '=';
    private static final char DELIMITER_CHAR = ';';
    private static final String IMAGE_URI = "image_uri";
    private static final String IMAGE_MIME_TYPE = "image_mime_type";
    private static final String MESSAGE = "message";
    private static final String FACEBOOK_TOKEN = "facebook_token";

    // Members
    private static StatusShoutData instance;
    private RecordStoreHelper helper;
    private FileSelectDetail selectedImageDetails;
    private String message;
    private String facebookToken;

    /** 
     * @return The singleton instance of this class.
     */
    public static StatusShoutData getInstance() {
        if (instance == null) {
            instance = new StatusShoutData();
        }
        
        return instance;
    }

    /**
     * Constructor.
     */
    private StatusShoutData() {
        helper = new RecordStoreHelper();
    }

    // Setters and getters ->

    public void setSelectedImageDetails(FileSelectDetail selectedImageDetails) {
        this.selectedImageDetails = selectedImageDetails;
    }

    public FileSelectDetail getSelectedImageDetails() {
        return selectedImageDetails;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public String getMessage() {
        return message;
    }

    public void setFacebookToken(String token) {
        facebookToken = token;
    }

    public String getFacebookToken() {
        return facebookToken;
    }

    // <- setters and getters

    /**
     * Loads the app data from the record store.
     */
    public void load() {
        selectedImageDetails = new FileSelectDetail();
        
        if (helper.open(RECORD_STORE_NAME)) {
            Vector records = helper.getRecords();
            StringBuffer sb = new StringBuffer();
        
            for (int i = 0; records != null && i < records.size(); ++i) {
                sb.append(records.elementAt(i));
            }
            
            stringToData(sb.toString());
            helper.close();
        }
        else {
            System.out.println("StatusShoutData.load(): Failed to load the data!");
        }
    }

    /**
     * Saves the app data to record store.
     */
    public void save() {
        helper.clear(RECORD_STORE_NAME);
        
        if (helper.open(RECORD_STORE_NAME)) {
            final String data = dataToString();
            helper.addRecord(data.getBytes());
            helper.close();
        }
        else {
            System.out.println("StatusShoutData.load(): Failed to save the data!");
        }
    }

    /** 
     * @return The app data a string.
     */
    private String dataToString() {
        StringBuffer sb = new StringBuffer();
        
        if (selectedImageDetails != null) {
            sb.append(IMAGE_URI).append(EQUALS_CHAR)
                .append(selectedImageDetails.url).append(DELIMITER_CHAR);
            sb.append(IMAGE_MIME_TYPE).append(EQUALS_CHAR)
                .append(selectedImageDetails.mimeType).append(DELIMITER_CHAR);
        }
        
        if (message != null && message.length() > 0) {
            sb.append(MESSAGE).append(EQUALS_CHAR).append(message).append(DELIMITER_CHAR);
        }
        
        if (facebookToken != null && facebookToken.length() > 0) {
            sb.append(FACEBOOK_TOKEN).append(EQUALS_CHAR)
                .append(facebookToken).append(DELIMITER_CHAR);
        }
        
        return sb.toString();
    }

    /**
     * Parses the data from the given string.
     * @param dataString The string containing the app data.
     */
    private void stringToData(String dataString) {
        String[] keyValuePairs = split(dataString, DELIMITER_CHAR);
        
        if (keyValuePairs != null) {
            for (int i = 0; i < keyValuePairs.length; ++i) {
                final String[] keyValuePair = split(keyValuePairs[i], EQUALS_CHAR);
                
                if (keyValuePair.length == 2) {
                    System.out.println("StatusShoutData.stringToData(): "
                            + keyValuePair[0] + " = " + keyValuePair[1]);
                    
                    if (keyValuePair[1] == null || keyValuePair[1].equals("null")) {
                        System.out.println("StatusShoutData.stringToData(): Null value: "
                                + keyValuePairs[i]);
                    }
                    else if (keyValuePair[0].equals(IMAGE_URI)) {
                        selectedImageDetails.url = keyValuePair[1];
                    }
                    else if (keyValuePair[0].equals(IMAGE_MIME_TYPE)) {
                        selectedImageDetails.mimeType = keyValuePair[1];
                    }
                    else if (keyValuePair[0].equals(MESSAGE)) {
                        message = keyValuePair[1];
                    }
                    else if (keyValuePair[0].equals(FACEBOOK_TOKEN)) {
                        facebookToken = keyValuePair[1];
                    }
                }
                else {
                    System.out.println("StatusShoutData.stringToData(): Failed to parse a key value pair: "
                            + keyValuePairs[i]);
                }
            }
        }
        else {
            System.out.println("StatusShoutData.stringToData(): Failed to parse key value pairs!");
        }
    }

    /**
     * Splits the given string based on the given delimiter.
     * @param string The string to split.
     * @param delimiter The delimiter character.
     * @return The splitted strings in an array.
     */
    private String[] split(final String string, final char delimiter) {
        if (string == null || string.length() == 0) {
            return null;
        }
        
        final int length = string.length();
        int stringCount = 1;
        
        for (int i = 0; i < length - 1; ++i) {
            if (string.charAt(i) == delimiter) {
                stringCount++;
            }
        }
        
        String[] strings = new String[stringCount];
        
        int index = string.indexOf(delimiter);
        int currentStringIndex = 0;
        int previousIndex = 0;
        
        while (index > 0) {
            strings[currentStringIndex] = string.substring(previousIndex, index);
            currentStringIndex++;
            previousIndex = index + 1;
            
            if (length > index + 1) {
                index = string.indexOf(delimiter, index + 1);
                
                if (index == -1 && length > previousIndex) {
                    strings[currentStringIndex] = string.substring(previousIndex, length);
                }
            }
            else {
                index = -1;
            }
        }
        
        return strings;
    }
}