Saturday, February 22, 2014

Example business rules code in Java

   I used to do lots of business rules implementation via Drools

A sample Java example is shown below.
This rule allow Claim submission up to 2ed Tuesday of the month.
Tons of util methods like to validate date ranges, returning month integer as String,
finding second Tuesday of a month, First day etc.
I will add Drools integration code separately.


       ///In case of null input date, logic uses today's date.

static public boolean  isValidClaimDate(Date inputDate ) {
  Date checkDate = null;
  if ( inputDate == null){
  Calendar c = Calendar.getInstance();
  checkDate = c.getTime();
  }else{
  checkDate = inputDate;
  }
  
  Date firstDay = getFirstDay();
  Date secondTuesday = getSecondTuesday();   
  if (firstDay.before(checkDate) && secondTuesday.after(checkDate)) {
  
  LOGGER.info("Validing current date"+ checkDate + " as  true");
return true;
  }else{
  LOGGER.info("Validing current date"+ checkDate + " as  false");
return false;
}
}
///works fine
static public Date getSecondTuesday() {
  Calendar c = Calendar.getInstance();
  int month = c.get ( Calendar.MONTH );
  int year = c.get ( Calendar.YEAR );
  Calendar cal = Calendar.getInstance(TimeZone.getTimeZone(Constants.estTimeZone));
  cal.set( year, month , 1,18,0);
   while (cal.get(Calendar.DAY_OF_WEEK) != Calendar.TUESDAY) {
       cal.add(Calendar.DAY_OF_MONTH, 1);
   }
   //now add 7 more days to get 2ed week
   cal.add(Calendar.DAY_OF_MONTH, 7);
   
  return cal.getTime();
}
///check
static public Date getFirstDay() {
  Calendar c = Calendar.getInstance();
  int month = c.get ( Calendar.MONTH );
  int year = c.get ( Calendar.YEAR );
  
  Calendar cal = Calendar.getInstance(TimeZone.getTimeZone(Constants.estTimeZone));
  cal.set( year, month ,1);
  cal.set(Calendar.HOUR_OF_DAY, 0);
  cal.set(Calendar.HOUR, 0);
      cal.set(Calendar.MINUTE, 0);
      cal.set(Calendar.SECOND, 0);
  return cal.getTime();
}
static public boolean validUploadDateRange( int month, int year ) {
  Calendar c = Calendar.getInstance(TimeZone.getTimeZone(Constants.estTimeZone));
  int currentMonth = c.get ( Calendar.MONTH ); //note: this scale starts from "0"
  int currentYear = c.get ( Calendar.YEAR );
  
  if ( month == currentMonth && currentYear==year){
  return true;
  }
  
  return false;
}
static public boolean validClaimsTimeRange( String monthStr, String yearStr ) {
   if (monthStr == null || monthStr.equals("")){
    return false;
   }
   
   if (yearStr == null || yearStr.equals("")){
    return false;
   }
   
   int month=0;
   int year=0;
   
try {
     month = Integer.parseInt(monthStr);
     year = Integer.parseInt(yearStr);
} catch (NumberFormatException e) {
                 return false;
}
return (validUploadDateRange(month,year));
}
static public String  validUploadMonthAsStr( ) {
  Calendar cal = Calendar.getInstance(TimeZone.getTimeZone(Constants.estTimeZone));
  int currentMonth = cal.get ( Calendar.MONTH ); //note: this scale starts from "0"
  int currentYear = cal.get ( Calendar.YEAR );
  
  cal.set( currentYear, currentMonth-1 ,1);
  cal.set(Calendar.HOUR_OF_DAY, 0);
  cal.set(Calendar.HOUR, 0);
      cal.set(Calendar.MINUTE, 0);
      cal.set(Calendar.SECOND, 0);
  Date pastMonth  = cal.getTime();
  String month = new SimpleDateFormat("MMMM").format(pastMonth);
  return month;
}
}

No comments: