mirror of https://github.com/OpenIdentityPlatform/OpenDJ.git

Jean-Noël Rouvignac
05.19.2016 c3a6c42b47b5ed31e802ea7eed11aa2baf57749d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
/*
 * The contents of this file are subject to the terms of the Common Development and
 * Distribution License (the License). You may not use this file except in compliance with the
 * License.
 *
 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
 * specific language governing permission and limitations under the License.
 *
 * When distributing Covered Software, include this CDDL Header Notice in each file and include
 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
 * Header, with the fields enclosed by brackets [] replaced by your own identifying
 * information: "Portions Copyright [year] [name of copyright owner]".
 *
 * Copyright 2008-2010 Sun Microsystems, Inc.
 * Portions Copyright 2014-2016 ForgeRock AS.
 */
package org.opends.guitools.controlpanel.ui;
 
import static org.forgerock.opendj.server.config.meta.BackendIndexCfgDefn.IndexType.*;
import static org.opends.messages.AdminToolMessages.*;
 
import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;
 
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
 
import org.forgerock.i18n.LocalizableMessage;
import org.forgerock.opendj.config.LDAPProfile;
import org.forgerock.opendj.config.PropertyException;
import org.forgerock.opendj.config.client.ManagementContext;
import org.forgerock.opendj.config.client.ldap.LDAPManagementContext;
import org.forgerock.opendj.server.config.client.BackendIndexCfgClient;
import org.forgerock.opendj.server.config.client.PluggableBackendCfgClient;
import org.forgerock.opendj.server.config.meta.BackendIndexCfgDefn;
import org.forgerock.opendj.server.config.meta.BackendIndexCfgDefn.IndexType;
import org.opends.guitools.controlpanel.datamodel.IndexDescriptor;
import org.opends.guitools.controlpanel.ui.components.TitlePanel;
import org.opends.guitools.controlpanel.ui.renderer.CustomListCellRenderer;
import org.opends.guitools.controlpanel.util.Utilities;
import org.opends.quicksetup.Installation;
import org.opends.server.config.ConfigException;
import org.forgerock.opendj.ldap.schema.AttributeType;
import org.opends.server.types.OpenDsException;
 
/**
 * Abstract class used to refactor some code between the classes that are used
 * to edit/create an index.
 */
abstract class AbstractIndexPanel extends StatusGenericPanel
{
  private static final long serialVersionUID = 4465529396749593707L;
 
  /** Custom attributes message. */
  static final LocalizableMessage CUSTOM_ATTRIBUTES = INFO_CTRL_PANEL_CUSTOM_ATTRIBUTES_LABEL.get();
  /** Standard attributes message. */
  static final LocalizableMessage STANDARD_ATTRIBUTES = INFO_CTRL_PANEL_STANDARD_ATTRIBUTES_LABEL.get();
 
  /** Minimum value for entry limit. */
  static final int MIN_ENTRY_LIMIT =
      BackendIndexCfgDefn.getInstance().getIndexEntryLimitPropertyDefinition().getLowerLimit();
  /** Maximum value for entry limit. */
  static final int MAX_ENTRY_LIMIT =
      BackendIndexCfgDefn.getInstance().getIndexEntryLimitPropertyDefinition().getUpperLimit();
 
  /** LocalizableMessage to be displayed to indicate that an index is not configurable. */
  static final LocalizableMessage NON_CONFIGURABLE_INDEX = INFO_CTRL_PANEL_NON_CONFIGURABLE_INDEX_LABEL.get();
  /** LocalizableMessage to be displayed to indicate that an index has been modified. */
  static final LocalizableMessage INDEX_MODIFIED = INFO_CTRL_PANEL_INDEX_MODIFIED_LABEL.get();
 
  /** Default value for entry limit. */
  static final int DEFAULT_ENTRY_LIMIT = 4000;
 
  final TitlePanel titlePanel = new TitlePanel(LocalizableMessage.EMPTY, LocalizableMessage.EMPTY);
 
  /** Attributes combo box. */
  final JComboBox attributes = Utilities.createComboBox();
 
  /** Name of the index label. */
  final JLabel name = Utilities.createDefaultLabel();
 
