Example usage for javax.el ValueExpression getValue

List of usage examples for javax.el ValueExpression getValue

Introduction

In this page you can find the example usage for javax.el ValueExpression getValue.

Prototype

public abstract Object getValue(ELContext context);

Source Link

Usage

From source file:com.lassitercg.faces.components.sheet.Sheet.java

/**
 * Override to update model with local values. Note that this is where
 * things can be fragile in that we can successfully update some values and
 * fail on others. There is no clean way to roll back the updates, but we
 * also need to fail processing.//  ww  w. ja va  2  s  .c om
 * <p>
 * TODO consider keeping old values as we update (need for event anyhow) and
 * if there is a failure attempt to roll back by updating successful model
 * updates with the old value. This may not all be necessary.
 */
@Override
public void updateModel(FacesContext context) {
    HashSet<Object> dirtyRows = new HashSet<Object>();
    Iterator<Entry<RowColIndex, String>> commentsEntries = comments.entrySet().iterator();
    while (commentsEntries.hasNext()) {
        final Entry<RowColIndex, String> entry = commentsEntries.next();
        final String newValue = entry.getValue();
        final Object rowKey = entry.getKey().getRowKey();
        final int col = entry.getKey().getColIndex();
        final Column column = getColumns().get(col);
        final RowMap map = rowMap.get(rowKey);

        String oldValue = null;
        ValueExpression valueExpression = column.getValueExpression("comment");
        if (valueExpression != null) {
            oldValue = (String) valueExpression.getValue(context.getELContext());
        }
        commentsEntries.remove();
        if (!StringUtils.equals(oldValue, newValue)) {
            appendUpdateEvent(map.sortedIndex, col, map.value, oldValue, newValue);
            dirtyRows.add(rowKey);
        }
    }

    Iterator<Entry<RowColIndex, Object>> entries = localValues.entrySet().iterator();
    // Keep track of the dirtied rows for ajax callbacks so we can send
    // updates on what was touched      
    while (entries.hasNext()) {
        final Entry<RowColIndex, Object> entry = entries.next();

        final Object newValue = entry.getValue();
        final Object rowKey = entry.getKey().getRowKey();
        final int col = entry.getKey().getColIndex();
        final Column column = getColumns().get(col);
        final RowMap map = rowMap.get(rowKey);
        this.setRowIndex(context, map.sortedIndex);

        //         System.out.println("Local key=" + rowKey + " and sortedRow is " + map.sortedIndex);

        ValueExpression ve = column.getValueExpression(PropertyKeys.value.name());
        ELContext elContext = context.getELContext();
        Object oldValue = ve.getValue(elContext);
        if (!column.isReadonlyCell()) {
            ve.setValue(elContext, newValue);
        }
        entries.remove();
        appendUpdateEvent(map.sortedIndex, col, map.value, oldValue, newValue);
        dirtyRows.add(rowKey);
    }
    setLocalValueSet(false);
    setRowIndex(context, -1);

    this.sortAndFilter();

    if (context.getPartialViewContext().isPartialRequest())
        this.renderRowUpdateScript(context, dirtyRows);
}

From source file:org.primefaces.extensions.component.sheet.Sheet.java

/**
 * Gets the row header text value as a string for use in javascript
 *
 * @param context//from  w  w  w .j a v a2s  .c  om
 * @return
 */
protected String getRowHeaderValueAsString(final FacesContext context) {
    final ValueExpression veRowHeader = getRowHeaderValueExpression();
    final Object value = veRowHeader.getValue(context.getELContext());
    if (value == null) {
        return StringUtils.EMPTY;
    } else {
        return value.toString();
    }
}

From source file:org.primefaces.extensions.component.sheet.Sheet.java

/**
 * Override to update model with local values. Note that this is where things can be fragile in that we can successfully update some values and fail on
 * others. There is no clean way to roll back the updates, but we also need to fail processing. TODO consider keeping old values as we update (need for
 * event anyhow) and if there is a failure attempt to roll back by updating successful model updates with the old value. This may not all be necessary.
 */// w ww  .  ja v  a2s  . c  om
