Search in sources :

Example 11 with StateContext

use of com.sun.faces.context.StateContext in project mojarra by eclipse-ee4j.

the class FaceletPartialStateManagementStrategy method saveView.

/**
 * Save the view.
 *
 * @param context the Faces context.
 * @return the saved view.
 */
@Override
public Object saveView(FacesContext context) {
    if (LOGGER.isLoggable(Level.FINEST)) {
        LOGGER.finest("FaceletPartialStateManagementStrategy.saveView");
    }
    if (context == null) {
        return null;
    }
    UIViewRoot viewRoot = context.getViewRoot();
    if (viewRoot.isTransient()) {
        return null;
    }
    Util.checkIdUniqueness(context, viewRoot, new HashSet<>(viewRoot.getChildCount() << 1));
    final Map<String, Object> stateMap = new HashMap<>();
    final StateContext stateContext = StateContext.getStateContext(context);
    context.getAttributes().put(SKIP_ITERATION_HINT, true);
    Set<VisitHint> hints = EnumSet.of(VisitHint.SKIP_ITERATION);
    VisitContext visitContext = VisitContext.createVisitContext(context, null, hints);
    final FacesContext finalContext = context;
    try {
        viewRoot.visitTree(visitContext, (context1, target) -> {
            VisitResult result = VisitResult.ACCEPT;
            Object stateObj;
            if (!target.isTransient()) {
                if (stateContext.componentAddedDynamically(target)) {
                    target.getAttributes().put(DYNAMIC_COMPONENT, target.getParent().getChildren().indexOf(target));
                    stateObj = new StateHolderSaver(finalContext, target);
                } else {
                    stateObj = target.saveState(context1.getFacesContext());
                }
                if (stateObj != null) {
                    stateMap.put(target.getClientId(context1.getFacesContext()), stateObj);
                }
            } else {
                return VisitResult.REJECT;
            }
            return result;
        });
    } finally {
        context.getAttributes().remove(SKIP_ITERATION_HINT);
    }
    saveDynamicActions(context, stateContext, stateMap);
    StateContext.release(context);
    return new Object[] { null, stateMap };
}
Also used : FacesContext(jakarta.faces.context.FacesContext) HashMap(java.util.HashMap) VisitHint(jakarta.faces.component.visit.VisitHint) VisitContext(jakarta.faces.component.visit.VisitContext) StateContext(com.sun.faces.context.StateContext) VisitResult(jakarta.faces.component.visit.VisitResult) UIViewRoot(jakarta.faces.component.UIViewRoot)

Example 12 with StateContext

use of com.sun.faces.context.StateContext in project mojarra by eclipse-ee4j.

the class FaceletPartialStateManagementStrategy method restoreView.

/**
 * Restore the view.
 *
 * @param context the Faces context.
 * @param viewId the view id.
 * @param renderKitId the render kit id.
 * @return the view root.
 */
