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 Sun Microsystems, Inc.
025 *      Portions Copyright 2014-2015 ForgeRock AS
026 */
027
028package org.opends.guitools.controlpanel.task;
029
030import java.util.Collection;
031import java.util.HashSet;
032import java.util.Set;
033import java.util.TreeSet;
034
035import org.opends.guitools.controlpanel.datamodel.BackendDescriptor;
036import org.opends.guitools.controlpanel.datamodel.BaseDNDescriptor;
037import org.opends.guitools.controlpanel.datamodel.ControlPanelInfo;
038import org.opends.guitools.controlpanel.ui.ProgressDialog;
039import org.opends.server.types.DN;
040
041/**
042 * Abstract task used to factorize some code shared by different tasks involving
043 * indexes.
044 *
045 */
046public abstract class IndexTask extends Task
047{
048  /**
049   * The set of backends that are affected by this task.
050   */
051  protected Set<String> backendSet;
052  /**
053   * The set of base DNs that are affected by this task.
054   */
055  protected Set<String> baseDNs;
056
057  /**
058   * Constructor of the task.
059   * @param info the control panel information.
060   * @param dlg the progress dialog where the task progress will be displayed.
061   * @param baseDN the base DN where the indexes are defined.
062   */
063  protected IndexTask(ControlPanelInfo info, ProgressDialog dlg,
064      String baseDN)
065  {
066    super(info, dlg);
067    baseDNs = new HashSet<>();
068    baseDNs.add(baseDN);
069    initializeBackendSet();
070  }
071
072  /**
073   * Constructor of the task.
074   * @param info the control panel information.
075   * @param dlg the progress dialog where the task progress will be displayed.
076   * @param baseDNs the list of base DNs where the indexes are defined.
077   */
078  protected IndexTask(ControlPanelInfo info, ProgressDialog dlg,
079      Collection<String> baseDNs)
080  {
081    super(info, dlg);
082    backendSet = new HashSet<>();
083    this.baseDNs = new TreeSet<>();
084    this.baseDNs.addAll(baseDNs);
085    initializeBackendSet();
086  }
087
088  /** Initialize the list of backends that are affected by this task. */
089  private void initializeBackendSet()
090  {
091    backendSet = new TreeSet<>();
092    DN theDN = null;
093    for (String baseDN : baseDNs)
094    {
095      try
096      {
097        theDN = DN.valueOf(baseDN);
098      }
099      catch (Throwable t)
100      {
101        throw new IllegalArgumentException("Could not decode dn " + baseDN, t);
102      }
103      BackendDescriptor backend = findBackendByID(theDN);
104      if (backend != null) {
105        backendSet.add(backend.getBackendID());
106      }
107    }
108  }
109
110  private BackendDescriptor findBackendByID(DN dn)
111  {
112    for (BackendDescriptor backend : getInfo().getServerDescriptor().getBackends())
113    {
114      for (BaseDNDescriptor b : backend.getBaseDns())
115      {
116        if (b.getDn().equals(dn))
117        {
118          return backend;
119        }
120      }
121    }
122    return null;
123  }
124
125  /** {@inheritDoc} */
126  public Set<String> getBackends()
127  {
128    return backendSet;
129  }
130}