* @param categoryCode
* @return
* @throws sg.edu.nus.iss.se07.common.exceptions.AppException
*/
public CategorySet readDataSet(String categoryCode) throws AppException {
CategorySet dataObjectSet = null;
BufferedReader br = null;
try {
br = FileUtil.getBufferedReader(fileName);
if (br != null) {
String contents = FileUtil.getContents(br);
if (contents == null) {
return null;
}
String[] lines = StringUtil.parse(contents, format.getEndOfLineSymbols());
if (lines == null) {
Logger.getLogger(CategoryDA.class.getName()).log(Level.SEVERE, "[CategoryDA::readDataSet]Record not found.");
throw new IOException("[CategoryDA::readDataSet]Record not found.");
} else {
if (lines.length < 1) {
Logger.getLogger(CategoryDA.class.getName()).log(Level.SEVERE, "[CategoryDA::readDataSet]Record not found.");
throw new IOException("[CategoryDA::readDataSet]Record not found.");
}
dataObjectSet = new CategorySet();
for (int i = 0; i < lines.length; i++) {
String record = lines[i];
String[] fields = StringUtil.parse(record, String.valueOf((char) format.getDelimiterChar()));
if (fields == null) {
Logger.getLogger(CategoryDA.class.getName()).log(Level.SEVERE, "[CategoryDA::readDataSet]Unable to read record no " + (String.valueOf(i + 1)));
throw new IOException("[CategoryDA::readDataSet]Unable to read record no " + (String.valueOf(i + 1)));
} else {
if (fields.length != 2) {
Logger.getLogger(CategoryDA.class.getName()).log(Level.SEVERE, "[CategoryDA::readDataSet]Record no " + (String.valueOf(i + 1)) + " is corrupted");
//throw new IOException("[CategoryDA::readDataSet]Record no " + (String.valueOf(i + 1)) + " is corrupted");
} else {
String code = fields[0];
String name = fields[1];
if (categoryCode.equalsIgnoreCase(code)) {
Category category = new Category();
category.setCategoryCode(code);
category.setCategoryName(name);
dataObjectSet.add(category);
category = null;
i = lines.length;
}
}
}