@Override
public void updateModel(final FacesContext context) {
    final Iterator<Entry<SheetRowColIndex, Object>> entries = localValues.entrySet().iterator();
    // Keep track of the dirtied rows for ajax callbacks so we can send
    // updates on what was touched
    final HashSet<String> dirtyRows = new HashSet<String>();
    while (entries.hasNext()) {
        final Entry<SheetRowColIndex, Object> entry = entries.next();

        final Object newValue = entry.getValue();
        final String rowKey = entry.getKey().getRowKey();
        final int col = entry.getKey().getColIndex();
        final SheetColumn column = getColumns().get(col);
        setRowVar(context, rowKey);
        final Object rowVal = rowMap.get(rowKey);

        final ValueExpression ve = column.getValueExpression(PropertyKeys.value.name());
        final ELContext elContext = context.getELContext();
        final Object oldValue = ve.getValue(elContext);
        ve.setValue(elContext, newValue);
        entries.remove();
        appendUpdateEvent(getRowKeyValue(context), col, rowVal, oldValue, newValue);
        dirtyRows.add(rowKey);
    }
    setLocalValueSet(false);
    setRowVar(context, null);

    if (context.getPartialViewContext().isPartialRequest()) {
        renderRowUpdateScript(context, dirtyRows);
    }
}

From source file:org.primefaces.extensions.component.sheet.Sheet.java

/**
 * Gets the rowKey for the current row/*from   ww w .j a va  2s.  c o  m*/
 *
 * @param context the faces context
 * @return a row key value or null if the expression is not set
 */
protected Object getRowKeyValue(final FacesContext context) {
    final ValueExpression veRowKey = getValueExpression(PropertyKeys.rowKey.name());
    if (veRowKey == null) {
        throw new RuntimeException("RowKey required on sheet!");
    }
    final Object value = veRowKey.getValue(context.getELContext());
    if (value == null) {
        throw new RuntimeException("RowKey must resolve to non-null value for updates to work properly");
    }
    return value;
}

From source file:org.richfaces.skin.SkinFactoryImpl.java

/**
 * Calculate name for current skin. For EL init parameter store value
 * binding for speed calculations.//  w  ww  . j  a  v  a  2s. c  om
 * 
 * @param context
 * @param useBase
 * @return name of currens skin from init parameter ( "DEFAULT" if no
 *         parameter ) or {@link Skin } as result of evaluation EL
 *         expression.
 */
protected Object getSkinOrName(FacesContext context, boolean useBase) {
    // Detect skin name
    ValueExpression binding;
    String skin;

    synchronized (this) {
        if (useBase) {
            binding = baseSkinBinding;
            skin = baseSkinName;
        } else {
            binding = skinBinding;
            skin = skinName;
        }

        if (binding == null && skin == null) {
            String currentSkinName = context.getExternalContext()
                    .getInitParameter(useBase ? BASE_SKIN_PARAMETER : SKIN_PARAMETER);
            if (null == currentSkinName) {
                // Check for a old ( deprecated ) parameter name.
                currentSkinName = context.getExternalContext()
                        .getInitParameter(useBase ? A4J_BASE_SKIN_PARAMETER : A4J_SKIN_PARAMETER);
                if (null != currentSkinName) {
                    log.warn("Init parameter for a skin name changed to "
                            + (useBase ? BASE_SKIN_PARAMETER : SKIN_PARAMETER));
                }

            }
            if (currentSkinName == null) {
                // not set - usr default.
                return DEFAULT_SKIN_NAME;
            }
            if (ELUtils.isValueReference(currentSkinName)) {
                // For EL expression as skin name
                binding = context.getApplication().getExpressionFactory()
                        .createValueExpression(context.getELContext(), currentSkinName, Object.class);
            } else {
                skin = currentSkinName;
            }

            if (useBase) {
                baseSkinBinding = binding;
                baseSkinName = skin;
            } else {
                skinBinding = binding;
                skinName = skin;
            }
        }

        // }
    }
    if (binding != null) {
        return binding.getValue(context.getELContext());
    } else {
        return skin;
    }
}

