001/*
002 * (C) Copyright 2006-2007 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     <a href="mailto:at@nuxeo.com">Anahide Tchertchian</a>
018 *
019 * $Id: LayoutRowTagHandler.java 30553 2008-02-24 15:51:31Z atchertchian $
020 */
021
022package org.nuxeo.ecm.platform.forms.layout.facelets;
023
024import java.io.IOException;
025import java.util.ArrayList;
026import java.util.HashMap;
027import java.util.List;
028import java.util.Map;
029
030import javax.el.ELException;
031import javax.el.ValueExpression;
032import javax.faces.FacesException;
033import javax.faces.component.UIComponent;
034import javax.faces.view.facelets.FaceletContext;
035import javax.faces.view.facelets.FaceletHandler;
036import javax.faces.view.facelets.TagAttribute;
037import javax.faces.view.facelets.TagConfig;
038import javax.faces.view.facelets.TagHandler;
039
040import org.apache.commons.logging.Log;
041import org.apache.commons.logging.LogFactory;
042import org.nuxeo.ecm.platform.forms.layout.api.Layout;
043import org.nuxeo.ecm.platform.forms.layout.api.LayoutRow;
044
045/**
046 * Layout row recursion tag handler.
047 * <p>
048 * Iterate over the layout rows and apply next handlers as many times as needed.
049 * <p>
050 * Only works when used inside a tag using the {@link LayoutTagHandler} template client.
051 *
052 * @author <a href="mailto:at@nuxeo.com">Anahide Tchertchian</a>
053 */
054public class LayoutRowTagHandler extends TagHandler {
055
056    private static final Log log = LogFactory.getLog(LayoutRowTagHandler.class);
057
058    protected final TagConfig config;
059
060    public LayoutRowTagHandler(TagConfig config) {
061        super(config);
062        this.config = config;
063    }
064
065    /**
066     * For each row in layout, exposes row variables and applies next handler.
067     * <p>
068     * Needs layout to be exposed in context, so works in conjunction with {@link LayoutTagHandler}.
069     * <p>
070     * Row variables exposed: {@link RenderVariables.rowVariables#layoutRow} and
071     * {@link RenderVariables.rowVariables#layoutRowIndex}, as well as
072     * {@link RenderVariables.columnVariables#layoutColumn} and
073     * {@link RenderVariables.columnVariables#layoutColumnIndex}, that act are aliases.
074     */
075    public void apply(FaceletContext ctx, UIComponent parent) throws IOException, FacesException, ELException {
076        // resolve rows from layout in context
077        Layout layout = null;
078        String layoutVariableName = RenderVariables.layoutVariables.layout.name();
079        FaceletHandlerHelper helper = new FaceletHandlerHelper(config);
080        TagAttribute layoutAttribute = helper.createAttribute(layoutVariableName, "#{" + layoutVariableName + "}");
081        if (layoutAttribute != null) {
082            layout = (Layout) layoutAttribute.getObject(ctx, Layout.class);
083        }
084        if (layout == null) {
085            log.error("Could not resolve layout " + layoutAttribute);
086            return;
087        }
088
089        LayoutRow[] rows = layout.getRows();
090        if (rows == null || rows.length == 0) {
091            return;
092        }
093
094        int rowCounter = 0;
095        for (LayoutRow row : rows) {
096            // expose row variables
097            Map<String, ValueExpression> variables = new HashMap<String, ValueExpression>();
098            ValueExpression rowVe = ctx.getExpressionFactory().createValueExpression(row, LayoutRow.class);
099            variables.put(RenderVariables.rowVariables.layoutRow.name(), rowVe);
100            variables.put(RenderVariables.columnVariables.layoutColumn.name(), rowVe);
101            ValueExpression rowIndexVe = ctx.getExpressionFactory().createValueExpression(Integer.valueOf(rowCounter),
102                    Integer.class);
103            variables.put(RenderVariables.rowVariables.layoutRowIndex.name(), rowIndexVe);
104            variables.put(RenderVariables.columnVariables.layoutColumnIndex.name(), rowIndexVe);
105
106            List<String> blockedPatterns = new ArrayList<String>();
107            blockedPatterns.add(RenderVariables.rowVariables.layoutRow.name());
108            blockedPatterns.add(RenderVariables.rowVariables.layoutRowIndex.name());
109            blockedPatterns.add(RenderVariables.columnVariables.layoutColumn.name());
110            blockedPatterns.add(RenderVariables.columnVariables.layoutColumnIndex.name());
111
112            FaceletHandler handler = helper.getAliasFaceletHandler(row.getTagConfigId(), variables, blockedPatterns,
113                    nextHandler);
114            handler.apply(ctx, parent);
115            rowCounter++;
116        }
117    }
118}