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.security.auth;
016    
017    import com.liferay.portal.kernel.language.LanguageUtil;
018    import com.liferay.portal.kernel.util.ArrayUtil;
019    import com.liferay.portal.kernel.util.StringPool;
020    import com.liferay.portal.kernel.util.StringUtil;
021    
022    import java.util.Locale;
023    import java.util.Map;
024    import java.util.concurrent.ConcurrentHashMap;
025    
026    /**
027     * @author Brian Wing Shun Chan
028     */
029    public class FullNameDefinitionFactory {
030    
031            public static FullNameDefinition getInstance(Locale locale) {
032                    return _instance._getInstance(locale);
033            }
034    
035            private FullNameDefinitionFactory() {
036            }
037    
038            private FullNameDefinition _getInstance(Locale locale) {
039                    FullNameDefinition fullNameDefinition = _fullNameDefinitions.get(
040                            locale);
041    
042                    if (fullNameDefinition != null) {
043                            return fullNameDefinition;
044                    }
045    
046                    fullNameDefinition = new FullNameDefinition();
047    
048                    String[] requiredFieldNames = _getRequiredFieldNames(locale);
049    
050                    for (String requiredFieldName : requiredFieldNames) {
051                            fullNameDefinition.addRequiredField(requiredFieldName);
052                    }
053    
054                    String[] fieldNames = StringUtil.split(
055                            LanguageUtil.get(locale, "lang.user.name.field.names"));
056    
057                    fieldNames = ArrayUtil.append(requiredFieldNames, fieldNames);
058    
059                    ArrayUtil.reverse(fieldNames);
060    
061                    fieldNames = ArrayUtil.unique(fieldNames);
062    
063                    ArrayUtil.reverse(fieldNames);
064    
065                    for (String userNameField : fieldNames) {
066                            FullNameField fullNameField = new FullNameField();
067    
068                            fullNameField.setName(userNameField);
069    
070                            String[] values = StringUtil.split(
071                                    LanguageUtil.get(
072                                            locale, "lang.user.name." + userNameField + ".values",
073                                            StringPool.BLANK));
074    
075                            fullNameField.setValues(values);
076    
077                            fullNameField.setRequired(
078                                    fullNameDefinition.isFieldRequired(userNameField));
079    
080                            fullNameDefinition.addFullNameField(fullNameField);
081                    }
082    
083                    _fullNameDefinitions.put(locale, fullNameDefinition);
084    
085                    return fullNameDefinition;
086            }
087    
088            private String[] _getRequiredFieldNames(Locale locale) {
089                    String[] requiredFieldNames = StringUtil.split(
090                            LanguageUtil.get(locale, "lang.user.name.required.field.names"));
091    
092                    if (!ArrayUtil.contains(requiredFieldNames, "first-name")) {
093                            requiredFieldNames = ArrayUtil.append(
094                                    new String[] {"first-name"}, requiredFieldNames);
095                    }
096    
097                    return requiredFieldNames;
098            }
099    
100            private static final FullNameDefinitionFactory _instance =
101                    new FullNameDefinitionFactory();
102    
103            private final Map<Locale, FullNameDefinition> _fullNameDefinitions =
104                    new ConcurrentHashMap<>();
105    
106    }