Using Model Encoders with a PolymerTemplate Model

Sometimes you may want to use ready made beans in your model which you can’t control (i.e. they are given to you as binary class files). But their structure may be inappropriate for your template model used in a user interface representation.

It usually happens if you use data base backend with JPA entities. Such entities usually have unique identifier with Long type. Long type is not supported by Flow at all (it can’t be mapped correctly to any client type). So you have to exclude any property with this type (see @Exclude in Using Beans with a PolymerTemplate Model).

But what if you need this property on the client side for some reason (e.g. you want this identifier of a bean to be able to reference it)? In this case you can use @Encode annotation. You can just encode a Long value to a String value and it will be sent to the client-side as a string. Then once the client side sends this value back it will be decoded back to a Long.

Here is the Person JPA entity class with the model class :

Java

  1. @Entity
  2. public class Person implements Serializable {
  3. @Id
  4. @GeneratedValue
  5. private Long id;
  6. public Long getId() {
  7. return id;
  8. }
  9. }
  10. public interface MyModel extends TemplateModel {
  11. @Encode(value=LongToStringEncoder.class, path="id")
  12. void setPerson(Person person);
  13. Person getPerson();
  14. }

And here is its encoder:

Java

  1. public class LongToStringEncoder implements ModelEncoder<Long, String> {
  2. @Override
  3. public String encode(Long modelValue) {
  4. return Optional.ofNullable(modelValue).map(Object::toString)
  5. .orElse(null);
  6. }
  7. @Override
  8. public Long decode(String presentationValue) {
  9. return Optional.ofNullable(presentationValue).map(Long::valueOf)
  10. .orElse(null);
  11. }
  12. }

Now you can access id property of the Person bean in your code on the client side in the usual way but it will have the String type.

Note
The annotation parameter path=”id” is used to address the id sub-property of the person property. By default the path value is “” which means that an encoder is applied to the property itself.

You can use any type to encode to any supported type. Another usecase for encoders may be splitting one property value to several sub-properties to use them in different UI controls instead of one. E.g. we may want to have 3 input fields for a person birthday instead of one: one for the day, month and year. So you have a Date property in your model:

Java

  1. public interface MyModel extends TemplateModel {
  2. Date getBirthDate();
  3. @Encode(DateToDateBeanEncoder.class)
  4. void setBirthDate(Date birthDate);
  5. }

Then you may define a bean DateBean:

Java

  1. public class DateBean implements Serializable {
  2. private String day;
  3. private String month;
  4. private String year;
  5. public String getDay() {
  6. return day;
  7. }
  8. public void setDay(String day) {
  9. this.day = day;
  10. }
  11. public String getMonth() {
  12. return month;
  13. }
  14. public void setMonth(String month) {
  15. this.month = month;
  16. }
  17. public String getYear() {
  18. return year;
  19. }
  20. public void setYear(String year) {
  21. this.year = year;
  22. }
  23. }

and the encoder class:

Java

  1. public class DateToDateBeanEncoder implements ModelEncoder<Date, DateBean> {
  2. @Override
  3. public DateBean encode(Date modelValue) {
  4. if (modelValue == null) {
  5. return null;
  6. }
  7. DateBean bean = new DateBean();
  8. Calendar calendar = GregorianCalendar.getInstance();
  9. calendar.setTime(modelValue);
  10. bean.setDay(Integer.toString(calendar.get(Calendar.DAY_OF_MONTH)));
  11. bean.setMonth(Integer.toString(calendar.get(Calendar.MONTH) + 1));
  12. bean.setYear(Integer.toString(calendar.get(Calendar.YEAR)));
  13. return bean;
  14. }
  15. @Override
  16. public Date decode(DateBean presentationValue) {
  17. if (presentationValue == null) {
  18. return null;
  19. }
  20. int year = Integer.parseInt(presentationValue.getYear());
  21. int day = Integer.parseInt(presentationValue.getDay());
  22. int month = Integer.parseInt(presentationValue.getMonth()) - 1;
  23. Calendar calendar = GregorianCalendar.getInstance();
  24. calendar.set(year, month, day);
  25. return calendar.getTime();
  26. }
  27. }

Now you can use the following HTML template file for your component (here is only the template snippet):

HTML

  1. <template>
  2. <div style="width: 200px;">
  3. <label>Birth date:</label>
  4. <label for="day">Enter your birthday:</label><paper-input id="day" value="{{birthDate.day}}"></paper-input>
  5. <label for="month">Enter the month of your birthday:</label><paper-input id="month" value="{{birthDate.month}}"></paper-input>
  6. <label for="year">Enter the year of your birthday:</label><paper-input id="year" value="{{birthDate.year}}"></paper-input>
  7. <button on-click="commit" id="commit">Commit</button>
  8. </div>
  9. </template>

So here one Date property is encoded to 3 sub-properties: the day, month and year. Each of them has its own editor but on the server side it’s still the same one property birthDate.

Note
Please note that you still need use your original property name birthDate in this example to access to sub-properties. So those 3 sub-properties requires prefix which is the original property name and its name is still the same birthDate (and not a dateBean e.g.).