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.util.comparator;
016    
017    import com.liferay.portal.kernel.util.StringPool;
018    import com.liferay.portal.kernel.util.Validator;
019    import com.liferay.portal.model.Portlet;
020    import com.liferay.portal.util.PortalUtil;
021    
022    import java.io.Serializable;
023    
024    import java.util.Comparator;
025    import java.util.Locale;
026    
027    import javax.servlet.ServletContext;
028    
029    /**
030     * @author Brian Wing Shun Chan
031     */
032    public class PortletTitleComparator
033            implements Comparator<Portlet>, Serializable {
034    
035            public PortletTitleComparator(Locale locale) {
036                    this(null, locale);
037            }
038    
039            public PortletTitleComparator(
040                    ServletContext servletContext, Locale locale) {
041    
042                    _servletContext = servletContext;
043                    _locale = locale;
044            }
045    
046            @Override
047            public int compare(Portlet portlet1, Portlet portlet2) {
048                    String portletTitle1 = StringPool.BLANK;
049                    String portletTitle2 = StringPool.BLANK;
050    
051                    if (_servletContext != null) {
052                            portletTitle1 = PortalUtil.getPortletTitle(
053                                    portlet1, _servletContext, _locale);
054                            portletTitle2 = PortalUtil.getPortletTitle(
055                                    portlet2, _servletContext, _locale);
056                    }
057                    else {
058                            portletTitle1 = PortalUtil.getPortletTitle(portlet1, _locale);
059                            portletTitle2 = PortalUtil.getPortletTitle(portlet2, _locale);
060                    }
061    
062                    if (Validator.isNull(portletTitle1) &&
063                            Validator.isNull(portletTitle2)) {
064    
065                            return 0;
066                    }
067    
068                    if (Validator.isNull(portletTitle1)) {
069                            return 1;
070                    }
071    
072                    if (Validator.isNull(portletTitle2)) {
073                            return -1;
074                    }
075    
076                    return portletTitle1.compareTo(portletTitle2);
077            }
078    
079            private final Locale _locale;
080            private final ServletContext _servletContext;
081    
082    }