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.tools.accounts;
029
030import org.opencms.file.CmsGroup;
031import org.opencms.file.CmsUser;
032import org.opencms.jsp.CmsJspActionElement;
033import org.opencms.main.CmsException;
034import org.opencms.main.CmsRuntimeException;
035import org.opencms.main.OpenCms;
036import org.opencms.security.CmsPrincipal;
037import org.opencms.util.CmsRequestUtil;
038import org.opencms.util.CmsStringUtil;
039import org.opencms.util.CmsUUID;
040import org.opencms.workplace.list.A_CmsListDialog;
041import org.opencms.workplace.list.CmsHtmlList;
042import org.opencms.workplace.list.CmsListColumnAlignEnum;
043import org.opencms.workplace.list.CmsListColumnDefinition;
044import org.opencms.workplace.list.CmsListDefaultAction;
045import org.opencms.workplace.list.CmsListDirectAction;
046import org.opencms.workplace.list.CmsListItem;
047import org.opencms.workplace.list.CmsListItemDetails;
048import org.opencms.workplace.list.CmsListItemDetailsFormatter;
049import org.opencms.workplace.list.CmsListMetadata;
050import org.opencms.workplace.list.CmsListOrderEnum;
051
052import java.io.IOException;
053import java.util.ArrayList;
054import java.util.HashSet;
055import java.util.Iterator;
056import java.util.List;
057import java.util.Set;
058
059import javax.servlet.ServletException;
060import javax.servlet.http.HttpServletRequest;
061import javax.servlet.http.HttpServletResponse;
062import javax.servlet.jsp.PageContext;
063
064/**
065 * Allows to select a group to transfer the permissions and attributes from another one.<p>
066 *
067 * @since 6.0.0
068 */
069public class CmsGroupTransferList extends A_CmsListDialog {
070
071    /** list action id constant. */
072    public static final String LIST_ACTION_TRANSFER = "at";
073
074    /** list column id constant. */
075    public static final String LIST_COLUMN_DESCRIPTION = "cc";
076
077    /** list column id constant. */
078    public static final String LIST_COLUMN_DISPLAY = "cdis";
079
080    /** list column id constant. */
081    public static final String LIST_COLUMN_NAME = "cn";
082
083    /** list column id constant. */
084    public static final String LIST_COLUMN_PARENT = "cp";
085
086    /** list column id constant. */
087    public static final String LIST_COLUMN_TRANSFER = "ct";
088
089    /** list column id constant. */
090    public static final String LIST_COLUMN_USERS = "cu";
091
092    /** list action id constant. */
093    public static final String LIST_DEFACTION_TRANSFER = "dt";
094
095    /** list item detail id constant. */
096    public static final String LIST_DETAIL_CHILDREN = "dc";
097
098    /** list item detail id constant. */
099    public static final String LIST_DETAIL_USERS = "du";
100
101    /** List id constant. */
102    public static final String LIST_ID = "lgt";
103
104    /** Stores the value of the group name, could be a list of names. */
105    private String m_groupName;
106
107    /** Stores the value of the request parameter for the group id, could be a list of ids. */
108    private String m_paramGroupid;
109
110    /**
111     * Public constructor.<p>
112     *
113     * @param jsp an initialized JSP action element
114     */
115    public CmsGroupTransferList(CmsJspActionElement jsp) {
116
117        this(LIST_ID, jsp);
118    }
119
120    /**
121     * Public constructor with JSP variables.<p>
122     *
123     * @param context the JSP page context
124     * @param req the JSP request
125     * @param res the JSP response
126     */
127    public CmsGroupTransferList(PageContext context, HttpServletRequest req, HttpServletResponse res) {
128
129        this(new CmsJspActionElement(context, req, res));
130    }
131
132    /**
133     * Protected constructor.<p>
134     *
135     * @param listId the id of the specialized list
136     * @param jsp an initialized JSP action element
137     */
138    protected CmsGroupTransferList(String listId, CmsJspActionElement jsp) {
139
140        super(
141            jsp,
142            listId,
143            Messages.get().container(Messages.GUI_GROUPS_TRANSFER_LIST_NAME_0),
144            LIST_COLUMN_DISPLAY,
145            CmsListOrderEnum.ORDER_ASCENDING,
146            LIST_COLUMN_DISPLAY);
147    }
148
149    /**
150     * This method should handle every defined list multi action,
151     * by comparing <code>{@link #getParamListAction()}</code> with the id
152     * of the action to execute.<p>
153     *
154     * @throws CmsRuntimeException to signal that an action is not supported
155     *
156     */
157    @Override
158    public void executeListMultiActions() throws CmsRuntimeException {
159
160        throwListUnsupportedActionException();
161    }
162
163    /**
164     * @see org.opencms.workplace.list.A_CmsListDialog#executeListSingleActions()
165     */
166    @Override
167    public void executeListSingleActions() throws IOException, ServletException, CmsRuntimeException {
168
169        if (getParamListAction().equals(LIST_DEFACTION_TRANSFER) || getParamListAction().equals(LIST_ACTION_TRANSFER)) {
170            try {
171                getCms().deleteGroup(new CmsUUID(getParamGroupid()), new CmsUUID(getSelectedItem().getId()));
172                CmsRequestUtil.forwardRequest(getParamCloseLink(), getJsp().getRequest(), getJsp().getResponse());
173                setForwarded(true);
174            } catch (CmsException e) {
175                throw new CmsRuntimeException(
176                    Messages.get().container(Messages.ERR_TRANSFER_GROUP_1, getSelectedItem().get(LIST_COLUMN_NAME)),
177                    e);
178            }
179        } else {
180            throwListUnsupportedActionException();
181        }
182        listSave();
183    }
184
185    /**
186     * Returns the group Name.<p>
187     *
188     * @return the group Name
189     */
190    public String getGroupName() {
191
192        return m_groupName;
193    }
194
195    /**
196     * Returns the group id parameter value.<p>
197     *
198     * @return the group id parameter value
199     */
200    public String getParamGroupid() {
201
202        return m_paramGroupid;
203    }
204
205    /**
206     * Sets the group id parameter value.<p>
207     *
208     * @param groupId the group id parameter value
209     */
210    public void setParamGroupid(String groupId) {
211
212        m_paramGroupid = groupId;
213    }
214
215    /**
216     * @see org.opencms.workplace.list.A_CmsListDialog#customHtmlStart()
217     */
218    @Override
219    protected String customHtmlStart() {
220
221        StringBuffer result = new StringBuffer(2048);
222        result.append(
223            dialogBlockStart(Messages.get().container(Messages.GUI_GROUPS_TRANSFER_NOTICE_0).key(getLocale())));
224        result.append("\n");
225        if (getCurrentToolPath().indexOf("/edit/") < 0) {
226            result.append(key(Messages.GUI_GROUP_DEPENDENCIES_SELECTED_GROUPS_0));
227            result.append(":<br>\n");
228            List<String> users = CmsStringUtil.splitAsList(getGroupName(), CmsHtmlList.ITEM_SEPARATOR, true);
229            result.append("<ul>\n");
230            Iterator<String> it = users.iterator();
231            while (it.hasNext()) {
232                String name = it.next();
233                result.append("<li>");
234                result.append(name);
235                result.append("</li>\n");
236            }
237            result.append("</ul>\n");
238        }
239        result.append(key(Messages.GUI_GROUPS_TRANSFER_NOTICE_TEXT_0));
240        result.append(dialogBlockEnd());
241        return result.toString();
242    }
243
244    /**
245     * @see org.opencms.workplace.list.A_CmsListDialog#fillDetails(java.lang.String)
246     */
247    @Override
248    protected void fillDetails(String detailId) {
249
250        // get content
251        List<CmsListItem> groups = getList().getAllContent();
252        Iterator<CmsListItem> itGroups = groups.iterator();
253        while (itGroups.hasNext()) {
254            CmsListItem item = itGroups.next();
255            String groupName = item.get(LIST_COLUMN_NAME).toString();
256            StringBuffer html = new StringBuffer(512);
257            try {
258                if (detailId.equals(LIST_DETAIL_USERS)) {
259                    // users
260                    Iterator<CmsUser> itUsers = getCms().getUsersOfGroup(groupName).iterator();
261                    while (itUsers.hasNext()) {
262                        html.append(itUsers.next().getFullName());
263                        if (itUsers.hasNext()) {
264                            html.append("<br>");
265                        }
266                        html.append("\n");
267                    }
268                } else if (detailId.equals(LIST_DETAIL_CHILDREN)) {
269                    // children
270                    Iterator<CmsGroup> itChildren = getCms().getChildren(groupName, false).iterator();
271                    while (itChildren.hasNext()) {
272                        html.append(itChildren.next().getName());
273                        if (itChildren.hasNext()) {
274                            html.append("<br>");
275                        }
276                        html.append("\n");
277                    }
278                } else {
279                    continue;
280                }
281            } catch (Exception e) {
282                // ignore
283            }
284            item.set(detailId, html.toString());
285        }
286    }
287
288    /**
289     * Returns the list of groups to display.<p>
290     *
291     * @return the list of groups to display
292     *
293     * @throws CmsException if something goes wrong
294     */
295    protected List<CmsGroup> getGroups() throws CmsException {
296
297        return CmsPrincipal.filterCoreGroups(OpenCms.getOrgUnitManager().getGroups(getCms(), "", true));
298    }
299
300    /**
301     * @see org.opencms.workplace.list.A_CmsListDialog#getListItems()
302     */
303    @Override
304    protected List<CmsListItem> getListItems() throws CmsException {
305
306        List<CmsListItem> ret = new ArrayList<CmsListItem>();
307        // get content
308        List<CmsGroup> groups = getGroups();
309        Set<String> selGroups = new HashSet<String>(
310            CmsStringUtil.splitAsList(getParamGroupid(), CmsHtmlList.ITEM_SEPARATOR, true));
311        Iterator<CmsGroup> itGroups = groups.iterator();
312        while (itGroups.hasNext()) {
313            CmsGroup group = itGroups.next();
314            if (selGroups.contains(group.getId().toString())) {
315                continue;
316            }
317            CmsListItem item = getList().newItem(group.getId().toString());
318            item.set(LIST_COLUMN_NAME, group.getName());
319            item.set(LIST_COLUMN_DISPLAY, OpenCms.getWorkplaceManager().translateGroupName(group.getName(), true));
320            item.set(LIST_COLUMN_DESCRIPTION, group.getDescription(getLocale()));
321            try {
322                item.set(LIST_COLUMN_PARENT, getCms().readGroup(group.getParentId()).getName());
323            } catch (Exception e) {
324                // ignore
325            }
326            ret.add(item);
327        }
328
329        return ret;
330    }
331
332    /**
333     * @see org.opencms.workplace.CmsWorkplace#initMessages()
334     */
335    @Override
336    protected void initMessages() {
337
338        // add specific dialog resource bundle
339        addMessages(Messages.get().getBundleName());
340        // add default resource bundles
341        super.initMessages();
342    }
343
344    /**
345     * @see org.opencms.workplace.list.A_CmsListDialog#setColumns(org.opencms.workplace.list.CmsListMetadata)
346     */
347    @Override
348    protected void setColumns(CmsListMetadata metadata) {
349
350        // create column for transfer
351        CmsListColumnDefinition transferCol = new CmsListColumnDefinition(LIST_COLUMN_TRANSFER);
352        transferCol.setName(Messages.get().container(Messages.GUI_GROUPS_TRANSFER_LIST_COLS_TRANSFER_0));
353        transferCol.setHelpText(Messages.get().container(Messages.GUI_GROUPS_TRANSFER_LIST_COLS_TRANSFER_HELP_0));
354        transferCol.setWidth("20");
355        transferCol.setAlign(CmsListColumnAlignEnum.ALIGN_CENTER);
356        transferCol.setSorteable(false);
357
358        // add transfer action
359        setTransferAction(transferCol);
360        // add it to the list definition
361        metadata.addColumn(transferCol);
362
363        CmsListColumnDefinition nameCol = new CmsListColumnDefinition(LIST_COLUMN_NAME);
364        nameCol.setName(Messages.get().container(Messages.GUI_GROUPS_LIST_COLS_NAME_0));
365        nameCol.setVisible(false);
366        metadata.addColumn(nameCol);
367
368        // create column for name
369        CmsListColumnDefinition displayCol = new CmsListColumnDefinition(LIST_COLUMN_DISPLAY);
370        displayCol.setName(Messages.get().container(Messages.GUI_GROUPS_LIST_COLS_NAME_0));
371        displayCol.setWidth("20%");
372
373        // create default transfer action
374        CmsListDefaultAction defTransferAction = new CmsListDefaultAction(LIST_DEFACTION_TRANSFER);
375        defTransferAction.setName(
376            Messages.get().container(Messages.GUI_GROUPS_TRANSFER_LIST_DEFACTION_TRANSFER_NAME_0));
377        defTransferAction.setHelpText(
378            Messages.get().container(Messages.GUI_GROUPS_TRANSFER_LIST_DEFACTION_TRANSFER_HELP_0));
379        displayCol.addDefaultAction(defTransferAction);
380
381        // add it to the list definition
382        metadata.addColumn(displayCol);
383
384        // add column for description
385        CmsListColumnDefinition descriptionCol = new CmsListColumnDefinition(LIST_COLUMN_DESCRIPTION);
386        descriptionCol.setName(Messages.get().container(Messages.GUI_GROUPS_LIST_COLS_DESCRIPTION_0));
387        descriptionCol.setWidth("60%");
388        metadata.addColumn(descriptionCol);
389
390        // add column for parent
391        CmsListColumnDefinition parentCol = new CmsListColumnDefinition(LIST_COLUMN_PARENT);
392        parentCol.setName(Messages.get().container(Messages.GUI_GROUPS_LIST_COLS_PARENT_0));
393        parentCol.setWidth("20%");
394        metadata.addColumn(parentCol);
395    }
396
397    /**
398     * @see org.opencms.workplace.list.A_CmsListDialog#setIndependentActions(org.opencms.workplace.list.CmsListMetadata)
399     */
400    @Override
401    protected void setIndependentActions(CmsListMetadata metadata) {
402
403        // add user users details
404        CmsListItemDetails usersDetails = new CmsListItemDetails(LIST_DETAIL_USERS);
405        usersDetails.setAtColumn(LIST_COLUMN_DISPLAY);
406        usersDetails.setVisible(false);
407        usersDetails.setShowActionName(Messages.get().container(Messages.GUI_GROUPS_DETAIL_SHOW_USERS_NAME_0));
408        usersDetails.setShowActionHelpText(Messages.get().container(Messages.GUI_GROUPS_DETAIL_SHOW_USERS_HELP_0));
409        usersDetails.setHideActionName(Messages.get().container(Messages.GUI_GROUPS_DETAIL_HIDE_USERS_NAME_0));
410        usersDetails.setHideActionHelpText(Messages.get().container(Messages.GUI_GROUPS_DETAIL_HIDE_USERS_HELP_0));
411        usersDetails.setName(Messages.get().container(Messages.GUI_GROUPS_DETAIL_USERS_NAME_0));
412        usersDetails.setFormatter(
413            new CmsListItemDetailsFormatter(Messages.get().container(Messages.GUI_GROUPS_DETAIL_USERS_NAME_0)));
414        metadata.addItemDetails(usersDetails);
415
416        // add user children details
417        CmsListItemDetails childDetails = new CmsListItemDetails(LIST_DETAIL_CHILDREN);
418        childDetails.setAtColumn(LIST_COLUMN_DISPLAY);
419        childDetails.setVisible(false);
420        childDetails.setShowActionName(Messages.get().container(Messages.GUI_GROUPS_DETAIL_SHOW_CHILDREN_NAME_0));
421        childDetails.setShowActionHelpText(Messages.get().container(Messages.GUI_GROUPS_DETAIL_SHOW_CHILDREN_HELP_0));
422        childDetails.setHideActionName(Messages.get().container(Messages.GUI_GROUPS_DETAIL_HIDE_CHILDREN_NAME_0));
423        childDetails.setHideActionHelpText(Messages.get().container(Messages.GUI_GROUPS_DETAIL_HIDE_CHILDREN_HELP_0));
424        childDetails.setName(Messages.get().container(Messages.GUI_GROUPS_DETAIL_CHILDREN_NAME_0));
425        childDetails.setFormatter(
426            new CmsListItemDetailsFormatter(Messages.get().container(Messages.GUI_GROUPS_DETAIL_CHILDREN_NAME_0)));
427        metadata.addItemDetails(childDetails);
428    }
429
430    /**
431     * @see org.opencms.workplace.list.A_CmsListDialog#setMultiActions(org.opencms.workplace.list.CmsListMetadata)
432     */
433    @Override
434    protected void setMultiActions(CmsListMetadata metadata) {
435
436        // no-op
437    }
438
439    /**
440     * Sets the icon actions for the transfer list.<p>
441     *
442     * @param transferCol the column to set the action
443     */
444    protected void setTransferAction(CmsListColumnDefinition transferCol) {
445
446        CmsListDirectAction transferAction = new CmsListDirectAction(LIST_ACTION_TRANSFER);
447        transferAction.setName(Messages.get().container(Messages.GUI_GROUPS_TRANSFER_LIST_ACTION_TRANSFER_NAME_0));
448        transferAction.setHelpText(Messages.get().container(Messages.GUI_GROUPS_TRANSFER_LIST_ACTION_TRANSFER_HELP_0));
449        transferAction.setIconPath(A_CmsUsersList.PATH_BUTTONS + "group.png");
450        transferCol.addDirectAction(transferAction);
451    }
452
453    /**
454     * @see org.opencms.workplace.list.A_CmsListDialog#validateParamaters()
455     */
456    @Override
457    protected void validateParamaters() throws Exception {
458
459        // test the needed parameters
460        m_groupName = "";
461        Iterator<String> itGroups = CmsStringUtil.splitAsList(
462            getParamGroupid(),
463            CmsHtmlList.ITEM_SEPARATOR,
464            true).iterator();
465        while (itGroups.hasNext()) {
466            CmsUUID id = new CmsUUID(itGroups.next());
467            m_groupName += getCms().readGroup(id).getName();
468            if (itGroups.hasNext()) {
469                m_groupName += CmsHtmlList.ITEM_SEPARATOR;
470            }
471        }
472    }
473}