Kabera Mugwaneza Alexis Kabera Mugwaneza Alexis - 4 months ago 16
Java Question

How to check if database table is empty in java play framework

I'm able to retrieve data from database but not to add condition which will display an alert when database table is empty.

Here is my code:

index.scala.html
:



@(formList: List[Users],form: Form[Users])

@main("history") {

@for(i <- Users.all()) {

@if(i.client.equalsIgnoreCase("potato")) {

<Table>
<tbody>
<tr>
<td><a href="/#"><i >@i.firstname @i.lastname </i></a></td>
<td><a href="/#"><i>@i.phonenumber</i></a></td>
<td><a href="/#"><i>@i.amount</i></a></td>
<td><a href="/#"><i>@i.doneAt</i></a></td>
<td><a href="@routes.Application.edit(i.id)"><i><span class="glyphicon glyphicon-pencil "></span></i></a></td>
<td><a href="@routes.Application.delete(i.id) "><i><span class="glyphicon glyphicon-trash "></span></i></a></td>
</tr>
</tbody>
</table>
}
}
}


Controller method:



public static Result history(long id) {
Form<Users> taskData = form(Users.class);
return ok(history.render(Users.all(), taskData));
}

Answer

tl;dr Move User.all() to the controller and pass it via template parameters, then add the if statement to render the alert if the list is empty.


You’ll have to add an if statement to check if the user list is empty, and display the alert if it is.

@if (Users.all().isEmpty) {
  // Show the alert.
}

In order to avoid fetching the users twice, you could use the defining function.

@defining(Users.all()) { users =>
  @if (users.isEmpty) {
    // Show the alert.
  }
  @for(user <- users) {
    …
  }
}

But I’d strongly suggest to move the user fetching to the controller, and make the template accept the user list as a parameter. That way you’ll keep the template simple. When it gets rendered, it will just present the data, instead of potentially doing heavy-lifting like fetching records from the database.

The end result could look like this.

Template

@(users: List[Users], formList: List[Users],form: Form[Users])

@main("history") {

    @if (users.isEmpty) {
        <div class="alert">
            …
        </div>
    }

    @for(i <- users) {

        @if(i.client.equalsIgnoreCase("potato")) {

            <Table>
                <tbody>
                    <tr>
                        <td><a href="/#"><i >@i.firstname @i.lastname </i></a></td>
                        <td><a href="/#"><i>@i.phonenumber</i></a></td>
                        <td><a href="/#"><i>@i.amount</i></a></td>
                        <td><a href="/#"><i>@i.doneAt</i></a></td>
                        <td><a href="@routes.Application.edit(i.id)"><i><span class="glyphicon glyphicon-pencil "></span></i></a></td>
                        <td><a href="@routes.Application.delete(i.id) "><i><span class="glyphicon glyphicon-trash "></span></i></a></td>
                    </tr>
                </tbody>
            </table>
        }
    }
}

Controller

public static Result history(long id) {
    Form<Users> taskData = form(Users.class);
    return ok(history.render(Users.all(), Users.MTN(), taskData));
}