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