001    /**
002     * Copyright (c) 2000-2013 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.kernel.dao.db;
016    
017    import com.liferay.portal.kernel.util.HashUtil;
018    import com.liferay.portal.kernel.util.StringBundler;
019    import com.liferay.portal.kernel.util.StringPool;
020    import com.liferay.portal.kernel.util.StringUtil;
021    import com.liferay.portal.kernel.util.Validator;
022    
023    import java.util.Arrays;
024    
025    /**
026     * @author James Lefeu
027     * @author Peter Shin
028     * @author Shuyang Zhou
029     */
030    public class IndexMetadata extends Index implements Comparable<IndexMetadata> {
031    
032            public IndexMetadata(
033                    String indexName, String tableName, boolean unique,
034                    String... columnNames) {
035    
036                    super(indexName, tableName, unique);
037    
038                    if (columnNames == null) {
039                            throw new NullPointerException("Column names are missing");
040                    }
041    
042                    _columnNames = columnNames;
043    
044                    StringBundler sb = new StringBundler(8 + columnNames.length * 2);
045    
046                    if (unique) {
047                            sb.append("create unique ");
048                    }
049                    else {
050                            sb.append("create ");
051                    }
052    
053                    sb.append("index ");
054                    sb.append(indexName);
055                    sb.append(" on ");
056                    sb.append(tableName);
057    
058                    sb.append(StringPool.SPACE);
059                    sb.append(StringPool.OPEN_PARENTHESIS);
060    
061                    for (String columnName : columnNames) {
062                            sb.append(columnName);
063                            sb.append(StringPool.COMMA_AND_SPACE);
064                    }
065    
066                    sb.setIndex(sb.index() - 1);
067    
068                    sb.append(StringPool.CLOSE_PARENTHESIS);
069                    sb.append(StringPool.SEMICOLON);
070    
071                    _createSQL = sb.toString();
072    
073                    sb.setIndex(0);
074    
075                    sb.append("drop index ");
076                    sb.append(indexName);
077                    sb.append(" on ");
078                    sb.append(tableName);
079                    sb.append(StringPool.SEMICOLON);
080    
081                    _dropSQL = sb.toString();
082            }
083    
084            @Override
085            public int compareTo(IndexMetadata indexMetadata) {
086                    String columnNames = StringUtil.merge(_columnNames);
087    
088                    String indexMetadataColumnNames = StringUtil.merge(
089                            indexMetadata._columnNames);
090    
091                    return columnNames.compareTo(indexMetadataColumnNames);
092            }
093    
094            @Override
095            public boolean equals(Object obj) {
096                    if (this == obj) {
097                            return true;
098                    }
099    
100                    if (!(obj instanceof IndexMetadata)) {
101                            return false;
102                    }
103    
104                    IndexMetadata indexMetadata = (IndexMetadata)obj;
105    
106                    if (Validator.equals(getTableName(), indexMetadata.getTableName()) &&
107                            Arrays.equals(getColumnNames(), indexMetadata.getColumnNames())) {
108    
109                            return true;
110                    }
111    
112                    return false;
113            }
114    
115            public String[] getColumnNames() {
116                    return _columnNames;
117            }
118    
119            public String getCreateSQL() {
120                    return _createSQL;
121            }
122    
123            public String getDropSQL() {
124                    return _dropSQL;
125            }
126    
127            @Override
128            public int hashCode() {
129                    int hashCode = HashUtil.hash(0, getTableName());
130    
131                    for (String columnName : _columnNames) {
132                            hashCode = HashUtil.hash(hashCode, columnName);
133                    }
134    
135                    return hashCode;
136            }
137    
138            public Boolean redundantTo(IndexMetadata indexMetadata) {
139                    String[] indexMetadataColumnNames = indexMetadata._columnNames;
140    
141                    if (_columnNames.length <= indexMetadataColumnNames.length) {
142                            for (int i = 0; i < _columnNames.length; i++) {
143                                    if (!_columnNames[i].equals(indexMetadataColumnNames[i])) {
144                                            return null;
145                                    }
146                            }
147    
148                            if (isUnique()) {
149                                    return Boolean.FALSE;
150                            }
151                            else {
152                                    return Boolean.TRUE;
153                            }
154                    }
155    
156                    Boolean redundant = indexMetadata.redundantTo(this);
157    
158                    if (redundant == null) {
159                            return null;
160                    }
161    
162                    return !redundant;
163            }
164    
165            @Override
166            public String toString() {
167                    return _createSQL;
168            }
169    
170            private String[] _columnNames;
171            private String _createSQL;
172            private String _dropSQL;
173    
174    }