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