Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
315 views
in Technique[技术] by (71.8m points)

primefaces - p:autocomplete for a global Entity Converter

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. But still the converter doesn′t work.

Here my code:

<p:autoComplete id="searchStartpage" size="35" maxlength="200"
                                    queryDelay="0" maxResults="10" minQueryLength="1"
                                    value="#{searchGeneralRequestController.selectedObject}"
                                    completeMethod="#{searchGeneralRequestController.completeObject}"
                                    var="o" itemLabel="#{object.id}" itemValue="#{object}"
                                    converter="objectForSearchConverter" forceSelection="false"
                                    itemtipMyPosition="left center" cache="false"
                                    itemtipAtPosition="right center">

                                    <p:ajax event="itemSelect"
                                        listener="#{searchGeneralRequestController.handleSelect}" />

                                    <p:column rendered="#{o.getClass().getSimpleName() == 'Mandatory'}">  
                                        #{o.surname} #{o.name}
                                    </p:column>

                                    <p:column rendered="#{o.getClass().getSimpleName() == 'Article'}">  
                                        #{o.name}
                                    </p:column>

                                </p:autoComplete>

And here my Converter:

public Object getAsObject(FacesContext facesContext, UIComponent component,
        String submittedValue) {
    if (submittedValue.trim().equals("")) {
        return null;
    } else {
        try {
            // searchValue = submittedValue;
            int number = Integer.parseInt(submittedValue);

            for (Object p : objectList) {

                if (p instanceof Article) {
                    Article article = (Article) p;
                    if (article.getId() == number) {
                        return p;
                    }
                }

                if (p instanceof Customer) {
                    Customer customer = (Customer) p;
                    if (customer.getId() == number) {
                        return p;
                    }
                }

                if (p instanceof User) {
                    User user = (User) p;
                    if (user.getId() == number) {
                        return p;
                    }
                }

                if (p instanceof Mandatory) {
                    Mandatory mandatory = (Mandatory) p;
                    if (mandatory.getId() == number) {
                        return p;
                    }
                }
            }

        } catch (NumberFormatException exception) {
            throw new ConverterException(new FacesMessage(
                    FacesMessage.SEVERITY_ERROR, "Conversion Error",
                    "Not a valid player"));
        }
    }

    return null;
}

@Override
    public String getAsString(FacesContext context, UIComponent component,
            Object value) {
        if (value == null || value.equals("")) {
            return "";
        } else {
            Long id = (value instanceof Mandatory) ? ((Mandatory) value)
                    .getId() : null;
            return (id != null) ? String.valueOf(id) : null;
        }
    }

All my entities have hash and string method. The Object value is always null

Can anybody help, please

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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;
    }

}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...