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.workplace.commons;
029
030import org.opencms.file.CmsGroup;
031import org.opencms.jsp.CmsJspActionElement;
032import org.opencms.main.CmsException;
033import org.opencms.main.CmsRuntimeException;
034import org.opencms.main.OpenCms;
035import org.opencms.security.CmsPrincipal;
036import org.opencms.util.CmsStringUtil;
037import org.opencms.workplace.list.A_CmsListDefaultJsAction;
038import org.opencms.workplace.list.A_CmsListDialog;
039import org.opencms.workplace.list.CmsListColumnAlignEnum;
040import org.opencms.workplace.list.CmsListColumnDefinition;
041import org.opencms.workplace.list.CmsListDefaultAction;
042import org.opencms.workplace.list.CmsListDirectAction;
043import org.opencms.workplace.list.CmsListItem;
044import org.opencms.workplace.list.CmsListMetadata;
045import org.opencms.workplace.list.CmsListOrderEnum;
046import org.opencms.workplace.list.CmsListSearchAction;
047import org.opencms.workplace.tools.CmsToolMacroResolver;
048
049import java.util.ArrayList;
050import java.util.Iterator;
051import java.util.List;
052
053import javax.servlet.http.HttpServletRequest;
054import javax.servlet.http.HttpServletResponse;
055import javax.servlet.jsp.PageContext;
056
057/**
058 * Group selection dialog.<p>
059 *
060 * @since 6.0.0
061 */
062public class CmsGroupSelectionList extends A_CmsListDialog {
063
064    /** list action id constant. */
065    public static final String LIST_ACTION_ICON = "ai";
066
067    /** list action id constant. */
068    public static final String LIST_ACTION_SELECT = "js";
069
070    /** list column id constant. */
071    public static final String LIST_COLUMN_DISPLAY = "cdis";
072
073    /** list column id constant. */
074    public static final String LIST_COLUMN_ICON = "ci";
075
076    /** list column id constant. */
077    public static final String LIST_COLUMN_NAME = "cn";
078
079    /** list id constant. */
080    public static final String LIST_ID = "lug";
081
082    /** Stores the value of the request parameter for the flags. */
083    private String m_paramFlags;
084
085    /** Stores the value of the request parameter for the organizational unit fqn. */
086    private String m_paramOufqn;
087
088    /** Stores the value of the request parameter for the user name. */
089    private String m_paramUser;
090
091    /**
092     * Public constructor.<p>
093     *
094     * @param jsp an initialized JSP action element
095     */
096    public CmsGroupSelectionList(CmsJspActionElement jsp) {
097
098        super(
099            jsp,
100            LIST_ID,
101            Messages.get().container(Messages.GUI_GROUPSELECTION_LIST_NAME_0),
102            LIST_COLUMN_DISPLAY,
103            CmsListOrderEnum.ORDER_ASCENDING,
104            null);
105    }
106
107    /**
108     * Public constructor with JSP variables.<p>
109     *
110     * @param context the JSP page context
111     * @param req the JSP request
112     * @param res the JSP response
113     */
114    public CmsGroupSelectionList(PageContext context, HttpServletRequest req, HttpServletResponse res) {
115
116        this(new CmsJspActionElement(context, req, res));
117    }
118
119    /**
120     * @see org.opencms.workplace.tools.CmsToolDialog#dialogTitle()
121     */
122    @Override
123    public String dialogTitle() {
124
125        // build title
126        StringBuffer html = new StringBuffer(512);
127        html.append("<div class='screenTitle'>\n");
128        html.append("\t<table width='100%' cellspacing='0'>\n");
129        html.append("\t\t<tr>\n");
130        html.append("\t\t\t<td>\n");
131        if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(getParamUser())) {
132            html.append(
133                Messages.get().getBundle(getLocale()).key(Messages.GUI_GROUPSELECTION_USER_TITLE_1, getParamUser()));
134        } else {
135            html.append(Messages.get().getBundle(getLocale()).key(Messages.GUI_GROUPSELECTION_INTRO_TITLE_0));
136        }
137        html.append("\n\t\t\t</td>");
138        html.append("\t\t</tr>\n");
139        html.append("\t</table>\n");
140        html.append("</div>\n");
141        return CmsToolMacroResolver.resolveMacros(html.toString(), this);
142    }
143
144    /**
145     * @see org.opencms.workplace.list.A_CmsListDialog#executeListMultiActions()
146     */
147    @Override
148    public void executeListMultiActions() throws CmsRuntimeException {
149
150        throwListUnsupportedActionException();
151    }
152
153    /**
154     * @see org.opencms.workplace.list.A_CmsListDialog#executeListSingleActions()
155     */
156    @Override
157    public void executeListSingleActions() throws CmsRuntimeException {
158
159        throwListUnsupportedActionException();
160    }
161
162    /**
163     * Returns the flags parameter value.<p>
164     *
165     * @return the flags parameter value
166     */
167    public String getParamFlags() {
168
169        return m_paramFlags;
170    }
171
172    /**
173     * Returns the organizational unit fqn parameter.<p>
174     *
175     * @return the organizational unit fqn paramter
176     */
177    public String getParamOufqn() {
178
179        return m_paramOufqn;
180    }
181
182    /**
183     * Returns the user name parameter.<p>
184     *
185     * @return the user name paramter
186     */
187    public String getParamUser() {
188
189        return m_paramUser;
190    }
191
192    /**
193     * Sets the flags parameter value.<p>
194     *
195     * @param flags the flags parameter value to set
196     */
197    public void setParamFlags(String flags) {
198
199        m_paramFlags = flags;
200    }
201
202    /**
203     * Sets the organizational unit fqn.<p>
204     *
205     * @param ouFqn the organizational unit fqn to set
206     */
207    public void setParamOufqn(String ouFqn) {
208
209        if (ouFqn == null) {
210            ouFqn = "";
211        }
212        m_paramOufqn = ouFqn;
213    }
214
215    /**
216     * Sets the user name.<p>
217     *
218     * @param userName the user name to set
219     */
220    public void setParamUser(String userName) {
221
222        m_paramUser = userName;
223    }
224
225    /**
226     * @see org.opencms.workplace.list.A_CmsListDialog#fillDetails(java.lang.String)
227     */
228    @Override
229    protected void fillDetails(String detailId) {
230
231        // noop
232    }
233
234    /**
235     * Returns the groups to show for selection.<p>
236     *
237     * @return A list of group objects
238     *
239     * @throws CmsException if womething goes wrong
240     */
241    protected List<CmsGroup> getGroups() throws CmsException {
242
243        List<CmsGroup> groups = new ArrayList<CmsGroup>();
244        if (getParamUser() != null) {
245            groups.addAll(getCms().getGroupsOfUser(getParamUser(), false));
246        } else {
247            groups.addAll(OpenCms.getRoleManager().getManageableGroups(getCms(), "", true));
248        }
249        if (getParamFlags() != null) {
250            int flags = Integer.parseInt(getParamFlags());
251            CmsPrincipal.filterFlag(groups, flags);
252        }
253        if ((getParamOufqn() != null) && !getParamOufqn().equals("null")) {
254            Iterator<CmsGroup> groupsIter = groups.iterator();
255            while (groupsIter.hasNext()) {
256                CmsGroup group = groupsIter.next();
257                if (!group.getOuFqn().startsWith(getParamOufqn())) {
258                    groupsIter.remove();
259                }
260            }
261        }
262        return groups;
263    }
264
265    /**
266     * @see org.opencms.workplace.list.A_CmsListDialog#getListItems()
267     */
268    @Override
269    protected List<CmsListItem> getListItems() throws CmsException {
270
271        List<CmsListItem> ret = new ArrayList<CmsListItem>();
272
273        // get content
274        List<CmsGroup> groups = getGroups();
275        Iterator<CmsGroup> itGroups = groups.iterator();
276        while (itGroups.hasNext()) {
277            CmsGroup group = itGroups.next();
278            CmsListItem item = getList().newItem(group.getId().toString());
279            item.set(LIST_COLUMN_NAME, group.getName());
280            item.set(LIST_COLUMN_DISPLAY, OpenCms.getWorkplaceManager().translateGroupName(group.getName(), true));
281            ret.add(item);
282        }
283        return ret;
284    }
285
286    /**
287     * @see org.opencms.workplace.list.A_CmsListDialog#setColumns(org.opencms.workplace.list.CmsListMetadata)
288     */
289    @Override
290    protected void setColumns(CmsListMetadata metadata) {
291
292        // create column for icon display
293        CmsListColumnDefinition iconCol = new CmsListColumnDefinition(LIST_COLUMN_ICON);
294        iconCol.setName(Messages.get().container(Messages.GUI_GROUPSELECTION_LIST_COLS_ICON_0));
295        iconCol.setHelpText(Messages.get().container(Messages.GUI_GROUPSELECTION_LIST_COLS_ICON_HELP_0));
296        iconCol.setWidth("20");
297        iconCol.setAlign(CmsListColumnAlignEnum.ALIGN_CENTER);
298        iconCol.setSorteable(false);
299        // set icon action
300        CmsListDirectAction iconAction = new CmsListDirectAction(LIST_ACTION_ICON);
301        iconAction.setName(Messages.get().container(Messages.GUI_GROUPSELECTION_LIST_ICON_NAME_0));
302        iconAction.setHelpText(Messages.get().container(Messages.GUI_GROUPSELECTION_LIST_ICON_HELP_0));
303        iconAction.setIconPath("buttons/group.png");
304        iconAction.setEnabled(false);
305        iconCol.addDirectAction(iconAction);
306        // add it to the list definition
307        metadata.addColumn(iconCol);
308
309        CmsListColumnDefinition nameCol = new CmsListColumnDefinition(LIST_COLUMN_NAME);
310        nameCol.setName(Messages.get().container(Messages.GUI_GROUPSELECTION_LIST_COLS_NAME_0));
311        nameCol.setVisible(false);
312        metadata.addColumn(nameCol);
313
314        // create column for login
315        CmsListColumnDefinition displayCol = new CmsListColumnDefinition(LIST_COLUMN_DISPLAY);
316        displayCol.setName(Messages.get().container(Messages.GUI_GROUPSELECTION_LIST_COLS_NAME_0));
317        displayCol.setWidth("100%");
318        CmsListDefaultAction selectAction = new A_CmsListDefaultJsAction(LIST_ACTION_SELECT) {
319
320            /**
321             * @see org.opencms.workplace.list.A_CmsListDirectJsAction#jsCode()
322             */
323            @Override
324            public String jsCode() {
325
326                return "window.opener.setGroupFormValue('"
327                    + getItem().get(LIST_COLUMN_NAME)
328                    + "'); window.opener.focus(); window.close();";
329            }
330        };
331        selectAction.setName(Messages.get().container(Messages.GUI_GROUPSELECTION_LIST_ACTION_SELECT_NAME_0));
332        selectAction.setHelpText(Messages.get().container(Messages.GUI_GROUPSELECTION_LIST_ACTION_SELECT_HELP_0));
333        displayCol.addDefaultAction(selectAction);
334        // add it to the list definition
335        metadata.addColumn(displayCol);
336    }
337
338    /**
339     * @see org.opencms.workplace.list.A_CmsListDialog#setIndependentActions(org.opencms.workplace.list.CmsListMetadata)
340     */
341    @Override
342    protected void setIndependentActions(CmsListMetadata metadata) {
343
344        CmsListSearchAction searchAction = new CmsListSearchAction(metadata.getColumnDefinition(LIST_COLUMN_DISPLAY));
345        searchAction.setCaseInSensitive(true);
346        metadata.setSearchAction(searchAction);
347    }
348
349    /**
350     * @see org.opencms.workplace.list.A_CmsListDialog#setMultiActions(org.opencms.workplace.list.CmsListMetadata)
351     */
352    @Override
353    protected void setMultiActions(CmsListMetadata metadata) {
354
355        // no-op
356    }
357
358    /**
359     * @see org.opencms.workplace.list.A_CmsListDialog#validateParamaters()
360     */
361    @Override
362    protected void validateParamaters() throws Exception {
363
364        try {
365            getCms().readUser(getParamUser()).getName();
366        } catch (Exception e) {
367            setParamUser(null);
368        }
369        try {
370            Integer.valueOf(getParamFlags());
371        } catch (Throwable e) {
372            setParamFlags(null);
373        }
374    }
375}