001/* 002 * (C) Copyright 2014 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.directory; 020 021import java.io.IOException; 022import java.util.ArrayList; 023import java.util.List; 024 025import javax.faces.component.UIComponent; 026import javax.faces.component.html.HtmlSelectOneListbox; 027import javax.faces.view.facelets.ComponentConfig; 028import javax.faces.view.facelets.FaceletContext; 029import javax.faces.view.facelets.FaceletHandler; 030import javax.faces.view.facelets.MetaRuleset; 031import javax.faces.view.facelets.MetaTagHandler; 032import javax.faces.view.facelets.TagAttribute; 033import javax.faces.view.facelets.TagAttributes; 034import javax.faces.view.facelets.TagConfig; 035 036import org.apache.commons.lang.StringUtils; 037import org.apache.commons.logging.Log; 038import org.apache.commons.logging.LogFactory; 039import org.nuxeo.ecm.platform.ui.web.tag.handler.GenericHtmlComponentHandler; 040import org.nuxeo.ecm.platform.ui.web.tag.handler.TagConfigFactory; 041 042import com.sun.faces.facelets.tag.TagAttributeImpl; 043import com.sun.faces.facelets.tag.TagAttributesImpl; 044 045/** 046 * Legacy class for nxdir:selectOneListbox tag 047 * 048 * @since 6.0 049 */ 050public class DirectorySelectOneListboxHandler extends MetaTagHandler { 051 052 private static final Log log = LogFactory.getLog(DirectorySelectOneListboxHandler.class); 053 054 protected enum DeprecatedPropertyKeys { 055 displayValueOnly; 056 } 057 058 static final List<String> deprecatedProps = new ArrayList<>(); 059 static { 060 for (DeprecatedPropertyKeys item : DeprecatedPropertyKeys.values()) { 061 deprecatedProps.add(item.name()); 062 } 063 } 064 065 protected enum OptionPropertyKeys { 066 directoryName, localize, displayIdAndLabel, ordering, caseSensistive, 067 // 068 displayObsoleteEntries, notDisplayDefaultOption, filter; 069 } 070 071 static List<String> optionProps = new ArrayList<>(); 072 static { 073 for (OptionPropertyKeys item : OptionPropertyKeys.values()) { 074 optionProps.add(item.name()); 075 } 076 } 077 078 protected final TagConfig tagConfig; 079 080 protected List<TagAttribute> select; 081 082 protected List<TagAttribute> options; 083 084 public DirectorySelectOneListboxHandler(TagConfig config) { 085 super(config); 086 tagConfig = config; 087 initAttributes(tag.getAttributes().getAll()); 088 } 089 090 protected void initAttributes(TagAttribute[] attrs) { 091 select = new ArrayList<>(); 092 options = new ArrayList<>(); 093 boolean orderingFound = false; 094 if (attrs != null) { 095 for (TagAttribute attr : attrs) { 096 String name = attr.getLocalName(); 097 if (StringUtils.equals(name, OptionPropertyKeys.ordering.name())) { 098 orderingFound = true; 099 } 100 if (optionProps.contains(name)) { 101 options.add(attr); 102 } else { 103 if (deprecatedProps.contains(name)) { 104 log.error(String.format("Property %s is not taken into account " 105 + "anymore on tag nxdir:selectOneListbox", name)); 106 } 107 select.add(attr); 108 } 109 } 110 } 111 // add default needed values for lookup to work ok 112 options.add(getTagAttribute("var", "item")); 113 options.add(getTagAttribute("itemValue", "#{item.id}")); 114 // Check if the attribute "ordering" has been defined, otherwise, the default value is added 115 if (!orderingFound) { 116 options.add(getTagAttribute(OptionPropertyKeys.ordering.name(), "label")); 117 } 118 } 119 120 protected TagAttribute getTagAttribute(String name, String value) { 121 return new TagAttributeImpl(tagConfig.getTag().getLocation(), tagConfig.getTag().getNamespace(), name, name, 122 value); 123 } 124 125 @Override 126 public void apply(FaceletContext ctx, UIComponent parent) throws IOException { 127 // generate component handlers to be used instead 128 TagAttributes optionAttributes = new TagAttributesImpl(options.toArray(new TagAttribute[] {})); 129 ComponentConfig optionsConfig = TagConfigFactory.createComponentConfig(tagConfig, tagConfig.getTagId(), 130 optionAttributes, new FaceletHandler() { 131 @Override 132 public void apply(FaceletContext ctx, UIComponent parent) throws IOException { 133 // do nothing 134 } 135 }, UIDirectorySelectItems.COMPONENT_TYPE, null); 136 FaceletHandler optionsHandler = new GenericHtmlComponentHandler(optionsConfig); 137 TagAttributes selectAttributes = new TagAttributesImpl(select.toArray(new TagAttribute[] {})); 138 ComponentConfig selectConfig = TagConfigFactory.createComponentConfig(tagConfig, tagConfig.getTagId(), 139 selectAttributes, optionsHandler, getSelectComponentType(), null); 140 new GenericHtmlComponentHandler(selectConfig).apply(ctx, parent); 141 } 142 143 protected String getSelectComponentType() { 144 return HtmlSelectOneListbox.COMPONENT_TYPE; 145 } 146 147 @Override 148 @SuppressWarnings("rawtypes") 149 protected MetaRuleset createMetaRuleset(Class type) { 150 return null; 151 } 152 153}