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 2015 ForgeRock AS
025 */
026package org.opends.guitools.controlpanel.datamodel;
027
028import java.util.HashSet;
029import java.util.LinkedHashSet;
030import java.util.Set;
031
032import org.opends.server.admin.std.meta.BackendIndexCfgDefn;
033import org.opends.server.admin.std.meta.LocalDBIndexCfgDefn;
034import org.opends.server.backends.jeb.RemoveOnceLocalDBBackendIsPluggable;
035
036/**
037 * Defines the set of values for the index type and provides adaptors to convert
038 * from/to corresponding configuration classes.
039 */
040@RemoveOnceLocalDBBackendIsPluggable
041public enum IndexTypeDescriptor
042{
043  /**
044   * This index type is used to improve the efficiency of searches using
045   * approximate matching search filters.
046   */
047  APPROXIMATE(LocalDBIndexCfgDefn.IndexType.APPROXIMATE, BackendIndexCfgDefn.IndexType.APPROXIMATE,
048      org.forgerock.opendj.server.config.meta.LocalDBIndexCfgDefn.IndexType.APPROXIMATE,
049      org.forgerock.opendj.server.config.meta.BackendIndexCfgDefn.IndexType.APPROXIMATE),
050
051  /**
052   * This index type is used to improve the efficiency of searches using
053   * equality search filters.
054   */
055  EQUALITY(LocalDBIndexCfgDefn.IndexType.EQUALITY, BackendIndexCfgDefn.IndexType.EQUALITY,
056      org.forgerock.opendj.server.config.meta.LocalDBIndexCfgDefn.IndexType.EQUALITY,
057      org.forgerock.opendj.server.config.meta.BackendIndexCfgDefn.IndexType.EQUALITY),
058
059  /**
060   * This index type is used to improve the efficiency of searches using
061   * extensible matching search filters.
062   */
063  EXTENSIBLE(LocalDBIndexCfgDefn.IndexType.EXTENSIBLE, BackendIndexCfgDefn.IndexType.EXTENSIBLE,
064      org.forgerock.opendj.server.config.meta.LocalDBIndexCfgDefn.IndexType.EXTENSIBLE,
065      org.forgerock.opendj.server.config.meta.BackendIndexCfgDefn.IndexType.EXTENSIBLE),
066
067  /**
068   * This index type is used to improve the efficiency of searches using
069   * "greater than or equal to" or "less then or equal to" search filters.
070   */
071  ORDERING(LocalDBIndexCfgDefn.IndexType.ORDERING, BackendIndexCfgDefn.IndexType.ORDERING,
072      org.forgerock.opendj.server.config.meta.LocalDBIndexCfgDefn.IndexType.ORDERING,
073      org.forgerock.opendj.server.config.meta.BackendIndexCfgDefn.IndexType.ORDERING),
074
075  /**
076   * This index type is used to improve the efficiency of searches using the
077   * presence search filters.
078   */
079  PRESENCE(LocalDBIndexCfgDefn.IndexType.PRESENCE, BackendIndexCfgDefn.IndexType.PRESENCE,
080      org.forgerock.opendj.server.config.meta.LocalDBIndexCfgDefn.IndexType.PRESENCE,
081      org.forgerock.opendj.server.config.meta.BackendIndexCfgDefn.IndexType.PRESENCE),
082
083  /**
084   * This index type is used to improve the efficiency of searches using
085   * substring search filters.
086   */
087  SUBSTRING(LocalDBIndexCfgDefn.IndexType.SUBSTRING, BackendIndexCfgDefn.IndexType.SUBSTRING,
088      org.forgerock.opendj.server.config.meta.LocalDBIndexCfgDefn.IndexType.SUBSTRING,
089      org.forgerock.opendj.server.config.meta.BackendIndexCfgDefn.IndexType.SUBSTRING);
090
091  private final LocalDBIndexCfgDefn.IndexType oldConfigLocalDBIndexType;
092  private final BackendIndexCfgDefn.IndexType oldConfigBackendIndexType;
093  private final org.forgerock.opendj.server.config.meta.LocalDBIndexCfgDefn.IndexType localDBIndexType;
094  private final org.forgerock.opendj.server.config.meta.BackendIndexCfgDefn.IndexType backendIndexType;
095
096  private IndexTypeDescriptor(final LocalDBIndexCfgDefn.IndexType oldConfigLocalDBIndexType,
097      final BackendIndexCfgDefn.IndexType oldConfigBackendIndexType,
098      final org.forgerock.opendj.server.config.meta.LocalDBIndexCfgDefn.IndexType localDBIndexType,
099      final org.forgerock.opendj.server.config.meta.BackendIndexCfgDefn.IndexType backendIndexType)
100  {
101    this.oldConfigLocalDBIndexType = oldConfigLocalDBIndexType;
102    this.oldConfigBackendIndexType = oldConfigBackendIndexType;
103    this.localDBIndexType = localDBIndexType;
104    this.backendIndexType = backendIndexType;
105  }
106
107  /**
108   * Convert the index type to the equivalent
109   * {@code BackendIndexCfgDefn.IndexType}.
110   *
111   * @return The index type to the equivalent
112   *         {@code BackendIndexCfgDefn.IndexType}
113   */
114  public BackendIndexCfgDefn.IndexType toBackendIndexType()
115  {
116    return oldConfigBackendIndexType;
117  }
118
119  /**
120   * Convert the index type to the equivalent
121   * {@code LocalDBIndexCfgDefn.IndexType}.
122   *
123   * @return The index type to the equivalent
124   *         {@code LocalDBIndexCfgDefn.IndexType}
125   */
126  public LocalDBIndexCfgDefn.IndexType toLocalDBIndexType()
127  {
128    return oldConfigLocalDBIndexType;
129  }
130
131  private static IndexTypeDescriptor fromBackendIndexType(final BackendIndexCfgDefn.IndexType indexType)
132  {
133    switch (indexType)
134    {
135    case APPROXIMATE:
136      return APPROXIMATE;
137    case EQUALITY:
138      return EQUALITY;
139    case EXTENSIBLE:
140      return EXTENSIBLE;
141    case ORDERING:
142      return ORDERING;
143    case PRESENCE:
144      return PRESENCE;
145    case SUBSTRING:
146      return SUBSTRING;
147    default:
148      throw new IllegalArgumentException("No IndexTypeDescriptor corresponding to: " + indexType);
149    }
150  }
151
152  private static IndexTypeDescriptor fromLocalDBIndexType(final LocalDBIndexCfgDefn.IndexType indexType)
153  {
154    switch (indexType)
155    {
156    case APPROXIMATE:
157      return APPROXIMATE;
158    case EQUALITY:
159      return EQUALITY;
160    case EXTENSIBLE:
161      return EXTENSIBLE;
162    case ORDERING:
163      return ORDERING;
164    case PRESENCE:
165      return PRESENCE;
166    case SUBSTRING:
167      return SUBSTRING;
168    default:
169      throw new IllegalArgumentException("No IndexTypeDescriptor corresponding to: " + indexType);
170    }
171  }
172
173  /**
174   * Convert the provided {@code Set<BackendIndexCfgDefn.IndexType>} to a
175   * {@code Set<IndexTypeDescriptor>}.
176   *
177   * @param indexTypes
178   *          A set of {@code Set<BackendIndexCfgDefn.IndexType>}
179   * @return A set of {@code Set<IndexTypeDescriptor>} corresponding to the
180   *         provided {@code Set<BackendIndexCfgDefn.IndexType>}
181   */
182  public static Set<IndexTypeDescriptor> fromBackendIndexTypes(final Set<BackendIndexCfgDefn.IndexType> indexTypes)
183  {
184    final Set<IndexTypeDescriptor> indexTypeDescriptors = new LinkedHashSet<>();
185    for (final BackendIndexCfgDefn.IndexType indexType : indexTypes)
186    {
187      indexTypeDescriptors.add(fromBackendIndexType(indexType));
188    }
189    return indexTypeDescriptors;
190  }
191
192  /**
193   * Convert the provided {@code Set<LocalDBIndexCfgDefn.IndexType} to a
194   * {@code Set<IndexTypeDescriptor>}.
195   *
196   * @param indexTypes
197   *          A set of {@code Set<LocalDBIndexCfgDefn.IndexType>}
198   * @return A set of {@code Set<IndexTypeDescriptor>} corresponding to the
199   *         provided {@code Set<LocalDBIndexCfgDefn.IndexType>}
200   */
201  public static Set<IndexTypeDescriptor> fromLocalDBIndexTypes(final Set<LocalDBIndexCfgDefn.IndexType> indexTypes)
202  {
203    final Set<IndexTypeDescriptor> indexTypeDescriptors = new LinkedHashSet<>();
204    for (final LocalDBIndexCfgDefn.IndexType indexType : indexTypes)
205    {
206      indexTypeDescriptors.add(fromLocalDBIndexType(indexType));
207    }
208    return indexTypeDescriptors;
209  }
210
211  /**
212   * Convert the provided {@code Set<IndexTypeDescriptor>} to a
213   * {@code Set<BackendIndexCfgDefn.IndexType>}.
214   *
215   * @param indexTypeDescriptors
216   *          A set of {@code Set<IndexTypeDescriptor>}
217   * @return A set of {@code Set<BackendIndexCfgDefn.IndexType>} corresponding
218   *         to the provided {@code Set<IndexTypeDescriptor>}
219   */
220  public static Set<BackendIndexCfgDefn.IndexType> toBackendIndexTypes(
221      final Set<IndexTypeDescriptor> indexTypeDescriptors)
222  {
223    final Set<BackendIndexCfgDefn.IndexType> indexTypes = new LinkedHashSet<>();
224    for (final IndexTypeDescriptor indexTypeDescriptor : indexTypeDescriptors)
225    {
226      indexTypes.add(indexTypeDescriptor.toBackendIndexType());
227    }
228    return indexTypes;
229  }
230
231  /**
232   * Convert the provided {@code Set<IndexTypeDescriptor>} to a
233   * {@code Set<LocalDBIndexCfgDefn.IndexType>}.
234   *
235   * @param indexTypeDescriptors
236   *          A set of {@code Set<IndexTypeDescriptor>}
237   * @return A set of {@code Set<LocalDBIndexCfgDefn.IndexType>} corresponding
238   *         to the provided {@code Set<IndexTypeDescriptor>}
239   */
240  public static Set<LocalDBIndexCfgDefn.IndexType> toLocalDBIndexTypes(
241      final Set<IndexTypeDescriptor> indexTypeDescriptors)
242  {
243    final Set<LocalDBIndexCfgDefn.IndexType> indexTypes = new LinkedHashSet<>();
244    for (final IndexTypeDescriptor indexTypeDescriptor : indexTypeDescriptors)
245    {
246      indexTypes.add(indexTypeDescriptor.toLocalDBIndexType());
247    }
248    return indexTypes;
249  }
250
251  /**
252   * Convert the provided {@code Set<IndexTypeDescriptor>} to a
253   * {@code Set<org.forgerock.opendj.server.config.meta.LocalDBIndexCfgDefn.IndexType>}.
254   *
255   * @param indexTypeDescriptors
256   *          A set of {@code Set<IndexTypeDescriptor>}
257   * @return A set of
258   *         {@code Set<org.forgerock.opendj.server.config.meta.LocalDBIndexCfgDefn.IndexType>}
259   *         corresponding to the provided {@code Set<IndexTypeDescriptor>}
260   */
261  @RemoveOnceLocalDBBackendIsPluggable
262  public static Set<org.forgerock.opendj.server.config.meta.LocalDBIndexCfgDefn.IndexType> toNewConfigLocalDBIndexTypes(
263      final Set<IndexTypeDescriptor> indexTypeDescriptors)
264  {
265    Set<org.forgerock.opendj.server.config.meta.LocalDBIndexCfgDefn.IndexType> newConfigIndexTypes = new HashSet<>();
266    for (IndexTypeDescriptor indexType : indexTypeDescriptors)
267    {
268      newConfigIndexTypes.add(indexType.localDBIndexType);
269    }
270    return newConfigIndexTypes;
271  }
272
273  /**
274   * Convert the provided {@code Set<IndexTypeDescriptor>} to a
275   * {@code Set<org.forgerock.opendj.server.config.meta.BackendIndexCfgDefn.IndexType>}.
276   *
277   * @param indexTypeDescriptors
278   *          A set of {@code Set<IndexTypeDescriptor>}
279   * @return A set of
280   *         {@code Set<org.forgerock.opendj.server.config.meta.BackendIndexCfgDefn.IndexType>}
281   *         corresponding to the provided {@code Set<IndexTypeDescriptor>}
282   */
283  public static Set<org.forgerock.opendj.server.config.meta.BackendIndexCfgDefn.IndexType> toNewConfigBackendIndexTypes(
284      final Set<IndexTypeDescriptor> indexTypeDescriptors)
285  {
286    Set<org.forgerock.opendj.server.config.meta.BackendIndexCfgDefn.IndexType> newConfigIndexTypes = new HashSet<>();
287    for (IndexTypeDescriptor indexType : indexTypeDescriptors)
288    {
289      newConfigIndexTypes.add(indexType.backendIndexType);
290    }
291    return newConfigIndexTypes;
292  }
293
294}