1

I need to write a simple customization bean for a dynamic view panel so dates will always be displayed as yyyy-MM-dd but I have no clue which method to overwrite and how to modify my value so it shows what I want.

Any starter code would be apprciated (and yes, I looked at Jesse's code and it is way too complex for what I want to achieve).

Thanks


Edit: This now the code I have in my customization bean, but it does absolutely nothing...

public class DynamicViewCustomizerBean_Ben extends DominoViewCustomizer {

    public static class ExtendedViewColumnConverter extends ViewColumnConverter {
        @Override
        public String getValueAsString(final FacesContext context, final UIComponent component, final Object value) {
            if(value instanceof DateTime) {
                SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
                return fmt.format(value);
            }
            if(value instanceof Date) {
                SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
                return fmt.format(value);
            }

            // for other cases, just return super
            return super.getValueAsString(context, component, value);
        }
    }
}

And yes, the name of my customization bean is set properly on my Dynamic view panel:

<xe:dynamicViewPanel id="dynamicViewPanel1"
    showColumnHeader="true"
    customizerBean="com.videotron.xpages.DynamicViewCustomizerBean_Ben"
    var="rowData">
...

Am I missing something? Is it the good event that is being overridden? I'm asking because if I set a value of "test" instead of the fmt.format(), it doesn't even show up. Nothing in the logs, no visible errors... I can't seem to find a working example of this on the web...

Ben Dubuc
  • 523
  • 3
  • 14
  • Your class is called DynamicViewCustomizerBean but you refer to it as DominoViewCustomizerBean – Per Henrik Lausten May 24 '18 at 13:06
  • PerHenrik, I guess I copied before I corrected this mistake. In any case, I edited the whole thing to make sure I had the proper class names and it still doesn't work. I know the time conversion code works because it works with Jesse's customizer bean (but it also does a lot of things I don't want) but no luck. Do I need to override other classes? The code I have there is the only code in my bean. – Ben Dubuc May 24 '18 at 18:28
  • 1
    ah, yes you need more than that. I am sure you need to override the afterCreateColumn method as well as this is where you refer to your own ViewColumnConverter – Per Henrik Lausten May 24 '18 at 18:59

2 Answers2

1

In the ExtendedViewColumnConverter.getValueAsString(FacesContext, UIComponent, Object) method of your customizer bean you need to return the desired value if the value object is a Date instance.

Here's a simple example:

if (value instanceof Date) {
    DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
    return fmt.format(value);
}
Per Henrik Lausten
  • 21,331
  • 3
  • 29
  • 76
  • I updated the post with my code because it doesn't change the date format using the customizer bean. Any hints on what might be wrong? – Ben Dubuc May 24 '18 at 12:25
  • 1
    I added the code that ws misisng from Jesse Gallagher's customizer bean found on OpenNTF and now it works fine. Not easy finding what objects are required and which methods to use! – Ben Dubuc May 25 '18 at 13:30
1

I tend to use a request scoped bean that holds a few useful methods I found myself often to need.

The java class:

public class DateBean implements Serializable {

    private static final long serialVersionUID = 1L;

    private Locale locale;
    private Date now;
    private String shortDatePattern;

    public void setLocale(Locale locale) {
        this.locale = locale;
    }

    public Date getNow() {
        if (now == null) {
            now = new Date();
        }

        return now;
    }

    public String getShortDatePattern() {
        if (shortDatePattern == null) {
            SimpleDateFormat sdf = (SimpleDateFormat) SimpleDateFormat.getDateInstance(
                    SimpleDateFormat.SHORT, locale);
            shortDatePattern = sdf.toLocalizedPattern()
                    .replaceAll("y+", "yyyy")
                    .replaceAll("M+","MM")
                    .replaceAll("d+", "dd");
        }

        return shortDatePattern;
    }

    ...

}

Of course, this is just an example, you can tweak to your like

In the faces-config.xml

<managed-bean>
    <managed-bean-name>date</managed-bean-name>
    <managed-bean-class>demo.DateBean
    </managed-bean-class>
    <managed-bean-scope>request</managed-bean-scope>
    <managed-property>
        <property-name>locale</property-name>
        <value>#{facesContext.viewRoot.locale}</value>
    </managed-property>
</managed-bean> 

Then, on the XPage:

<xp:text value="#{record.myDate}">
    <xp:this.converter>
        <xp:convertDateTime type="date"
            pattern="${date.shortDatePattern}" />
    </xp:this.converter>
</xp:text>
shillem
  • 1,260
  • 7
  • 12