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