Guest User

Untitled

a guest
Nov 18th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.06 KB | None | 0 0
  1. protected Double validateDoubleNotNegative(Row row, int idx, int format, boolean mandatory) {
  2. Cell cell = row.getCell(idx);
  3. boolean isValueNegativeOne = false;
  4. boolean isNaN = false;
  5. if (cell == null || cell.getCellType() == Cell.CELL_TYPE_BLANK ||
  6. (cell.getCellType() == Cell.CELL_TYPE_STRING && cell.getStringCellValue().isEmpty())) {
  7. return null;
  8. } else {
  9. //make sure its a number
  10. try {
  11. //makes sure the value from the user isn't -1 as opposed to the -1 returned by parseDouble below
  12. double num = cell.getNumericCellValue();
  13. if (num == -1.0) {
  14. isValueNegativeOne = true;
  15. }
  16. isNaN = false;
  17. } catch (IllegalStateException e) {
  18. //could be an empty formula
  19. if (cell.getCellType() == Cell.CELL_TYPE_FORMULA) {
  20. String str = cell.getStringCellValue();
  21. if ( ! str.isEmpty()) {
  22. //it's not an empty formula cell
  23. isNaN = true;
  24. }
  25. } else {
  26. isNaN = true;
  27. }
  28. }
  29. }
  30.  
  31. Double result = Utility.parseDouble(getCellValue(row, idx, format, mandatory) + "", -1);
  32.  
  33. if(result == null || (result.equals(-1.0) && ! isValueNegativeOne && ! isNaN)) {
  34. return null;
  35. } else if (result != null && isNaN) {
  36. DataUploadCheckList mandatoryArgumentCheck = new DataUploadCheckList();
  37. mandatoryArgumentCheck.setSheetName(row.getSheet().getSheetName());
  38. mandatoryArgumentCheck.setCheckListFlag(DataUploadConstants.ERROR);
  39. mandatoryArgumentCheck.setDescription(
  40. DataUploadConstants.INVALID_VALUE_ERROR
  41. + cell.getRow().getSheet().getRow(0).getCell(cell.getColumnIndex()).getStringCellValue().replaceAll("\n", "").trim()
  42. + DataUploadConstants.NAN_VALUE_ERROR);
  43. mandatoryArgumentCheck.setRowNumber((long) row.getRowNum());
  44. this.mandatoryAttributesErrorList.add(mandatoryArgumentCheck);
  45. result = null;
  46. } else if (result != null && result.doubleValue() < 0.0) {
  47. DataUploadCheckList mandatoryArgumentCheck = new DataUploadCheckList();
  48. mandatoryArgumentCheck.setSheetName(row.getSheet().getSheetName());
  49. mandatoryArgumentCheck.setCheckListFlag(DataUploadConstants.ERROR);
  50. mandatoryArgumentCheck.setDescription(
  51. DataUploadConstants.INVALID_VALUE_ERROR
  52. + cell.getRow().getSheet().getRow(0).getCell(cell.getColumnIndex()).getStringCellValue().replaceAll("\n", "").trim()
  53. + DataUploadConstants.NEGATIVE_VALUE_ERROR);
  54. mandatoryArgumentCheck.setRowNumber((long) row.getRowNum());
  55. this.mandatoryAttributesErrorList.add(mandatoryArgumentCheck);
  56. result = null;
  57. }
  58. return result;
  59. }
Add Comment
Please, Sign In to add comment