001/*
002 * (C) Copyright 2016 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 *     Anahide Tchertchian
018 */
019package org.nuxeo.ecm.platform.ui.web.tag.handler;
020
021import java.io.IOException;
022import java.util.ArrayList;
023import java.util.List;
024
025import javax.faces.component.UIComponent;
026import javax.faces.component.UIForm;
027import javax.faces.component.html.HtmlForm;
028import javax.faces.view.facelets.ComponentConfig;
029import javax.faces.view.facelets.ComponentHandler;
030import javax.faces.view.facelets.FaceletContext;
031import javax.faces.view.facelets.MetaRuleset;
032import javax.faces.view.facelets.MetaTagHandler;
033import javax.faces.view.facelets.TagAttribute;
034import javax.faces.view.facelets.TagConfig;
035
036import com.sun.faces.facelets.tag.TagAttributeImpl;
037import com.sun.faces.facelets.tag.TagAttributesImpl;
038
039/**
040 * Tag handler generating a form (or not) depending on attributes values, useful when handling widgets that may need to
041 * be surrounded by a form (or not) depending on their configuration.
042 *
043 * @since 8.2
044 */
045public class FormTagHandler extends MetaTagHandler {
046
047    protected final TagConfig config;
048
049    protected final TagAttribute id;
050
051    protected final TagAttribute skip;
052
053    protected final TagAttribute disableDoubleClickShield;
054
055    protected final TagAttribute useAjaxForm;
056
057    protected final TagAttribute disableMultipartForm;
058
059    protected final TagAttribute onsubmit;
060
061    protected final TagAttribute styleClass;
062
063    public FormTagHandler(TagConfig config) {
064        super(config);
065        this.config = config;
066
067        id = getAttribute("id");
068        skip = getAttribute("skip");
069        disableDoubleClickShield = getAttribute("disableDoubleClickShield");
070        useAjaxForm = getAttribute("useAjaxForm");
071        disableMultipartForm = getAttribute("disableMultipartForm");
072        onsubmit = getAttribute("onsubmit");
073        styleClass = getAttribute("styleClass");
074    }
075
076    @Override
077    public void apply(FaceletContext ctx, UIComponent parent) throws IOException {
078        // resolve addForm early
079        boolean doAdd = true;
080        if (skip != null) {
081            doAdd = !skip.getBoolean(ctx);
082        }
083
084        if (doAdd) {
085            // wrap in a Form component handler
086            List<TagAttribute> attrs = new ArrayList<TagAttribute>();
087            attrs.addAll(copyAttributes(id, disableDoubleClickShield, onsubmit, styleClass));
088
089            // resolve ajax and multipart behaviors early too
090            boolean useMultiPart = true;
091            if (useAjaxForm != null && useAjaxForm.getBoolean(ctx)) {
092                useMultiPart = false;
093            }
094            if (disableMultipartForm != null && disableMultipartForm.getBoolean(ctx)) {
095                useMultiPart = false;
096            }
097            if (useMultiPart) {
098                attrs.add(createAttribute("enctype", "multipart/form-data"));
099            }
100            ComponentConfig cconfig = TagConfigFactory.createComponentConfig(config, tagId,
101                    new TagAttributesImpl(attrs.toArray(new TagAttribute[] {})), nextHandler, HtmlForm.COMPONENT_TYPE,
102                    UIForm.COMPONENT_TYPE);
103            new ComponentHandler(cconfig).apply(ctx, parent);
104        } else {
105            // apply next directly
106            this.nextHandler.apply(ctx, parent);
107        }
108
109    }
110
111    protected TagAttribute copyAttribute(TagAttribute attribute) {
112        return new TagAttributeImpl(config.getTag().getLocation(), "", attribute.getLocalName(),
113                attribute.getLocalName(), attribute.getValue());
114    }
115
116    protected List<TagAttribute> copyAttributes(TagAttribute... attributes) {
117        List<TagAttribute> res = new ArrayList<TagAttribute>();
118        if (attributes != null) {
119            for (TagAttribute attr : attributes) {
120                if (attr != null) {
121                    res.add(copyAttribute(attr));
122                }
123            }
124        }
125        return res;
126    }
127
128    protected TagAttribute createAttribute(String name, String value) {
129        return new TagAttributeImpl(config.getTag().getLocation(), "", name, name, value);
130    }
131
132    @SuppressWarnings("rawtypes")
133    @Override
134    protected MetaRuleset createMetaRuleset(Class type) {
135        return null;
136    }
137
138}