p:autocomplete for a global Entity Converter

天大地大妈咪最大 提交于 2019-11-28 14:39:42

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;
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!