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