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