From source file:org.apache.myfaces.application.ApplicationImpl.java

@Override
public UIComponent createComponent(FacesContext context, Resource componentResource) {
    checkNull(context, "context");
    checkNull(componentResource, "componentResource");

    UIComponent component = null;/*from w  ww . java 2s  . c  o  m*/
    Resource resource;
    String fqcn;
    Class<? extends UIComponent> componentClass = null;

    /*
     * Obtain a reference to the ViewDeclarationLanguage for this Application instance by calling
     * ViewHandler.getViewDeclarationLanguage(javax.faces.context.FacesContext, java.lang.String), passing the
     * viewId found by calling UIViewRoot.getViewId() on the UIViewRoot in the argument FacesContext.
     */
    UIViewRoot view = context.getViewRoot();
    Application application = context.getApplication();
    ViewDeclarationLanguage vdl = application.getViewHandler().getViewDeclarationLanguage(context,
            view.getViewId());

    /*
     * Obtain a reference to the composite component metadata for this composite component by calling
     * ViewDeclarationLanguage.getComponentMetadata(javax.faces.context.FacesContext,
     * javax.faces.application.Resource), passing the facesContext and componentResource arguments to this method.
     * This version of JSF specification uses JavaBeans as the API to the component metadata.
     */
    BeanInfo metadata = vdl.getComponentMetadata(context, componentResource);
    if (metadata == null) {
        throw new FacesException("Could not get component metadata for " + componentResource.getResourceName()
                + ". Did you forget to specify <composite:interface>?");
    }

    /*
     * Determine if the component author declared a component-type for this component instance by obtaining the
     * BeanDescriptor from the component metadata and calling its getValue() method, passing
     * UIComponent.COMPOSITE_COMPONENT_TYPE_KEY as the argument. If non-null, the result must be a ValueExpression
     * whose value is the component-type of the UIComponent to be created for this Resource component. Call through
     * to createComponent(java.lang.String) to create the component.
     */
    BeanDescriptor descriptor = metadata.getBeanDescriptor();
    ValueExpression componentType = (ValueExpression) descriptor
            .getValue(UIComponent.COMPOSITE_COMPONENT_TYPE_KEY);
    boolean annotationsApplied = false;
    if (componentType != null) {
        component = application.createComponent((String) componentType.getValue(context.getELContext()));
        annotationsApplied = true;
    } else {
        /*
         * Otherwise, determine if a script based component for this Resource can be found by calling
         * ViewDeclarationLanguage.getScriptComponentResource(javax.faces.context.FacesContext,
         * javax.faces.application.Resource). If the result is non-null, and is a script written in one of the
         * languages listed in JSF 4.3 of the specification prose document, create a UIComponent instance from the
         * script resource.
         */
        resource = vdl.getScriptComponentResource(context, componentResource);
        if (resource != null) {
            String name = resource.getResourceName();
            String className = name.substring(0, name.lastIndexOf('.'));

            component = (UIComponent) ClassUtils.newInstance(className);
        } else {
            /*
             * Otherwise, let library-name be the return from calling Resource.getLibraryName() on the argument
             * componentResource and resource-name be the return from calling Resource.getResourceName() on the
             * argument componentResource. Create a fully qualified Java class name by removing any file extension
             * from resource-name and let fqcn be library-name + "." + resource-name. If a class with the name of
             * fqcn cannot be found, take no action and continue to the next step. If any of 
             * InstantiationException,
             * IllegalAccessException, or ClassCastException are thrown, wrap the exception in a FacesException and
             * re-throw it. If any other exception is thrown, log the exception and continue to the next step.
             */

            boolean isProduction = FacesContext.getCurrentInstance().isProjectStage(ProjectStage.Production);
            String name = componentResource.getResourceName();
            String className = name.substring(0, name.lastIndexOf('.'));
            fqcn = componentResource.getLibraryName() + "." + className;

            if (isProduction) {
                componentClass = (Class<? extends UIComponent>) _componentClassMap.get(fqcn);
            }
            if (componentClass == null) {
                try {
                    componentClass = ClassUtils.classForName(fqcn);
                    if (isProduction) {
                        _componentClassMap.put(fqcn, componentClass);
                    }
                } catch (ClassNotFoundException e) {
                    // Remember here that classForName did not find Class
                    if (isProduction) {
                        _componentClassMap.put(fqcn, NOTHING.getClass());
                    }
                }
            }

            if (componentClass != null && NOTHING.getClass() != componentClass) {
                try {
                    component = componentClass.newInstance();
                } catch (InstantiationException e) {
                    log.log(Level.SEVERE, "Could not instantiate component class name = " + fqcn, e);
                    throw new FacesException("Could not instantiate component class name = " + fqcn, e);
                } catch (IllegalAccessException e) {
                    log.log(Level.SEVERE, "Could not instantiate component class name = " + fqcn, e);
                    throw new FacesException("Could not instantiate component class name = " + fqcn, e);
                } catch (Exception e) {
                    log.log(Level.SEVERE, "Could not instantiate component class name = " + fqcn, e);
                }
            }

            /*
             * If none of the previous steps have yielded a UIComponent instance, call
             * createComponent(java.lang.String) passing "javax.faces.NamingContainer" as the argument.
             */
            if (component == null) {
                component = application.createComponent(context, UINamingContainer.COMPONENT_TYPE, null);
                annotationsApplied = true;
            }
        }
    }

    /*
     * Call UIComponent.setRendererType(java.lang.String) on the UIComponent instance, passing
     * "javax.faces.Composite" as the argument.
     */
    component.setRendererType("javax.faces.Composite");

    /*
     * Store the argument Resource in the attributes Map of the UIComponent under the key,
     * Resource.COMPONENT_RESOURCE_KEY.
     */
    component.getAttributes().put(Resource.COMPONENT_RESOURCE_KEY, componentResource);

    /*
     * Store composite component metadata in the attributes Map of the UIComponent under the key,
     * UIComponent.BEANINFO_KEY.
     */
    component.getAttributes().put(UIComponent.BEANINFO_KEY, metadata);

    /*
     * Before the component instance is returned, it must be inspected for the presence of a 
     * ListenerFor annotation.
     * If this annotation is present, the action listed in ListenerFor must be taken on the component, 
     * before it is
     * returned from this method.
     */
    if (!annotationsApplied) {
        _handleAnnotations(context, component, component);
    }

    return component;
}

