Search in sources :

Example 81 with SystemException

use of com.github.bordertech.wcomponents.util.SystemException in project wcomponents by BorderTech.

the class AbstractCompare method getTriggerValue.

/**
 * Get the value for the trigger.
 * <p>
 * If no request is passed in, the current value of the trigger is used.
 * </p>
 * <p>
 * It will return the same "value" the client would have used in its subordinate logic.
 * </p>
 * <p>
 * The trigger value will either be (i) a date formatted String for WDateFields, (ii) a BigDecimal for WNumberFields
 * or (iii) a List of String values for MultiSelect components or (iv) a String value.
 * </p>
 *
 * @param request the request being processed, can be null
 * @return the value to be used for the trigger
 */
protected Object getTriggerValue(final Request request) {
    // Date Compare (Use Date Formatted String - YYYY-MM-DD)
    if (trigger instanceof WDateField) {
        final WDateField input = (WDateField) trigger;
        Date date;
        if (request == null) {
            date = input.getValue();
        } else {
            date = input.getRequestValue(request);
        }
        return date == null ? null : new SimpleDateFormat(INTERNAL_DATE_FORMAT).format(date);
    } else if (trigger instanceof WNumberField) {
        // Number Compare (Use Number Object)
        final WNumberField input = (WNumberField) trigger;
        if (request == null) {
            return input.getValue();
        } else {
            return input.getRequestValue(request);
        }
    } else if (trigger instanceof AbstractWSingleSelectList) {
        // String Compare for Single Select Lists (Use the Option's Code)
        final AbstractWSingleSelectList list = (AbstractWSingleSelectList) trigger;
        final Object selected;
        if (request == null) {
            selected = list.getValue();
        } else {
            selected = list.getRequestValue(request);
        }
        // Convert selected option to its "code" (Should always have a value)
        String code = list.optionToCode(selected);
        return code;
    } else if (trigger instanceof AbstractWMultiSelectList) {
        // String Compare for Multi Select Lists (Use the Option's Code)
        final AbstractWMultiSelectList list = (AbstractWMultiSelectList) trigger;
        final List<?> selected;
        if (request == null) {
            selected = list.getValue();
        } else {
            selected = list.getRequestValue(request);
        }
        // Empty is treated the same as null
        if (selected == null || selected.isEmpty()) {
            return null;
        }
        // Convert selected options to their "code" (Should always have a value)
        List<String> codes = new ArrayList<>(selected.size());
        for (Object select : selected) {
            String code = list.optionToCode(select);
            codes.add(code);
        }
        return codes;
    } else if (trigger instanceof Input) {
        // String Compare - Use the String Value of the Input
        final Input input = (Input) trigger;
        final Object inputValue;
        if (request == null) {
            inputValue = input.getValue();
        } else {
            inputValue = input.getRequestValue(request);
        }
        // Treat empty the same as null
        return (inputValue == null || Util.empty(inputValue.toString())) ? null : inputValue.toString();
    } else {
        throw new SystemException("Trigger is not a valid type.");
    }
}
Also used : Input(com.github.bordertech.wcomponents.Input) WNumberField(com.github.bordertech.wcomponents.WNumberField) SystemException(com.github.bordertech.wcomponents.util.SystemException) WDateField(com.github.bordertech.wcomponents.WDateField) ArrayList(java.util.ArrayList) List(java.util.List) AbstractWSelectList(com.github.bordertech.wcomponents.AbstractWSelectList) AbstractWSingleSelectList(com.github.bordertech.wcomponents.AbstractWSingleSelectList) AbstractWMultiSelectList(com.github.bordertech.wcomponents.AbstractWMultiSelectList) SimpleDateFormat(java.text.SimpleDateFormat) AbstractWMultiSelectList(com.github.bordertech.wcomponents.AbstractWMultiSelectList) Date(java.util.Date) AbstractWSingleSelectList(com.github.bordertech.wcomponents.AbstractWSingleSelectList)

Example 82 with SystemException

use of com.github.bordertech.wcomponents.util.SystemException in project wcomponents by BorderTech.

the class SubordinateBuilder method build.

/**
 * This builds the SubordinateControl. This method will throw a SystemException if the condition is invalid, or
 * there are no actions specified.
 *
 * @return a SubordinateControl built using this builder.
 */
public WSubordinateControl build() {
    if (!condition().validate()) {
        throw new SystemException("Invalid condition: " + condition);
    }
    if (getActionsWhenTrue().isEmpty() && getActionsWhenFalse().isEmpty()) {
        throw new SystemException("No actions to execute");
    }
    WSubordinateControl subordinate = new WSubordinateControl();
    Rule rule = new Rule();
    BooleanExpression expression = getCondition();
    rule.setCondition(expression.build());
    for (Action action : getActionsWhenTrue()) {
        rule.addActionOnTrue(action.build());
    }
    for (Action action : getActionsWhenFalse()) {
        rule.addActionOnFalse(action.build());
    }
    subordinate.addRule(rule);
    return subordinate;
}
Also used : SystemException(com.github.bordertech.wcomponents.util.SystemException) WSubordinateControl(com.github.bordertech.wcomponents.subordinate.WSubordinateControl) Rule(com.github.bordertech.wcomponents.subordinate.Rule)

Example 83 with SystemException

use of com.github.bordertech.wcomponents.util.SystemException in project wcomponents by BorderTech.

