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.ResourceBundleLoader;
021    import com.liferay.portal.kernel.util.StringUtil;
022    
023    import java.util.ArrayList;
024    import java.util.Collection;
025    import java.util.Locale;
026    import java.util.ResourceBundle;
027    
028    /**
029     * @author Adolfo Pérez
030     */
031    public class RepositoryConfigurationBuilder {
032    
033            public RepositoryConfigurationBuilder() {
034                    this(LanguageUtil.getPortalResourceBundleLoader());
035            }
036    
037            public RepositoryConfigurationBuilder(
038                    ResourceBundleLoader resourceBundleLoader, String... names) {
039    
040                    _resourceBundleLoader = resourceBundleLoader;
041    
042                    for (String name : names) {
043                            addParameter(name);
044                    }
045            }
046    
047            public RepositoryConfigurationBuilder addParameter(String name) {
048                    String labelKey = HtmlUtil.escape(
049                            StringUtil.replace(
050                                    StringUtil.toLowerCase(name), CharPool.UNDERLINE,
051                                    CharPool.DASH));
052    
053                    return addParameter(name, labelKey);
054            }
055    
056            public RepositoryConfigurationBuilder addParameter(
057                    String labelKey, String name) {
058    
059                    _parameters.add(new ParameterImpl(labelKey, name));
060    
061                    return this;
062            }
063    
064            public RepositoryConfiguration build() {
065                    return new RepositoryConfigurationImpl(new ArrayList<>(_parameters));
066            }
067    
068            private final Collection<RepositoryConfiguration.Parameter> _parameters =
069                    new ArrayList<>();
070            private final ResourceBundleLoader _resourceBundleLoader;
071    
072            private static class RepositoryConfigurationImpl
073                    implements RepositoryConfiguration {
074    
075                    public RepositoryConfigurationImpl(Collection<Parameter> parameters) {
076                            _parameters = parameters;
077                    }
078    
079                    @Override
080                    public Collection<Parameter> getParameters() {
081                            return _parameters;
082                    }
083    
084                    private final Collection<Parameter> _parameters;
085    
086            }
087    
088            private class ParameterImpl implements RepositoryConfiguration.Parameter {
089    
090                    public ParameterImpl(String name, String labelKey) {
091                            _name = name;
092                            _labelKey = labelKey;
093                    }
094    
095                    @Override
096                    public String getLabel(Locale locale) {
097                            ResourceBundle resourceBundle =
098                                    _resourceBundleLoader.loadResourceBundle(
099                                            LanguageUtil.getLanguageId(locale));
100    
101                            return LanguageUtil.get(resourceBundle, _labelKey);
102                    }
103    
104                    @Override
105                    public String getName() {
106                            return _name;
107                    }
108    
109                    private final String _labelKey;
110                    private final String _name;
111    
112            }
113    
114    }