I want to build a p:autocomplete for searching different objects from a entities. Like customer, article....
The searching works well and the entities will appear.
For AutoComplete
try this generic converter
import java.util.Map;
import java.util.Map.Entry;
import java.util.UUID;
import java.util.WeakHashMap;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;
@FacesConverter(value = "entityConverter")
public class EntityConverter implements Converter {
private static Map<Object, String> entities = new WeakHashMap<Object, String>();
@Override
public String getAsString(FacesContext context, UIComponent component, Object entity) {
synchronized (entities) {
if (!entities.containsKey(entity)) {
String uuid = UUID.randomUUID().toString();
entities.put(entity, uuid);
return uuid;
} else {
return entities.get(entity);
}
}
}
@Override
public Object getAsObject(FacesContext context, UIComponent component, String uuid) {
for (Entry<Object, String> entry : entities.entrySet()) {
if (entry.getValue().equals(uuid)) {
return entry.getKey();
}
}
return null;
}
}
Here my code for ManagedBean:
@Override
public Object getRowData(String rowKey) {
List<Object> countries = (List<Object>) getWrappedData();
for (Object searchGeneral : countries) {
if (searchGeneral instanceof Mandatory) {
Mandatory mandatory = (Mandatory) searchGeneral;
if (mandatory.getId().equals(rowKey))
return searchGeneral;
}
if (searchGeneral instanceof Article) {
Article article = (Article) searchGeneral;
if (article.getId().equals(rowKey))
return searchGeneral;
}
if (searchGeneral instanceof Customer) {
Customer customer = (Customer) searchGeneral;
if (customer.getId().equals(rowKey))
return searchGeneral;
}
}
return null;
}
@Override
public Object getRowKey(Object searchGeneral) {
if (searchGeneral instanceof Customer) {
Mandatory mandatory = (Mandatory) searchGeneral;
return mandatory.getId();
}
if (searchGeneral instanceof Customer) {
Customer customer = (Customer) searchGeneral;
return customer.getId();
}
return null;
}
public List<Object> completeObject(String query)
throws SearchGeneralNotFoundException, UserNotFoundException {
List<Object> suggestions = new ArrayList<Object>();
query = query.trim();
filteredSearchGeneral = searchGeneralService.searchForStartPage(query);
for (Object p : filteredSearchGeneral) {
if (p instanceof Article) {
Article article = (Article) p;
if (article.getName().toLowerCase()
.startsWith(query.toLowerCase()))
suggestions.add(p);
}
if (p instanceof Customer) {
Customer customer = (Customer) p;
if (customer.getName().toLowerCase()
.startsWith(query.toLowerCase()))
suggestions.add(p);
}
if (p instanceof Mandatory) {
Mandatory mandatory = (Mandatory) p;
if (mandatory.getSurname().toLowerCase()
.startsWith(query.toLowerCase()))
suggestions.add(p);
if (mandatory.getName().toLowerCase()
.startsWith(query.toLowerCase()))
suggestions.add(p);
}
if (p instanceof User) {
User user = (User) p;
if (user.getName().startsWith(query))
suggestions.add(p);
}
}
return suggestions;
}
public void handleSelect(SelectEvent event) {
Object object = (Object) event;
LOGGER.info("handleSelect: " + object.getClass().getName());
if (event.getClass().getSimpleName() == "Article") {
LOGGER.info("ARTICLE");
}
redirectToCorrectPage(object);
}
public String redirectToCorrectPage(Object object) {
LOGGER.info("redirectToCorrectPage");
String url = "";
FacesContext ctx = FacesContext.getCurrentInstance();
try {
if (selectedObject instanceof Mandatory) {
LOGGER.info("redirectToCorrectPage Mandatory");
ExternalContext extContext = ctx.getExternalContext();
url = extContext
.encodeActionURL(ctx
.getApplication()
.getViewHandler()
.getActionURL(ctx,
"/portal/mandatoryRegionEdit.xhtml"));
extContext.redirect(url);
}
}
catch (IOException ioe) {
throw new FacesException(ioe);
}
return url;
}