Search in sources :

Example 1 with FaceletState

use of org.apache.myfaces.view.facelets.tag.faces.FaceletState in project myfaces by apache.

the class ForEachHandler method applyOnRefresh.

private void applyOnRefresh(FaceletContext ctx, FaceletCompositionContext fcc, PageContext pctx, UIComponent parent, String uniqueId, Object src, ValueExpression srcVE, IterationState restoredSavedOption) throws IOException {
    int s = this.getBegin(ctx);
    int e = this.getEnd(ctx);
    int m = this.getStep(ctx);
    Integer sO = this.begin != null ? Integer.valueOf(s) : null;
    Integer eO = this.end != null ? Integer.valueOf(e) : null;
    Integer mO = this.step != null ? Integer.valueOf(m) : null;
    boolean t = this.getTransient(ctx);
    // Refresh, evaluate and synchronize state
    Iterator<?> itr = this.toIterator(src);
    IterationState iterationState = new IterationState();
    iterationState.setCounter(restoredSavedOption.getCounter());
    if (itr != null) {
        int i = 0;
        // move to start
        while (i < s && itr.hasNext()) {
            itr.next();
            i++;
        }
        String v = this.getVarName(ctx);
        String vs = this.getVarStatusName(ctx);
        ValueExpression vO = this.capture(v, pctx);
        ValueExpression vsO = this.capture(vs, pctx);
        int mi = 0;
        Object value = null;
        int stateIndex = 0;
        try {
            boolean first = true;
            while (i <= e && itr.hasNext()) {
                value = itr.next();
                Object[] stateValue = null;
                /*restoredSavedOption.getValueList().get(stateIndex);*/
                String base = null;
                boolean found = false;
                // The important thing here is use the same base for the generated component ids
                // for each element in the iteration that was used on the restore. To do that
                // 
                int stateIndexCheck = stateIndex;
                for (; stateIndexCheck < restoredSavedOption.getValueList().size(); stateIndexCheck++) {
                    stateValue = restoredSavedOption.getValueList().get(stateIndexCheck);
                    if (value.equals(stateValue[1])) {
                        found = true;
                        break;
                    }
                }
                if (found) {
                    stateIndex = stateIndexCheck;
                    base = (String) stateValue[0];
                    stateIndex++;
                } else {
                    // No state, added item, create new count
                    Integer count = iterationState.getCounter();
                    base = count.toString();
                    iterationState.setCounter(count + 1);
                    stateValue = null;
                }
                if (value instanceof Serializable) {
                    iterationState.getValueList().add(new Object[] { base, value, i });
                }
                try {
                    fcc.startComponentUniqueIdSection(base);
                    setVar(ctx, parent, uniqueId, base, t, src, srcVE, value, v, i);
                    boolean last = !itr.hasNext();
                    // set the varStatus
                    if (vs != null) {
                        IterationStatus itrS = new IterationStatus(first, last, i, sO, eO, mO, value);
                        ValueExpression ve;
                        if (t || srcVE == null) {
                            if (srcVE == null) {
                                ve = null;
                            } else {
                                ve = ctx.getExpressionFactory().createValueExpression(itrS, Object.class);
                            }
                        } else {
                            ve = new IterationStatusExpression(itrS);
                        }
                        setVar(ctx, parent, uniqueId, base + "_vs", vs, ve, srcVE);
                    }
                    // setVarStatus(ctx, pctx, t, sO, eO, mO, srcVE, value, vs, first, !itr.hasNext(), i);
                    // execute body
                    // !restoredSavedOption.equals(i)
                    boolean markInitialState = (stateValue == null);
                    boolean oldMarkInitialState = false;
                    Boolean isBuildingInitialState = null;
                    try {
                        if (markInitialState && fcc.isUsingPSSOnThisView()) {
                            // set markInitialState flag
                            oldMarkInitialState = fcc.isMarkInitialState();
                            fcc.setMarkInitialState(true);
                            isBuildingInitialState = (Boolean) ctx.getFacesContext().getAttributes().put(StateManager.IS_BUILDING_INITIAL_STATE, Boolean.TRUE);
                        }
                        this.nextHandler.apply(ctx, parent);
                    } finally {
                        if (markInitialState && fcc.isUsingPSSOnThisView()) {
                            // unset markInitialState flag
                            if (isBuildingInitialState == null) {
                                ctx.getFacesContext().getAttributes().remove(StateManager.IS_BUILDING_INITIAL_STATE);
                            } else {
                                ctx.getFacesContext().getAttributes().put(StateManager.IS_BUILDING_INITIAL_STATE, isBuildingInitialState);
                            }
                            fcc.setMarkInitialState(oldMarkInitialState);
                        }
                    }
                } finally {
                    fcc.endComponentUniqueIdSection(base);
                }
                // increment steps
                mi = 1;
                while (mi < m && itr.hasNext()) {
                    itr.next();
                    mi++;
                    i++;
                }
                i++;
                first = false;
            }
        } finally {
            removeVarAndVarStatus(pctx, v, vO, vs, vsO);
        }
    }
    FaceletState faceletState = ComponentSupport.getFaceletState(ctx, parent, true);
    faceletState.putState(uniqueId, iterationState);
}
Also used : Serializable(java.io.Serializable) ValueExpression(jakarta.el.ValueExpression) FaceletStateValueExpression(org.apache.myfaces.view.facelets.el.FaceletStateValueExpression) FaceletState(org.apache.myfaces.view.facelets.tag.faces.FaceletState)

