1

This can be seen as a continuation of my other question.

In my backend view, I have a list of some POJO. The POJO is converted to JSON with its toString() method:

BackingView.java

@Getter @Setter private List<SomePOJO> pojoList = ...

SomePojo.java

@EqualsAndHashCode(callSuper = false, of = {"id"})
public class SomePOJO implements Serializable {

    private static final long serialVersionUID = 1L;
    
    @Getter @Setter private Long id;
    @Getter @Setter private Long date;
    @Getter @Setter private Date name;
    ....
    
    @Override
    public String toString() {
        ObjectMapper mapper = new ObjectMapper();
        try {
            return mapper.writeValueAsString(this);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
            return null;
        }
    }

  //making a readable string-representation of the object to display on the orderList
  public String toStringOrderlistDisplay() {
      return "Pojo with id " + id + "and so on... " 
  }

In my frontend, I want to allow the user to sort this pojo-list using Primefaces orderList :

<p:orderList id="ordList" widgetVar="ordList"
                                 value="#{backingview.pojoList }" var="rfg"
                                 controlsLocation="left"  responsive="true" itemValue="#{rfg}"
                                 itemLabel="#{rfg.toStringOrderlistDisplay()}" converter="#{pojoConverter}">

</p:orderList>

PojoConverter.java

@Named
@FacesConverter(value = "pojoConverter")
public class PojoConverter implements Converter {
    @Override
    public Pojo getAsObject(FacesContext context, UIComponent component, String value) {
        if (value != null && value.trim().length() > 0) {
            try {
                ObjectMapper mapper = new ObjectMapper();
                return mapper.readValue(value, RechtsaktFallgeschichte.class);
            }
            catch (NumberFormatException | IOException ex) {
                ex.printStackTrace();
                throw new ConverterException(new FacesMessage(FacesMessage.SEVERITY_ERROR, "Conversion Error", "Not a valid Pojo."));
            }
        }
        else { return null; }
    }

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object value) {
        SomePOJO r = (SomePOJO) value;
        if (r != null) {
            return r.toString();
        }
        else { return null; }
    }
}

Converting to JSON seems to work just fine, if I print the output of the getAsString() method, everything looks as you would expect. However, when the getAsObject() method is called, the value-String-parameter always contains "[object Object]" instead of the POJOs JSON representation. What am I doing wrong here?

Christian O.
  • 468
  • 2
  • 12

1 Answers1

2

I don't know if it is possible to achieve what you are trying to do, but a better way (and the working one) is to pass the POJO id in the getAsString() method, and convert it back to Pojo using a cache map Map<Long, Pojo> pojoCache.

The converter should look like this:

PojoConverter.java

@Named
@FacesConverter(value = "pojoConverter")
public class PojoConverter implements Converter<Pojo> {

@Inject
private PojoService pojoService;

@Override
public Pojo getAsObject(FacesContext context, UIComponent component, String value) {
    if (value != null && value.trim().length() > 0) {
        return pojoService.getPojoCache().get(Long.valueOf(value));
    } else {
        return null;
    }
}

@Override
public String getAsString(FacesContext context, UIComponent component, Pojo value) {
    if (value != null) {
        return String.valueOf(value.getId());
    } else {
        return null;
    }}
}

For a more detailed explanation check this: p:orderList converter getAsObject() doesn't call Object.toString()

stefan-dan
  • 586
  • 5
  • 19