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.jsp.CmsJspActionElement;
032import org.opencms.main.CmsException;
033import org.opencms.main.CmsRuntimeException;
034import org.opencms.main.OpenCms;
035import org.opencms.workplace.list.CmsListColumnAlignEnum;
036import org.opencms.workplace.list.CmsListColumnDefinition;
037import org.opencms.workplace.list.CmsListDefaultAction;
038import org.opencms.workplace.list.CmsListDirectAction;
039import org.opencms.workplace.list.CmsListItem;
040import org.opencms.workplace.list.CmsListMetadata;
041import org.opencms.workplace.list.CmsListMultiAction;
042
043import java.util.HashSet;
044import java.util.Iterator;
045import java.util.List;
046import java.util.Map;
047import java.util.Set;
048
049import javax.servlet.http.HttpServletRequest;
050import javax.servlet.http.HttpServletResponse;
051import javax.servlet.jsp.PageContext;
052
053/**
054 * Not Usergroups view.<p>
055 *
056 * @since 6.0.0
057 */
058public class CmsNotUserGroupsList extends A_CmsUserGroupsList {
059
060    /** list action id constant. */
061    public static final String LIST_ACTION_ADD = "aa";
062
063    /** list action id constant. */
064    public static final String LIST_DEFACTION_ADD = "da";
065
066    /** list id constant. */
067    public static final String LIST_ID = "lnug";
068
069    /** list action id constant. */
070    public static final String LIST_MACTION_ADD = "ma";
071
072    /** a set of action id's to use for adding. */
073    protected static Set<String> m_addActionIds = new HashSet<String>();
074
075    /**
076     * Public constructor.<p>
077     *
078     * @param jsp an initialized JSP action element
079     */
080    public CmsNotUserGroupsList(CmsJspActionElement jsp) {
081
082        this(jsp, LIST_ID);
083    }
084
085    /**
086     * Public constructor with JSP variables.<p>
087     *
088     * @param context the JSP page context
089     * @param req the JSP request
090     * @param res the JSP response
091     */
092    public CmsNotUserGroupsList(PageContext context, HttpServletRequest req, HttpServletResponse res) {
093
094        this(new CmsJspActionElement(context, req, res));
095    }
096
097    /**
098     * Public constructor.<p>
099     *
100     * @param jsp an initialized JSP action element
101     * @param listId the id of the list
102     */
103    protected CmsNotUserGroupsList(CmsJspActionElement jsp, String listId) {
104
105        super(jsp, listId, Messages.get().container(Messages.GUI_NOTUSERGROUPS_LIST_NAME_0), true);
106    }
107
108    /**
109     * @see org.opencms.workplace.list.A_CmsListDialog#executeListMultiActions()
110     */
111    @Override
112    public void executeListMultiActions() throws CmsRuntimeException {
113
114        if (getParamListAction().equals(LIST_MACTION_ADD)) {
115            // execute the remove multiaction
116            try {
117                Iterator<CmsListItem> itItems = getSelectedItems().iterator();
118                while (itItems.hasNext()) {
119                    CmsListItem listItem = itItems.next();
120                    getCms().addUserToGroup(getParamUsername(), (String)listItem.get(LIST_COLUMN_NAME));
121                }
122            } catch (CmsException e) {
123                // refresh the list
124                Map<?, ?> objects = (Map<?, ?>)getSettings().getListObject();
125                if (objects != null) {
126                    objects.remove(A_CmsUsersList.class.getName());
127                }
128                throw new CmsRuntimeException(Messages.get().container(Messages.ERR_ADD_SELECTED_GROUPS_0), e);
129            }
130        } else {
131            throwListUnsupportedActionException();
132        }
133        listSave();
134    }
135
136    /**
137     * @see org.opencms.workplace.list.A_CmsListDialog#executeListSingleActions()
138     */
139    @Override
140    public void executeListSingleActions() throws CmsRuntimeException {
141
142        if (m_addActionIds.contains(getParamListAction())) {
143            CmsListItem listItem = getSelectedItem();
144            try {
145                getCms().addUserToGroup(getParamUsername(), (String)listItem.get(LIST_COLUMN_NAME));
146            } catch (CmsException e) {
147                // should never happen
148                throw new CmsRuntimeException(Messages.get().container(Messages.ERR_ADD_SELECTED_GROUP_0), e);
149            }
150        } else {
151            throwListUnsupportedActionException();
152        }
153        listSave();
154    }
155
156    /**
157     * @see org.opencms.workplace.tools.accounts.A_CmsUserGroupsList#getGroups(boolean)
158     */
159    @Override
160    protected List<CmsGroup> getGroups(boolean withOtherOus) throws CmsException {
161
162        List<CmsGroup> usergroups = getCms().getGroupsOfUser(getParamUsername(), false, withOtherOus);
163        List<CmsGroup> groups;
164        if (withOtherOus) {
165            groups = OpenCms.getRoleManager().getManageableGroups(getCms(), "", true);
166        } else {
167            groups = OpenCms.getRoleManager().getManageableGroups(getCms(), getParamOufqn(), false);
168        }
169        groups.removeAll(usergroups);
170        return groups;
171    }
172
173    /**
174     * @see org.opencms.workplace.tools.accounts.A_CmsUserGroupsList#setDefaultAction(org.opencms.workplace.list.CmsListColumnDefinition)
175     */
176    @Override
177    protected void setDefaultAction(CmsListColumnDefinition nameCol) {
178
179        // add add action
180        CmsListDefaultAction addAction = new CmsListDefaultAction(LIST_DEFACTION_ADD);
181        addAction.setName(Messages.get().container(Messages.GUI_GROUPS_LIST_DEFACTION_ADD_NAME_0));
182        addAction.setHelpText(Messages.get().container(Messages.GUI_GROUPS_LIST_DEFACTION_ADD_HELP_0));
183        nameCol.addDefaultAction(addAction);
184        // keep the id
185        m_addActionIds.add(addAction.getId());
186    }
187
188    /**
189     * @see org.opencms.workplace.tools.accounts.A_CmsUserGroupsList#setIconAction(org.opencms.workplace.list.CmsListColumnDefinition)
190     */
191    @Override
192    protected void setIconAction(CmsListColumnDefinition iconCol) {
193
194        CmsListDirectAction iconAction = new CmsListDirectAction(LIST_ACTION_ICON);
195        iconAction.setName(Messages.get().container(Messages.GUI_GROUPS_LIST_AVAILABLE_NAME_0));
196        iconAction.setHelpText(Messages.get().container(Messages.GUI_GROUPS_LIST_AVAILABLE_HELP_0));
197        iconAction.setIconPath(A_CmsUsersList.PATH_BUTTONS + "group.png");
198        iconAction.setEnabled(false);
199        iconCol.addDirectAction(iconAction);
200    }
201
202    /**
203     * @see org.opencms.workplace.list.A_CmsListDialog#setMultiActions(org.opencms.workplace.list.CmsListMetadata)
204     */
205    @Override
206    protected void setMultiActions(CmsListMetadata metadata) {
207
208        // add add multi action
209        CmsListMultiAction addMultiAction = new CmsListMultiAction(LIST_MACTION_ADD);
210        addMultiAction.setName(Messages.get().container(Messages.GUI_GROUPS_LIST_MACTION_ADD_NAME_0));
211        addMultiAction.setHelpText(Messages.get().container(Messages.GUI_GROUPS_LIST_MACTION_ADD_HELP_0));
212        addMultiAction.setConfirmationMessage(Messages.get().container(Messages.GUI_GROUPS_LIST_MACTION_ADD_CONF_0));
213        addMultiAction.setIconPath(ICON_MULTI_ADD);
214        metadata.addMultiAction(addMultiAction);
215    }
216
217    /**
218     * @see org.opencms.workplace.tools.accounts.A_CmsUserGroupsList#setStateActionCol(org.opencms.workplace.list.CmsListMetadata)
219     */
220    @Override
221    protected void setStateActionCol(CmsListMetadata metadata) {
222
223        // create column for state change
224        CmsListColumnDefinition stateCol = new CmsListColumnDefinition(LIST_COLUMN_STATE);
225        stateCol.setName(Messages.get().container(Messages.GUI_GROUPS_LIST_COLS_STATE_0));
226        stateCol.setHelpText(Messages.get().container(Messages.GUI_GROUPS_LIST_COLS_STATE_HELP_0));
227        stateCol.setWidth("20");
228        stateCol.setAlign(CmsListColumnAlignEnum.ALIGN_CENTER);
229        stateCol.setSorteable(false);
230        // add it to the list definition
231        metadata.addColumn(stateCol);
232
233        // add add action
234        CmsListDirectAction stateAction = new CmsListDirectAction(LIST_ACTION_ADD);
235        stateAction.setName(Messages.get().container(Messages.GUI_GROUPS_LIST_DEFACTION_ADD_NAME_0));
236        stateAction.setHelpText(Messages.get().container(Messages.GUI_GROUPS_LIST_DEFACTION_ADD_HELP_0));
237        stateAction.setIconPath(ICON_ADD);
238        stateCol.addDirectAction(stateAction);
239        // keep the id
240        m_addActionIds.add(stateAction.getId());
241    }
242}