JDEDateConvert Source Code
package com.abc_co.mrcextension;
import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import com.mrc.ext.FieldConvertAbstract;
/**
* Sample class to convert display value to db value and vice versa.
* (cyynnn <--> mmddyy)
*
* (1) Custom class must extend com.mrc.ext.FieldConvertAbstract
* (which is in mrcapps.jar)
* (2) Implement method dbValue(String s) to Convert display string to DB value
* (3) Implement method customNumber(BigDecimal value) to convert db value
* to display value.
* (4) Implement editInput() to edit input value.
* (5) If you are converting a character field, Implement method
* customString(String value) to convert db value to display value.
* (6) Make a jar file and put it in folder m-power/mrcjava/WEB-INF/lib.
* (7) Attach this class to the field you want to convert in
* Update Dictionary Field screen.
*
*/
public class JDEDateConvert extends FieldConvertAbstract {
/** Default cyynnn value for incorrect input */
static String dftVal = "99999";
/*********************************************************************
* Convert display string mmddyy to DB value cyynnn
*********************************************************************/
public String dbValue(String dspstr) {
if (dspstr == null || dspstr.toUpperCase().indexOf("*BLANK")>=0) {
return "0";
}
//if passed as mm/dd/yy instead of mmddyy
if (dspstr.indexOf("/") > 0) {
String msg = check(dspstr);
if (msg != null) {
return dftVal;
}
dspstr = dspstr.replaceAll("/", "");
}
String str = dspstr;
int year = 0;
String cyynnn = "";
if (str.length() == 6) { //mmddyy
String mm = str.substring(0,2);
String dd = str.substring(2,4);
String yy = str.substring(4);
cyynnn = cyynnn(yy, mm, dd);
} else if (str.length() == 5){ //mddyy
String mm = str.substring(0,1);
String dd = str.substring(1,3);
String yy = str.substring(3);
year = Integer.parseInt(yy);
cyynnn = cyynnn(yy, mm, dd);
} else {
return dftVal;
}
return cyynnn;
}
/*********************************************************************
* Convert DB value cyynnn to mmddyy
*********************************************************************/
public BigDecimal customNumber(BigDecimal value) {
String cyynnn = "" + value.intValue();
int year = 0;
int day = 0;
String yy = "";
if (cyynnn.length() == 6) {
yy = cyynnn.substring(1,3);
String nnn = cyynnn.substring(3);
year = 2000 + Integer.parseInt(yy);
day = Integer.parseInt(nnn);
} else if (cyynnn.length() == 5) {
yy = cyynnn.substring(0,2);
String nnn = cyynnn.substring(2);
year = 1900 + Integer.parseInt(yy);
day = Integer.parseInt(nnn);
} else {
return value;
}
Calendar cal = Calendar.getInstance();
cal.clear();
cal.set(Calendar.YEAR, year);
cal.set(Calendar.DAY_OF_YEAR, day);
int dd = cal.get(Calendar.DAY_OF_MONTH);
int mm = cal.get(Calendar.MONTH) + 1;
String mms = mm > 9 ? "" + mm : "0" + mm;
String dds = dd > 9 ? "" + dd : "0" + dd;
String mmddyy = mms + dds + yy;
BigDecimal valuenew = new BigDecimal(mmddyy);
return valuenew;
}
/*********************************************************************
* Format database value for display.
* Return null to default to mrc format
*********************************************************************/
public String formatDisplay(BigDecimal value) {
return null;
}
/*********************************************************************
* Make cyynnn from yy mm dd
*********************************************************************/
String cyynnn(String yy, String mm, String dd) {
//if mm dd yy not valid, pass back 99999
String mmddyy = mm + "/" + dd + "/" + yy;
if (editInput(mmddyy, "EQ") != null) {
return dftVal;
}
int ddi = Integer.parseInt(dd);
int mmi = Integer.parseInt(mm) - 1;
int yyi = Integer.parseInt(yy);
yyi = yyi > 70 ? 1900 + yyi : 2000 + yyi;
Calendar cal = Calendar.getInstance();
cal.clear();
cal.set(yyi, mmi, ddi);
int day = cal.get(Calendar.DAY_OF_YEAR);
String days = "" + day;
if (day < 10) {
days = "00" + day;
} else if (day < 100) {
days = "0" + day;
}
String c = yyi < 2000 ? "" : "1"; //only good for 60 years!
String cyynnn = c + yy + days;
return cyynnn;
}
/*********************************************************************
* Edit input field value
* value - s tring of the input
* rls - relation code (EQ, LS, CT etc.)
* Return null if input is Ok or return the error message.
*
* Edit if input is in mm/dd/yy format
*********************************************************************/
public String editInput(String value, String rls) {
String msg = null;
if (value == null ) {
return "invalid input";
}
String[] vals = value.split(" ");
if ((rls.equals("RG") || rls.equals("NR")) && vals.length != 2) {
return "RG or NR require 2 values";
}
//for these for do not need to edit for mm/dd/yy
//if (rls.equals("CT") || rls.equals("CA") || rls.equals("CO") ||
//rls.equals("SW")) {
// return null; //
//}
//These relations require single value
if (vals.length > 1 && (rls.equals("EQ") || rls.equals("NE")
|| rls.equals("LT") || rls.equals("GT") || rls.equals("LE")
|| rls.equals("GE") || rls.equals("CT") || rls.equals("SW"))) {
return "Enter one value for relation " + rls;
}
Date[] date2 = new Date[2]; //used for check range
for (int i = 0; i < vals.length; i++) {
String val = vals[i];
msg = check(val);
if (msg != null) { //has error
break;
}
if (i < 2) { //only store 0, 1
try {
String date = addSlash(val);
date2[i] = (Date)formatter.parse(date);
} catch (ParseException e) {
e.printStackTrace();
}
}
}
//check for range
if (msg == null && (rls.equals("RG") || rls.equals("NR"))) {
if (date2[1].compareTo(date2[0]) < 0) {
msg = "First date must be smaller than the second";
}
}
return msg;
}
/*********************************************************************
* Check string of mm/dd/yy is a valid date
*********************************************************************/
public String check(String value) {
String msg = null;
if (value.indexOf("/") < 0) {
msg = checkNoSlash(value);
msg = msg == null ? msg : "Invalid input: " + value;
} else {
msg = check1(value);
}
return msg;
}
/*********************************************************************
* Check string of mm/dd/yy is a valid date
*********************************************************************/
public String check1(String value) {
String msg = checkBasic(value);
if(msg == null){
msg = checkDate(value); //then check is true date 12/22/09
}
return msg;
}
/*********************************************************************
* Check date must be nn/nn/nn
*********************************************************************/
String expression = "^\\d?\\d/\\d{2}/\\d{2}$"; //0 or 1 d + d/dd/dd
public String checkBasic(String value) {
String msg = null;
CharSequence inputStr = value;
Pattern pattern = Pattern.compile(expression);
Matcher matcher = pattern.matcher(inputStr);
if(!matcher.matches()){
return "Invalid input: " + value;
}
return msg;
}
/*********************************************************************
* Check date must be a good date
*********************************************************************/
DateFormat formatter = new SimpleDateFormat("MM/dd/yy");
public String checkDate(String val) {
String msg = null;
formatter.setLenient(false);
try {
Date date = (Date)formatter.parse(val);
} catch (ParseException e) {
msg = "Invalid input: " + val;
}
return msg;
}
/*********************************************************************
* Check input that has no slash.
*********************************************************************/
public String checkNoSlash(String value) {
String str = value.trim();
if (str.length() != 5 && str.length() != 6) {
return "Invalid input: " + value;
}
String date = addSlash(str);
String msg = check1(date);
return msg;
}
/*********************************************************************
*Add slash
*********************************************************************/
public String addSlash(String str) {
if (str.indexOf("/") > 0) {
return str;
}
String date = "";
if (str.length() == 6) { //mmddyy
date = str.substring(0,2) + "/" + str.substring(2,4) + "/"
+ str.substring(4);
} else if (str.length() == 5){ //mddyy
date = str.substring(0,1) + "/" + str.substring(1,3) + "/"
+ str.substring(3);
}
return date;
}
/**
* Test
*/
public static void main(String[] args) {
JDEDateConvert test = new JDEDateConvert();
BigDecimal bd = new BigDecimal(111365);
BigDecimal bd0 = test.customNumber(bd );
System.out.println(111365 + " ---> " + bd0.intValue());
String dsp = "122298";
System.out.println(dsp + " d---> " + test.dbValue(dsp));
dsp = "122211";
System.out.println(dsp + " d---> " + test.dbValue(dsp));
dsp = "122201";
System.out.println(dsp + " d---> " + test.dbValue(dsp));
dsp = "12298";
System.out.println(dsp + " d---> " + test.dbValue(dsp));
dsp = "11/22/08";
System.out.println(dsp + " d---> " + test.dbValue(dsp));
dsp = "21106";
System.out.println(dsp + " ---> " + test.editInput(dsp, "EQ"));
dsp = "12/11/06";
System.out.println(dsp + " ---> " + test.editInput(dsp, "EQ"));
dsp = "21106 122206";
System.out.println(dsp + " ---> " + test.editInput(dsp, "RG"));
}
}