From source file:org.apache.myfaces.application.ApplicationImpl.java

@Override
public final UIComponent createComponent(final ValueExpression componentExpression,
        final FacesContext facesContext, final String componentType)
        throws FacesException, NullPointerException {

    /*/*from  ww w  . j  a v  a 2 s  .  co m*/
     * Before the component instance is returned, it must be inspected for the presence of a ListenerFor (or
     * ListenersFor) or ResourceDependency (or ResourceDependencies) annotation. If any of these annotations are
     * present, the action listed in ListenerFor or ResourceDependency must be taken on the component, 
     * before it is
     * returned from this method. This variant of createComponent must not inspect the Renderer for the 
     * component to
     * be returned for any of the afore mentioned annotations. Such inspection is the province of
     */

    checkNull(componentExpression, "componentExpression");
    checkNull(facesContext, "facesContext");
    checkNull(componentType, "componentType");

    ELContext elContext = facesContext.getELContext();

    try {
        Object retVal = componentExpression.getValue(elContext);

        UIComponent createdComponent;

        if (retVal instanceof UIComponent) {
            createdComponent = (UIComponent) retVal;
            _handleAnnotations(facesContext, createdComponent, createdComponent);
        } else {
            createdComponent = createComponent(facesContext, componentType);
            componentExpression.setValue(elContext, createdComponent);
        }

        return createdComponent;
    } catch (FacesException e) {
        throw e;
    } catch (Exception e) {
        throw new FacesException(e);
    }
}