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-2009 Sun Microsystems, Inc.
025 *      Portions Copyright 2014-2015 ForgeRock AS
026 */
027
028package org.opends.guitools.controlpanel.ui;
029
030import static org.opends.messages.AdminToolMessages.*;
031
032import java.util.ArrayList;
033import java.util.Collection;
034import java.util.HashSet;
035import java.util.LinkedHashSet;
036import java.util.SortedSet;
037import java.util.TreeSet;
038
039import org.opends.guitools.controlpanel.datamodel.BackendDescriptor;
040import org.opends.guitools.controlpanel.datamodel.BaseDNDescriptor;
041import org.opends.guitools.controlpanel.datamodel.ServerDescriptor;
042import org.opends.guitools.controlpanel.event.ConfigurationChangeEvent;
043import org.opends.guitools.controlpanel.task.DeleteBaseDNAndBackendTask;
044import org.opends.guitools.controlpanel.task.Task;
045import org.opends.guitools.controlpanel.util.Utilities;
046import org.forgerock.i18n.LocalizableMessage;
047import org.forgerock.i18n.LocalizableMessageBuilder;
048
049/**
050 * The panel displayed when the user clicks on 'Delete Backend...' in the
051 * browse entries dialog.
052 *
053 */
054public class DeleteBackendPanel extends DeleteBaseDNPanel
055{
056  private static final long serialVersionUID = 8744925738292396658L;
057
058  /** {@inheritDoc} */
059  public LocalizableMessage getTitle()
060  {
061    return INFO_CTRL_PANEL_DELETE_BACKEND_TITLE.get();
062  }
063
064  /**
065   * Returns the no backend found label.
066   * @return the no backend found label.
067   */
068  protected LocalizableMessage getNoElementsFoundLabel()
069  {
070    return INFO_CTRL_PANEL_NO_BACKENDS_FOUND_LABEL.get();
071  }
072
073  /**
074   * Returns the list label.
075   * @return the list label.
076   */
077  protected LocalizableMessage getListLabel()
078  {
079    return INFO_CTRL_PANEL_SELECT_BACKENDS_TO_DELETE.get();
080  }
081
082  /** {@inheritDoc} */
083  public void configurationChanged(ConfigurationChangeEvent ev)
084  {
085    ServerDescriptor desc = ev.getNewDescriptor();
086    final SortedSet<String> newElements = new TreeSet<>();
087    for (BackendDescriptor backend : desc.getBackends())
088    {
089      if (!backend.isConfigBackend())
090      {
091        newElements.add(backend.getBackendID());
092      }
093    }
094    updateList(newElements);
095    updateErrorPaneAndOKButtonIfAuthRequired(desc,
096        isLocal() ?
097            INFO_CTRL_PANEL_AUTHENTICATION_REQUIRED_FOR_BACKEND_DELETE.get() :
098      INFO_CTRL_PANEL_CANNOT_CONNECT_TO_REMOTE_DETAILS.get(desc.getHostname()));
099  }
100
101  /** {@inheritDoc} */
102  public void okClicked()
103  {
104    final LinkedHashSet<LocalizableMessage> errors = new LinkedHashSet<>();
105    ProgressDialog progressDialog = new ProgressDialog(
106        Utilities.createFrame(),
107        Utilities.getParentDialog(this), getTitle(), getInfo());
108    @SuppressWarnings("deprecation")
109    Object[] backends = list.getSelectedValues();
110    ArrayList<BackendDescriptor> backendsToDelete = new ArrayList<>();
111    for (Object o : backends)
112    {
113      String id = (String)o;
114      for (BackendDescriptor backend :
115        getInfo().getServerDescriptor().getBackends())
116      {
117        if (backend.getBackendID().equalsIgnoreCase(id))
118        {
119          backendsToDelete.add(backend);
120          break;
121        }
122      }
123    }
124    DeleteBaseDNAndBackendTask newTask = new DeleteBaseDNAndBackendTask(
125        getInfo(), progressDialog, backendsToDelete,
126        new HashSet<BaseDNDescriptor>());
127    for (Task task : getInfo().getTasks())
128    {
129      task.canLaunch(newTask, errors);
130    }
131    if (errors.isEmpty())
132    {
133      LocalizableMessage confirmationMessage = getConfirmationMessage(backendsToDelete);
134      if (displayConfirmationDialog(
135          INFO_CTRL_PANEL_CONFIRMATION_REQUIRED_SUMMARY.get(),
136          confirmationMessage))
137      {
138        launchOperation(newTask,
139            INFO_CTRL_PANEL_DELETING_BACKENDS_SUMMARY.get(),
140            INFO_CTRL_PANEL_DELETING_BACKENDS_COMPLETE.get(),
141            INFO_CTRL_PANEL_DELETING_BACKENDS_SUCCESSFUL.get(),
142            ERR_CTRL_PANEL_DELETING_BACKENDS_ERROR_SUMMARY.get(),
143            ERR_CTRL_PANEL_DELETING_BACKENDS_ERROR_DETAILS.get(),
144            null,
145            progressDialog);
146        progressDialog.setVisible(true);
147        Utilities.getParentDialog(this).setVisible(false);
148      }
149    }
150    if (!errors.isEmpty())
151    {
152      displayErrorDialog(errors);
153    }
154  }
155
156  private LocalizableMessage getConfirmationMessage(
157      Collection<BackendDescriptor> backendsToDelete)
158  {
159    LocalizableMessageBuilder mb = new LocalizableMessageBuilder();
160    mb.append(INFO_CTRL_PANEL_CONFIRMATION_DELETE_BACKENDS_DETAILS.get());
161    for (BackendDescriptor backend : backendsToDelete)
162    {
163      mb.append("<br> - ").append(backend.getBackendID());
164    }
165    mb.append("<br><br>");
166    mb.append(INFO_CTRL_PANEL_DO_YOU_WANT_TO_CONTINUE.get());
167    return mb.toMessage();
168  }
169}
170