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.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 selected OU.<p<
072     *
073     * @return the selected OU
074     */
075    public String getValue() {
076
077        return (String)m_ouSelect.getValue();
078    }
079
080    /**
081     * Checks if a given OU is available for selection.
082     * 
083     * @param ou the OU to check
084     * @return true if the OU is available
085     */
086    public boolean hasOrgUnit(String ou) {
087
088        return m_ouSelect.getContainerDataSource().getItem(normalizeOuName(ou)) != null;
089    }
090
091    /**
092     * Initializes the select options.<p>
093     *
094     * @param orgUnits the selectable OUs
095     * @param addEmptyOption adds empty 'not selected' option with the special value OU_NONE
096     */
097    public void initOrgUnits(List<CmsOrganizationalUnit> orgUnits, boolean addEmptyOption) {
098
099        if ((orgUnits.size() == 1) && (orgUnits.get(0).getParentFqn() == null)) {
100            setVisible(false);
101            m_alwaysHidden = true;
102        }
103        if (addEmptyOption) {
104            m_ouSelect.addItem(OU_NONE);
105            m_ouSelect.setItemCaption(OU_NONE, CmsVaadinUtils.getMessageText(Messages.GUI_LOGIN_NO_OU_SELECTED_0));
106        }
107        for (CmsOrganizationalUnit ou : orgUnits) {
108            String key = normalizeOuName(ou.getName());
109            m_ouSelect.addItem(key);
110            m_ouSelect.setItemCaption(key, ou.getDisplayName(A_CmsUI.get().getLocale()));
111        }
112    }
113
114    /**
115     * Returns true if the OU selector should remain hidden.<p>
116     *
117     * @return true if the OU selector should remain hidden
118     */
119    public boolean isAlwaysHidden() {
120
121        return m_alwaysHidden;
122    }
123
124    /**
125     * Sets the selected OU.<p>
126     *
127     * @param value the OU to select
128     */
129    public void setValue(String value) {
130
131        m_ouSelect.setValue(normalizeOuName(value));
132    }
133
134    /**
135     * Normalizes a given OU name.<p>
136     *
137     * @param ou the OU name
138     * @return the normalized version
139     */
140    String normalizeOuName(String ou) {
141
142        ou = CmsFileUtil.removeLeadingSeparator(ou);
143        ou = CmsFileUtil.removeTrailingSeparator(ou);
144        return ou;
145    }
146
147}