Time is now JSF converted and validated

This commit is contained in:
Tim Holloway 2022-01-09 08:22:20 -05:00
parent ab9255d3d4
commit f87854d02f
4 changed files with 95 additions and 13 deletions

View File

@ -0,0 +1,53 @@
/**
* 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));
}
}

View File

@ -12,7 +12,7 @@
<ui:composition template="/WEB-INF/layout/layout.xhtml">
<ui:define name="title">Gourmet Recipe Manager</ui:define>
<ui:define name="content">
<h:outputScript name="js/scrolltable.js"/>
<h:outputScript name="js/scrolltable.js" />
<style>
.ingSel {
width: 3em;
@ -28,6 +28,7 @@
width: 20em;
text-align: left;
}
.groupItem {
font-weight: bold;
background-color: green;
@ -92,6 +93,30 @@
value="#{recipeDetailBean.recipe.cuisine}"
completeMethod="#{recipeDetailBean.cuisineSuggestions}"
/>
<p:outputLabel for="@next"
value="Prep Time"
/>
<p:inputText id="rpreptime"
max="10"
value="#{recipeDetailBean.recipe.preptime}"
>
<f:validator validatorId="com.mousetech.gourmetj.utils.TimeValidator"/>
<f:converter
converterId="com.mousetech.gourmetj.utils.TimeConverter"
/>
</p:inputText>
<p:outputLabel for="@next"
value="Cooking Time"
/>
<p:inputText id="rcooktime"
max="10"
value="#{recipeDetailBean.recipe.cooktime}"
>
<f:validator validatorId="com.mousetech.gourmetj.utils.TimeValidator"/>
<f:converter
converterId="com.mousetech.gourmetj.utils.TimeConverter"
/>
</p:inputText>
<p:outputLabel for="@next"
value="Rating"
/>
@ -282,7 +307,9 @@
/>
</p:inputText>
</p:column>
<p:column style="width: 2.6em">
<p:column
style="width: 2.6em"
>
<f:facet name="header">
E
</f:facet>
@ -359,14 +386,13 @@
</p:tab>
</p:tabView>
<p:commandButton id="doSave" value="Save"
icon="ui-icon-pencil"
ajax="false"
icon="ui-icon-pencil" ajax="false"
disabled="{not recipeDetailBean.dirty}"
action="#{recipeDetailBean.doSave}"
/>
<p:commandButton id="doCancel" value="Cancel"
ajax="false"
immediate="true" action="recipeDetails.jsf"
ajax="false" immediate="true"
action="recipeDetails.jsf"
/>
</h:form>
</p:panel>

View File

@ -39,7 +39,7 @@
</div>
</h:form>
<h:form id="form2">
<p:dataTable id="table1" rows="30"
<p:dataTable id="table1" rows="30" style="margin-top: 6px"
value="#{adminMainBean.searchResults}" var="row"
paginator="true"
paginatorTemplate="{CurrentPageReport} {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}"
@ -75,7 +75,8 @@
</p:column>
<p:column headerText="Prep Time">
<h:outputText
value="#{userSession.formatTime(row.preptime)}"
value="#{row.preptime}"
converter="com.mousetech.gourmetj.utils.TimeConverter"
/>
</p:column>
</p:dataTable>

View File

@ -76,14 +76,16 @@
/>
<h:outputText
label="Prep Time: "
value="#{userSession.formatTime(recipeDetailBean.recipe.preptime)}"
value="#{recipeDetailBean.recipe.preptime}"
converter="com.mousetech.gourmetj.utils.TimeConverter"
/>
<p:outputLabel for="@next"
value="Cook Time:"
/>
<h:outputText
label="Cook Time: "
value="#{userSession.formatTime(recipeDetailBean.recipe.cooktime)}"
value="#{recipeDetailBean.recipe.cooktime}"
converter="com.mousetech.gourmetj.utils.TimeConverter"
/>
</p:panelGrid>
</p:panelGrid>