1

I'm trying to improve my usage of the converter, for an autocomplete object into a JSF project. This is my converter:

@FacesConverter(value = "articleColorConverter", forClass = ArticleColor.class)
public class ArticleColorConverter implements Converter {

    private ArticleColorDao articleColorDao;

    public ArticleColorConverter() {
        super();
        try {
            InitialContext ic = new InitialContext();
            articleColorDao = (ArticleColorDao) ic.lookup("java:module/ArticleColorDao");
        } catch (NamingException e) {
            e.printStackTrace();
        }
    }

    @Override
    public Object getAsObject(FacesContext ctx, UIComponent component, String value) {
        try {
            Long.valueOf(value);
            return articleColorDao.findArticleColorByPk(Long.valueOf(value));
        } catch (Exception e) {
            return null;
        }

    }

    @Override
    public String getAsString(FacesContext ctx, UIComponent component, Object value) {
        return value.toString();
    }
}

And this is entity that:

@Entity public class ArticleColor implements Serializable {

@Id
@GeneratedValue
private Long pk;
private String code;
private String description;
private boolean deleted;

This my toString() method:

Override
public String toString() {
    String result = description;
    if (code != null) {
        result += " (" + code + ")";
    }
    return result;
 }

And finally this is my autocomplete:

<p:autoComplete id="acArticleColor"
                                        value="#{createOrderSelectionView.productionOrder.articleColor}"
                                        completeMethod="#{createOrderSelectionView.completeTextArticleColor}"
                                        style="margin-bottom:10px;" var="articleColor"
                                        itemLabel="#{articleColor}" converter="articleColorConverter"
                                        itemValue="#{articleColor.pk}" forceSelection="true">
                                    </p:autoComplete>

These components works well, but I'm little confusing why should I use the database to retrieve the real from its key, even if I've loaded into the autocomplete the full list of objects. Am I wrong with something?

Jasper de Vries
  • 19,370
  • 6
  • 64
  • 102
I love coding
  • 1,183
  • 3
  • 18
  • 47
  • The question isn't duplicated, since I'm requesting a way to improve the approach of the link you've mentioned! – I love coding Feb 03 '19 at 10:03
  • You are doing exactly what my converters all do. I think what you are missing is that yes you loaded your AutoComplete with "objects" but if you look in the browser all you will see are String values like "45, 67, 93" representing the ID's. When you Form Submit from the browser all that is sent is a String like "43". So on your server side you need a converter to turn that String 43 back into a real object. It doesn't have to be a DAO for some of my items I use an ApplicationScoped cache if the list of objects doesn't change often. But to me you are doing all the right things. – Melloware Feb 03 '19 at 18:33
  • 1
    Your answer it very good, thank you very much! I think It can be set as a real answer and can be voted as the best one! As I know, in some case, the hashcode should perform the operation of convert, with the usage of the recent JSF framework, but if it works now well, it is a good for me! The idea of the cache, it is very good! – I love coding Feb 04 '19 at 08:29

1 Answers1

1

You are doing exactly what my converters all do.

I think what you are missing is that yes you loaded your AutoComplete with "objects" but if you look in the browser all you will see are String values like "45, 67, 93" representing the ID's.

When you Form Submit from the browser all that is sent is a String like "43". So on your server side you need a converter to turn that String 43 back into a real object. It doesn't have to be a DAO... for some of my items I use an ApplicationScoped cache if the list of objects doesn't change often. But to me you are doing all the right things.

Melloware
  • 10,435
  • 2
  • 32
  • 62