if (contents == null) {
return null;
}
String[] lines = StringUtil.parse(contents, format.getEndOfLineSymbols());
if (lines == null) {
throw new AppException("[ProductDA::readDataSet]Record not found.", "[ProductDA::readDataSet]", null);
} else {
if (lines.length < 1) {
throw new AppException("[ProductDA::readDataSet]Record not found.", "[ProductDA::readDataSet]", null);
}
dataObjectSet = new ArrayList<Tuple8<NameValue<String>, NameValue<String>, NameValue<String>, NameValue<Integer>, NameValue<Float>, NameValue<String>, NameValue<Integer>, NameValue<Integer>>>();
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) {
throw new AppException("[ProductDA::readDataSet]Unable to read record no " + (String.valueOf(i + 1)), "[ProductDA::readDataSet]", null);
} else {
if (fields.length != 8) {
Logger.getLogger(ProductDA.class.getName()).log(Level.SEVERE, "[ProductDA::readDataSet]Record no " + (String.valueOf(i + 1)) + " is corrupted. Incomplete fields.");
} else {
boolean recordValid = true;
String prodID = fields[0];
if (prodID == null) {
prodID = "";
Logger.getLogger(ProductDA.class.getName()).log(Level.SEVERE, "" +
"[ProductDA::readDataSet]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::readDataSet]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::readDataSet]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::readDataSet]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::readDataSet]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::readDataSet]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::readDataSet]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::readDataSet]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 (!"".equalsIgnoreCase(prodID) && !"".equalsIgnoreCase(productName) && !"".equalsIgnoreCase(productDescription)) {
NameValue<String> nmProductID = new NameValue<String>("ProductID", prodID);
NameValue<String> nmProductName = new NameValue<String>("ProductName", productName);
NameValue<String> nmProductDescription = new NameValue<String>("ProductDescription", productDescription);
NameValue<Integer> nmQuantityAvailable = new NameValue<Integer>("QuantityAvailable", quantityAvailable);
NameValue<Float> nmProductPrice = new NameValue<Float>("ProductPrice", productPrice);
NameValue<String> nmBarcodeNumber = new NameValue<String>("BarcodeNumber", barcodeNumber);
NameValue<Integer> nmReorderQuantity = new NameValue<Integer>("ReorderQuantity", reorderQuantity);
NameValue<Integer> nmOrderQuantity = new NameValue<Integer>("OrderQuantity", orderQuantity);
dataObjectSet.add(new Tuple8<NameValue<String>, NameValue<String>, NameValue<String>, NameValue<Integer>, NameValue<Float>, NameValue<String>, NameValue<Integer>, NameValue<Integer>>(nmProductID, nmProductName, nmProductDescription, nmQuantityAvailable, nmProductPrice, nmBarcodeNumber, nmReorderQuantity, nmOrderQuantity));
}
}
}
}
}
} else {
throw new AppException("[ProductDA::readDataSet]File not found.", "[ProductDA::readDataSet]", null);
}
} catch (IOException ex) {
throw new AppException(ex.getMessage(), "[ProductDA::readDataSet]", ex);
} finally {
if (br != null) {
try {
br.close();
} catch (IOException ex) {