05-26-2008 04:51 AM
03-03-2010 07:31 AM
package es.binovo.alfresco.web.ui.repo.converter;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.ParseException;
import java.util.Locale;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.ConverterException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class Long2DecimalConverter implements Converter {
// By default 2 digits for the fraction
private short fractionDigits;
private static Log logger = LogFactory.getLog(Long2DecimalConverter.class);
public Long2DecimalConverter() {
this.fractionDigits = 2;
if (logger.isDebugEnabled()) {
logger.debug("Constructed new Long2Deciaml converter.");
}
}
public Object getAsObject(FacesContext context, UIComponent component,
String strValue) throws ConverterException {
if (logger.isDebugEnabled()) {
logger.debug("getting '" + strValue + "' string as long.");
}
Locale locale = context.getViewRoot().getLocale();
return string2Long(strValue, locale);
}
public String getAsString(FacesContext context, UIComponent component,
Object value) throws ConverterException {
if (logger.isDebugEnabled()) {
logger.debug("getting '" + value + "' object as String.");
}
Locale locale = context.getViewRoot().getLocale();
return long2String((Long) value, locale);
}
public short getFractionDigits() {
return fractionDigits;
}
String long2String(Long value, Locale locale) {
String strValue = null;
if (value != null && value.toString().length() > 0) {
BigDecimal decimalValue = new BigDecimal(value.toString());
decimalValue = decimalValue.movePointLeft(this.fractionDigits);
DecimalFormat decimal = (DecimalFormat) NumberFormat
.getInstance(locale);
decimal.setMaximumFractionDigits(this.fractionDigits);
strValue = decimal.format(decimalValue);
}
if (logger.isDebugEnabled()) {
logger.debug("returning '" + strValue + "' string.");
}
return strValue;
}
public void setFractionDigits(short fractionDigits) {
// Long can not hold over 18 full digits (max is
// 9,223,372,036,854,775,807)
if (logger.isDebugEnabled()) {
logger.debug("Setting fraction digits to " + fractionDigits + ".");
}
if (fractionDigits > 18 || fractionDigits < 0) {
if (logger.isErrorEnabled()) {
logger.error(fractionDigits
+ " is not a valid fraction digit number."
+ " Allowed values are between 0 and 18.");
}
throw new IllegalArgumentException(
"Fraction digits must be between 0 and 18.");
}
this.fractionDigits = fractionDigits;
}
/**
* Parses a string and compounds the corresponding long value based on the
* locale an the number of fraction digits to keep in the long.
*
* @param strValue
* String to parse
* @param locale
* Locale to use in the parsing
* @return
*/
long string2Long(String strValue, Locale locale) {
long longValue;
DecimalFormat decimal = (DecimalFormat) NumberFormat
.getInstance(locale);
decimal.setParseBigDecimal(true);
BigDecimal decimalValue = null;
try {
decimalValue = (BigDecimal) decimal.parseObject(strValue);
} catch (ParseException e) {
if (logger.isErrorEnabled()) {
logger.error("String value " + strValue
+ " can not be parsed to a BigDecimal object.");
}
throw new ConverterException(e);
}
// prepare to convert to long
decimalValue = decimalValue.movePointRight(this.fractionDigits);
// Truncate non supported decimals
decimalValue = decimalValue.setScale(0, RoundingMode.DOWN);
try {
longValue = decimalValue.longValueExact();
} catch (ArithmeticException e) {
// Long value can not hold the number
if (logger.isErrorEnabled()) {
logger.error("Decimal value " + decimalValue
+ " is not exact or can not be hold in a long.");
}
throw new ConverterException(e);
}
if (logger.isDebugEnabled()) {
logger.debug("returning '" + longValue + "' long.");
}
return longValue;
}
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE faces-config PUBLIC "-//Sun Microsystems, Inc.//DTD JavaServer Faces Config 1.1//EN"
"http://java.sun.com/dtd/web-facesconfig_1_1.dtd">
<faces-config>
<converter>
<converter-id>PriceConverter</converter-id>
<converter-class>es.binovo.alfresco.web.ui.repo.converter.Long2DecimalConverter</converter-class>
<property>
<property-name>fractionDigits</property-name>
<property-class>java.lang.Short</property-class>
</property>
</converter>
</faces-config>
<config evaluator="node-type" condition="eun:ofertaProyecto">
<property-sheet>
<show-property name="eun:descuentoOferta" display-label="Descuento" converter="PriceConverter" fractionDigits="3"/>
<show-property name="eun:portesOferta" display-label="Portes" converter="PriceConverter" fractionDigits="3"/>
<show-property name="eun:tipoIvaOferta" display-label="Tipo IVA" converter="PriceConverter" fractionDigits="3"/>
<show-property name="eun:formaPagoOferta" display-label="Forma de pago" />
<show-property name="eun:plazoEntregaOferta" display-label="Plazo de entrega" />
<show-property name="eun:validezOferta" display-label="Valido hasta" />
</property-sheet>
</config>
03-03-2010 07:39 AM
03-04-2010 06:55 AM
03-08-2010 10:41 AM
public class NumberLocaleAwareTextFieldGenerator extends TextFieldGenerator {
private static Log logger = LogFactory
.getLog(NumberLocaleAwareTextFieldGenerator.class);
@Override
protected void setupConstraints(FacesContext context,
UIPropertySheet propertySheet, PropertySheetItem property,
PropertyDefinition propertyDef, UIComponent component) {
// if the property type is a number based type and the property
// sheet is in edit mode and validation is turned, on add the
// validateIsNumber validation function
if (propertySheet.inEditMode() && propertySheet.isValidationEnabled()
&& propertyDef != null) {
// check the type of the property is a number
if (propertyDef.getDataType().getName().equals(
DataTypeDefinition.DOUBLE)
|| propertyDef.getDataType().getName().equals(
DataTypeDefinition.FLOAT)
|| propertyDef.getDataType().getName().equals(
DataTypeDefinition.INT)
|| propertyDef.getDataType().getName().equals(
DataTypeDefinition.LONG)) {
List<String> params = new ArrayList<String>(5);
// Get the current locale
Locale locale = context.getViewRoot().getLocale();
DecimalFormatSymbols symbols = new DecimalFormatSymbols(locale);
char decimalSeparator = symbols.getDecimalSeparator();
char thousandSeparator = symbols.getGroupingSeparator();
if (logger.isDebugEnabled()) {
logger.debug("Preparing number validation for locale " + locale +
" with decimal separator '" + decimalSeparator
+ "' and thousand separator '" + thousandSeparator +"'.");
}
// add the value parameter
String value = "document.getElementById('"
+ component.getClientId(context) + "')";
params.add(value);
// add the validation failed message to show
String msg = Application.getMessage(context,
"validation_is_number");
addStringConstraintParam(params, MessageFormat.format(msg,
new Object[] { property.getResolvedDisplayLabel() }));
// Tell Javascript about locale related separators
addStringConstraintParam(params, String.valueOf(decimalSeparator));
addStringConstraintParam(params, String.valueOf(thousandSeparator));
// add the validation case to the property sheet
propertySheet.addClientValidation(new ClientValidation(
"validateIsLocalisedNumber", params, false));
}
}
}
}
/**
* Function taken from:
* http://simonwillison.net/2006/Jan/20/escape/
*/
function regexp_escape(text)
{
if (!arguments.callee.sRE) {
var specials = [
'/', '.', '*', '+', '?', '|',
'(', ')', '[', ']', '{', '}', '\\'
];
arguments.callee.sRE = new RegExp(
'(\\' + specials.join('|\\') + ')', 'g'
);
}
return text.replace(arguments.callee.sRE, '\\$1');
}
/**
* Ensures the value of the 'control' is a number
* as specified by the supplied locale related separators.
*
* @return true if the value is a number
*/
function validateIsLocalisedNumber(control, message, decimalSeparator, thousandSeparator, showMessage)
{
var result = true;
var decimalRegexp = new RegExp("^[-]?([0-9]*|[0-9]{1,3}("
+ regexp_escape(thousandSeparator) + "[0-9]{3})*)"
+ "(" + regexp_escape(decimalSeparator) + "[0-9]+)?$");
var value = control.value;
if (!decimalRegexp.test(value)) {
informUser(control, message, showMessage);
return false;
}
return true;
}
03-08-2010 03:23 PM
03-09-2010 11:21 AM
@Override
protected UIComponent createComponent(FacesContext context,
UIPropertySheet propertySheet, PropertySheetItem item) {
ResponseWriter out = context.getResponseWriter();
if (propertySheet.inEditMode()) {
try {
if (logger.isDebugEnabled()) {
logger.debug("Including custom validation JavaScript file.");
}
out.write("\n<script type='text/javascript' src='");
out.write(context.getExternalContext().getRequestContextPath());
out.write("/scripts/custom/validation.js");
out.write("'></script>\n");
} catch (IOException e) {
if (logger.isErrorEnabled()) {
logger.error("Unable to include custom validtaion JavaScript file.");
}
if (logger.isDebugEnabled()) {
logger.debug(e);
}
}
}
UIComponent component = super.createComponent(context, propertySheet,
item);
return component;
}
03-09-2010 12:38 PM
<!– Generates the required JavaScript in order for NumberLocaleAwareTextFieldGenerator to work.–>
<managed-bean>
<managed-bean-name>LocaleAwareNumberValidatorGenerator</managed-bean-name>
<managed-bean-class>org.alfresco.web.bean.generator.HtmlSeparatorGenerator</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
<managed-property>
<property-name>html</property-name>
<value>
<![CDATA[
<script type="text/javascript">
/**
* Function taken from:
* http://simonwillison.net/2006/Jan/20/escape/
*/
function regexp_escape(text)
{
if (!arguments.callee.sRE) {
var specials = [
'/', '.', '*', '+', '?', '|',
'(', ')', '[', ']', '{', '}', '\\'
];
arguments.callee.sRE = new RegExp(
'(\\' + specials.join('|\\') + ')', 'g'
);
}
return text.replace(arguments.callee.sRE, '\\$1');
}
/**
* Ensures the value of the 'control' is a number
* as specified by the supplied locale related separators.
*
* @return true if the value is a number
*/
function validateIsLocalisedNumber(control, message, decimalSeparator, thousandSeparator, showMessage)
{
var result = true;
var decimalRegexp = new RegExp("^[-]?([0-9]*|[0-9]{1,3}("
+ regexp_escape(thousandSeparator) + "[0-9]{3})*)"
+ "(" + regexp_escape(decimalSeparator) + "[0-9]+)?$");
var value = control.value;
if (!decimalRegexp.test(value)) {
informUser(control, message, showMessage);
return false;
}
return true;
}
</script>
]]>
</value>
</managed-property>
</managed-bean>
<config evaluator="node-type" condition="eun:ofertaConcurso">
<property-sheet>
<separator name="sep" show-in-view-mode="false" component-generator="LocaleAwareNumberValidatorGenerator" />
<show-property name="eun:descuentoOferta" display-label="Descuento" component-generator="NumberLocaleAwareTextFieldGenerator" converter="TwoDecimalConverter" />
<show-property name="eun:portesOferta" display-label="Portes" component-generator="NumberLocaleAwareTextFieldGenerator" converter="TwoDecimalConverter" />
<show-property name="eun:tipoIvaOferta" display-label="Tipo IVA" component-generator="NumberLocaleAwareTextFieldGenerator" converter="TwoDecimalConverter" />
<show-property name="eun:formaPagoOferta" display-label="Forma de pago" />
<show-property name="eun:plazoEntregaOferta" display-label="Plazo de entrega" />
<show-property name="eun:validezOferta" display-label="Valido hasta" />
</property-sheet>
</config>
03-09-2010 12:40 PM
Tags
Find what you came for
We want to make your experience in Hyland Connect as valuable as possible, so we put together some helpful links.