Skip to content

Instantly share code, notes, and snippets.

@roongr2k7
Last active December 15, 2015 10:09
Show Gist options
  • Save roongr2k7/5243913 to your computer and use it in GitHub Desktop.
Save roongr2k7/5243913 to your computer and use it in GitHub Desktop.
code evolution after 2 years. wanna say something to my self in 2 year ago, "WTF, what did you wrote?, and why you didn't follow our coding convention?"
// 2011-04-xx
String getRunningNo(){
def dateTime = new Date()
def date = dateTime.date
def month = dateTime.month+1
def prefix = sprintf("%02d%02d", date, month)
def lastRunningNo = Domain.executeQuery(
"select MAX(runningNo) as max from Domain where runningNo like ?",prefix+ '%'
)
if(lastRunningNo!=null && !lastRunningNo.isEmpty() ){
if(lastRunningNo[0]!=null){
int max=(lastRunningNo[0])[-8 .. -1].toInteger()
DecimalFormat df = new DecimalFormat("00000000")
String runningNo=df.format(max+1)
return prefix + runningNo;
}else{
return prefix + '00000001'
}
}else{
return prefix + '00000001'
}
}
// 2013-03-26
String generateRunningNo() {
String prefix = new Date().format('yyyyMM')
BigInteger lastRunningNo = Domain.createCriteria().get {
projections {
max 'RunningNo'
}
like 'RunningNo', prefix + '%'
}?.toBigInteger()
String nextRunningNo = lastRunningNo ? lastRunningNo + 1 : prefix + '000001'
return nextRunningNo
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment