001/*
002 * CDDL HEADER START
003 *
004 * The contents of this file are subject to the terms of the
005 * Common Development and Distribution License, Version 1.0 only
006 * (the "License").  You may not use this file except in compliance
007 * with the License.
008 *
009 * You can obtain a copy of the license at legal-notices/CDDLv1_0.txt
010 * or http://forgerock.org/license/CDDLv1.0.html.
011 * See the License for the specific language governing permissions
012 * and limitations under the License.
013 *
014 * When distributing Covered Code, include this CDDL HEADER in each
015 * file and include the License file at legal-notices/CDDLv1_0.txt.
016 * If applicable, add the following below this CDDL HEADER, with the
017 * fields enclosed by brackets "[]" replaced with your own identifying
018 * information:
019 *      Portions Copyright [yyyy] [name of copyright owner]
020 *
021 * CDDL HEADER END
022 *
023 *
024 *      Copyright 2008-2009 Sun Microsystems, Inc.
025 *      Portions Copyright 2014-2015 ForgeRock AS
026 */
027
028package org.opends.guitools.controlpanel.datamodel;
029
030import static org.opends.messages.AdminToolMessages.*;
031
032import org.forgerock.i18n.LocalizableMessage;
033
034/**
035 * The table model for the indexes.  This is the table model used by the table
036 * that appears on the right side of the Manage Index dialog when the user
037 * clicks on the node "Index" and it gives a global view of the indexes
038 * defined on a given backend.
039 *
040 */
041public class IndexTableModel extends AbstractIndexTableModel
042{
043
044  private static final long serialVersionUID = 6979651281772979301L;
045
046  @Override
047  protected String[] getColumnNames()
048  {
049    return new String[] {
050        getHeader(INFO_CTRL_PANEL_INDEXES_HEADER_ATTRIBUTE.get(), 30),
051        getHeader(INFO_CTRL_PANEL_INDEXES_HEADER_ENTRY_LIMIT.get(), 30),
052        getHeader(INFO_CTRL_PANEL_INDEXES_HEADER_INDEX_TYPES.get(), 30),
053        getHeader(INFO_CTRL_PANEL_INDEXES_HEADER_REQUIRES_REBUILD.get(), 30)
054    };
055  }
056
057  /**
058   * Comparable implementation.
059   * @param index1 the first index descriptor to compare.
060   * @param index2 the second index descriptor to compare.
061   * @return 1 if according to the sorting options set by the user the first
062   * index descriptor must be put before the second descriptor, 0 if they
063   * are equivalent in terms of sorting and -1 if the second descriptor must
064   * be put before the first descriptor.
065   */
066  @Override
067  public int compare(AbstractIndexDescriptor index1,
068      AbstractIndexDescriptor index2)
069  {
070    int result;
071    IndexDescriptor i1 = (IndexDescriptor)index1;
072    IndexDescriptor i2 = (IndexDescriptor)index2;
073
074    int[] possibleResults = {compareNames(i1, i2), compareEntryLimits(i1, i2),
075        compareTypes(i1, i2), compareRebuildRequired(i1, i2)};
076    result = possibleResults[sortColumn];
077    if (result == 0)
078    {
079      for (int i : possibleResults)
080      {
081        if (i != 0)
082        {
083          result = i;
084          break;
085        }
086      }
087    }
088    if (!sortAscending)
089    {
090      result = -result;
091    }
092    return result;
093  }
094
095  @Override
096  protected String[] getLine(AbstractIndexDescriptor index)
097  {
098    IndexDescriptor i = (IndexDescriptor)index;
099    return new String[] {
100      i.getName(), getEntryLimitValue(i), getIndexTypeString(i),
101      getRebuildRequiredString(i).toString()
102    };
103  }
104
105  /**
106   * Returns the String representing the entry limit value of the index.
107   * @return the String representing the entry limit value of the index.
108   */
109  private String getEntryLimitValue(IndexDescriptor i)
110  {
111    if (i.getEntryLimit() >= 0)
112    {
113      return String.valueOf(i.getEntryLimit());
114    }
115    else
116    {
117      return INFO_NOT_APPLICABLE_LABEL.get().toString();
118    }
119  }
120
121  // Comparison methods.
122
123  private int compareNames(IndexDescriptor i1, IndexDescriptor i2)
124  {
125    return i1.getName().compareTo(i2.getName());
126  }
127
128  private int compareEntryLimits(IndexDescriptor i1, IndexDescriptor i2)
129  {
130    return getEntryLimitValue(i1).compareTo(getEntryLimitValue(i2));
131  }
132
133  private int compareTypes(IndexDescriptor i1, IndexDescriptor i2)
134  {
135    return getIndexTypeString(i1).compareTo(getIndexTypeString(i2));
136  }
137
138  /**
139   * Returns the String representation of the index type for the index.
140   * @param index the index.
141   * @return the String representation of the index type for the index.
142   */
143  private String getIndexTypeString(IndexDescriptor index)
144  {
145    StringBuilder sb = new StringBuilder();
146    for (IndexTypeDescriptor type : index.getTypes())
147    {
148      if (sb.length() > 0)
149      {
150        sb.append(", ");
151      }
152      sb.append(getIndexName(type));
153    }
154    if (sb.length() == 0)
155    {
156      sb.append(INFO_NOT_APPLICABLE_LABEL.get());
157    }
158    return sb.toString();
159  }
160
161  private LocalizableMessage getIndexName(IndexTypeDescriptor type)
162  {
163    switch (type)
164    {
165    case SUBSTRING:
166      return INFO_CTRL_PANEL_INDEX_SUBSTRING.get();
167    case ORDERING:
168      return INFO_CTRL_PANEL_INDEX_ORDERING.get();
169    case PRESENCE:
170      return INFO_CTRL_PANEL_INDEX_PRESENCE.get();
171    case EQUALITY:
172      return INFO_CTRL_PANEL_INDEX_EQUALITY.get();
173    case APPROXIMATE:
174      return INFO_CTRL_PANEL_INDEX_APPROXIMATE.get();
175    default:
176      throw new RuntimeException("Unknown index type: "+type);
177    }
178  }
179}