/** * Copyright (C) 2022, Tim Holloway * * Date written: Jan 9, 2022 * Author: Tim Holloway */ package com.mousetech.gourmetj.utils; import javax.faces.application.FacesMessage; import javax.faces.component.UIComponent; import javax.faces.context.FacesContext; import javax.faces.convert.Converter; import javax.faces.convert.ConverterException; import javax.faces.convert.FacesConverter; import javax.faces.validator.ValidatorException; import org.apache.commons.lang3.StringUtils; /** * Converts complex time durations, using TimeFormatter * * @author timh * @since Jan 9, 2022 */ @FacesConverter("com.mousetech.gourmetj.utils.TimeConverter") public class TimeConverter implements Converter { /** * Parse incoming time string before passing to backing bean. */ @Override public Integer getAsObject(FacesContext context, UIComponent component, String value) { if (StringUtils.isBlank(value)) { return null; } Long tv = TimeFormatter.parseTime(value); if (tv == null) { throw new ConverterException( new FacesMessage("Invalid time")); } return tv.intValue(); } /** * Format display/edit text from backing bean to View */ @Override public String getAsString(FacesContext context, UIComponent component, Integer value) { return TimeFormatter.formatTime(Long.valueOf(value)); } }