Primefaces : How to create a autocomplete component that save new objects when don’t find it ?

This code is a autocomplete component and works in this way : User try to find some example with part of his name, if don’t find it automatically will create a new example in the database.

example.xhtml

<p:autoComplete id="autocompleteExample"
                dropdown="true"
                value="#{exampleController.example}"
                completeMethod="#{exampleController.autocompleteExample}"
                converter="exampleConverter"
                var="exemple"
                itemLabel="#{example.name}"
                itemValue="#{example}">

    <p:ajax event="change" 
            process="@this" 
            listener="#{exampleController.takeAllExamples()}"
            update="autocompleteExample" />
    <f:attribute name="someValue" 
                 id="someValue" 
                 value="#{myController.test.id}"/>
</p:autoComplete>

ExampleConverter.java

@FacesConverter(value = "exampleConverter")
public class ExampleConverter implements Converter {

    @Inject
    private ExampleService exampleService;

    @Inject
    private TestService testService;

    @Override
    public Object getAsObject(FacesContext facesContext, 
                              UIComponent uiComponent, String s) {
        if(s!=null) {
              
             Example example = 
                      exampleService.findExampleByName(s);
             if(example==null){
                example = new Example();
                example.setName(s);
                Long someValue = (Long)uiComponent.getAttributes()
                                                    .get("someValue");
                if ( someValue != null ){
                    Test test = testService.findById(someValue);
                    example.setTest(test);
                }
                exampleService.save(exemple);
                exampleService.flush();
            }
            return exemple;
        }
        return null;
    }

    @Override
    public String getAsString(FacesContext facesContext, 
                              UIComponent uiComponent, Object o) {
        if(o instanceof Example){
            Example exemple = (Example)o;
            return example.getName();
        }
        return "";
    }
}

ExampleService.java

public Example findExampleByName(String nome) {
    Criteria criteria = getSession().createCriteria(Example.class);
    criteria.add(Restrictions.like("name", name).ignoreCase());
    criteria.addOrder(Order.asc("name"));
    return (Example)criteria.uniqueResult();
}

ExampleController.java

private Example example;

public Example getExample(){
   return this.example;
}

public void setExample(Example example){
   this.example = example ;
}

public List<Example> autocompleteExample(String query) {
    List allExamples = exampleService.findAll();
    return allExamples.stream().filter(c -> 
                   c.getName().toUpperCase().contains(
                   query.toUpperCase())).collect(Collectors.toList());
}

public List<Example> takeAllExamples(){
   return exampleService.findAll();
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s