001    /**
002     * Copyright (c) 2000-2013 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.util;
016    
017    /**
018     * @author Jos?? Manuel Navarro
019     */
020    public class TableNameOrderByComparator extends OrderByComparator {
021    
022            public TableNameOrderByComparator(
023                    OrderByComparator orderByComparator, String tableName) {
024    
025                    _orderByComparator = orderByComparator;
026    
027                    setTableName(tableName);
028            }
029    
030            @Override
031            public int compare(Object obj1, Object obj2) {
032                    return _orderByComparator.compare(obj1, obj2);
033            }
034    
035            @Override
036            public String getOrderBy() {
037                    String orderBy = _orderByComparator.getOrderBy();
038    
039                    if (_tableName == null) {
040                            return orderBy;
041                    }
042    
043                    String[] columnNames = StringUtil.split(orderBy);
044    
045                    StringBundler sb = new StringBundler((3 * columnNames.length) - 1);
046    
047                    for (int i = 0; i < columnNames.length; ++i) {
048                            String columnName = columnNames[i];
049    
050                            if (columnName.indexOf(CharPool.PERIOD) != -1) {
051                                    columnName = StringUtil.split(columnName, CharPool.PERIOD)[1];
052                            }
053    
054                            sb.append(_tableName);
055                            sb.append(StringUtil.trim(columnName));
056    
057                            if (i < (columnNames.length - 1)) {
058                                    sb.append(StringPool.COMMA_AND_SPACE);
059                            }
060                    }
061    
062                    return sb.toString();
063            }
064    
065            @Override
066            public String[] getOrderByConditionFields() {
067                    return _orderByComparator.getOrderByConditionFields();
068            }
069    
070            @Override
071            public Object[] getOrderByConditionValues(Object obj) {
072                    return _orderByComparator.getOrderByConditionValues(obj);
073            }
074    
075            @Override
076            public String[] getOrderByFields() {
077                    return _orderByComparator.getOrderByFields();
078            }
079    
080            public OrderByComparator getWrappedOrderByComparator() {
081                    return _orderByComparator;
082            }
083    
084            @Override
085            public boolean isAscending() {
086                    return _orderByComparator.isAscending();
087            }
088    
089            @Override
090            public boolean isAscending(String field) {
091                    return _orderByComparator.isAscending(field);
092            }
093    
094            public void setTableName(String tableName) {
095                    if (Validator.isNotNull(tableName)) {
096                            if (tableName.endsWith(StringPool.PERIOD)) {
097                                    _tableName = tableName;
098                            }
099                            else {
100                                    _tableName = tableName + CharPool.PERIOD;
101                            }
102                    }
103                    else {
104                            _tableName = null;
105                    }
106            }
107    
108            @Override
109            public String toString() {
110                    return _orderByComparator.toString();
111            }
112    
113            private OrderByComparator _orderByComparator;
114            private String _tableName;
115    
116    }