  /** Backends label. */
  final JLabel lBackend = Utilities.createPrimaryLabel(INFO_CTRL_PANEL_BACKEND_LABEL.get());
  /** Read-only backend name label. */
  final JLabel backendName = Utilities.createDefaultLabel();
 
  final JLabel lAttribute = Utilities.createPrimaryLabel(INFO_CTRL_PANEL_ATTRIBUTE_LABEL.get());
 
  final JLabel lEntryLimit = Utilities.createPrimaryLabel(INFO_CTRL_PANEL_ENTRY_LIMIT_LABEL.get());
 
  final JTextField entryLimit = Utilities.createShortTextField();
 
  final JLabel lType = Utilities.createPrimaryLabel(INFO_CTRL_PANEL_INDEX_TYPE_LABEL.get());
  /** Panel containing all the index types. */
  final JPanel typesPanel = new JPanel(new GridBagLayout());
 
  /** Approximate index type check box. */
  final JCheckBox approximate = Utilities.createCheckBox(INFO_CTRL_PANEL_APPROXIMATE_LABEL.get());
  /** Equality index type check box. */
  final JCheckBox equality = Utilities.createCheckBox(INFO_CTRL_PANEL_EQUALITY_LABEL.get());
  /** Ordering index type check box. */
  final JCheckBox ordering = Utilities.createCheckBox(INFO_CTRL_PANEL_ORDERING_LABEL.get());
  /** Presence index type check box. */
  final JCheckBox presence = Utilities.createCheckBox(INFO_CTRL_PANEL_PRESENCE_LABEL.get());
  /** Substring index type check box. */
  final JCheckBox substring = Utilities.createCheckBox(INFO_CTRL_PANEL_SUBSTRING_LABEL.get());
  /** Array of checkboxes. */
  final JCheckBox[] types =       { approximate, equality, ordering, presence, substring };
  /** Array of index types that matches the array of checkboxes (types). */
  final IndexType[] configTypes = { APPROXIMATE, EQUALITY, ORDERING, PRESENCE, SUBSTRING };
 
  final JButton deleteIndex = Utilities.createButton(INFO_CTRL_PANEL_DELETE_INDEX_LABEL.get());
  final JButton saveChanges = Utilities.createButton(INFO_CTRL_PANEL_SAVE_CHANGES_LABEL.get());
 
  /** Label containing some warning information (such as the fact that the index cannot be edited). */
  final JLabel warning = Utilities.createDefaultLabel();
 
  /**
   * Repopulates the contents of the panel with the provided attribute type. It
   * will check the checkboxes for which the attribute has a matching rule.
   *
   * @param attr
   *          the attribute.
   */
  void repopulateTypesPanel(final AttributeType attr)
  {
    typesPanel.removeAll();
    GridBagConstraints gbc = new GridBagConstraints();
    gbc.gridy = 0;
    gbc.gridx = 0;
    gbc.anchor = GridBagConstraints.WEST;
    gbc.fill = GridBagConstraints.HORIZONTAL;
    if (attr != null)
    {
      if (attr.getApproximateMatchingRule() != null)
      {
        typesPanel.add(approximate, gbc);
        gbc.insets.top = 10;
        gbc.gridy++;
      }
      if (attr.getEqualityMatchingRule() != null)
      {
        typesPanel.add(equality, gbc);
        gbc.insets.top = 10;
        gbc.gridy++;
      }
      if (attr.getOrderingMatchingRule() != null)
      {
        typesPanel.add(ordering, gbc);
        gbc.insets.top = 10;
        gbc.gridy++;
      }
      typesPanel.add(presence, gbc);
      gbc.gridx = 1;
      gbc.weightx = 1.0;
      typesPanel.add(Box.createHorizontalGlue(), gbc);
      gbc.weightx = 0.0;
      gbc.gridx = 0;
      gbc.gridy++;
      gbc.insets.top = 10;
      if (attr.getSubstringMatchingRule() != null)
      {
        typesPanel.add(substring, gbc);
        gbc.insets.top = 10;
      }
    }
    typesPanel.validate();
  }
 
