001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH & Co. KG (https://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: https://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: https://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.login;
029
030import org.opencms.security.CmsOrganizationalUnit;
031import org.opencms.ui.A_CmsUI;
032import org.opencms.ui.CmsVaadinUtils;
033import org.opencms.ui.Messages;
034import org.opencms.util.CmsFileUtil;
035
036import java.util.List;
037
038import com.vaadin.ui.CustomComponent;
039import com.vaadin.v7.shared.ui.combobox.FilteringMode;
040import com.vaadin.v7.ui.ComboBox;
041
042/**
043 * Widget used to allow the user to search and select an organizational unit.<p>
044 */
045public class CmsLoginOuSelector extends CustomComponent {
046
047    /** Serial version id. */
048    private static final long serialVersionUID = 1L;
049
050    /** Special value for the 'not selected' option. */
051    public static final String OU_NONE = "OU_NONE";
052
053    /** The combo box containing the OU options. */
054    private ComboBox m_ouSelect = new ComboBox();
055
056    /** Flag to always hide the selector. */
057    private boolean m_alwaysHidden;
058
059    /**
060     * Creates a new instance.<P>
061     */
062    public CmsLoginOuSelector() {
063
064        m_ouSelect.setWidth("100%");
065        setCompositionRoot(m_ouSelect);
066        m_ouSelect.setFilteringMode(FilteringMode.CONTAINS);
067        m_ouSelect.setNullSelectionAllowed(false);
068    }
069
070    /**
071     * Gets the normalized OU name for a particular OU.
072     */
073    public static String getId(CmsOrganizationalUnit ou) {
074
075        return normalizeOuName(ou.getName());
076    }
077
078    /**
079     * Normalizes a given OU name.<p>
080     *
081     * @param ou the OU name
082     * @return the normalized version
083     */
084    public static String normalizeOuName(String ou) {
085
086        ou = CmsFileUtil.removeLeadingSeparator(ou);
087        ou = CmsFileUtil.removeTrailingSeparator(ou);
088        return ou;
089    }
090
091    /**
092     * Gets the selected OU.<p<
093     *
094     * @return the selected OU
095     */
096    public String getValue() {
097
098        return (String)m_ouSelect.getValue();
099    }
100
101    /**
102     * Checks if a given OU is available for selection.
103     *
104     * @param ou the OU to check
105     * @return true if the OU is available
106     */
107    public boolean hasOrgUnit(String ou) {
108
109        return m_ouSelect.getContainerDataSource().getItem(normalizeOuName(ou)) != null;
110    }
111
112    /**
113     * Initializes the select options.<p>
114     *
115     * @param orgUnits the selectable OUs
116     * @param addEmptyOption adds empty 'not selected' option with the special value OU_NONE
117     */
118    public void initOrgUnits(List<CmsOrganizationalUnit> orgUnits, boolean addEmptyOption) {
119
120        if ((orgUnits.size() == 1) && (orgUnits.get(0).getParentFqn() == null)) {
121            setVisible(false);
122            m_alwaysHidden = true;
123        }
124        if (addEmptyOption) {
125            m_ouSelect.addItem(OU_NONE);
126            m_ouSelect.setItemCaption(OU_NONE, CmsVaadinUtils.getMessageText(Messages.GUI_LOGIN_NO_OU_SELECTED_0));
127        }
128        for (CmsOrganizationalUnit ou : orgUnits) {
129            String key = normalizeOuName(ou.getName());
130            m_ouSelect.addItem(key);
131            m_ouSelect.setItemCaption(key, ou.getDisplayName(A_CmsUI.get().getLocale()));
132        }
133    }
134
135    /**
136     * Returns true if the OU selector should remain hidden.<p>
137     *
138     * @return true if the OU selector should remain hidden
139     */
140    public boolean isAlwaysHidden() {
141
142        return m_alwaysHidden;
143    }
144
145    /**
146     * Sets the selected OU.<p>
147     *
148     * @param value the OU to select
149     */
150    public void setValue(String value) {
151
152        m_ouSelect.setValue(normalizeOuName(value));
153    }
154
155}