I'm trying to create a simple application so that I'll get familiar with grails. What the app does is a CRUD of profit and date. And I used date as the primary key.
I'm done with the add and read and I'm now working on the delete part.
This is sample db
> ----------------------------
>| date | profit |
> ----------------------------
>| 2015-08-01 | 4.45678 |
>| 2015-08-02 | 76.45678 |
>| 2015-08-03 | 567 |
>| 2015-08-04 | 6789.60 |
> ----------------------------
This is the error message:
URI /SampleGrailsApp/dailyProfit/delete/2015-08-10%2000:00:00.0 Class org.hibernate.TypeMismatchException Message Provided id of the wrong type for class samplegrailsapp.DailyProfit. Expected: class java.util.Date, got class java.lang.Long
This is the controller
package samplegrailsapp
import java.text.DateFormat
import java.text.SimpleDateFormat
class DailyProfitController {
def scaffold = DailyProfit
def index() {
list()
}
def save() {
// Date myDate = params.date('test', 'yyyy-MM-dd');
params.date_month = (Integer.parseInt(params.date_month)<10)? "0" + params.date_month : params.date_month
params.date_day = (Integer.parseInt(params.date_day)<10)? "0" + params.date_day : params.date_day
params.date = params.date_year + "-" + params.date_month + "-" + params.date_day
def dailyProfit = new DailyProfit(params)
println params.toString()
dailyProfit.save()
list()
//render(view:"profitTable")
}
def list(){
def dailyProfit = DailyProfit.list()
render view:"profitTable", model: [dailyProfit : dailyProfit]
}
def delete(Date date){
def dailyProfit = DailyProfit.get(date)
dailyProfit.delete(flush: true)
list()
}
}
HTML file is
<table class="table table-hover table-bordered">
<thead>
<tr>
<th>Date</th>
<th>Profit</th>
<th>Delete?</th>
</tr>
</thead>
<tbody>
<g:each in="${dailyProfit}" var="dp">
<tr>
<td><g:formatDate format="yyyy-MM-dd" date="${dp.date}"/></td>
<td>
<g:formatNumber number="${dp.profit}" type="currency" currencyCode="PHP" format="###.##" />
</td>
<td>
<g:form controller="dailyProfit" action="delete" id="${dp.date}">
<g:actionSubmit value="Delete" >
<span class="glyphicon glyphicon-trash" aria-hidden="true"></span>
</g:actionSubmit>
</g:form>
</td>
</tr>
</g:each>
</tbody>
</table>
This is DailyProfit.groovy
package samplegrailsapp
import java.text.DateFormat
import java.text.SimpleDateFormat
import org.grails.databinding.BindingFormat
class DailyProfit {
@BindingFormat('yyyy-MM-dd')
Date date;
double profit;
static constraints = {
date (blank:false, nullable:false, validator: {value, object ->
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Calendar cal = Calendar.getInstance();
cal.setTime(new Date());
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
if((cal.getTime().before(value) )){
return false;
}
})
profit (blank:false, nullable:false, validator: {value, object ->
if (!value.toString().matches(/^([1-9]{1}[0-9]*\.{0,1}\d{0,5}|0\.[0-9]{1,2}|0|\.[0-9]{1,5})$/) ) return false;
})
}
static mapping = {
version false
//id generator:'assigned', name:'date'
id column: 'date', name: 'date', generator: 'assigned'
}
/*def beforeInsert() {
id = date
}*/
}
As you can see, I used this format for the date yyyy-MM-dd before it is written in the table. Now for the delete button which is located in the last column of the table, when I pass it to the action, it gets the raw format, not this yyyy-MM-dd and that's probably why I get the mismatch error. The question is what can I do so I can edit the date before it is passed to the delete action? or is there another way to delete a data? Thank you.