package com.wootion.rms;
import javax.microedition.rms.RecordEnumeration;
import javax.microedition.rms.RecordStore;
/**
* 类描述:     该类用来对PlayListBean对象进行RMS操作
* 单位:     华通科技
*
* @author 刘长雷
* @version 2008-3-12
*
*/
public class PlayListRMS {
private RecordStore rs;
/**
* 构造一个空歌曲信息RMS操作类
*
*/
public PlayListRMS() {
// TODO Auto-generated constructor stub
}
/**
*
* 方法描述:打开指定RMS    
*
* @param rsname
* RMS名称
*/
public void openRS(String rsname) {
try {
rs = RecordStore.openRecordStore(rsname, true);
}
catch (Exception e) {
System.out.println("打开记录异常");
}
}
/**
*
* 方法描述:关闭RMS    
*/
public void closeRS() {
try {
rs.closeRecordStore();
}
catch (Exception e) {
System.out.println("关闭记录异常");
}
}
/**
*
* 方法描述:添加一个播放列表歌曲信息记录    
*
* @param musicName
* 歌曲名
* @param musicUrl
* 歌曲存放地址
* @return 添加歌曲是否成功标志位,返回true:成功,返回false:失败。
*/
public boolean addRecord(String rsname, String musicName, String musicUrl) {
boolean success = false;
openRS(rsname);
try {
int index = rs.getNumRecords() + 1;
PlayListBean plb = new PlayListBean(index, musicName, musicUrl);
byte[] data = plb.toBytes();
rs.addRecord(data, 0, data.length);
success = true;
// System.out.println("添加记录成功");
}
catch (Exception e) {
e.printStackTrace();
System.out.println("添加记录异常");
}
closeRS();
return success;
}
/**
*
* 方法描述:    
* 保存参数设置
* @param rsname
* @param index
* @param value
*/
public void modParameter(String rsname, int index, int value){
openRS(rsname);
int id = getPRecordId(rsname, index);
try {
rs.enumerateRecords(null, null, false);
ParameterBean pb = new ParameterBean(index,value);
byte[] data = pb.toBytes();
if (id != -1) {
rs.setRecord(id, data, 0, data.length);
}
else {
rs.addRecord(data, 0, data.length);
}
}
catch (Exception e) {
System.out.println("修改记录异常");
e.printStackTrace();
}
closeRS();
}
public int getPRecordValue(String rsname, int index) {
openRS(rsname);
int value = -1;
RecordEnumeration re = null;
try {
re = rs.enumerateRecords(null, null, false); // enumeration
int sum = getNumOfRecords();
for (int i = 0; i < sum; i++) {
int j = re.nextRecordId();
ParameterBean pb = new ParameterBean(rs.getRecord(j));
if (pb.getIndex() == index) {
value = pb.getValue();
}
}
}
catch (Exception e) {
System.out.println("得到P记录号异常");
}
return value;
}
/**
*
* 方法描述:     根据index取得记录集编号
*
* @param rsname
* 记录集名称
* @param index
* 参数的下标
* @return id
*/
public int getPRecordId(String rsname, int index) {
int id = -1;
RecordEnumeration re = null;
try {
re = rs.enumerateRecords(null, null, false); // enumeration
int sum = getNumOfRecords();
for (int i = 0; i < sum; i++) {
int j = re.nextRecordId();
ParameterBean pb = new ParameterBean(rs.getRecord(j));
if (pb.getIndex() == index) {
id = j;
}
}
}
catch (Exception e) {
System.out.println("得到P记录号异常");
}
return id;
}
/**
*
* 方法描述:获取RMS中的记录条数    
*
* @return 记录条数
*/
public int getNumOfRecords() {// 得到RMS中记录的条数
try {
int x = rs.getNumRecords();
return x;
}
catch (Exception e) {
System.out.println("获取记录总数异常");
return 0;
}
}
/**
*
* 方法描述:返回RMS中的所有歌曲    
*
* @return 播放列表RMS中的所有歌曲
*/
public PlayListBean[] getRecords(String rsname) {// 取得RMS中的所有记录
openRS(rsname);
PlayListBean[] result = {};
try {
rs.enumerateRecords(null, null, false);
result = new PlayListBean[rs.getNumRecords()];
for (int i = 0; i < result.length; i++) {
PlayListBean plb = new PlayListBean(rs.getRecord(getId(i + 1)));
result[i] = plb;
}
}
catch (Exception e) {
System.out.println("取得所有记录异常");
}
closeRS();
return result;
}
/**
*
* 方法描述:根据记录编号(参数 int j)取得一条记录    
*
* @param j
* 记录编号
* @return 播放歌曲信息对象
*/
public PlayListBean getRecord(int j) {// 根据记录编号(参数 int j)取得一条记录
PlayListBean result = new PlayListBean();
try {
rs.enumerateRecords(null, null, false);
result = new PlayListBean(rs.getRecord(j));
}
catch (Exception e) {
System.out.println("根据编号取记录异常");
}
return result;
}
/**
*
* 方法描述:根据歌曲播放序号获取其在RMS中的存放ID    
*
* @param index
* 歌曲播放序号
* @return 歌曲在RMS中的记录号
*/
public int getId(int index) {// 得到记录号int j
RecordEnumeration re = null;
int x = 1;
try {
re = rs.enumerateRecords(null, null, false); // enumeration
int sum = getNumOfRecords();
for (int i = 0; i < sum; i++) {
int j = re.nextRecordId();
PlayListBean plb = new PlayListBean(rs.getRecord(j));
if (plb.getIndex() == index) {
x = j;
}
}
}
catch (Exception e) {
System.out.println("得到记录号异常");
}
return x;
}
/**
*
* 方法描述:修改RMS中歌曲信息    
*
* @param id
* 该歌曲在RMS中的存放ID
* @param index
* 播放顺序序号
* @param musicName
* 歌曲名称
* @param musicUrl
* 歌曲存放路径
* @return 修改是否成功,true:成功,false:失败.
*/
public boolean setRecord(int id, int index, String musicName,
String musicUrl) {
boolean success = false;
try {
rs.enumerateRecords(null, null, false);
PlayListBean plb = new PlayListBean(index, musicName, musicUrl);
byte[] data = plb.toBytes();
rs.setRecord(id, data, 0, data.length);
success = true;
}
catch (Exception e) {
System.out.println("修改记录异常");
e.printStackTrace();
}
return success;
}
/**
*
* 方法描述:删除播放歌曲信息的方法    
*
* @param id
* 歌曲在列表中存放的ID
*/
public void deleteRecord(int id) {
try {
rs.deleteRecord(id);
}
catch (Exception e) {
System.out.println("删除记录异常");
e.printStackTrace();
}
}
/**
*
* 方法描述:清空该RecordStore    
*/
public void deletePlRecordStore(String rsname) {
openRS(rsname);
try {
RecordStore.deleteRecordStore(rs.getName());
}
catch (Exception e) {
System.out.println("清空异常");
}
closeRS();
}
/**
*
* 方法描述:歌曲上移的方法,将其信息与序号在前的一首歌进行调换    
*
* @param index
* 歌曲播放序号
*/
public void moveUp(String rsname, int index) {
openRS(rsname);
int SourceRecordId = 0;
int ObjectRecordId = 0;
PlayListBean sourceplb = new PlayListBean();
PlayListBean objectplb = new PlayListBean();
SourceRecordId = getId(index);
ObjectRecordId = getId(index - 1);
sourceplb = getRecord(SourceRecordId);
objectplb = getRecord(ObjectRecordId);
setRecord(SourceRecordId, sourceplb.getIndex(), objectplb
.getMusicName(), objectplb.getMusicUrl());
setRecord(ObjectRecordId, objectplb.getIndex(), sourceplb
.getMusicName(), sourceplb.getMusicUrl());
closeRS();
}
/**
*
* 方法描述:歌曲下移的方法,将其信息与序号在后的一首歌进行调换    
*
* @param index
* 歌曲播放序号
*/
public void moveDown(String rsname, int index) {
openRS(rsname);
int SourceRecordId = 0;
int ObjectRecordId = 0;
PlayListBean sourceplb = new PlayListBean();
PlayListBean objectplb = new PlayListBean();
SourceRecordId = getId(index);
ObjectRecordId = getId(index + 1);
sourceplb = getRecord(SourceRecordId);
objectplb = getRecord(ObjectRecordId);
setRecord(SourceRecordId, sourceplb.getIndex(), objectplb
.getMusicName(), objectplb.getMusicUrl());
setRecord(ObjectRecordId, objectplb.getIndex(), sourceplb
.getMusicName(), sourceplb.getMusicUrl());
closeRS();
}
/**
*
* 方法描述:删除歌曲    
*
* @param index
* 歌曲序号
*/
public void deleteSong(String rsname, int index) {
openRS(rsname);
int SourceRecordId = getId(index);
deleteRecord(SourceRecordId);
int sum = getNumOfRecords();
for (int i = index; i int sRecordId = getId(i + 1);
PlayListBean plb = getRecord(sRecordId);
setRecord(sRecordId, i, plb.getMusicName(), plb.getMusicUrl());
}
closeRS();
}
}