001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.util.comparator;
016    
017    import com.liferay.portal.kernel.util.OrderByComparator;
018    import com.liferay.portal.model.Organization;
019    
020    /**
021     * @author Brian Wing Shun Chan
022     */
023    public class OrganizationTypeComparator extends OrderByComparator {
024    
025            public static String ORDER_BY_ASC = "orgType ASC, orgName ASC";
026    
027            public static String ORDER_BY_DESC = "orgType DESC, orgName DESC";
028    
029            public static String[] ORDER_BY_FIELDS = {"type", "name"};
030    
031            public OrganizationTypeComparator() {
032                    this(false);
033            }
034    
035            public OrganizationTypeComparator(boolean ascending) {
036                    _ascending = ascending;
037            }
038    
039            public int compare(Object obj1, Object obj2) {
040                    Organization organization1 = (Organization)obj1;
041                    Organization organization2 = (Organization)obj2;
042    
043                    int typeOrder1 = organization1.getTypeOrder();
044                    int typeOrder2 = organization2.getTypeOrder();
045    
046                    int value = typeOrder1 - typeOrder2;
047    
048                    if (value == 0) {
049                            value = organization1.getName().compareTo(organization2.getName());
050                    }
051    
052                    if (_ascending) {
053                            return value;
054                    }
055                    else {
056                            return -value;
057                    }
058            }
059    
060            public String getOrderBy() {
061                    if (_ascending) {
062                            return ORDER_BY_ASC;
063                    }
064                    else {
065                            return ORDER_BY_DESC;
066                    }
067            }
068    
069            public String[] getOrderByFields() {
070                    return ORDER_BY_FIELDS;
071            }
072    
073            public boolean isAscending() {
074                    return _ascending;
075            }
076    
077            private boolean _ascending;
078    
079    }