# Autocomplete con findRegex() Expresiones Regulares

## Services

```java
import com.avbravo.ejbspard.repository.PaisRepository;
import com.avbravo.avbravoutils.JsfUtil;
import com.avbravo.ejbspard.entity.*;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.Stateless;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import org.bson.Document;

/**
 *
 * @authoravbravo
 */
@Stateless
public class PaisServices {

    @Inject
    PaisRepository paisRepository;

    public List<Pais> complete(String query) {
        List<Pais> suggestions = new ArrayList<>();
           try {
               String field = (String) UIComponent.getCurrentComponent(FacesContext.getCurrentInstance()).getAttributes().get("field");               
               query = query.trim();
               if (query.length() < 1) {
                   return suggestions;
               }   
               suggestions=  paisRepository.findRegex(field, query,true,new Document(field,1));

           } catch (Exception e) {
                    JsfUtil.errorMessage("complete() " + e.getLocalizedMessage());
           }
           return suggestions;
    }

}
```

## En el Controller

Inyectar el Services, agregar el handleSelect(SelectEvent event).

```java
 @Inject
    PaisServices paisServices;

    Pais paisSelected;




     public PaisServices getPaisServices() {
        return paisServices;
    }

    public void setPaisServices(PaisServices paisServices) {
        this.paisServices = paisServices;
    }

     public Pais getPaisSelected() {
        return paisSelected;
    }

    public void setPaisSelected(Pais paisSelected) {
        this.paisSelected = paisSelected;
    }




     public void handleSelect(SelectEvent event) {
        try {
            paisList.removeAll(paisList);
            paisList.add(paisSelected);
            paisFiltered = paisList;
            paisDataModel = new PaisDataModel(paisList);
        } catch (Exception ex) {
            JsfUtil.errorMessage("handleSelect() " + ex.getLocalizedMessage());
        }
    }}
```

## En la página xhtml

Pasamos el parametro field con el nombre del atributo del entity por el que deseamos buscar

```
  <f:attribute name="field" value="idpais" />
```

```java
 <p:outputLabel value="#{app['info.by']} #{msg['field.idpais']}"/>
 <p:autoComplete  scrollHeight="250"   dropdown="false"   
                  emptyMessage="#{app['info.nohayregistros']}"                                                 
                  value="#{paisController.paisSelected}"  
                  completeMethod="#{paisController.paisServices.complete}"  
                  var="p"
                 itemLabel="#{p.idpais} "
                 itemValue="#{p}" forceSelection="true"> 
                 <f:converter binding="#{paisConverter}"/>
                 <f:attribute name="field" value="idpais" />

                            <f:attribute name="fielddropdown" value="false"/>
                            <f:attribute name="fieldquerylenth" value="1"/>
                 <p:ajax event="itemSelect" listener="#{paisController.handleSelect}"
                  update=" :form:msgs,:form:dataTable" />  
                       <f:facet name="itemtip">
                                        <h:panelGrid columns="1" cellpadding="5">
                                            <h:outputText value="#{msg['field.idpais']} #{p.idpais}" />
                                            <h:outputText value="#{msg['field.pais']} #{p.pais}" />
                                        </h:panelGrid>
                                    </f:facet>
                                </p:autoComplete> <p:outputLabel value="#{app['info.by']} #{msg['field.idpais']}"/>
                                <p:autoComplete  scrollHeight="250"   dropdown="false"   
                                                 emptyMessage="#{app['info.nohayregistros']}"                                                 
                                                 value="#{paisController.paisSelected}"  
                                                 completeMethod="#{paisController.paisServices.complete}"  
                                                 var="p"
                                                 itemLabel="#{p.idpais} "
                                                 itemValue="#{p}" forceSelection="true"> 
                                    <f:converter binding="#{paisConverter}"/>
                                       <f:attribute name="field" value="idpais" />

                                    <p:ajax event="itemSelect" listener="#{paisController.handleSelect}"
                                            update=" :form:msgs,:form:dataTable" />  
                                    <f:facet name="itemtip">
                                        <h:panelGrid columns="1" cellpadding="5">
                                            <h:outputText value="#{msg['field.idpais']} #{p.idpais}" />
                                            <h:outputText value="#{msg['field.pais']} #{p.pais}" />
                                        </h:panelGrid>
                                    </f:facet>
                                </p:autoComplete>
```

## Resultado

![](/files/-Lc1X65oAbUi9is59_xv)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://avbravo-2.gitbook.io/trucosjakartaee/overview/java-server-faces/autocomplete/autocomplete-con-findregex-expresiones-regulares.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
