/** * Copyright (C) 2022, Tim Holloway * * Date written: Jan 9, 2022 * Author: Tim Holloway */ package com.mousetech.gourmetj.utils; import jakarta.faces.application.FacesMessage; import jakarta.faces.component.UIComponent; import jakarta.faces.context.FacesContext; import jakarta.faces.convert.Converter; import jakarta.faces.convert.ConverterException; import jakarta.faces.convert.FacesConverter; import jakarta.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) { if ( value == null ) { return "--"; } return TimeFormatter.formatTime(Long.valueOf(value)); } }