@Override
public UIViewRoot restoreView(FacesContext context, String viewId, String renderKitId) {
    if (LOGGER.isLoggable(Level.FINEST)) {
        LOGGER.log(Level.FINEST, "FaceletPartialStateManagementStrategy.restoreView", new Object[] { viewId, renderKitId });
    }
    ResponseStateManager rsm = RenderKitUtils.getResponseStateManager(context, renderKitId);
    boolean processingEvents = context.isProcessingEvents();
    UIViewRoot viewRoot = context.getViewRoot();
    Object[] rawState = (Object[]) rsm.getState(context, viewId);
    if (rawState == null) {
        return null;
    }
    @SuppressWarnings("unchecked") final Map<String, Object> state = (Map<String, Object>) rawState[1];
    final StateContext stateContext = StateContext.getStateContext(context);
    if (state != null) {
        try {
            stateContext.setTrackViewModifications(false);
            context.getAttributes().put(SKIP_ITERATION_HINT, true);
            Set<VisitHint> hints = EnumSet.of(VisitHint.SKIP_ITERATION, VisitHint.EXECUTE_LIFECYCLE);
            VisitContext visitContext = VisitContext.createVisitContext(context, null, hints);
            viewRoot.visitTree(visitContext, (context1, target) -> {
                VisitResult result = VisitResult.ACCEPT;
                String cid = target.getClientId(context1.getFacesContext());
                Object stateObj = state.get(cid);
                if (stateObj != null && !stateContext.componentAddedDynamically(target)) {
                    boolean restoreStateNow = true;
                    if (stateObj instanceof StateHolderSaver) {
                        restoreStateNow = !((StateHolderSaver) stateObj).componentAddedDynamically();
                    }
                    if (restoreStateNow) {
                        try {
                            target.restoreState(context1.getFacesContext(), stateObj);
                        } catch (Exception e) {
                            String msg = MessageUtils.getExceptionMessageString(MessageUtils.PARTIAL_STATE_ERROR_RESTORING_ID, cid, e.toString());
                            throw new FacesException(msg, e);
                        }
                    }
                }
                return result;
            });
            restoreDynamicActions(context, stateContext, state);
        } finally {
            stateContext.setTrackViewModifications(true);
            context.getAttributes().remove(SKIP_ITERATION_HINT);
        }
    } else {
        viewRoot = null;
    }
    context.setProcessingEvents(processingEvents);
    return viewRoot;
}
Also used : VisitHint(jakarta.faces.component.visit.VisitHint) VisitContext(jakarta.faces.component.visit.VisitContext) StateContext(com.sun.faces.context.StateContext) FacesException(jakarta.faces.FacesException) FacesException(jakarta.faces.FacesException) ResponseStateManager(jakarta.faces.render.ResponseStateManager) VisitResult(jakarta.faces.component.visit.VisitResult) UIViewRoot(jakarta.faces.component.UIViewRoot) HashMap(java.util.HashMap) Map(java.util.Map)

Example 13 with StateContext

use of com.sun.faces.context.StateContext in project mojarra by eclipse-ee4j.

the class FaceletViewHandlingStrategy method buildView.

/**
 * @see ViewDeclarationLanguage#buildView(FacesContext, UIViewRoot)
 */
@Override
public void buildView(FacesContext ctx, UIViewRoot view) throws IOException {
    StateContext stateCtx = StateContext.getStateContext(ctx);
    if (isViewPopulated(ctx, view)) {
        Facelet facelet = faceletFactory.getFacelet(ctx, view.getViewId());
        // virute of re-applying the handlers.
        try {
            stateCtx.setTrackViewModifications(false);
            facelet.apply(ctx, view);
            reapplyDynamicActions(ctx);
            if (stateCtx.isPartialStateSaving(ctx, view.getViewId())) {
                markInitialStateIfNotMarked(view);
            }
        } finally {
            stateCtx.setTrackViewModifications(true);
        }
        return;
    }
    view.setViewId(view.getViewId());
    LOGGER.log(FINE, () -> "Building View: " + view.getViewId());
    if (faceletFactory == null) {
        faceletFactory = ApplicationAssociate.getInstance(ctx.getExternalContext()).getFaceletFactory();
    }
    RequestStateManager.set(ctx, FACELET_FACTORY, faceletFactory);
    Facelet facelet = faceletFactory.getFacelet(ctx, view.getViewId());
    // populate UIViewRoot
    try {
        ctx.getAttributes().put(IS_BUILDING_INITIAL_STATE, Boolean.TRUE);
        stateCtx.setTrackViewModifications(false);
        facelet.apply(ctx, view);
        if (facelet instanceof XMLFrontMatterSaver) {
            XMLFrontMatterSaver frontMatterSaver = (XMLFrontMatterSaver) facelet;
            Doctype doctype = frontMatterSaver.getSavedDoctype();
            if (doctype != null) {
                saveDOCTYPEToFacesContextAttributes(doctype);
            }
            String XMLDECL = frontMatterSaver.getSavedXMLDecl();
            if (XMLDECL != null) {
                saveXMLDECLToFacesContextAttributes(XMLDECL);
            }
        }
        Doctype doctype = getDOCTYPEFromFacesContextAttributes(ctx);
        if (doctype != null) {
            view.setDoctype(doctype);
        }
        if (!stateCtx.isPartialStateSaving(ctx, view.getViewId())) {
            reapplyDynamicActions(ctx);
        }
        startTrackViewModifications(ctx, view);
    } finally {
        ctx.getAttributes().remove(IS_BUILDING_INITIAL_STATE);
    }
    ctx.getApplication().publishEvent(ctx, PostAddToViewEvent.class, UIViewRoot.class, view);
    markInitialState(ctx, view);
    setViewPopulated(ctx, view);
}
Also used : Facelet(jakarta.faces.view.facelets.Facelet) XMLFrontMatterSaver(com.sun.faces.facelets.impl.XMLFrontMatterSaver) StateContext.getStateContext(com.sun.faces.context.StateContext.getStateContext) StateContext(com.sun.faces.context.StateContext) Doctype(jakarta.faces.component.Doctype)

