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-2010 Sun Microsystems, Inc.
025 *      Portions Copyright 2015 ForgeRock AS
026 */
027
028package org.opends.guitools.controlpanel.datamodel;
029
030import java.util.Collections;
031import java.util.List;
032
033import org.forgerock.opendj.ldap.SearchScope;
034import org.opends.server.admin.std.meta.BackendVLVIndexCfgDefn;
035import org.opends.server.admin.std.meta.LocalDBVLVIndexCfgDefn;
036import org.opends.server.backends.jeb.RemoveOnceLocalDBBackendIsPluggable;
037import org.opends.server.types.DN;
038
039/**
040 * The class used to describe the VLV index configuration.
041 */
042public class VLVIndexDescriptor extends AbstractIndexDescriptor
043{
044  private final DN baseDN;
045  private final SearchScope scope;
046  private final String filter;
047  private List<VLVSortOrder> sortOrder = Collections.emptyList();
048  private int hashCode;
049
050  /**
051   * Constructor for the VLVIndexDescriptor.
052   *
053   * @param name
054   *          the name of the index.
055   * @param backend
056   *          the backend where the index is defined.
057   * @param baseDN
058   *          the baseDN of the search indexed by the VLV index.
059   * @param scope
060   *          the scope of the search indexed by the VLV index.
061   * @param filter
062   *          the filter or the search indexed by the VLV index.
063   * @param sortOrder
064   *          the sort order list of the VLV index.
065   */
066  public VLVIndexDescriptor(String name, BackendDescriptor backend, DN baseDN, SearchScope scope, String filter,
067      List<VLVSortOrder> sortOrder)
068  {
069    super(name, backend);
070    this.baseDN = baseDN;
071    this.scope = scope;
072    this.filter = filter;
073    this.sortOrder = Collections.unmodifiableList(sortOrder);
074
075    recalculateHashCode();
076  }
077
078  @Override
079  public int compareTo(AbstractIndexDescriptor o)
080  {
081    return getName().toLowerCase().compareTo(o.getName().toLowerCase());
082  }
083
084  @Override
085  public int hashCode()
086  {
087    return hashCode;
088  }
089
090  /**
091   * Returns the baseDN of the search indexed by the VLV index.
092   *
093   * @return the baseDN of the search indexed by the VLV index.
094   */
095  public DN getBaseDN()
096  {
097    return baseDN;
098  }
099
100  /**
101   * Returns the filter of the search indexed by the VLV index.
102   *
103   * @return the filter of the search indexed by the VLV index.
104   */
105  public String getFilter()
106  {
107    return filter;
108  }
109
110  /**
111   * Returns the scope of the search indexed by the VLV index.
112   *
113   * @return the scope of the search indexed by the VLV index.
114   */
115  public SearchScope getScope()
116  {
117    return scope;
118  }
119
120  /**
121   * Returns the sort order list of the VLV index.
122   *
123   * @return the sort order list of the VLV index.
124   */
125  public List<VLVSortOrder> getSortOrder()
126  {
127    return sortOrder;
128  }
129
130  @Override
131  public boolean equals(Object o)
132  {
133    if (o == this)
134    {
135      return true;
136    }
137    if (!(o instanceof VLVIndexDescriptor))
138    {
139      return false;
140    }
141
142    final VLVIndexDescriptor index = (VLVIndexDescriptor) o;
143    return index.getName().equalsIgnoreCase(getName())
144        && index.getBaseDN().equals(getBaseDN())
145        && index.getFilter().equals(getFilter())
146        && index.getScope() == getScope()
147        && index.getSortOrder().equals(getSortOrder())
148        && backendIdEqual(index);
149  }
150
151  private boolean backendIdEqual(VLVIndexDescriptor index)
152  {
153    return getBackend() != null
154        && index.getBackend() != null
155        // Only compare the backend IDs.  In this context is better to
156        // do this since the backend object contains some state (like
157        // number entries) that can change.
158        && getBackend().getBackendID().equals(index.getBackend().getBackendID());
159  }
160
161  @Override
162  protected void recalculateHashCode()
163  {
164    final StringBuilder sb = new StringBuilder();
165    for (final VLVSortOrder s : sortOrder)
166    {
167      sb.append(s.getAttributeName()).append(s.isAscending()).append(",");
168    }
169    if (getBackend() != null)
170    {
171      sb.append(getBackend().getBackendID());
172    }
173    hashCode = (getName()+baseDN+scope+filter+sb).hashCode();
174  }
175
176  /**
177   * Returns the equivalent {@code BackendVLVIndexCfgDefn.Scope} to the provided
178   * search scope.
179   *
180   * @param scope
181   *          The {@code SearchScope} to convert.
182   * @return the equivalent {@code BackendVLVIndexCfgDefn.Scope} to the provided
183   *         search scope.
184   */
185  public static BackendVLVIndexCfgDefn.Scope getBackendVLVIndexScope(final SearchScope scope)
186  {
187    switch (scope.asEnum())
188    {
189    case BASE_OBJECT:
190      return BackendVLVIndexCfgDefn.Scope.BASE_OBJECT;
191    case SINGLE_LEVEL:
192      return BackendVLVIndexCfgDefn.Scope.SINGLE_LEVEL;
193    case SUBORDINATES:
194      return BackendVLVIndexCfgDefn.Scope.SUBORDINATE_SUBTREE;
195    case WHOLE_SUBTREE:
196      return BackendVLVIndexCfgDefn.Scope.WHOLE_SUBTREE;
197    case UNKNOWN:
198    default:
199      throw new IllegalArgumentException("Unsupported SearchScope: " + scope);
200    }
201  }
202
203  /**
204   * Returns the equivalent {@code LocalDBVLVIndexCfgDefn.Scope} to the provided
205   * search scope.
206   *
207   * @param scope
208   *          The {@code SearchScope} to convert.
209   * @return the equivalent {@code LocalDBVLVIndexCfgDefn.Scope} to the provided
210   *         search scope.
211   */
212  @RemoveOnceLocalDBBackendIsPluggable
213  public static LocalDBVLVIndexCfgDefn.Scope getLocalDBVLVIndexScope(final SearchScope scope)
214  {
215    switch (scope.asEnum())
216    {
217    case BASE_OBJECT:
218      return LocalDBVLVIndexCfgDefn.Scope.BASE_OBJECT;
219    case SINGLE_LEVEL:
220      return LocalDBVLVIndexCfgDefn.Scope.SINGLE_LEVEL;
221    case SUBORDINATES:
222      return LocalDBVLVIndexCfgDefn.Scope.SUBORDINATE_SUBTREE;
223    case WHOLE_SUBTREE:
224      return LocalDBVLVIndexCfgDefn.Scope.WHOLE_SUBTREE;
225    case UNKNOWN:
226    default:
227      throw new IllegalArgumentException("Unsupported SearchScope: " + scope);
228    }
229  }
230
231  /**
232   * Convert the provided {@code BackendVLVIndexCfgDefn.Scope} to
233   * {@code SearchScope}.
234   *
235   * @param scope
236   *          The scope to convert.
237   * @return the provided {@code BackendVLVIndexCfgDefn.Scope} to
238   *         {@code SearchScope}
239   */
240  public static SearchScope toSearchScope(final BackendVLVIndexCfgDefn.Scope scope)
241  {
242    switch (scope)
243    {
244    case BASE_OBJECT:
245      return SearchScope.BASE_OBJECT;
246    case SINGLE_LEVEL:
247      return SearchScope.SINGLE_LEVEL;
248    case SUBORDINATE_SUBTREE:
249      return SearchScope.SUBORDINATES;
250    case WHOLE_SUBTREE:
251      return SearchScope.WHOLE_SUBTREE;
252    default:
253      throw new IllegalArgumentException("Unsupported BackendVLVIndexCfgDefn.Scope: " + scope);
254    }
255  }
256
257  /**
258   * Convert the provided {@code LocalDBVLVIndexCfgDefn.Scope} to
259   * {@code SearchScope}.
260   *
261   * @param scope
262   *          The scope to convert.
263   * @return the provided {@code LocalDBVLVIndexCfgDefn.Scope} to
264   *         {@code SearchScope}
265   */
266  @RemoveOnceLocalDBBackendIsPluggable
267  public static SearchScope toSearchScope(final LocalDBVLVIndexCfgDefn.Scope scope)
268  {
269    switch (scope)
270    {
271    case BASE_OBJECT:
272      return SearchScope.BASE_OBJECT;
273    case SINGLE_LEVEL:
274      return SearchScope.SINGLE_LEVEL;
275    case SUBORDINATE_SUBTREE:
276      return SearchScope.SUBORDINATES;
277    case WHOLE_SUBTREE:
278      return SearchScope.WHOLE_SUBTREE;
279    default:
280      throw new IllegalArgumentException("Unsupported LocalDBVLVIndexCfgDefn.Scope: " + scope);
281    }
282  }
283
284}