Example 2 with FaceletState

use of org.apache.myfaces.view.facelets.tag.faces.FaceletState in project myfaces by apache.

the class ForEachHandler method apply.

@Override
public void apply(FaceletContext ctx, UIComponent parent) throws IOException, FacesException, FaceletException, ELException {
    int e = this.getEnd(ctx);
    Object src = null;
    ValueExpression srcVE = null;
    if (this.items != null) {
        srcVE = this.items.getValueExpression(ctx, Object.class);
        src = srcVE.getValue(ctx);
    } else {
        byte[] b = new byte[e + 1];
        for (int i = 0; i < b.length; i++) {
            b[i] = (byte) i;
        }
        src = b;
    }
    FaceletCompositionContext fcc = FaceletCompositionContext.getCurrentInstance(ctx);
    AbstractFaceletContext actx = (AbstractFaceletContext) ctx;
    // Just increment one number to ensure the prefix doesn't conflict later if two
    // c:forEach are close between each other. Note c:forEach is different from
    // c:if tag and doesn't require a section because c:forEach requires to provide
    // multiple sections starting with a specified "base" related to the element
    // position and value in the collection.
    fcc.incrementUniqueComponentId();
    String uniqueId = actx.generateUniqueFaceletTagId(fcc.generateUniqueId(), tagId);
    if (src != null) {
        PageContext pctx = actx.getPageContext();
        // c:forEach is special because it requires FaceletState even if no pss is used.
        FaceletState restoredFaceletState = ComponentSupport.getFaceletState(ctx, parent, false);
        IterationState restoredSavedOption = (restoredFaceletState == null) ? null : (IterationState) restoredFaceletState.getState(uniqueId);
        if (restoredSavedOption != null) {
            if (!PhaseId.RESTORE_VIEW.equals(ctx.getFacesContext().getCurrentPhaseId())) {
                // Refresh, evaluate and synchronize state
                applyOnRefresh(ctx, fcc, pctx, parent, uniqueId, src, srcVE, restoredSavedOption);
            } else {
                // restore view, don't record iteration, use the saved value
                applyOnRestore(ctx, fcc, pctx, parent, uniqueId, src, srcVE, restoredSavedOption);
            }
        } else {
            // First time
            applyFirstTime(ctx, fcc, pctx, parent, uniqueId, src, srcVE);
        }
    }
    if (fcc.isUsingPSSOnThisView() && fcc.isRefreshTransientBuildOnPSS() && !fcc.isRefreshingTransientBuild()) {
        // Mark the parent component to be saved and restored fully.
        ComponentSupport.markComponentToRestoreFully(ctx.getFacesContext(), parent);
    }
    if (fcc.isDynamicComponentSection()) {
        ComponentSupport.markComponentToRefreshDynamically(ctx.getFacesContext(), parent);
    }
}
Also used : FaceletCompositionContext(org.apache.myfaces.view.facelets.FaceletCompositionContext) ValueExpression(jakarta.el.ValueExpression) FaceletStateValueExpression(org.apache.myfaces.view.facelets.el.FaceletStateValueExpression) PageContext(org.apache.myfaces.view.facelets.PageContext) FaceletState(org.apache.myfaces.view.facelets.tag.faces.FaceletState) AbstractFaceletContext(org.apache.myfaces.view.facelets.AbstractFaceletContext)

Example 3 with FaceletState

use of org.apache.myfaces.view.facelets.tag.faces.FaceletState in project myfaces by apache.

the class ForEachHandler method setVar.

private void setVar(FaceletContext ctx, UIComponent parent, String uniqueId, String base, String v, ValueExpression ve, ValueExpression srcVE) {
    AbstractFaceletContext actx = ((AbstractFaceletContext) ctx);
    PageContext pctx = actx.getPageContext();
    if (srcVE != null) {
        FaceletState faceletState = ComponentSupport.getFaceletState(ctx, parent, true);
        faceletState.putBinding(uniqueId, base, ve);
        // Put the indirect EL into context
        ValueExpression fve = new FaceletStateValueExpression(uniqueId, base);
        pctx.getAttributes().put(v, fve);
    } else {
        pctx.getAttributes().put(v, ve);
    }
}
Also used : FaceletStateValueExpression(org.apache.myfaces.view.facelets.el.FaceletStateValueExpression) ValueExpression(jakarta.el.ValueExpression) FaceletStateValueExpression(org.apache.myfaces.view.facelets.el.FaceletStateValueExpression) PageContext(org.apache.myfaces.view.facelets.PageContext) FaceletState(org.apache.myfaces.view.facelets.tag.faces.FaceletState) AbstractFaceletContext(org.apache.myfaces.view.facelets.AbstractFaceletContext)

Example 4 with FaceletState

use of org.apache.myfaces.view.facelets.tag.faces.FaceletState in project myfaces by apache.

the class ViewPoolMyFacesRequestTestCase method testPartialPage1_1.

@Test
public void testPartialPage1_1() throws Exception {
    Locale locale = null;
    startViewRequest("/partialPage1.xhtml");
    processLifecycleExecute();
    locale = facesContext.getViewRoot().getLocale();
    executeBuildViewCycle(facesContext);
    // Now let's try to remove some component programatically
    // that invalidates the view to be reused without a refresh,
    // so in the pool it should be marked as REFRESH_REQUIRED
    UIPanel panel = (UIPanel) facesContext.getViewRoot().findComponent("mainForm:panel1");
    panel.getParent().getChildren().remove(panel);
    UICommand submitButton = (UICommand) facesContext.getViewRoot().findComponent("mainForm:submit");
    executeViewHandlerRender(facesContext);
    executeAfterRender(facesContext);
    client.submit(submitButton);
    processLifecycleExecute();
    FaceletState faceletState = (FaceletState) facesContext.getViewRoot().getAttributes().get(ComponentSupport.FACELET_STATE_INSTANCE);
    UIViewRoot root = new UIViewRoot();
    root.setLocale(locale);
    root.setRenderKitId("HTML_BASIC");
    root.setViewId("/partialPage1.xhtml");
    ViewPoolProcessor processor = ViewPoolProcessor.getInstance(facesContext);
    ViewPool viewPool = processor.getViewPool(facesContext, root);
    // Check the view was used
    ViewEntry entry2 = viewPool.popStaticOrPartialStructureView(facesContext, root);
    Assert.assertNull(entry2);
    ViewEntry entry3 = viewPool.popDynamicStructureView(facesContext, root, faceletState);
    Assert.assertNull(entry3);
}
Also used : Locale(java.util.Locale) UIPanel(jakarta.faces.component.UIPanel) UICommand(jakarta.faces.component.UICommand) FaceletState(org.apache.myfaces.view.facelets.tag.faces.FaceletState) ViewPoolProcessor(org.apache.myfaces.view.facelets.ViewPoolProcessor) UIViewRoot(jakarta.faces.component.UIViewRoot) Test(org.junit.Test)

Example 5 with FaceletState

use of org.apache.myfaces.view.facelets.tag.faces.FaceletState in project myfaces by apache.

the class ViewPoolMyFacesRequestTestCase method testPartialPage1_2.

@Test
public void testPartialPage1_2() throws Exception {
    Locale locale = null;
    startViewRequest("/partialPage1.xhtml");
    processLifecycleExecute();
    locale = facesContext.getViewRoot().getLocale();
    executeBuildViewCycle(facesContext);
    // Now let's try to remove some component programatically
    // that invalidates the view to be reused without a refresh,
    // so in the pool it should be marked as REFRESH_REQUIRED
    UIPanel panel = (UIPanel) facesContext.getViewRoot().findComponent("mainForm:panel1");
    panel.getParent().getChildren().remove(panel);
    facesContext.getViewRoot().getViewMap().put("someKey", "someValue");
    UICommand submitButton = (UICommand) facesContext.getViewRoot().findComponent("mainForm:submit");
    executeViewHandlerRender(facesContext);
    executeAfterRender(facesContext);
    client.submit(submitButton);
    processLifecycleExecute();
    Assert.assertEquals("someValue", facesContext.getViewRoot().getViewMap().get("someKey"));
    FaceletState faceletState = (FaceletState) facesContext.getViewRoot().getAttributes().get(ComponentSupport.FACELET_STATE_INSTANCE);
    UIViewRoot root = new UIViewRoot();
    root.setLocale(locale);
    root.setRenderKitId("HTML_BASIC");
    root.setViewId("/partialPage1.xhtml");
    ViewPoolProcessor processor = ViewPoolProcessor.getInstance(facesContext);
    ViewPool viewPool = processor.getViewPool(facesContext, root);
    // Check the view was used
    ViewEntry entry2 = viewPool.popStaticOrPartialStructureView(facesContext, root);
    Assert.assertNull(entry2);
    ViewEntry entry3 = viewPool.popDynamicStructureView(facesContext, root, faceletState);
    Assert.assertNull(entry3);
}
Also used : Locale(java.util.Locale) UIPanel(jakarta.faces.component.UIPanel) UICommand(jakarta.faces.component.UICommand) FaceletState(org.apache.myfaces.view.facelets.tag.faces.FaceletState) ViewPoolProcessor(org.apache.myfaces.view.facelets.ViewPoolProcessor) UIViewRoot(jakarta.faces.component.UIViewRoot) Test(org.junit.Test)

Aggregations

FaceletState (org.apache.myfaces.view.facelets.tag.faces.FaceletState)24 UIViewRoot (jakarta.faces.component.UIViewRoot)16 Locale (java.util.Locale)13 ViewPoolProcessor (org.apache.myfaces.view.facelets.ViewPoolProcessor)13 Test (org.junit.Test)13 UICommand (jakarta.faces.component.UICommand)10 ValueExpression (jakarta.el.ValueExpression)7 UIPanel (jakarta.faces.component.UIPanel)6 FaceletStateValueExpression (org.apache.myfaces.view.facelets.el.FaceletStateValueExpression)6 AbstractFaceletContext (org.apache.myfaces.view.facelets.AbstractFaceletContext)4 ViewPool (org.apache.myfaces.view.facelets.pool.ViewPool)3 FacesContext (jakarta.faces.context.FacesContext)2 Serializable (java.io.Serializable)2 FaceletCompositionContext (org.apache.myfaces.view.facelets.FaceletCompositionContext)2 PageContext (org.apache.myfaces.view.facelets.PageContext)2 FacesException (jakarta.faces.FacesException)1 UIForm (jakarta.faces.component.UIForm)1 UIOutput (jakarta.faces.component.UIOutput)1 ResponseStateManager (jakarta.faces.render.ResponseStateManager)1 ViewDeclarationLanguage (jakarta.faces.view.ViewDeclarationLanguage)1