1
votes

I try to use the pickList component of Primefaces. My converter does not work properly and I don't know why.

This is my ManagedBean:

@ManagedBean(name = "comMB")
@SessionScoped
public class TeamCompetitionBean implements Serializable {
    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    private DualListModel<Team> teams;
    List<Team> source;
    List<Team> source1;
    List<Team> target;

    @ManagedProperty("#{team}")
    private TeamServiceI teamService;

    List<String> teamNameList ;

    // public TeamCompetitionBean() {

    public DualListModel<Team> getTeams() {

        // Players
        teamNameList = new ArrayList<String>();
        source = new ArrayList<Team>();
        target = new ArrayList<Team>();

        source.addAll(getTeamService().getTeam());

        teams = new DualListModel<Team>(source, target);
        return teams;

    }

    public void setTeams(DualListModel<Team> teams) {
        this.teams = teams;
    }

    public void onTransfer(TransferEvent event) {
        StringBuilder builder = new StringBuilder();
        for (Object item : event.getItems()) {
            builder.append(((Team) item).getTeamName()).append("<br />");
        }

        FacesMessage msg = new FacesMessage();
        msg.setSeverity(FacesMessage.SEVERITY_INFO);
        msg.setSummary("Items Transferred");
        msg.setDetail(builder.toString());

        FacesContext.getCurrentInstance().addMessage(null, msg);
    }

    public TeamServiceI getTeamService() {
        return teamService;
    }

    public void setTeamService(TeamServiceI teamService) {
        this.teamService = teamService;
    }

    public List<Team> getSource() {

        return source;
    }

    public void setSource(List<Team> source) {
        this.source = source;
    }

    public List<Team> getTarget() {
        return target;
    }

    public void setTarget(List<Team> target) {
        this.target = target;
    }


    public void afficher(){
        System.out.println(target);
        System.out.println(source);
    }

}

and this is my entity class that I would like to load in my pickList:

@Entity
@Table(name = "team", catalog = "competition_manager")
public class Team implements java.io.Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private Integer idTeam;
    private Stadium stadium;
    private League league;
    private String teamName;

// getters and setters

@Override
    public String toString() {
        return teamName.toString();
    }

     @Override
     public boolean equals(Object obj) {
      if (!(obj instanceof Team)) {
       return false;
       }
      Team f = (Team) obj;

       return (this.idTeam == f.getIdTeam());

    }

Now, this is my custom Converter:

@FacesConverter(forClass = Team.class, value = "teamConverter")
public class TeamConverter implements Converter {


    Team team;

    public Object getAsObject(FacesContext facesContext, UIComponent component,
            String value) {

        System.out.println("hello object");

        if (value == null || value.length() == 0) {
            return null;
        }
        ApplicationContext ctx = FacesContextUtils
                .getWebApplicationContext(FacesContext.getCurrentInstance());
        TeamBean controller = (TeamBean) ctx.getBean("teamMB");

        List<Team> liststagiaire = controller.getTeamList();

        for (int i = 0; i < liststagiaire.size(); i++)

        {
            team = liststagiaire.get(i);
            if (team.getIdTeam() == getKey(value)) {
                break;
            }

        }

        return team;
    }

    java.lang.Integer getKey(String value) {
        java.lang.Integer key;
        key = Integer.valueOf(value);
        return key;
    }

    String getStringKey(java.lang.Integer value) {
        StringBuffer sb = new StringBuffer();
        sb.append(value);
        return sb.toString();
    }

    public String getAsString(FacesContext facesContext, UIComponent component,
            Object object) {

        System.out.println("hello string");

        if (object == null) {
            System.out.println("hello string null");
            return null;
        }
        if (object instanceof Team) {
            System.out.println("hello string intance of");
            Team o = (Team) object;
            String i = getStringKey(o.getIdTeam());

            return i;
        } else {
            System.out.println("hello throw");
            throw new IllegalArgumentException("object " + object
                    + " is of type " + object.getClass().getName()
                    + "; expected type: " + Team.class.getName());
        }
    }

}

And finally this is my XHTML page:

<p:pickList id="teamPickList" value="#{comMB.teams}" var="team"
            itemValue="#{team}" itemLabel="#{team}" converter="teamConverter">          
        </p:pickList>
1
What does it mean "my converter does not work properly"? It doesn't get called? Does it return a wrong value? Also when is List<String> teamNameList initialized? I cannot see that. - Balázs Németh
Not related to the problem but you could replace String i = getStringKey(o.getIdTeam()); by String i = o.getIdTeam().toString(); - Alexandre Lavoie
i'm sorry, it seems that i didn't pay attention to my code.. in fact List<String> teamNameList is useless....i don't use it for now. - NiÑo
@Alexander Lavoie: getStringKey did not accept String parameter - NiÑo
When a wall of code is posted instead of an SSCCE, it's better to clearly elaborate "doesn't work" in developer's terms instead of in enduser's terms. Track the code execution step by step until something unexpected happens and then elaborate that unexpectation in detail along with the expectation. - BalusC

1 Answers

0
votes

Your problem is comming from this line (in your class TeamConverter) :

if (team.getIdTeam() == getKey(value)) {

You can't compare Integer objects like that, because doing like this you are comparing reference. You should replace this line by

if (team.getIdTeam().intValue() == getKey(value).intValue()) {

You have the same problem in your class Team :

return (this.idTeam == f.getIdTeam());

should be replaced by :

return (this.idTeam.intValue() == f.getIdTeam().intValue());

Not related :

You don't need to use getKey and getStringKey, you could replace them simply like this :

getKey(value)  // this

Integer.valueOf(value)  // by this

and

getStringKey(o.getIdTeam()) // this

o.getIdTeam().toString() // by this

Also you should replace itemLabel="#{team}" by itemLabel="#{team.teamName}" in your view.