001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH (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, 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.OpenCms;
034import org.opencms.util.CmsStringUtil;
035import org.opencms.workplace.CmsWorkplace;
036import org.opencms.xml.content.I_CmsXmlContentHandler.DisplayType;
037import org.opencms.xml.types.A_CmsXmlContentValue;
038
039import java.util.List;
040import java.util.Locale;
041
042/**
043 * Provides a OpenCms Group selection widget, for use on a widget dialog.<p>
044 *
045 * @since 6.0.0
046 */
047public class CmsGroupWidget extends A_CmsWidget implements I_CmsADEWidget {
048
049    /** Configuration parameter to set the flags of the groups to display, optional. */
050    public static final String CONFIGURATION_FLAGS = "flags";
051
052    /** Configuration parameter to set the organizational unit of the groups to display, optional. */
053    public static final String CONFIGURATION_OUFQN = "oufqn";
054
055    /** Configuration parameter to set the user of the groups to display, optional. */
056    public static final String CONFIGURATION_USER = "user";
057
058    /** The the flags used in the popup window. */
059    private Integer m_flags;
060
061    /** The the organizational unit used in the popup window. */
062    private String m_ouFqn;
063
064    /** The the user used in the popup window. */
065    private String m_userName;
066
067    /**
068     * Creates a new group selection widget.<p>
069     */
070    public CmsGroupWidget() {
071
072        // empty constructor is required for class registration
073        this("");
074    }
075
076    /**
077     * Creates a new group selection widget with the parameters to configure the popup window behaviour.<p>
078     *
079     * @param flags the group flags to restrict the group selection, can be <code>null</code>
080     * @param userName the user to restrict the group selection, can be <code>null</code>
081     */
082    public CmsGroupWidget(Integer flags, String userName) {
083
084        m_flags = flags;
085        m_userName = userName;
086        m_ouFqn = null;
087    }
088
089    /**
090     * Creates a new group selection widget with the parameters to configure the popup window behaviour.<p>
091     *
092     * @param flags the group flags to restrict the group selection, can be <code>null</code>
093     * @param userName the user to restrict the group selection, can be <code>null</code>
094     * @param ouFqn the organizational unit to restrict the group selection, can be <code>null</code>
095     */
096    public CmsGroupWidget(Integer flags, String userName, String ouFqn) {
097
098        m_flags = flags;
099        m_userName = userName;
100        m_ouFqn = ouFqn;
101    }
102
103    /**
104     * Creates a new group selection widget with the given configuration.<p>
105     *
106     * @param configuration the configuration to use
107     */
108    public CmsGroupWidget(String configuration) {
109
110        super(configuration);
111    }
112
113    /**
114     * @see org.opencms.widgets.A_CmsWidget#getConfiguration()
115     */
116    @Override
117    public String getConfiguration() {
118
119        StringBuffer result = new StringBuffer(8);
120
121        // append flags to configuration
122        if (m_flags != null) {
123            if (result.length() > 0) {
124                result.append("|");
125            }
126            result.append(CONFIGURATION_FLAGS);
127            result.append("=");
128            result.append(m_flags);
129        }
130        // append user to configuration
131        if (m_userName != null) {
132            if (result.length() > 0) {
133                result.append("|");
134            }
135            result.append(CONFIGURATION_USER);
136            result.append("=");
137            result.append(m_userName);
138        }
139        // append oufqn to configuration
140        if (m_ouFqn != null) {
141            if (result.length() > 0) {
142                result.append("|");
143            }
144            result.append(CONFIGURATION_OUFQN);
145            result.append("=");
146            result.append(m_ouFqn);
147        }
148        return result.toString();
149    }
150
151    /**
152     * @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)
153     */
154    public String getConfiguration(
155        CmsObject cms,
156        A_CmsXmlContentValue schemaType,
157        CmsMessages messages,
158        CmsResource resource,
159        Locale contentLocale) {
160
161        return getConfiguration();
162    }
163
164    /**
165     * @see org.opencms.widgets.I_CmsADEWidget#getCssResourceLinks(org.opencms.file.CmsObject)
166     */
167    public List<String> getCssResourceLinks(CmsObject cms) {
168
169        return null;
170    }
171
172    /**
173     * @see org.opencms.widgets.I_CmsADEWidget#getDefaultDisplayType()
174     */
175    public DisplayType getDefaultDisplayType() {
176
177        return DisplayType.singleline;
178    }
179
180    /**
181     * @see org.opencms.widgets.I_CmsWidget#getDialogIncludes(org.opencms.file.CmsObject, org.opencms.widgets.I_CmsWidgetDialog)
182     */
183    @Override
184    public String getDialogIncludes(CmsObject cms, I_CmsWidgetDialog widgetDialog) {
185
186        StringBuffer result = new StringBuffer(16);
187        result.append(getJSIncludeFile(CmsWorkplace.getSkinUri() + "components/widgets/groupselector.js"));
188        return result.toString();
189    }
190
191    /**
192     * @see org.opencms.widgets.I_CmsWidget#getDialogWidget(org.opencms.file.CmsObject, org.opencms.widgets.I_CmsWidgetDialog, org.opencms.widgets.I_CmsWidgetParameter)
193     */
194    public String getDialogWidget(CmsObject cms, I_CmsWidgetDialog widgetDialog, I_CmsWidgetParameter param) {
195
196        String id = param.getId();
197        StringBuffer result = new StringBuffer(128);
198
199        result.append("<td class=\"xmlTd\">");
200        result.append(
201            "<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" class=\"maxwidth\"><tr><td style=\"width: 100%;\">");
202        result.append("<input style=\"width: 99%;\" class=\"xmlInput");
203        if (param.hasError()) {
204            result.append(" xmlInputError");
205        }
206        result.append("\" value=\"");
207        result.append(param.getStringValue(cms));
208        result.append("\" name=\"");
209        result.append(id);
210        result.append("\" id=\"");
211        result.append(id);
212        result.append("\"></td>");
213        result.append(widgetDialog.dialogHorizontalSpacer(10));
214        result.append(
215            "<td><table class=\"editorbuttonbackground\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\"><tr>");
216
217        StringBuffer buttonJs = new StringBuffer(8);
218        buttonJs.append("javascript:openGroupWin('");
219        buttonJs.append(OpenCms.getSystemInfo().getOpenCmsContext());
220        buttonJs.append("/system/workplace/commons/group_selection.jsp");
221        buttonJs.append("','EDITOR',  '");
222        buttonJs.append(id);
223        buttonJs.append("', document, ");
224        if (m_flags != null) {
225            buttonJs.append("'");
226            buttonJs.append(m_flags);
227            buttonJs.append("'");
228        } else {
229            buttonJs.append("null");
230        }
231        buttonJs.append(", ");
232        if (m_userName != null) {
233            buttonJs.append("'");
234            buttonJs.append(m_userName);
235            buttonJs.append("'");
236        } else {
237            buttonJs.append("null");
238        }
239        buttonJs.append(", ");
240        if (m_ouFqn != null) {
241            buttonJs.append("'");
242            buttonJs.append(m_ouFqn);
243            buttonJs.append("'");
244        } else {
245            buttonJs.append("null");
246        }
247        buttonJs.append(");");
248
249        result.append(
250            widgetDialog.button(
251                buttonJs.toString(),
252                null,
253                "group",
254                org.opencms.workplace.Messages.GUI_DIALOG_BUTTON_SEARCH_0,
255                widgetDialog.getButtonStyle()));
256        result.append("</tr></table>");
257        result.append("</td></tr></table>");
258
259        result.append("</td>");
260
261        return result.toString();
262    }
263
264    /**
265     * Returns the flags, or <code>null</code> if all.<p>
266     *
267     * @return the flags, or <code>null</code> if all
268     */
269    public Integer getFlags() {
270
271        return m_flags;
272    }
273
274    /**
275     * @see org.opencms.widgets.I_CmsADEWidget#getInitCall()
276     */
277    public String getInitCall() {
278
279        return null;
280    }
281
282    /**
283     * @see org.opencms.widgets.I_CmsADEWidget#getJavaScriptResourceLinks(org.opencms.file.CmsObject)
284     */
285    public List<String> getJavaScriptResourceLinks(CmsObject cms) {
286
287        return null;
288    }
289
290    /**
291     * Returns the organizational unit fqn, or <code>null</code> if all.<p>
292     *
293     * @return the organizational unit fqn
294     */
295    public String getOufqn() {
296
297        return m_ouFqn;
298    }
299
300    /**
301     * Returns the user name, or <code>null</code> if all.<p>
302     *
303     * @return the user name
304     */
305    public String getUserName() {
306
307        return m_userName;
308    }
309
310    /**
311     * @see org.opencms.widgets.I_CmsADEWidget#getWidgetName()
312     */
313    public String getWidgetName() {
314
315        return CmsGroupWidget.class.getName();
316    }
317
318    /**
319     * @see org.opencms.widgets.I_CmsADEWidget#isInternal()
320     */
321    public boolean isInternal() {
322
323        return true;
324    }
325
326    /**
327     * @see org.opencms.widgets.I_CmsWidget#newInstance()
328     */
329    public I_CmsWidget newInstance() {
330
331        return new CmsGroupWidget(getConfiguration());
332    }
333
334    /**
335     * @see org.opencms.widgets.A_CmsWidget#setConfiguration(java.lang.String)
336     */
337    @Override
338    public void setConfiguration(String configuration) {
339
340        m_userName = null;
341        m_flags = null;
342        if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(configuration)) {
343            int flagsIndex = configuration.indexOf(CONFIGURATION_FLAGS);
344            if (flagsIndex != -1) {
345                // user is given
346                String flags = configuration.substring(CONFIGURATION_FLAGS.length() + 1);
347                if (flags.indexOf('|') != -1) {
348                    // cut eventual following configuration values
349                    flags = flags.substring(0, flags.indexOf('|'));
350                }
351                try {
352                    m_flags = Integer.valueOf(flags);
353                } catch (Throwable t) {
354                    // invalid flags
355                }
356            }
357            int groupIndex = configuration.indexOf(CONFIGURATION_USER);
358            if (groupIndex != -1) {
359                // user is given
360                String user = configuration.substring(groupIndex + CONFIGURATION_USER.length() + 1);
361                if (user.indexOf('|') != -1) {
362                    // cut eventual following configuration values
363                    user = user.substring(0, user.indexOf('|'));
364                }
365                m_userName = user;
366            }
367            int oufqnIndex = configuration.indexOf(CONFIGURATION_OUFQN);
368            if (oufqnIndex != -1) {
369                // user is given
370                String oufqn = configuration.substring(oufqnIndex + CONFIGURATION_OUFQN.length() + 1);
371                if (oufqn.indexOf('|') != -1) {
372                    // cut eventual following configuration values
373                    oufqn = oufqn.substring(0, oufqn.indexOf('|'));
374                }
375                m_ouFqn = oufqn;
376            }
377        }
378        super.setConfiguration(configuration);
379    }
380}