001// Copyright 2007, 2008, 2009, 2010, 2011 The Apache Software Foundation
002//
003// Licensed under the Apache License, Version 2.0 (the "License");
004// you may not use this file except in compliance with the License.
005// You may obtain a copy of the License at
006//
007// http://www.apache.org/licenses/LICENSE-2.0
008//
009// Unless required by applicable law or agreed to in writing, software
010// distributed under the License is distributed on an "AS IS" BASIS,
011// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012// See the License for the specific language governing permissions and
013// limitations under the License.
014
015package org.apache.tapestry5.internal.beaneditor;
016
017import org.apache.tapestry5.beaneditor.BeanModel;
018import org.apache.tapestry5.ioc.internal.util.InternalCommonsUtils;
019
020/**
021 * Utilities used in a few places to modify an existing {@link BeanModel}.
022 */
023public final class BeanModelUtils
024{
025
026    final private static String[] EMPTY_STRING_ARRAY = new String[0];
027
028    /**
029     * Performs standard set of modifications to a {@link org.apache.tapestry5.beaneditor.BeanModel}
030     * . First new
031     * properties may be added, then properties removed, then properties reordered.
032     *
033     * @param model                to modifiy
034     * @param addPropertyNames     comma seperated list of property names to add, or null
035     * @param includePropertyNames comma seperated list of property names to include
036     * @param excludePropertyNames comma seperated list of property names to exclude, or null
037     * @param reorderPropertyNames comma seperated list of property names to reorder, or null
038     */
039    public static void modify(BeanModel model, String addPropertyNames,
040                              String includePropertyNames, String excludePropertyNames, String reorderPropertyNames)
041    {
042        if (addPropertyNames != null)
043            add(model, addPropertyNames);
044
045        if (includePropertyNames != null)
046            include(model, join(includePropertyNames, addPropertyNames));
047
048        if (excludePropertyNames != null)
049            exclude(model, excludePropertyNames);
050
051        if (reorderPropertyNames != null)
052            reorder(model, reorderPropertyNames);
053    }
054
055    private static final String join(String firstList, String optionalSecondList)
056    {
057        if (InternalCommonsUtils.isBlank(optionalSecondList))
058            return firstList;
059
060        return firstList + "," + optionalSecondList;
061    }
062
063    /**
064     * Adds empty properties to the bean model. New properties are added with a <em>null</em>
065     * {@link org.apache.tapestry5.PropertyConduit}. `
066     *
067     * @param model         to be modified
068     * @param propertyNames comma-separated list of property names
069     * @see BeanModel#addEmpty(String)
070     */
071    public static void add(BeanModel model, String propertyNames)
072    {
073        for (String name : split(propertyNames))
074        {
075            model.addEmpty(name);
076        }
077    }
078
079    /**
080     * Removes properties from the bean model.
081     *
082     * @param model
083     * @param propertyNames comma-separated list of property names
084     * @see BeanModel#exclude(String...)
085     */
086    public static void exclude(BeanModel model, String propertyNames)
087    {
088        model.exclude(split(propertyNames));
089    }
090
091    /**
092     * Selects a subset of the properties to keep, and reorders them.
093     */
094    public static void include(BeanModel model, String propertyNames)
095    {
096        model.include(split(propertyNames));
097    }
098
099    /**
100     * Reorders properties within the bean model.
101     *
102     * @param model
103     * @param propertyNames comma-separated list of property names
104     * @see BeanModel#reorder(String...)
105     */
106    public static void reorder(BeanModel model, String propertyNames)
107    {
108        model.reorder(split(propertyNames));
109    }
110    
111    static String[] split(String propertyNames)
112    {
113        String trimmed = propertyNames.trim();
114
115        if (trimmed.length() == 0)
116            return EMPTY_STRING_ARRAY;
117
118        return trimmed.split("\\s*,\\s*");
119    }
120}