*
* @param productCode
* @return Product records in form Array List.
*/
public Product readData(String productCode) throws DataAccessException {
Product dataObject = 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) {
throw new DataAccessException("[ProductDA::readData]Record not found.");
} else {
if (lines.length < 1) {
throw new DataAccessException("[ProductDA::readData]Record not found.");
}
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(ProductDA.class.getName()).log(Level.SEVERE, "[ProductDA::readData]Unable to read record no " + (String.valueOf(i + 1)));
throw new DataAccessException("[ProductDA::readData]Unable to read record no " + (String.valueOf(i + 1)));
} else {
if (fields.length != 8) {
Logger.getLogger(ProductDA.class.getName()).log(Level.SEVERE, "[ProductDA::readData]Record no " + (String.valueOf(i + 1)) + " is corrupted");
} else {
String id = fields[0];
String name = fields[1];
String desc = fields[2];
int qty = -1;
try {
qty = Integer.parseInt(fields[3]);
} catch (NumberFormatException nfx) {
throw new DataAccessException(nfx.getMessage(), nfx);
}
float price = 0f;
try {
price = Float.parseFloat(fields[4]);
} catch (NumberFormatException nfx) {
throw new DataAccessException(nfx.getMessage(), nfx);
}
String barcode = fields[5];
int reorderQty = -1;
try {
reorderQty = Integer.parseInt(fields[6]);
} catch (NumberFormatException nfx) {
throw new DataAccessException(nfx.getMessage(), nfx);
}
int orderQty = -1;
try {
orderQty = Integer.parseInt(fields[7]);
} catch (NumberFormatException nfx) {
throw new DataAccessException(nfx.getMessage(), nfx);
}
if (productCode.equalsIgnoreCase(id)) {
dataObject = new Product();
dataObject.setProductID(id);
dataObject.setProductName(name);
dataObject.setProductDescription(desc);
dataObject.setQuantityAvailable(qty);
dataObject.setProductPrice(price);
dataObject.setBarcodeNumber(barcode);
dataObject.setReorderQuantity(reorderQty);
dataObject.setOrderQuantity(orderQty);
i = lines.length;
}
}
}
}