?? model.java
字號(hào):
/*
* Created on 2004-6-20
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package com.north.phonebook.model;
import java.io.IOException;
import javax.microedition.rms.*;
import com.north.phonebook.ui.*;
/**
* @author P2800
*
* TODO To change the template for this generated type comment go to Window -
* Preferences - Java - Code Style - Code Templates
*/
public class Model
{
private RecordStore rs = null;
private RecordStore rs_index = null;
private static class AccountFilter implements RecordFilter
{
private String userName;
public AccountFilter(String userName)
{
this.userName = userName;
}
public boolean matches(byte[] data)
{
try
{
return Account.matches(data, userName);
} catch (IOException e)
{
e.printStackTrace();
return false;
}
}
}
private static class IndexFilter implements RecordFilter
{
private String key;
private int type = 101;
public static final int EQUALS = 101;
public static final int STARTWITH = 102;
public IndexFilter(String key)
{
this.key = key;
}
public IndexFilter(String key,int type)
{
this.key = key;
this.type = type;
}
public boolean matches(byte[] data)
{
try
{
return Index.matches(data, key,type);
} catch (IOException e)
{
e.printStackTrace();
return false;
}
}
}
public Model() throws ApplicationException
{
try
{
rs = RecordStore.openRecordStore(Title.rsName, true);
rs_index = RecordStore.openRecordStore(Title.rs_index, true);
} catch (RecordStoreException e)
{
throw new ApplicationException(e);
}
} /*
* (non-Javadoc)
*
* @see com.north.phonebook.model.RMSHandler#addRecord(java.lang.Object)
*/
public void reOpenRecordStore() throws ApplicationException
{
try
{
rs = RecordStore.openRecordStore(Title.rsName, true);
rs_index = RecordStore.openRecordStore(Title.rs_index, true);
} catch (RecordStoreException e)
{
throw new ApplicationException(e);
}
}
public void addIndex(Index index) throws ApplicationException
{
try
{
byte[] index_data = index.serialize();
if (rs_index.getNumRecords() > 0)
{
RecordEnumeration records = rs_index.enumerateRecords(
new IndexFilter(index.getKey()), null, false);
if (records.hasNextElement())
{
rs_index.setRecord(records.nextRecordId(), index_data, 0,
index_data.length);
} else
{
rs_index.addRecord(index_data, 0, index_data.length);
}
} else
{
rs_index.addRecord(index_data, 0, index_data.length);
}
} catch (IOException e)
{
throw new ApplicationException(e);
} catch (RecordStoreException e)
{
throw new ApplicationException(e);
}
}
public Index[] searchIndex(String key) throws ApplicationException
{
try
{
if (rs_index.getNumRecords() > 0)
{
RecordEnumeration records = rs_index.enumerateRecords(
new IndexFilter(key,102), null, false);
int length = records.numRecords();
if (length == 0)
{
return new Index[0];
} else
{
Index[] index = new Index[length];
for (int i = 0; i < length; i++)
{
index[i] = Index.deserialize(rs_index.getRecord(records
.nextRecordId()));
}
return index;
}
} else
{
return new Index[0];
}
} catch (RecordStoreException e)
{
e.printStackTrace();
return new Index[0];
}
catch(IOException e)
{
e.printStackTrace();
return new Index[0];
}
}
public Index getIndex(String key) throws ApplicationException
{
try
{
if (rs_index.getNumRecords() > 0)
{
RecordEnumeration records = rs_index.enumerateRecords(
new AccountFilter(key), null, false);
Index index = null;
if (records.numRecords() == 1)
{
index = Index.deserialize(rs_index.getRecord(records
.nextRecordId()));
}
return index;
} else
{
return null;
}
} catch (RecordStoreException e)
{
throw new ApplicationException(e);
} catch (IOException e)
{
throw new ApplicationException(e);
}
}
public void deleteIndex(String key) throws ApplicationException
{
try
{
if (rs_index.getNumRecords() > 0)
{
RecordEnumeration records = rs_index.enumerateRecords(
new IndexFilter(key), null, false);
while (records.hasNextElement())
{
rs_index.deleteRecord(records.nextRecordId());
}
}
return;
} catch (RecordStoreException e)
{
throw new ApplicationException(e);
}
}
public boolean isRecordExist(String userName) throws ApplicationException
{
try
{
if (rs_index.getNumRecords() > 0)
{
RecordEnumeration records = rs_index.enumerateRecords(
new IndexFilter(userName), null, false);
if (records.numRecords() > 0)
{
return true;
} else
{
return false;
}
} else
{
return false;
}
} catch (RecordStoreException e)
{
e.printStackTrace();
return true;
}
}
public void addRecord(Account account) throws ApplicationException
{
try
{
byte[] data = account.serialize();
int id = rs.addRecord(data, 0, data.length);
Index index = new Index(account.getUserName(), id);
addIndex(index);
} catch (IOException e)
{
} catch (RecordStoreException e)
{
throw new ApplicationException(e);
}
}
/*
* (non-Javadoc)
*
* @see com.north.phonebook.model.RMSHandler#deleteRecord(int)
*/
public void deleteRecord(int recordID) throws ApplicationException
{
}
public void deleteRecord(String userName) throws ApplicationException
{
try
{
Index index = getIndex(userName);
if (index != null)
{
int recordID = index.getRecordID();
System.out.println(recordID);
rs.deleteRecord(recordID);
deleteIndex(userName);
}
} catch (RecordStoreException e)
{
throw new ApplicationException(e);
}
}
public void clearAllRecord() throws ApplicationException
{
try
{
rs.closeRecordStore();
rs_index.closeRecordStore();
RecordStore.deleteRecordStore(Title.rsName);
RecordStore.deleteRecordStore(Title.rs_index);
this.reOpenRecordStore();
}
catch (RecordStoreException e)
{
throw new ApplicationException(e);
}
}
public Account getAccount(String userName) throws ApplicationException
{
try
{
Index index = getIndex(userName);
Account account = null;
if (index != null)
{
account = Account
.deserialize(rs.getRecord(index.getRecordID()));
}
return account;
}
catch (RecordStoreException e)
{
throw new ApplicationException(e);
} catch (IOException e)
{
throw new ApplicationException(e);
}
}
public Index[] listRecord() throws ApplicationException
{
try
{
RecordEnumeration enum = rs_index
.enumerateRecords(null, null, true);
byte[] data = new byte[20];
int length = -1;
int i = 0;
int id = 0;
Index[] index = new Index[rs_index.getNumRecords()];
while (enum.hasNextElement())
{
id = enum.nextRecordId();
length = rs_index.getRecordSize(id);
if (length > data.length)
{
data = new byte[length + 10];
}
rs_index.getRecord(id, data, 0);
index[i] = Index.deserialize(data);
i++;
}
return index;
} catch (IOException e)
{
e.printStackTrace();
return new Index[0];
} catch (RecordStoreException e)
{
e.printStackTrace();
return new Index[0];
}
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -