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  /** {@inheritDoc} */
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  public int compare(AbstractIndexDescriptor index1,
067      AbstractIndexDescriptor index2)
068  {
069    int result;
070    IndexDescriptor i1 = (IndexDescriptor)index1;
071    IndexDescriptor i2 = (IndexDescriptor)index2;
072
073    int[] possibleResults = {compareNames(i1, i2), compareEntryLimits(i1, i2),
074        compareTypes(i1, i2), compareRebuildRequired(i1, i2)};
075    result = possibleResults[sortColumn];
076    if (result == 0)
077    {
078      for (int i : possibleResults)
079      {
080        if (i != 0)
081        {
082          result = i;
083          break;
084        }
085      }
086    }
087    if (!sortAscending)
088    {
089      result = -result;
090    }
091    return result;
092  }
093
094  /** {@inheritDoc} */
095  protected String[] getLine(AbstractIndexDescriptor index)
096  {
097    IndexDescriptor i = (IndexDescriptor)index;
098    return new String[] {
099      i.getName(), getEntryLimitValue(i), getIndexTypeString(i),
100      getRebuildRequiredString(i).toString()
101    };
102  }
103
104  /**
105   * Returns the String representing the entry limit value of the index.
106   * @return the String representing the entry limit value of the index.
107   */
108  private String getEntryLimitValue(IndexDescriptor i)
109  {
110    if (i.getEntryLimit() >= 0)
111    {
112      return String.valueOf(i.getEntryLimit());
113    }
114    else
115    {
116      return INFO_NOT_APPLICABLE_LABEL.get().toString();
117    }
118  }
119
120  // Comparison methods.
121
122  private int compareNames(IndexDescriptor i1, IndexDescriptor i2)
123  {
124    return i1.getName().compareTo(i2.getName());
125  }
126
127  private int compareEntryLimits(IndexDescriptor i1, IndexDescriptor i2)
128  {
129    return getEntryLimitValue(i1).compareTo(getEntryLimitValue(i2));
130  }
131
132  private int compareTypes(IndexDescriptor i1, IndexDescriptor i2)
133  {
134    return getIndexTypeString(i1).compareTo(getIndexTypeString(i2));
135  }
136
137  /**
138   * Returns the String representation of the index type for the index.
139   * @param index the index.
140   * @return the String representation of the index type for the index.
141   */
142  private String getIndexTypeString(IndexDescriptor index)
143  {
144    StringBuilder sb = new StringBuilder();
145    for (IndexTypeDescriptor type : index.getTypes())
146    {
147      LocalizableMessage v;
148      switch (type)
149      {
150      case SUBSTRING:
151        v = INFO_CTRL_PANEL_INDEX_SUBSTRING.get();
152        break;
153      case ORDERING:
154        v = INFO_CTRL_PANEL_INDEX_ORDERING.get();
155        break;
156      case PRESENCE:
157        v = INFO_CTRL_PANEL_INDEX_PRESENCE.get();
158        break;
159      case EQUALITY:
160        v = INFO_CTRL_PANEL_INDEX_EQUALITY.get();
161        break;
162      case APPROXIMATE:
163        v = INFO_CTRL_PANEL_INDEX_APPROXIMATE.get();
164        break;
165      default:
166        throw new RuntimeException("Unknown index type: "+type);
167      }
168      if (sb.length() > 0)
169      {
170        sb.append(", ");
171      }
172      sb.append(v);
173    }
174    if (sb.length() == 0)
175    {
176      sb.append(INFO_NOT_APPLICABLE_LABEL.get());
177    }
178    return sb.toString();
179  }
180}