* @param productID
* @return Category records in form Array List.
* @throws AppException
*/
public Product readData(String productID) throws AppException {
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 AppException("[ProductDA::readData]Record not found.", "[ProductDA::readData]", null);
} else {
if (lines.length < 1) {
throw new AppException("[ProductDA::readData]Record not found.", "[ProductDA::readData]", null);
}
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 AppException("[ProductDA::readData]Unable to read record no " + (String.valueOf(i + 1)), "[ProductDA::readData]", null);
} else {
if (fields.length != 8) {
Logger.getLogger(ProductDA.class.getName()).log(Level.SEVERE, "[ProductDA::readData]Record no " + (String.valueOf(i + 1)) + " is corrupted");
} else {
boolean recordValid = true;
String prodID = fields[0];
if (prodID == null) {
prodID = "-";
Logger.getLogger(ProductDA.class.getName()).log(Level.SEVERE, "" +
"[ProductDA::readData]Record no " + (String.valueOf(i + 1)) + "is corrupted.\n" +
"Field: ProductID\n");
}
String productName = fields[1];
if (productName == null) {
productName = "-";
Logger.getLogger(ProductDA.class.getName()).log(Level.SEVERE, "" +
"[ProductDA::readData]Record no " + (String.valueOf(i + 1)) + "is corrupted.\n" +
"Field: ProductName\n");
}
String productDescription = fields[2];
if (productDescription == null) {
productDescription = "-";
Logger.getLogger(ProductDA.class.getName()).log(Level.SEVERE, "" +
"[ProductDA::readData]Record no " + (String.valueOf(i + 1)) + "is corrupted.\n" +
"Field: ProductDescription\n");
}
int quantityAvailable = 0;
try {
quantityAvailable = Integer.parseInt(fields[3]);
} catch (NumberFormatException nfx) {
recordValid = false;
quantityAvailable = 0;
Logger.getLogger(ProductDA.class.getName()).log(Level.SEVERE, "" +
"[ProductDA::readData]Record no " + (String.valueOf(i + 1)) + "is corrupted.\n" +
"Field: QuantityAvailable\n" +
"Value: " + String.valueOf(quantityAvailable), nfx);
//throw new AppException(nfx.getMessage(), nfx);
}
float productPrice = -9999.9999f;
try {
productPrice = Float.parseFloat(fields[4]);
} catch (NumberFormatException nfx) {
recordValid = false;
productPrice = -9999.9999f;
Logger.getLogger(ProductDA.class.getName()).log(Level.SEVERE, "" +
"[ProductDA::readData]Record no " + (String.valueOf(i + 1)) + "is corrupted.\n" +
"Field: ProductPrice\n" +
"Value: " + String.valueOf(productPrice), nfx);
//throw new AppException(nfx.getMessage(), nfx);
}
String barcodeNumber = fields[5];
if (barcodeNumber == null) {
barcodeNumber = "-";
Logger.getLogger(ProductDA.class.getName()).log(Level.SEVERE, "" +
"[ProductDA::readData]Record no " + (String.valueOf(i + 1)) + "is corrupted.\n" +
"Field: BarCode Number\n");
}
int reorderQuantity = 0;
try {
reorderQuantity = Integer.parseInt(fields[6]);
} catch (NumberFormatException nfx) {
recordValid = false;
productPrice = -9999.9999f;
Logger.getLogger(ProductDA.class.getName()).log(Level.SEVERE, "" +
"[ProductDA::readData]Record no " + (String.valueOf(i + 1)) + "is corrupted.\n" +
"Field: ReOrderQuantity\n" +
"Value: " + String.valueOf(reorderQuantity), nfx);
//throw new AppException(nfx.getMessage(), nfx);
}
int orderQuantity = Integer.parseInt(fields[7]);
try {
orderQuantity = Integer.parseInt(fields[7]);
} catch (NumberFormatException nfx) {
recordValid = false;
productPrice = -9999.9999f;
Logger.getLogger(ProductDA.class.getName()).log(Level.SEVERE, "" +
"[ProductDA::readData]Record no " + (String.valueOf(i + 1)) + "is corrupted.\n" +
"Field: OrderQuantity\n" +
"Value: " + String.valueOf(orderQuantity), nfx);
//throw new AppException(nfx.getMessage(), nfx);
}
if (!recordValid) {
Logger.getLogger(ProductDA.class.getName()).log(Level.WARNING, "Some records are found invalid. Default value are assigned.");
}
if (productID.equalsIgnoreCase(prodID)) {
dataObject = new Product();
dataObject.setProductID(prodID);
dataObject.setProductName(productName);
dataObject.setProductDescription(productDescription);
dataObject.setQuantityAvailable(quantityAvailable);
dataObject.setProductPrice(productPrice);
dataObject.setBarcodeNumber(barcodeNumber);
dataObject.setReorderQuantity(reorderQuantity);
dataObject.setOrderQuantity(orderQuantity);
i = lines.length;
}
}
}
}