001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com)
006 *
007 * This library is free software; you can redistribute it and/or
008 * modify it under the terms of the GNU Lesser General Public
009 * License as published by the Free Software Foundation; either
010 * version 2.1 of the License, or (at your option) any later version.
011 *
012 * This library is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015 * Lesser General Public License for more details.
016 *
017 * For further information about Alkacon Software, please see the
018 * company website: http://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: http://www.opencms.org
022 *
023 * You should have received a copy of the GNU Lesser General Public
024 * License along with this library; if not, write to the Free Software
025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
026 */
027
028package org.opencms.ui.apps.user;
029
030import org.opencms.file.CmsGroup;
031import org.opencms.file.CmsObject;
032import org.opencms.file.CmsUser;
033import org.opencms.main.CmsException;
034import org.opencms.main.CmsLog;
035import org.opencms.main.OpenCms;
036import org.opencms.security.CmsOrganizationalUnit;
037import org.opencms.ui.A_CmsUI;
038import org.opencms.ui.CmsVaadinUtils;
039import org.opencms.ui.FontOpenCms;
040import org.opencms.ui.apps.Messages;
041import org.opencms.ui.components.CmsBasicDialog;
042
043import java.util.Collections;
044import java.util.List;
045
046import org.apache.commons.logging.Log;
047
048import com.vaadin.ui.Button;
049import com.vaadin.ui.Button.ClickEvent;
050import com.vaadin.ui.Button.ClickListener;
051import com.vaadin.ui.FormLayout;
052import com.vaadin.ui.Panel;
053import com.vaadin.ui.Window;
054import com.vaadin.v7.shared.ui.label.ContentMode;
055import com.vaadin.v7.ui.Label;
056
057/**
058 * Dialog for delete of principals and ous.<p>
059 */
060public class CmsDeleteOUDialog extends CmsBasicDialog {
061
062    /**vaadin serial id. */
063    private static final long serialVersionUID = -7191571070148172989L;
064
065    /** The log object for this class. */
066    private static final Log LOG = CmsLog.getLog(CmsDeleteOUDialog.class);
067
068    /**vaadin component. */
069    private Label m_label;
070
071    /**vaadin component. */
072    private Label m_icon;
073
074    /**vaadin component. */
075    private Button m_cancelButton;
076
077    /**vaadin component. */
078    private Button m_okButton;
079
080    /**vaadin component. */
081    private FormLayout m_principalSelectLayout;
082
083    /**CmsObject. */
084    private CmsObject m_cms;
085
086    /**The name of the ou which should be deleted, may be null.*/
087    private String m_ouName;
088
089    /**vaadin component. */
090    private Panel m_dependencyPanel;
091
092    /**
093     * public constructor used for OUs.<p>
094     *
095     * @param cms CmsObject
096     * @param ouName name of ou to delete
097     * @param window window showing dialog
098     * @param app
099     */
100    public CmsDeleteOUDialog(CmsObject cms, String ouName, Window window, CmsAccountsApp app) {
101
102        m_ouName = ouName;
103        init(cms, window, app);
104        m_dependencyPanel.setVisible(false);
105        m_principalSelectLayout.setVisible(false);
106        try {
107            List<CmsUser> userList = OpenCms.getOrgUnitManager().getUsers(m_cms, ouName, true);
108            List<CmsOrganizationalUnit> oUs = OpenCms.getOrgUnitManager().getOrganizationalUnits(m_cms, ouName, true);
109
110            if (userList.isEmpty() && oUs.isEmpty() && hasNoGroups()) {
111                m_label.setValue(
112                    CmsVaadinUtils.getMessageText(Messages.GUI_USERMANAGEMENT_DELETE_OU_CONFIRM_1, ouName));
113            } else {
114                m_label.setValue(
115                    CmsVaadinUtils.getMessageText(Messages.GUI_USERMANAGEMENT_DELETE_OU_NOT_POSSIBLE_1, ouName));
116                m_okButton.setEnabled(false);
117            }
118        } catch (CmsException e) {
119            LOG.error("Unable to read OU", e);
120        }
121    }
122
123    /**
124     * Deletes the princiapl or OU.<p>
125     */
126    void deletePrincipal() {
127
128        //Delete OU
129        try {
130            OpenCms.getOrgUnitManager().deleteOrganizationalUnit(m_cms, m_ouName);
131        } catch (CmsException e) {
132            LOG.error("Unable to delete OU", e);
133        }
134    }
135
136    /**
137     * Ou has no groups which have to be deleted first? (all except for the Users group)
138     *
139     * @return true if Ou can be deleted directly
140     */
141    private boolean hasNoGroups() {
142
143        try {
144            List<CmsGroup> groups = OpenCms.getOrgUnitManager().getGroups(m_cms, m_ouName, true);
145            if (groups.size() == 0) {
146                return true;
147            }
148            for (CmsGroup g : groups) {
149                if (!g.getSimpleName().equals("Users")) {
150                    return false;
151                }
152            }
153            return true;
154        } catch (CmsException e) {
155            LOG.error("Unable to reade groups of OU", e);
156            return false;
157
158        }
159    }
160
161    /**
162     * Initialized the dialog.<p>
163     *
164     * @param cms CmsObject
165     * @param window window
166     * @param app
167     */
168    private void init(CmsObject cms, final Window window, final CmsAccountsApp app) {
169
170        CmsVaadinUtils.readAndLocalizeDesign(this, CmsVaadinUtils.getWpMessagesForCurrentLocale(), null);
171        try {
172            displayResourceInfoDirectly(
173                Collections.singletonList(
174                    CmsAccountsApp.getOUInfo(
175                        OpenCms.getOrgUnitManager().readOrganizationalUnit(A_CmsUI.getCmsObject(), m_ouName))));
176        } catch (CmsException e) {
177            LOG.error("Unable to read OU", e);
178        }
179        m_icon.setContentMode(ContentMode.HTML);
180        m_icon.setValue(FontOpenCms.WARNING.getHtml());
181        m_cms = cms;
182        m_okButton.addClickListener(new ClickListener() {
183
184            private static final long serialVersionUID = -7845894751587879028L;
185
186            public void buttonClick(ClickEvent event) {
187
188                deletePrincipal();
189                window.close();
190                app.reload();
191
192            }
193
194        });
195
196        m_cancelButton.addClickListener(new ClickListener() {
197
198            private static final long serialVersionUID = 6649262870116199591L;
199
200            public void buttonClick(ClickEvent event) {
201
202                window.close();
203
204            }
205
206        });
207    }
208
209}