Web implementation of the Gourmet Recipe Manager
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

53 lines
1.3 KiB

/**
* Copyright (C) 2022, Tim Holloway
*
* Date written: Jan 9, 2022
* Author: Tim Holloway <timh@mousetech.com>
*/
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<Integer> {
/**
* 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));
}
}