    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
    015    package org.apache.tapestry5.internal.beaneditor;
    016
    017    import org.apache.tapestry5.beaneditor.BeanModel;
    018    import org.apache.tapestry5.internal.InternalConstants;
    019    import org.apache.tapestry5.ioc.internal.util.InternalUtils;
    020
    021    /**
    022     * Utilities used in a few places to modify an existing {@link BeanModel}.
    023     */
    024    public final class BeanModelUtils
    025    {
    026
    027        /**
    028         * Performs standard set of modifications to a {@link org.apache.tapestry5.beaneditor.BeanModel}
    029         * . First new
    030         * properties may be added, then properties removed, then properties reordered.
    031         *
    032         * @param model                to modifiy
    033         * @param addPropertyNames     comma seperated list of property names to add, or null
    034         * @param includePropertyNames comma seperated list of property names to include
    035         * @param excludePropertyNames comma seperated list of property names to exclude, or null
    036         * @param reorderPropertyNames comma seperated list of property names to reorder, or null
    037         */
    038        public static void modify(BeanModel model, String addPropertyNames,
    039                                  String includePropertyNames, String excludePropertyNames, String reorderPropertyNames)
    040        {
    041            if (addPropertyNames != null)
    042                add(model, addPropertyNames);
    043
    044            if (includePropertyNames != null)
    045                include(model, join(includePropertyNames, addPropertyNames));
    046
    047            if (excludePropertyNames != null)
    048                exclude(model, excludePropertyNames);
    049
    050            if (reorderPropertyNames != null)
    051                reorder(model, reorderPropertyNames);
    052        }
    053
    054        private static final String join(String firstList, String optionalSecondList)
    055        {
    056            if (InternalUtils.isBlank(optionalSecondList))
    057                return firstList;
    058
    059            return firstList + "," + optionalSecondList;
    060        }
    061
    062        /**
    063         * Adds empty properties to the bean model. New properties are added with a <em>null</em>
    064         * {@link org.apache.tapestry5.PropertyConduit}. `
    065         *
    066         * @param model         to be modified
    067         * @param propertyNames comma-separated list of property names
    068         * @see BeanModel#addEmpty(String)
    069         */
    070        public static void add(BeanModel model, String propertyNames)
    071        {
    072            for (String name : split(propertyNames))
    073            {
    074                model.addEmpty(name);
    075            }
    076        }
    077
    078        /**
    079         * Removes properties from the bean model.
    080         *
    081         * @param model
    082         * @param propertyNames comma-separated list of property names
    083         * @see BeanModel#exclude(String...)
    084         */
    085        public static void exclude(BeanModel model, String propertyNames)
    086        {
    087            model.exclude(split(propertyNames));
    088        }
    089
    090        /**
    091         * Selects a subset of the properties to keep, and reorders them.
    092         */
    093        public static void include(BeanModel model, String propertyNames)
    094        {
    095            model.include(split(propertyNames));
    096        }
    097
    098        /**
    099         * Reorders properties within the bean model.
    100         *
    101         * @param model
    102         * @param propertyNames comma-separated list of property names
    103         * @see BeanModel#reorder(String...)
    104         */
    105        public static void reorder(BeanModel model, String propertyNames)
    106        {
    107            model.reorder(split(propertyNames));
    108        }
    109
    110        static String[] split(String propertyNames)
    111        {
    112            String trimmed = propertyNames.trim();
    113
    114            if (trimmed.length() == 0)
    115                return InternalConstants.EMPTY_STRING_ARRAY;
    116
    117            return trimmed.split("\\s*,\\s*");
    118        }
    119    }