1

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>
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
NiÑo
  • 133
  • 1
  • 3
  • 11
  • 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 teamNameList` initialized? I cannot see that. – Balázs Németh May 30 '13 at 11:06
  • Not related to the problem but you could replace `String i = getStringKey(o.getIdTeam());` by `String i = o.getIdTeam().toString();` – Alexandre Lavoie May 30 '13 at 11:08
  • i'm sorry, it seems that i didn't pay attention to my code.. in fact List teamNameList is useless....i don't use it for now. – NiÑo May 30 '13 at 11:13
  • @Alexander Lavoie: getStringKey did not accept String parameter – NiÑo May 30 '13 at 11:16
  • 1
    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 May 30 '13 at 13:20

1 Answers1

0

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.

Community
  • 1
  • 1
Alexandre Lavoie
  • 8,711
  • 3
  • 31
  • 72
  • Thank you for your response, but , it still not working. look at the exception raised: object com.arobase.service.Impl.TeamServiceImpl@501787f1 is of type com.sun.proxy.$Proxy690; expected type: com.arobase.model.Team – NiÑo May 30 '13 at 11:20
  • @user2436180 I've edited the answer I've found other things that you should pay attention. Your exception was not raised before? Could you point out which line is raising this exception? Also I don't see any `TeamServiceImpl` class in your question. – Alexandre Lavoie May 30 '13 at 11:32
  • I've edited my code like you suggested...but the problem is still unresolved. This is the exception from the stack Trace: java.lang.IllegalArgumentException: object com.arobase.service.Impl.TeamServiceImpl@58e99405 is of type com.sun.proxy.$Proxy863; expected type: com.arobase.model.Team at com.arobase.managed.bean.TeamConverter.getAsString(TeamConverter.java:79) – NiÑo May 30 '13 at 13:24
  • @user2436180 Have you tried to remove your `instanceof Team` validation? – Alexandre Lavoie May 30 '13 at 14:14
  • when i remove it the exception becomes: java.lang.ClassCastException: com.sun.proxy.$Proxy176 cannot be cast to com.arobase.model.Team at com.arobase.managed.bean.TeamConverter.getAsString(TeamConverter.java:73) – NiÑo May 30 '13 at 14:34
  • This problem comes from what you are inserting in this line : `source.addAll(getTeamService().getTeam());`, will need to see what this function is returning. – Alexandre Lavoie May 30 '13 at 14:51
  • also when i try to load team list without putting any converter, i obtain a list of codes such as com.arobase.service.impl.TeamServiceImpl@3ef5 – NiÑo May 30 '13 at 14:57
  • concerning source.All(getTeamService().getTeam()); TeamService is an interface of Service layer, getTeam is a method that return a list of all teams from database. i tried to display the content of the List source in an ordinary datatable and i got the right list of teams with names and initials (i can display any attribute from this list) – NiÑo May 30 '13 at 15:00
  • Your problem comes from it for sure, probably you need to convert it to `List`, `getTeamService().getTeam()` is returning something else, maybe `List`. – Alexandre Lavoie May 30 '13 at 15:03
  • how can i convert it ? could you explain more to me please ? – NiÑo May 30 '13 at 15:42
  • Post your `getTeam()` function in the question, I'll check what you need to do. – Alexandre Lavoie May 30 '13 at 15:47
  • this is my getTeam method placed in my DAO layer `@SuppressWarnings("unchecked") public List getTeam() { List list = getSessionFactory().getCurrentSession() .createQuery("from Team").list(); return list; }` – NiÑo May 31 '13 at 07:35
  • @user2436180 I suggest you to use a debugger and trace inside the line `source.addAll(getTeamService().getTeam());` your error comes from something inside that. I can't help much since I'm not in front of your computer, also I think I've done much, I've pointed out many errors in your code. – Alexandre Lavoie May 31 '13 at 09:25
  • can you give me your skype id, if you want of course, it's very important to me to resolve this problem. may be when i share my screen so that you can look at my code it will be easier to locate the problem. concerning my method `getTeam()` be sure that it gives the right list of object...as i sais i test to display this list in a datatable and i got the right list – NiÑo May 31 '13 at 10:47
  • @user2436180 I don't like this, but you can add me for this time ... lavoie.alexandre – Alexandre Lavoie May 31 '13 at 18:16