001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.kernel.repository;
016    
017    import com.liferay.portal.kernel.language.LanguageUtil;
018    import com.liferay.portal.kernel.util.CharPool;
019    import com.liferay.portal.kernel.util.HtmlUtil;
020    import com.liferay.portal.kernel.util.ResourceBundleUtil;
021    import com.liferay.portal.kernel.util.StringUtil;
022    import com.liferay.portal.util.Portal;
023    
024    import java.util.ArrayList;
025    import java.util.Collection;
026    import java.util.Locale;
027    import java.util.ResourceBundle;
028    
029    /**
030     * @author Adolfo P??rez
031     */
032    public class RepositoryConfigurationBuilder {
033    
034            public RepositoryConfigurationBuilder() {
035                    this(Portal.class, "content.Language");
036            }
037    
038            public RepositoryConfigurationBuilder(
039                    Class<?> clazz, String resourceBundleBaseName, String... names) {
040    
041                    _clazz = clazz;
042                    _resourceBundleBaseName = resourceBundleBaseName;
043    
044                    for (String name : names) {
045                            addParameter(name);
046                    }
047            }
048    
049            public RepositoryConfigurationBuilder addParameter(String name) {
050                    String labelKey = HtmlUtil.escape(
051                            StringUtil.replace(
052                                    StringUtil.toLowerCase(name), CharPool.UNDERLINE,
053                                    CharPool.DASH));
054    
055                    return addParameter(name, labelKey);
056            }
057    
058            public RepositoryConfigurationBuilder addParameter(
059                    String labelKey, String name) {
060    
061                    _parameters.add(new ParameterImpl(labelKey, name));
062    
063                    return this;
064            }
065    
066            public RepositoryConfiguration build() {
067                    return new RepositoryConfigurationImpl(new ArrayList<>(_parameters));
068            }
069    
070            private final Class<?> _clazz;
071            private final Collection<RepositoryConfiguration.Parameter> _parameters =
072                    new ArrayList<>();
073            private final String _resourceBundleBaseName;
074    
075            private class ParameterImpl implements RepositoryConfiguration.Parameter {
076    
077                    public ParameterImpl(String name, String labelKey) {
078                            _name = name;
079                            _labelKey = labelKey;
080                    }
081    
082                    @Override
083                    public String getLabel(Locale locale) {
084                            ResourceBundle resourceBundle = ResourceBundleUtil.getBundle(
085                                    _resourceBundleBaseName, locale, _clazz);
086    
087                            return LanguageUtil.get(resourceBundle, _labelKey);
088                    }
089    
090                    @Override
091                    public String getName() {
092                            return _name;
093                    }
094    
095                    private final String _labelKey;
096                    private final String _name;
097    
098            }
099    
100            private class RepositoryConfigurationImpl
101                    implements RepositoryConfiguration {
102    
103                    public RepositoryConfigurationImpl(Collection<Parameter> parameters) {
104                            _parameters = parameters;
105                    }
106    
107                    @Override
108                    public Collection<Parameter> getParameters() {
109                            return _parameters;
110                    }
111    
112                    private final Collection<Parameter> _parameters;
113    
114            }
115    
116    }