the class PlainTextRendererImpl method renderInline.

/**
 * {@inheritDoc}
 */
@Override
public void renderInline(final String templateInline, final Map<String, Object> context, final Map<String, WComponent> taggedComponents, final Writer writer, final Map<String, Object> options) {
    LOG.debug("Rendering inline plain text template.");
    boolean xmlEncode = options.containsKey(XML_ENCODE);
    try {
        String output = templateInline;
        if (xmlEncode) {
            output = WebUtilities.encode(output);
        }
        writer.write(output);
    } catch (Exception e) {
        throw new SystemException("Problems with inline plain text template. " + e.getMessage(), e);
    }
}
Also used : SystemException(com.github.bordertech.wcomponents.util.SystemException) SystemException(com.github.bordertech.wcomponents.util.SystemException)

Example 84 with SystemException

use of com.github.bordertech.wcomponents.util.SystemException in project wcomponents by BorderTech.

the class VelocityRendererImpl method renderInline.

/**
 * {@inheritDoc}
 */
@Override
public void renderInline(final String templateInline, final Map<String, Object> context, final Map<String, WComponent> taggedComponents, final Writer writer, final Map<String, Object> options) {
    LOG.debug("Rendering inline velocity template.");
    try {
        // Map the tagged components to be used in the replace writer
        Map<String, WComponent> componentsByKey = TemplateUtil.mapTaggedComponents(context, taggedComponents);
        // Setup context
        VelocityContext velocityContext = new VelocityContext();
        for (Map.Entry<String, Object> entry : context.entrySet()) {
            velocityContext.put(entry.getKey(), entry.getValue());
        }
        // Write inline template
        UIContext uic = UIContextHolder.getCurrent();
        try (TemplateWriter velocityWriter = new TemplateWriter(writer, componentsByKey, uic)) {
            getVelocityEngine().evaluate(velocityContext, velocityWriter, templateInline, templateInline);
        }
    } catch (Exception e) {
        throw new SystemException("Problems with inline velocity template." + e.getMessage(), e);
    }
}
Also used : WComponent(com.github.bordertech.wcomponents.WComponent) SystemException(com.github.bordertech.wcomponents.util.SystemException) UIContext(com.github.bordertech.wcomponents.UIContext) VelocityContext(org.apache.velocity.VelocityContext) Map(java.util.Map) SystemException(com.github.bordertech.wcomponents.util.SystemException) ResourceNotFoundException(org.apache.velocity.exception.ResourceNotFoundException)

Example 85 with SystemException

use of com.github.bordertech.wcomponents.util.SystemException in project wcomponents by BorderTech.

the class DateFieldPivotValidator method isValid.

/**
 * {@inheritDoc}
 */
@Override
protected boolean isValid() {
    // Get the date we are validating.
    WDateField dateField = (WDateField) this.getInputField();
    Date date = dateField.getDate();
    if (date == null) {
        // No date, so nothing to validate.
        return true;
    }
    // Determine the pivot date
    Date pivot = null;
    if (variablePivot != null) {
        pivot = variablePivot.getDate();
        if (pivot == null) {
            // No pivot value, so default to true.
            return true;
        }
    } else if (fixedPivot != null) {
        pivot = fixedPivot;
    }
    // We take a null pivot date as meaning "today"
    if (pivot == null) {
        pivot = new Date();
    }
    // Round the dates to nearest day.
    pivot = DateUtilities.roundToDay(pivot);
    date = DateUtilities.roundToDay(date);
    // Perform the comparison with the pivot
    switch(operator) {
        case BEFORE:
            return date.before(pivot);
        case BEFORE_OR_EQUAL:
            return !pivot.before(date);
        case EQUAL:
            return date.equals(pivot);
        case AFTER_OR_EQUAL:
            return !pivot.after(date);
        case AFTER:
            return date.after(pivot);
        default:
            throw new SystemException("Unknown operator. [" + operator + "]");
    }
}
Also used : SystemException(com.github.bordertech.wcomponents.util.SystemException) WDateField(com.github.bordertech.wcomponents.WDateField) Date(java.util.Date)

Aggregations

SystemException (com.github.bordertech.wcomponents.util.SystemException)91 XmlStringBuilder (com.github.bordertech.wcomponents.XmlStringBuilder)17 WComponent (com.github.bordertech.wcomponents.WComponent)15 UIContext (com.github.bordertech.wcomponents.UIContext)14 ComponentWithContext (com.github.bordertech.wcomponents.ComponentWithContext)10 Test (org.junit.Test)9 WebElement (org.openqa.selenium.WebElement)8 WebXmlRenderContext (com.github.bordertech.wcomponents.servlet.WebXmlRenderContext)6 IOException (java.io.IOException)5 Select (org.openqa.selenium.support.ui.Select)5 Environment (com.github.bordertech.wcomponents.Environment)4 PrintWriter (java.io.PrintWriter)4 ArrayList (java.util.ArrayList)4 ActionEscape (com.github.bordertech.wcomponents.ActionEscape)3 Date (java.util.Date)3 List (java.util.List)3 ResourceNotFoundException (org.apache.velocity.exception.ResourceNotFoundException)3 AjaxOperation (com.github.bordertech.wcomponents.AjaxOperation)2 DefaultWComponent (com.github.bordertech.wcomponents.DefaultWComponent)2 OptionGroup (com.github.bordertech.wcomponents.OptionGroup)2