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 GmbH & Co. KG, 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.widgets;
029
030import org.opencms.file.CmsObject;
031import org.opencms.main.CmsException;
032import org.opencms.main.CmsLog;
033import org.opencms.main.OpenCms;
034import org.opencms.security.CmsOrganizationalUnit;
035import org.opencms.security.CmsRole;
036import org.opencms.util.CmsStringUtil;
037import org.opencms.workplace.CmsWorkplace;
038
039import java.util.ArrayList;
040import java.util.List;
041import java.util.Locale;
042
043import org.apache.commons.logging.Log;
044
045/**
046 * Provides a OpenCms orgaizational unit selection widget, for use on a widget dialog.<p>
047 *
048 * @since 6.5.6
049 */
050public class CmsOrgUnitWidget extends A_CmsSelectWidget implements I_CmsADEWidget {
051
052    /** Configuration parameter to set the role the current user must have in the selected ou, optional. */
053    public static final String CONFIGURATION_ROLE = "role";
054
055    /** The logger instance for this class. */
056    private static final Log LOG = CmsLog.getLog(CmsOrgUnitWidget.class);
057
058    /** The role used in the popup window. */
059    private CmsRole m_role;
060
061    /**
062     * Creates a new organizational unit selection widget.<p>
063     */
064    public CmsOrgUnitWidget() {
065
066        // empty constructor is required for class registration
067        this("");
068    }
069
070    /**
071     * Creates a new user selection widget with the parameters to configure the popup window behaviour.<p>
072     *
073     * @param role the role to restrict the organizational unit selection, can be <code>null</code>
074     */
075    public CmsOrgUnitWidget(CmsRole role) {
076
077        m_role = role;
078    }
079
080    /**
081     * Creates a new organizational unit selection widget with the given configuration.<p>
082     *
083     * @param configuration the configuration to use
084     */
085    public CmsOrgUnitWidget(String configuration) {
086
087        super(configuration);
088
089    }
090
091    /**
092     * @see org.opencms.widgets.A_CmsWidget#getConfiguration()
093     */
094    @Override
095    public String getConfiguration() {
096
097        StringBuffer result = new StringBuffer(8);
098
099        // append flags to configuration
100        if (m_role != null) {
101            if (result.length() > 0) {
102                result.append("|");
103            }
104            result.append(CONFIGURATION_ROLE);
105            result.append("=");
106            result.append(m_role.getGroupName());
107        }
108
109        return result.toString();
110    }
111
112    /**
113     * @see org.opencms.widgets.I_CmsWidget#getDialogIncludes(org.opencms.file.CmsObject, org.opencms.widgets.I_CmsWidgetDialog)
114     */
115    @Override
116    public String getDialogIncludes(CmsObject cms, I_CmsWidgetDialog widgetDialog) {
117
118        StringBuffer result = new StringBuffer(16);
119        result.append(getJSIncludeFile(CmsWorkplace.getSkinUri() + "components/widgets/orgunitselector.js"));
120        return result.toString();
121    }
122
123    /**
124     * @see org.opencms.widgets.I_CmsWidget#getDialogWidget(org.opencms.file.CmsObject, org.opencms.widgets.I_CmsWidgetDialog, org.opencms.widgets.I_CmsWidgetParameter)
125     */
126    public String getDialogWidget(CmsObject cms, I_CmsWidgetDialog widgetDialog, I_CmsWidgetParameter param) {
127
128        String id = param.getId();
129        StringBuffer result = new StringBuffer(128);
130
131        result.append("<td class=\"xmlTd\">");
132        result.append(
133            "<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" class=\"maxwidth\"><tr><td style=\"width: 100%;\">");
134        result.append("<input style=\"width: 99%;\" class=\"xmlInput");
135        if (param.hasError()) {
136            result.append(" xmlInputError");
137        }
138        result.append("\" value=\"");
139        result.append(param.getStringValue(cms));
140        result.append("\" name=\"");
141        result.append(id);
142        result.append("\" id=\"");
143        result.append(id);
144        result.append("\"></td>");
145        result.append(widgetDialog.dialogHorizontalSpacer(10));
146        result.append(
147            "<td><table class=\"editorbuttonbackground\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\"><tr>");
148
149        StringBuffer buttonJs = new StringBuffer(8);
150        buttonJs.append("javascript:openOrgUnitWin('");
151        buttonJs.append(OpenCms.getSystemInfo().getOpenCmsContext());
152        buttonJs.append("/system/workplace/commons/orgunit_selection.jsp");
153        buttonJs.append("','EDITOR',  '");
154        buttonJs.append(id);
155        buttonJs.append("', document, ");
156        if (m_role != null) {
157            buttonJs.append("'");
158            buttonJs.append(m_role.getGroupName());
159            buttonJs.append("'");
160        } else {
161            buttonJs.append("null");
162        }
163        buttonJs.append(");");
164
165        result.append(
166            widgetDialog.button(
167                buttonJs.toString(),
168                null,
169                "orgunit",
170                org.opencms.workplace.Messages.GUI_DIALOG_BUTTON_SEARCH_0,
171                widgetDialog.getButtonStyle()));
172        result.append("</tr></table>");
173        result.append("</td></tr></table>");
174
175        result.append("</td>");
176
177        return result.toString();
178    }
179
180    /**
181     * Returns the role, or <code>null</code> if none.<p>
182     *
183     * @return the role, or <code>null</code> if none
184     */
185    public CmsRole getRole() {
186
187        return m_role;
188    }
189
190    /**
191     * @see org.opencms.widgets.I_CmsADEWidget#getWidgetName()
192     */
193    @Override
194    public String getWidgetName() {
195
196        return CmsComboWidget.class.getName();
197    }
198
199    /**
200     * @see org.opencms.widgets.I_CmsWidget#newInstance()
201     */
202    public I_CmsWidget newInstance() {
203
204        return new CmsOrgUnitWidget(getConfiguration());
205    }
206
207    /**
208     * @see org.opencms.widgets.A_CmsWidget#setConfiguration(java.lang.String)
209     */
210    @Override
211    public void setConfiguration(String configuration) {
212
213        m_role = null;
214        if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(configuration)) {
215            int roleIndex = configuration.indexOf(CONFIGURATION_ROLE);
216            if (roleIndex != -1) {
217                // role is given
218                String groupName = configuration.substring(CONFIGURATION_ROLE.length() + 1);
219                if (groupName.indexOf('|') != -1) {
220                    // cut eventual following configuration values
221                    groupName = groupName.substring(0, groupName.indexOf('|'));
222                }
223                m_role = CmsRole.valueOfGroupName(groupName);
224            }
225        }
226        super.setConfiguration(configuration);
227    }
228
229    /**
230     * @see org.opencms.widgets.A_CmsSelectWidget#parseSelectOptions(org.opencms.file.CmsObject, org.opencms.widgets.I_CmsWidgetDialog, org.opencms.widgets.I_CmsWidgetParameter)
231     */
232    @Override
233    protected List<CmsSelectWidgetOption> parseSelectOptions(
234        CmsObject cms,
235        I_CmsWidgetDialog widgetDialog,
236        I_CmsWidgetParameter param) {
237
238        Locale locale = OpenCms.getWorkplaceManager().getWorkplaceLocale(cms);
239
240        List<CmsOrganizationalUnit> orgUnits = new ArrayList<CmsOrganizationalUnit>();
241        try {
242            if (m_role != null) {
243                orgUnits.addAll(OpenCms.getRoleManager().getOrgUnitsForRole(cms, m_role.forOrgUnit(""), true));
244            } else {
245                orgUnits.addAll(OpenCms.getOrgUnitManager().getOrganizationalUnits(cms, "", true));
246            }
247        } catch (CmsException e) {
248            LOG.error(e.getLocalizedMessage(), e);
249        }
250
251        List<CmsSelectWidgetOption> result = new ArrayList<>();
252        for (CmsOrganizationalUnit ou : orgUnits) {
253            CmsSelectWidgetOption option = new CmsSelectWidgetOption(ou.getName(), false, ou.getDisplayName(locale));
254            result.add(option);
255        }
256        setSelectOptions(result);
257        return result;
258    }
259}