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

View File

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

View File

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