Brad Mace
Brad Mace

Reputation: 27886

Customizing search feature in Play's CRUD module

Edit: searching appears to work in a very basic manner, searching for whatever text is entered in all the String member fields of the relevant model, with no special search operators.

If there's any way to customize how the search works, I'd like to know. Right now it's looking like I'll just have to ignore the built-in search stuff and roll my own.


Since adding the notes field, it's started returning nothing instead of everything. The only thing that was worked so far is entering "test" in the search box, which returns the record with "test" in the notes field. Searching for "notes:test" or "notes=test" doesn't work, nor does any attempt to search the agent or status fields. I've tried things like "1400" which is a value I've verified is present in the agent field via phpMyAdmin, and things like "0" or "VALID" for the status field, which is what all the records have.

model fields:

public class AgentShift extends Model {
    public enum Status { VALID, RESCHEDULED, EXTENDED, DELETED } // REDUCED?

    @Required @Min(1000) @Max(99999)
    public int agent;
    public Status status = Status.VALID;

    @Required
    @Columns(columns={@Column(name="start_time"),@Column(name="end_time")})
    @Type(type="org.joda.time.contrib.hibernate.PersistentInterval")
    public Interval scheduled;

    @Type(type="org.joda.time.contrib.hibernate.PersistentLocalTimeAsTime")
    public LocalTime agent_in;
    @Type(type="org.joda.time.contrib.hibernate.PersistentLocalTimeAsTime")
    public LocalTime agent_out;

    public String notes;
}

modified crud list page:

#{extends 'main.html' /}
#{set title: 'Agent Shifts' /} 

<div id="crudList" class="${type.name}">

    <h2 id="crudListTitle">&{'crud.list.title', type.name}</h2>

    <div id="crudListSearch">
        #{crud.search /}
    </div>

    <div id="crudListTable">
        #{crud.table fields:['id','agent','scheduled','status','notes'] }
            #{crud.custom 'scheduled'}
                #{if object.scheduled.getStart().toDateMidnight().equals(object.scheduled.getEnd().toDateMidnight())}
                    ${org.joda.time.format.DateTimeFormat.forStyle("SS").printTo(out, object.scheduled.getStart())} to 
                    ${org.joda.time.format.DateTimeFormat.forStyle("-S").printTo(out, object.scheduled.getEnd())}
                #{/if}
                #{else}
                    ${org.joda.time.format.DateTimeFormat.forStyle("SS").printTo(out, object.scheduled.getStart())} to 
                    ${org.joda.time.format.DateTimeFormat.forStyle("SS").printTo(out, object.scheduled.getEnd())}
                #{/else}
            #{/crud.custom}
            *{
            #{crud.custom 'entered_by'}
                #{if object.entered_by}
                    ${object.entered_by}
                #{/if}
                #{else} ?
                #{/else}
            #{/crud.custom}
            #{crud.custom 'created'}
                ${org.joda.time.format.DateTimeFormat.forStyle("SS").printTo(out, object.created)}
            #{/crud.custom}
                }*
        #{/crud.table}
    </div>

    <div id="crudListPagination">
        #{crud.pagination /}
    </div>

    <p id="crudListAdd">
        <a href="@{blank()}">&{'crud.add', type.modelName}</a>
    </p>

</div>

Upvotes: 3

Views: 1981

Answers (2)

Felipe Oliveira
Felipe Oliveira

Reputation: 1041

You can use the elastic search module.

1) Define Controller:

@ElasticSearchController.For(ElasticSearchSampleModel.class)
public class ElasticSearchExample extends ElasticSearchController {

}

2) Define standard JPA model with @ElasticSearchable annotation:

@Entity
@ElasticSearchable
public class ElasticSearchSampleModel extends Model {

    /** The Constant serialVersionUID. */
    private static final long serialVersionUID = 1L;

    /** The field1. */
    public String field1;

    /** The field2. */
    public String field2;

    /**
     * To String
     * 
     * @see play.db.jpa.JPABase#toString()
     */
    @Override
    public String toString() {
        return "ElasticSearchSampleModel [field1=" + this.field1 + ", field2=" + this.field2 + "]";
    }

}

You can find more information at http://geeks.aretotally.in/play-framework-module-elastic-search-distributed-searching-with-json-http-rest-or-java.

Upvotes: 2

Pere Villega
Pere Villega

Reputation: 16439

Here you can see how to customize the CRUD pages, including how to show certain fields of the object in the screen.

The search strings are supposed to be in the format:

 <field name>:<value>

For example:

name:Liam

That search would filter all objects whose name contains Liam. The field name must be a field displayed in the list page. I'm not sure if it works with @Lob, but usually that's not a field you want to display in the list page so it shouldn't be an issue.

I have to say that in Play 1.1 I had some issues with some columns, in which starting the search didn't work (it raised an error) and I couldn't solve it. It doesn't seem to happen in 1.2.1 (I have no idea if it is a fix or simply a change I did without noticing)

EDIT:

On the updated question, the list page seems right.

One bold idea: have you checked that the database has the proper columns? I remember I had some issues with Hibernate not playing fair when I changed some models and a few columns where not updated properly, causing odd behavior. It may be worth it to remove the table completely and let Play recreate it.

If that doesn't solve it most likely this is a Play bug in the CRUD controller, you would need to find the source.

My first concern would be the lack on a rel annotation on Interval, and the usage of LocalTime and the enum Status. It should not matter, but... I'm afraid I can only suggest you to do an incremental test to locale the issue:

  • remove everything except agent and notes, and try the search.
  • If it fails, raise a bug on Play's Lighthouse as it shouldn't
  • If it works, keep adding fields one at a time and retesting the search. This will be fast with Play, and you may detect the annotation/field causing the issue and report on Play's Lighthouse

Upvotes: 2

Related Questions