  /**
   * Creates the basic layout of the panel.
   *
   * @param c
   *          the container of the layout.
   * @param gbc
   *          the grid bag constraints to be used.
   * @param nameReadOnly
   *          whether the panel is read-only or not.
   */
  void createBasicLayout(final Container c, final GridBagConstraints gbc, final boolean nameReadOnly)
  {
    gbc.gridx = 0;
    gbc.gridy = 0;
    gbc.gridwidth = 3;
    addErrorPane(c, gbc);
 
    if (nameReadOnly)
    {
      gbc.gridy++;
      titlePanel.setTitle(INFO_CTRL_PANEL_INDEX_DETAILS_LABEL.get());
      gbc.fill = GridBagConstraints.HORIZONTAL;
      gbc.anchor = GridBagConstraints.WEST;
      gbc.insets.top = 10;
      gbc.weightx = 1.0;
      JPanel p = new JPanel(new GridBagLayout());
      p.setOpaque(false);
      c.add(p, gbc);
      GridBagConstraints gbc2 = new GridBagConstraints();
      gbc2.weightx = 0.0;
      gbc2.gridwidth = GridBagConstraints.RELATIVE;
      p.add(titlePanel, gbc2);
      gbc2.gridwidth = GridBagConstraints.REMAINDER;
      gbc2.fill = GridBagConstraints.HORIZONTAL;
      gbc2.weightx = 1.0;
      p.add(Box.createHorizontalGlue(), gbc2);
    }
 
    gbc.gridwidth = 1;
    gbc.gridy++;
    gbc.anchor = GridBagConstraints.WEST;
    gbc.fill = GridBagConstraints.HORIZONTAL;
    gbc.insets.left = 0;
    gbc.gridx = 0;
    gbc.weightx = 0.0;
    c.add(lAttribute, gbc);
    gbc.insets.left = 10;
    gbc.gridx = 1;
    if (!nameReadOnly)
    {
      attributes.addItemListener(new IgnoreItemListener(attributes));
      attributes.setRenderer(new CustomListCellRenderer(attributes));
      c.add(attributes, gbc);
    }
    else
    {
      c.add(name, gbc);
    }
    gbc.insets.top = 10;
    gbc.gridy++;
    gbc.insets.left = 0;
    gbc.gridx = 0;
    c.add(lBackend, gbc);
    gbc.insets.left = 10;
    gbc.gridx = 1;
    c.add(backendName, gbc);
 
    gbc.gridy++;
    gbc.insets.left = 0;
    gbc.gridx = 0;
    c.add(lEntryLimit, gbc);
    gbc.insets.left = 10;
    gbc.gridx = 1;
    JPanel p = new JPanel(new GridBagLayout());
    p.setOpaque(false);
    c.add(p, gbc);
    GridBagConstraints gbc2 = new GridBagConstraints();
    gbc2.weightx = 0.0;
    gbc2.gridwidth = GridBagConstraints.RELATIVE;
    p.add(entryLimit, gbc2);
    gbc2.gridwidth = GridBagConstraints.REMAINDER;
    gbc2.fill = GridBagConstraints.HORIZONTAL;
    gbc2.weightx = 1.0;
    p.add(Box.createHorizontalGlue(), gbc2);
 
    gbc.gridx = 0;
    gbc.insets.left = 0;
    gbc.gridy++;
    gbc.weightx = 0.0;
    gbc.weightx = 0.0;
    gbc.anchor = GridBagConstraints.NORTHWEST;
    c.add(lType, gbc);
 
    gbc.gridx = 1;
    gbc.anchor = GridBagConstraints.WEST;
    gbc.insets.left = 10;
    gbc.weightx = 1.0;
    JCheckBox[] types = { approximate, equality, ordering, presence, substring };
    typesPanel.setOpaque(false);
    c.add(typesPanel, gbc);
    gbc.gridy++;
    gbc2 = new GridBagConstraints();
    gbc.gridwidth = GridBagConstraints.REMAINDER;
    for (JCheckBox type : types)
    {
      type.setOpaque(false);
      typesPanel.add(type, gbc2);
      gbc2.anchor = GridBagConstraints.WEST;
      gbc2.insets.top = 10;
    }
 
    gbc.weighty = 1.0;
    gbc.weightx = 0.0;
    gbc.gridx = 0;
    gbc.fill = GridBagConstraints.VERTICAL;
    c.add(Box.createVerticalGlue(), gbc);
  }
 
