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-2016 ForgeRock AS.
016 */
017package org.opends.guitools.controlpanel.datamodel;
018
019import static org.opends.messages.AdminToolMessages.*;
020
021import org.forgerock.i18n.LocalizableMessage;
022import org.forgerock.opendj.server.config.meta.BackendIndexCfgDefn.IndexType;
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 */
030public class IndexTableModel extends AbstractIndexTableModel
031{
032
033  private static final long serialVersionUID = 6979651281772979301L;
034
035  @Override
036  protected String[] getColumnNames()
037  {
038    return new String[] {
039        getHeader(INFO_CTRL_PANEL_INDEXES_HEADER_ATTRIBUTE.get(), 30),
040        getHeader(INFO_CTRL_PANEL_INDEXES_HEADER_ENTRY_LIMIT.get(), 30),
041        getHeader(INFO_CTRL_PANEL_INDEXES_HEADER_INDEX_TYPES.get(), 30),
042        getHeader(INFO_CTRL_PANEL_INDEXES_HEADER_REQUIRES_REBUILD.get(), 30)
043    };
044  }
045
046  /**
047   * Comparable implementation.
048   * @param index1 the first index descriptor to compare.
049   * @param index2 the second index descriptor to compare.
050   * @return 1 if according to the sorting options set by the user the first
051   * index descriptor must be put before the second descriptor, 0 if they
052   * are equivalent in terms of sorting and -1 if the second descriptor must
053   * be put before the first descriptor.
054   */
055  @Override
056  public int compare(AbstractIndexDescriptor index1,
057      AbstractIndexDescriptor index2)
058  {
059    int result;
060    IndexDescriptor i1 = (IndexDescriptor)index1;
061    IndexDescriptor i2 = (IndexDescriptor)index2;
062
063    int[] possibleResults = {compareNames(i1, i2), compareEntryLimits(i1, i2),
064        compareTypes(i1, i2), compareRebuildRequired(i1, i2)};
065    result = possibleResults[sortColumn];
066    if (result == 0)
067    {
068      for (int i : possibleResults)
069      {
070        if (i != 0)
071        {
072          result = i;
073          break;
074        }
075      }
076    }
077    if (!sortAscending)
078    {
079      result = -result;
080    }
081    return result;
082  }
083
084  @Override
085  protected String[] getLine(AbstractIndexDescriptor index)
086  {
087    IndexDescriptor i = (IndexDescriptor)index;
088    return new String[] {
089      i.getName(), getEntryLimitValue(i), getIndexTypeString(i),
090      getRebuildRequiredString(i).toString()
091    };
092  }
093
094  /**
095   * Returns the String representing the entry limit value of the index.
096   * @return the String representing the entry limit value of the index.
097   */
098  private String getEntryLimitValue(IndexDescriptor i)
099  {
100    if (i.getEntryLimit() >= 0)
101    {
102      return String.valueOf(i.getEntryLimit());
103    }
104    else
105    {
106      return INFO_NOT_APPLICABLE_LABEL.get().toString();
107    }
108  }
109
110  // Comparison methods.
111
112  private int compareNames(IndexDescriptor i1, IndexDescriptor i2)
113  {
114    return i1.getName().compareTo(i2.getName());
115  }
116
117  private int compareEntryLimits(IndexDescriptor i1, IndexDescriptor i2)
118  {
119    return getEntryLimitValue(i1).compareTo(getEntryLimitValue(i2));
120  }
121
122  private int compareTypes(IndexDescriptor i1, IndexDescriptor i2)
123  {
124    return getIndexTypeString(i1).compareTo(getIndexTypeString(i2));
125  }
126
127  /**
128   * Returns the String representation of the index type for the index.
129   * @param index the index.
130   * @return the String representation of the index type for the index.
131   */
132  private String getIndexTypeString(IndexDescriptor index)
133  {
134    StringBuilder sb = new StringBuilder();
135    for (IndexType type : index.getTypes())
136    {
137      if (sb.length() > 0)
138      {
139        sb.append(", ");
140      }
141      sb.append(getIndexName(type));
142    }
143    if (sb.length() == 0)
144    {
145      sb.append(INFO_NOT_APPLICABLE_LABEL.get());
146    }
147    return sb.toString();
148  }
149
150  private LocalizableMessage getIndexName(IndexType type)
151  {
152    switch (type)
153    {
154    case SUBSTRING:
155      return INFO_CTRL_PANEL_INDEX_SUBSTRING.get();
156    case ORDERING:
157      return INFO_CTRL_PANEL_INDEX_ORDERING.get();
158    case PRESENCE:
159      return INFO_CTRL_PANEL_INDEX_PRESENCE.get();
160    case EQUALITY:
161      return INFO_CTRL_PANEL_INDEX_EQUALITY.get();
162    case APPROXIMATE:
163      return INFO_CTRL_PANEL_INDEX_APPROXIMATE.get();
164    default:
165      throw new RuntimeException("Unknown index type: "+type);
166    }
167  }
168}