Example 14 with StateContext

use of com.sun.faces.context.StateContext in project mojarra by eclipse-ee4j.

the class FaceletViewHandlingStrategy method reapplyDynamicActions.

/**
 * Reapply the dynamic actions after Facelets reapply.
 *
 * <p>
 * Note a precondition to this method is that tracking view modifications is turned off during the execution of this
 * method. The caller of this method is responsible for turning tracking view modifications off and on as required.
 * </p>
 *
 * @param context the Faces context.
 */
private void reapplyDynamicActions(FacesContext context) {
    StateContext stateContext = StateContext.getStateContext(context);
    List<ComponentStruct> actions = stateContext.getDynamicActions();
    if (actions != null) {
        for (ComponentStruct action : actions) {
            if (REMOVE.equals(action.getAction())) {
                reapplyDynamicRemove(context, action);
            }
            if (ADD.equals(action.getAction())) {
                reapplyDynamicAdd(context, action);
            }
        }
    }
}
Also used : StateContext.getStateContext(com.sun.faces.context.StateContext.getStateContext) StateContext(com.sun.faces.context.StateContext) ComponentStruct(com.sun.faces.util.ComponentStruct)

Example 15 with StateContext

use of com.sun.faces.context.StateContext in project mojarra by eclipse-ee4j.

the class FaceletViewHandlingStrategy method reapplyDynamicRemove.

/**
 * Reapply the dynamic remove after Facelets reapply.
 *
 * @param context the Faces context.
 * @param struct the component struct.
 */
private void reapplyDynamicRemove(FacesContext context, ComponentStruct struct) {
    UIComponent child = locateComponentByClientId(context, context.getViewRoot(), struct.getClientId());
    if (child != null) {
        StateContext stateContext = StateContext.getStateContext(context);
        stateContext.getDynamicComponents().put(struct.getClientId(), child);
        UIComponent parent = child.getParent();
        if (struct.getFacetName() != null) {
            parent.getFacets().remove(struct.getFacetName());
        } else {
            parent.getChildren().remove(child);
        }
    }
}
Also used : UIComponent(jakarta.faces.component.UIComponent) StateContext.getStateContext(com.sun.faces.context.StateContext.getStateContext) StateContext(com.sun.faces.context.StateContext)

Aggregations

StateContext (com.sun.faces.context.StateContext)15 UIComponent (jakarta.faces.component.UIComponent)7 UIViewRoot (jakarta.faces.component.UIViewRoot)6 VisitHint (jakarta.faces.component.visit.VisitHint)6 StateContext.getStateContext (com.sun.faces.context.StateContext.getStateContext)4 VisitContext (jakarta.faces.component.visit.VisitContext)4 VisitResult (jakarta.faces.component.visit.VisitResult)4 HashMap (java.util.HashMap)4 FacesException (jakarta.faces.FacesException)2 FacesContext (jakarta.faces.context.FacesContext)2 ResponseStateManager (jakarta.faces.render.ResponseStateManager)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 XMLFrontMatterSaver (com.sun.faces.facelets.impl.XMLFrontMatterSaver)1 ComponentStruct (com.sun.faces.util.ComponentStruct)1 ViewHandler (jakarta.faces.application.ViewHandler)1 Doctype (jakarta.faces.component.Doctype)1 ViewDeclarationLanguage (jakarta.faces.view.ViewDeclarationLanguage)1 Facelet (jakarta.faces.view.facelets.Facelet)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1