  /**
   * Returns a sorted set of index types (that matches what the user selected on
   * the check boxes).
   *
   * @return a sorted set of indexes (that matches what the user selected on the
   *         check boxes).
   */
  SortedSet<IndexType> getTypes()
  {
    SortedSet<IndexType> indexTypes = new TreeSet<>();
    for (int i = 0; i < types.length; i++)
    {
      if (types[i].isSelected())
      {
        indexTypes.add(configTypes[i]);
      }
    }
    return indexTypes;
  }
 
  /**
   * Returns a list of error message with the problems encountered in the data
   * provided by the user.
   *
   * @return a list of error message with the problems encountered in the data
   *         provided by the user.
   */
  List<LocalizableMessage> getErrors()
  {
    List<LocalizableMessage> errors = new ArrayList<>();
    setPrimaryValid(lEntryLimit);
    setPrimaryValid(lType);
 
    String newEntryLimit = entryLimit.getText().trim();
 
    try
    {
      int n = Integer.parseInt(newEntryLimit);
      if (n < MIN_ENTRY_LIMIT || MAX_ENTRY_LIMIT < n)
      {
        errors.add(ERR_CTRL_PANEL_INVALID_ENTRY_LIMIT_LABEL.get(MIN_ENTRY_LIMIT, MAX_ENTRY_LIMIT));
        setPrimaryInvalid(lEntryLimit);
      }
    }
    catch (Throwable t)
    {
      errors.add(ERR_CTRL_PANEL_INVALID_ENTRY_LIMIT_LABEL.get(MIN_ENTRY_LIMIT, MAX_ENTRY_LIMIT));
      setPrimaryInvalid(lEntryLimit);
    }
 
    if (getTypes().isEmpty())
    {
      errors.add(ERR_CTRL_PANEL_NO_INDEX_TYPE_SELECTED.get());
      setPrimaryInvalid(lType);
    }
 
    return errors;
  }
 
  void createIndexOffline(final String backendName, final String attributeName,
      final Set<IndexType> indexTypes, final int indexEntryLimit) throws OpenDsException
  {
    updateIndexOffline(backendName, null, attributeName, indexTypes, indexEntryLimit);
  }
 
  void modifyIndexOffline(final String backendName, final String attributeName, final IndexDescriptor indexToModify,
      final Set<IndexType> indexTypes, final int indexEntryLimit) throws OpenDsException
  {
    updateIndexOffline(backendName, indexToModify, attributeName, indexTypes, indexEntryLimit);
  }
 
  private void updateIndexOffline(final String backendName, final IndexDescriptor indexToModify,
      final String attributeName, final Set<IndexType> indexTypes, final int indexEntryLimit)
      throws OpenDsException
  {
    getInfo().initializeConfigurationFramework();
    final File configFile = Installation.getLocal().getCurrentConfigurationFile();
    final LDAPProfile ldapProfile = LDAPProfile.getInstance();
    try (ManagementContext context = LDAPManagementContext.newLDIFManagementContext(configFile, ldapProfile))
    {
      final PluggableBackendCfgClient backend =
          (PluggableBackendCfgClient) context.getRootConfiguration().getBackend(backendName);
      updateBackendIndexOnline(backend, indexToModify, attributeName, indexTypes, indexEntryLimit);
    }
    catch (final Exception e)
    {
      throw new ConfigException(LocalizableMessage.raw(e.getMessage(), e));
    }
  }
 
  private void updateBackendIndexOnline(final PluggableBackendCfgClient backend,
      final IndexDescriptor indexToModify, final String attributeName, final Set<IndexType> indexTypes,
      final int indexEntryLimit) throws Exception
  {
    final boolean isCreation = indexToModify == null;
    final List<PropertyException> exceptions = new ArrayList<>();
    final BackendIndexCfgClient index = isCreation
        ? backend.createBackendIndex(BackendIndexCfgDefn.getInstance(), attributeName, exceptions)
        : backend.getBackendIndex(attributeName);
 
    if (isCreation || indexTypes.equals(indexToModify.getTypes()))
    {
      index.setIndexType(indexTypes);
    }
 
    if (indexEntryLimit != index.getIndexEntryLimit())
    {
      index.setIndexEntryLimit(indexEntryLimit);
    }
    index.commit();
    Utilities.throwFirstFrom(exceptions);
 }
}