import javax.microedition.rms.RecordEnumeration;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;


/**
 * A class that wraps a RMS Database and stores values in the form of
 * [Property Name]:[Value]. The Property Name has to be a constant string
 * while the Value can hold any string value. 
 * @author Dayson Pais (percy.pais@gmail.com)
 */
public class EasyDatabase
{
	private String dbName;
	private RecordStore rs;
	
	public EasyDatabase(String dbName)
	{
		this.dbName = dbName;
		
		try
		{
			this.rs = RecordStore.openRecordStore(this.dbName, false);
		}
		catch(RecordStoreException e) { }
	}
	
	/**
	 * Returns the value associated with a Property Name
	 * @param property Unique Property Name
	 */
	public String getProperty(String property)
	{
		int id = this.getRecordId(property);
		if(id > -1)
		{
			try
			{
				String buffer = new String(this.rs.getRecord(id));
				return buffer.substring(property.length() + 1);
			}
			catch(RecordStoreException e) { e.printStackTrace(); }
		}
		return "";
	}
	
	/**
	 * Updates a Property's value in the database.
	 * Has the option of creating the value if it does not exist
	 * @param property
	 * @param value
	 * @param createIfNecessary
	 */
	public void updateProperty(String property, String value, boolean createIfNecessary)
	{
		int id = this.getRecordId(property);
		if(id > -1)
		{
			byte[] buffer = (property + ":" + value).getBytes();
			try
			{
				this.rs.setRecord(id, buffer, 0, buffer.length);
			}
			catch(RecordStoreException e) { e.printStackTrace(); }
		}
		else if(createIfNecessary)
		{
			this.addProperty(property, value);
		}
	}
	
	
	/**
	 * Returns the record id of a property name to help retreive value or update value
	 * @param property
	 * @return
	 */
	private int getRecordId(String property)
	{
		try {
			RecordEnumeration records = this.rs.enumerateRecords(null, null, false);
			while(records.hasNextElement())
			{
				int id = records.nextRecordId();
				String buffer = new String(this.rs.getRecord(id));
				String[] split = new String[2];
				int splitIndex = buffer.indexOf(":");
				split[0] = buffer.substring(0, splitIndex);
				split[1] = buffer.substring(splitIndex, buffer.length());
				if(split[0].equals(property))
				{
					return id; //return the record id
				}
			}
			
		} catch (RecordStoreException e) {
			e.printStackTrace();
		}
		return -1;
	}
	
	
	/**
	 * Adds a Value associated with a Property Name
	 * @param property Unique Property Name
	 * @param value Value
	 */
	public void addProperty(String property, String value)
	{
		byte[] buffer = (property + ":" + value).getBytes();
		try
		{
			this.rs.addRecord(buffer, 0, buffer.length);
		}
		catch (RecordStoreException e) { e.printStackTrace();	}
	}
	
	/**
	 * Creates the database if it doesn't exist.
	 * First check if it exists using dbExists()
	 */
	public void createDB()
	{
		try
		{
			this.rs = RecordStore.openRecordStore(this.dbName, true);
		}
		catch(RecordStoreException e){ e.printStackTrace();	}
	}
	
	/**
	 * Checks if the database exists
	 * @return true/false
	 */
	public boolean dBExists()
	{
		if(this.rs == null)
			return false;
		return true;
	}	
}