Search in sources :

Example 1 with ConverterException

use of javax.faces.convert.ConverterException in project microservices by pwillhan.

the class Base64Converter method getString.

public String getString(Object value) {
    if (!(value instanceof Serializable))
        throw new ConverterException(new FacesMessage("Must be java.io.Serializable: " + value));
    try (ByteArrayOutputStream bo = new ByteArrayOutputStream();
        ObjectOutputStream so = new ObjectOutputStream(MimeUtility.encode(bo, "base64"))) {
        so.writeObject(value);
        so.flush();
        return bo.toString();
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    }
}
Also used : ConverterException(javax.faces.convert.ConverterException) FacesMessage(javax.faces.application.FacesMessage) ConverterException(javax.faces.convert.ConverterException)

Example 2 with ConverterException

use of javax.faces.convert.ConverterException in project oxCore by GluuFederation.

the class EnumConverter method getAsObjectImpl.

public Object getAsObjectImpl(FacesContext context, UIComponent comp, String value) throws ConverterException {
    ValueExpression expr = comp.getValueExpression("value");
    Class enumType = expr == null ? null : expr.getType(context.getELContext());
    if (enumType != null && enumType.isEnum()) {
        return Enum.valueOf(enumType, value);
    } else {
        for (Object child : comp.getChildren()) {
            if (child instanceof UIComponent) {
                UIComponent c = (UIComponent) child;
                expr = c.getValueExpression("value");
                Object val = expr == null ? null : expr.getValue(context.getELContext());
                if (val == null) {
                    throw new ConverterException("Cannot get items");
                }
                Class t = val.getClass();
                if (t.isArray() && t.getComponentType().isEnum()) {
                    return Enum.valueOf(t.getComponentType(), value);
                } else if (val instanceof Collection) {
                    Object firstItem = ((Collection) val).iterator().next();
                    if (firstItem instanceof Enum) {
                        t = ((Enum) firstItem).getDeclaringClass();
                    } else {
                        t = firstItem.getClass();
                    }
                    return Enum.valueOf(t, value);
                }
            }
        }
    }
    throw new ConverterException("Unable to find selectItems with enum values.");
}
Also used : ConverterException(javax.faces.convert.ConverterException) ValueExpression(javax.el.ValueExpression) UIComponent(javax.faces.component.UIComponent) Collection(java.util.Collection)

Example 3 with ConverterException

use of javax.faces.convert.ConverterException in project ART-TIME by Artezio.

the class Sheet method validate.

/**
 * Converts each submitted value into a local value and stores it back in
 * the hash. If all values convert without error, then the component is
 * valid, and we can proceed to the processUpdates.
 */
@Override
public void validate(FacesContext context) {
    Iterator<Entry<RowColIndex, String>> entries = submittedValues.entrySet().iterator();
    boolean hadBadUpdates = !getBadUpdates().isEmpty();
    getBadUpdates().clear();
    while (entries.hasNext()) {
        final Entry<RowColIndex, String> entry = entries.next();
        final Column column = getColumns().get(entry.getKey().colIndex);
        final String newValue = entry.getValue();
        final Object rowKey = entry.getKey().getRowKey();
        final int col = entry.getKey().getColIndex();
        final RowMap map = rowMap.get(rowKey);
        this.setRowIndex(context, map.sortedIndex);
        // attempt to convert new value from string to correct object type
        // based on column converter. Use PF util as helper
        Converter converter = ComponentUtils.getConverter(context, column);
        // assume string value if converter not found
        Object newValueObj = newValue;
        if (converter != null)
            try {
                newValueObj = converter.getAsObject(context, this, newValue);
            } catch (ConverterException e) {
                // add offending cell to list of bad updates
                // and to a stringbuffer for error messages (so we have one
                // message for the component)
                setValid(false);
                FacesMessage message = e.getFacesMessage();
                if (message == null) {
                    message = new FacesMessage(FacesMessage.SEVERITY_ERROR, e.getMessage(), e.getMessage());
                }
                context.addMessage(this.getClientId(context), message);
                String messageText = message.getDetail();
                this.getBadUpdates().add(new BadUpdate(getRowKeyValue(context), col, column, newValue, messageText));
                continue;
            }
        // value is fine, no further validations (again, not to be confused
        // with validators. until we have a "required" or something like
        // that, nothing else to do).
        setLocalValue(rowKey, col, newValueObj);
        // process validators on column
        column.setValue(newValueObj);
        try {
            column.validate(context);
        } finally {
            column.resetValue();
        }
        entries.remove();
    }
    this.setRowIndex(context, -1);
    final boolean newBadUpdates = !getBadUpdates().isEmpty();
    String errorMessage = this.getErrorMessage();
    if (hadBadUpdates || newBadUpdates) {
        // update the bad data var if partial request
        if (context.getPartialViewContext().isPartialRequest()) {
            this.sortAndFilter();
            this.renderBadUpdateScript(context);
        }
    }
    if (newBadUpdates && errorMessage != null) {
        FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR, errorMessage, errorMessage);
        context.addMessage(null, message);
    }
}
Also used : ConverterException(javax.faces.convert.ConverterException) Entry(java.util.Map.Entry) Converter(javax.faces.convert.Converter) FacesMessage(javax.faces.application.FacesMessage)

Example 4 with ConverterException

use of javax.faces.convert.ConverterException in project microservices by pwillhan.

the class PersistentAttributeConverter method getAttribute.

public SingularAttribute getAttribute(String value) {
    // TODO: This only supports 'Item_.name', not 'User_.address.city.zipCode'
    String entityName = value.substring(0, value.lastIndexOf("."));
    String attributeName = value.substring(entityName.length() + 1);
    SingularAttribute attribute = null;
    for (EntityType<?> entityType : emf.getMetamodel().getEntities()) {
        if (entityType.getName().equals(entityName)) {
            try {
                attribute = entityType.getSingularAttribute(attributeName);
            } catch (IllegalArgumentException ex) {
                throw new ConverterException(new FacesMessage("Persistent entity '" + entityName + "' does not have attribute: " + attributeName));
            }
        }
    }
    if (attribute == null)
        throw new ConverterException(new FacesMessage("Persistent attribute not found: " + value));
    return attribute;
}
Also used : SingularAttribute(javax.persistence.metamodel.SingularAttribute) ConverterException(javax.faces.convert.ConverterException) FacesMessage(javax.faces.application.FacesMessage)

Aggregations

ConverterException (javax.faces.convert.ConverterException)4 FacesMessage (javax.faces.application.FacesMessage)3 Collection (java.util.Collection)1 Entry (java.util.Map.Entry)1 ValueExpression (javax.el.ValueExpression)1 UIComponent (javax.faces.component.UIComponent)1 Converter (javax.faces.convert.Converter)1 SingularAttribute (javax.persistence.metamodel.SingularAttribute)1