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.opendj.ldap.SearchScope;
033import org.opends.guitools.controlpanel.util.Utilities;
034
035/**
036 * The table model for the VLV indexes.  This is the table model used by the
037 * table that appears on the right side of the Manage Index dialog when the user
038 * clicks on the node "VLV Indexes" and it gives a global view of the VLV
039 * indexes defined on a given backend.
040 *
041 */
042public class VLVIndexTableModel extends AbstractIndexTableModel
043{
044  private static final long serialVersionUID = 897379916278218775L;
045
046  /** {@inheritDoc} */
047  protected String[] getColumnNames()
048  {
049    return new String[] {
050        getHeader(INFO_CTRL_PANEL_VLV_INDEXES_HEADER_NAME.get()),
051        getHeader(INFO_CTRL_PANEL_VLV_INDEXES_HEADER_BASE_DN.get(), 30),
052        getHeader(INFO_CTRL_PANEL_VLV_INDEXES_HEADER_SCOPE.get()),
053        getHeader(INFO_CTRL_PANEL_VLV_INDEXES_HEADER_FILTER.get()),
054        getHeader(INFO_CTRL_PANEL_VLV_INDEXES_HEADER_SORT_ORDER.get(), 30),
055        getHeader(INFO_CTRL_PANEL_VLV_INDEXES_HEADER_REQUIRES_REBUILD.get(), 30)
056    };
057  }
058
059  /**
060   * Comparable implementation.
061   * @param index1 the first VLV index descriptor to compare.
062   * @param index2 the second VLV index descriptor to compare.
063   * @return 1 if according to the sorting options set by the user the first
064   * index descriptor must be put before the second descriptor, 0 if they
065   * are equivalent in terms of sorting and -1 if the second descriptor must
066   * be put before the first descriptor.
067   */
068  public int compare(AbstractIndexDescriptor index1,
069      AbstractIndexDescriptor index2)
070  {
071    int result;
072    VLVIndexDescriptor i1 = (VLVIndexDescriptor)index1;
073    VLVIndexDescriptor i2 = (VLVIndexDescriptor)index2;
074
075    int[] possibleResults = {compareNames(i1, i2), compareBaseDNs(i1, i2),
076        compareScopes(i1, i2), compareFilters(i1, i2),
077        compareSortOrders(i1, i2), compareRebuildRequired(i1, i2)};
078    result = possibleResults[sortColumn];
079    if (result == 0)
080    {
081      for (int i : possibleResults)
082      {
083        if (i != 0)
084        {
085          result = i;
086          break;
087        }
088      }
089    }
090    if (!sortAscending)
091    {
092      result = -result;
093    }
094    return result;
095  }
096
097  /** {@inheritDoc} */
098  protected String[] getLine(AbstractIndexDescriptor index)
099  {
100    VLVIndexDescriptor i = (VLVIndexDescriptor)index;
101    return new String[] {
102        i.getName(), getDNValue(i), getScopeDisplayValue(i), i.getFilter(),
103        getSortOrderDisplayValue(i), getRebuildRequiredString(i).toString()
104    };
105  }
106
107  /**
108   * Returns the VLV index DN value in String format.
109   * @param i the VLV index.
110   * @return the VLV index DN value in String format.
111   */
112  private String getDNValue(VLVIndexDescriptor i)
113  {
114    return Utilities.unescapeUtf8(i.getBaseDN().toString());
115  }
116
117  /**
118   * Returns the VLV index scope value in String format. This is the value used
119   * to make String comparisons.
120   *
121   * @param scope
122   *          the VLV index.
123   * @return the VLV index scope value in String format.
124   */
125  private String toUIString(final SearchScope scope)
126  {
127    switch (scope.asEnum())
128    {
129    case BASE_OBJECT:
130      return INFO_CTRL_PANEL_VLV_INDEX_BASE_OBJECT_LABEL.get().toString();
131    case SINGLE_LEVEL:
132      return INFO_CTRL_PANEL_VLV_INDEX_SINGLE_LEVEL_LABEL.get().toString();
133    case SUBORDINATES:
134      return INFO_CTRL_PANEL_VLV_INDEX_SUBORDINATE_SUBTREE_LABEL.get().toString();
135    case WHOLE_SUBTREE:
136      return INFO_CTRL_PANEL_VLV_INDEX_WHOLE_SUBTREE_LABEL.get().toString();
137    default:
138      throw new IllegalArgumentException("Unknown scope: " + scope);
139    }
140  }
141
142  /**
143   * Returns the VLV index scope display value in String format. This is the
144   * value to be stored in the table model.
145   *
146   * @param i
147   *          the VLV index.
148   * @return the VLV index DN value in String format.
149   */
150  private String getScopeDisplayValue(final VLVIndexDescriptor i)
151  {
152    return "<html>" + toUIString(i.getScope());
153  }
154
155  /**
156   * Returns the VLV index sort order value in String format.  This is the value
157   * used to make String comparisons.
158   * @param i the VLV index.
159   * @return the VLV index DN value in String format.
160   */
161  private String getSortOrderStringValue(VLVIndexDescriptor i)
162  {
163    StringBuilder sb = new StringBuilder();
164    for (VLVSortOrder sortOrder : i.getSortOrder())
165    {
166      if (sb.length() > 0)
167      {
168        sb.append(", ");
169      }
170      sb.append(sortOrder.getAttributeName());
171      if (sortOrder.isAscending())
172      {
173        sb.append(" (ascending)");
174      }
175      else
176      {
177        sb.append(" (descending)");
178      }
179    }
180    if (sb.length() == 0)
181    {
182      sb.append(INFO_NOT_APPLICABLE_LABEL.get());
183    }
184    return sb.toString();
185  }
186
187  /**
188   * Returns the VLV index sort order value in String format.  This is the value
189   * stored in the table model.
190   * @param i the VLV index.
191   * @return the VLV index sort order value in String format.
192   */
193  private String getSortOrderDisplayValue(VLVIndexDescriptor i)
194  {
195    return "<html>"+getSortOrderStringValue(i).replaceAll(", ",",<br>");
196  }
197
198  //Comparison methods.
199
200  private int compareBaseDNs(VLVIndexDescriptor i1, VLVIndexDescriptor i2)
201  {
202    return getDNValue(i1).compareTo(getDNValue(i2));
203  }
204
205  private int compareScopes(VLVIndexDescriptor i1, VLVIndexDescriptor i2)
206  {
207    return toUIString(i1.getScope()).compareTo(toUIString(i2.getScope()));
208  }
209
210  private int compareFilters(VLVIndexDescriptor i1, VLVIndexDescriptor i2)
211  {
212    return i1.getFilter().compareTo(i2.getFilter());
213  }
214
215  private int compareSortOrders(VLVIndexDescriptor i1, VLVIndexDescriptor i2)
216  {
217    return getSortOrderStringValue(i1).compareTo(getSortOrderStringValue(i2));
218  }
219}