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.file.CmsUserSearchParameters;
033import org.opencms.file.CmsUserSearchParameters.SortKey;
034import org.opencms.jsp.CmsJspActionElement;
035import org.opencms.main.CmsException;
036import org.opencms.main.CmsRuntimeException;
037import org.opencms.main.OpenCms;
038import org.opencms.util.CmsUUID;
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.CmsListMultiAction;
046import org.opencms.workplace.list.CmsListOrderEnum;
047import org.opencms.workplace.list.CmsListState;
048
049import java.util.HashSet;
050import java.util.Iterator;
051import java.util.List;
052import java.util.Set;
053
054import javax.servlet.http.HttpServletRequest;
055import javax.servlet.http.HttpServletResponse;
056import javax.servlet.jsp.PageContext;
057
058import com.google.common.collect.Lists;
059
060/**
061 * User groups view.<p>
062 *
063 * @since 6.0.0
064 */
065public class CmsGroupUsersList extends A_CmsGroupUsersList {
066
067    /** list action id constant. */
068    public static final String LIST_ACTION_REMOVE = "ar";
069
070    /** list action id constant. */
071    public static final String LIST_DEFACTION_REMOVE = "dr";
072
073    /** list item detail id constant. */
074    public static final String LIST_DETAIL_ORGUNIT = "dou";
075
076    /** list id constant. */
077    public static final String LIST_ID = "lgu";
078
079    /** list action id constant. */
080    public static final String LIST_MACTION_REMOVE = "mr";
081
082    /** a set of action id's to use for removing. */
083    protected static Set<String> m_removeActionIds = new HashSet<String>();
084
085    /**
086     * Public constructor.<p>
087     *
088     * @param jsp an initialized JSP action element
089     */
090    public CmsGroupUsersList(CmsJspActionElement jsp) {
091
092        this(jsp, false);
093    }
094
095    /**
096     * Public constructor.<p>
097     *
098     * @param jsp an initialized JSP action element
099     * @param lazy the lazy flag
100     */
101    public CmsGroupUsersList(CmsJspActionElement jsp, boolean lazy) {
102
103        this(jsp, LIST_ID, lazy);
104    }
105
106    /**
107     * Public constructor with JSP variables.<p>
108     *
109     * @param context the JSP page context
110     * @param req the JSP request
111     * @param res the JSP response
112     */
113    public CmsGroupUsersList(PageContext context, HttpServletRequest req, HttpServletResponse res) {
114
115        this(context, req, res, false);
116    }
117
118    /**
119     * Public constructor with JSP variables.<p>
120     *
121     * @param context the JSP page context
122     * @param req the JSP request
123     * @param res the JSP response
124     * @param lazy the lazy flag
125     */
126    public CmsGroupUsersList(PageContext context, HttpServletRequest req, HttpServletResponse res, boolean lazy) {
127
128        this(new CmsJspActionElement(context, req, res), lazy);
129    }
130
131    /**
132     * Protected constructor.<p>
133     * @param jsp an initialized JSP action element
134     * @param listId the id of the specialized list
135     */
136    protected CmsGroupUsersList(CmsJspActionElement jsp, String listId) {
137
138        this(jsp, listId, false);
139    }
140
141    /**
142     * Protected constructor.<p>
143     * @param jsp an initialized JSP action element
144     * @param listId the id of the specialized list
145     * @param lazy the lazy flag
146     */
147    protected CmsGroupUsersList(CmsJspActionElement jsp, String listId, boolean lazy) {
148
149        super(jsp, listId, Messages.get().container(Messages.GUI_GROUPUSERS_LIST_NAME_0), true, lazy);
150    }
151
152    /**
153     * @see org.opencms.workplace.list.A_CmsListDialog#executeListMultiActions()
154     */
155    @Override
156    public void executeListMultiActions() throws CmsRuntimeException {
157
158        if (getParamListAction().equals(LIST_MACTION_REMOVE)) {
159            // execute the remove multiaction
160            Iterator<CmsListItem> itItems = getSelectedItems().iterator();
161            while (itItems.hasNext()) {
162                CmsListItem listItem = itItems.next();
163                String userName = (String)listItem.get(LIST_COLUMN_LOGIN);
164                try {
165                    getCms().removeUserFromGroup(userName, getParamGroupname());
166                } catch (CmsException e) {
167                    // noop
168                }
169            }
170        } else {
171            throwListUnsupportedActionException();
172        }
173        listSave();
174    }
175
176    /**
177     * @see org.opencms.workplace.list.A_CmsListDialog#executeListSingleActions()
178     */
179    @Override
180    public void executeListSingleActions() throws CmsRuntimeException {
181
182        if (m_removeActionIds.contains(getParamListAction())) {
183            CmsListItem listItem = getSelectedItem();
184            try {
185                getCms().removeUserFromGroup((String)listItem.get(LIST_COLUMN_LOGIN), getParamGroupname());
186            } catch (CmsException e) {
187                // should never happen
188                throw new CmsRuntimeException(Messages.get().container(Messages.ERR_REMOVE_SELECTED_GROUP_0), e);
189            }
190        } else {
191            throwListUnsupportedActionException();
192        }
193        listSave();
194    }
195
196    /**
197     * @see org.opencms.workplace.tools.accounts.A_CmsGroupUsersList#getListItems()
198     */
199    @Override
200    protected List<CmsListItem> getListItems() throws CmsException {
201
202        if (!m_lazy) {
203            return super.getListItems();
204        } else {
205            CmsUserSearchParameters params = getSearchParams();
206            List<CmsUser> users = OpenCms.getOrgUnitManager().searchUsers(getCms(), params);
207            int count = (int)OpenCms.getOrgUnitManager().countUsers(getCms(), params);
208            getList().setSize(count);
209            List<CmsListItem> result = Lists.newArrayList();
210            for (CmsUser user : users) {
211                CmsListItem item = makeListItem(user);
212                result.add(item);
213            }
214            return result;
215        }
216    }
217
218    /**
219     * Gets the search parameters.<p>
220     *
221     * @return the search parameters
222     *
223     * @throws CmsException if something goes wrong
224     */
225    protected CmsUserSearchParameters getSearchParams() throws CmsException {
226
227        CmsListState state = getListState();
228        CmsUserSearchParameters params = new CmsUserSearchParameters();
229        String searchFilter = state.getFilter();
230        params.setSearchFilter(searchFilter);
231        if (!hasOuDetail()) {
232            params.setOrganizationalUnit(OpenCms.getOrgUnitManager().readOrganizationalUnit(getCms(), getParamOufqn()));
233        }
234        params.setPaging(getList().getMaxItemsPerPage(), state.getPage());
235        params.setSorting(getSortKey(state.getColumn()), state.getOrder().equals(CmsListOrderEnum.ORDER_ASCENDING));
236        CmsGroup group = getCms().readGroup(getParamGroupname());
237        params.setGroup(group);
238        params.setFilterByGroupOu(false);
239        return params;
240    }
241
242    /**
243     * Gets the sort key for a column.<p>
244     *
245     * @param column the column
246     *
247     * @return the sort key
248     */
249    protected SortKey getSortKey(String column) {
250
251        if (column == null) {
252            return null;
253        }
254        if (column.equals(LIST_COLUMN_FULLNAME)) {
255            return SortKey.fullName;
256        } else if (column.equals(LIST_COLUMN_NAME)) {
257            return SortKey.loginName;
258        }
259        return null;
260    }
261
262    /**
263     * @see org.opencms.workplace.tools.accounts.A_CmsGroupUsersList#getUsers(boolean)
264     */
265    @Override
266    protected List<CmsUser> getUsers(boolean withOtherOus) throws CmsException {
267
268        return getCms().getUsersOfGroup(getParamGroupname(), withOtherOus);
269    }
270
271    /**
272     * @see org.opencms.workplace.tools.accounts.A_CmsGroupUsersList#setColumns(org.opencms.workplace.list.CmsListMetadata)
273     */
274    @Override
275    protected void setColumns(CmsListMetadata meta) {
276
277        if (m_lazy) {
278            meta.setSelfManaged(true);
279        }
280        super.setColumns(meta);
281    }
282
283    /**
284     * @see org.opencms.workplace.tools.accounts.A_CmsGroupUsersList#setDefaultAction(org.opencms.workplace.list.CmsListColumnDefinition)
285     */
286    @Override
287    protected void setDefaultAction(CmsListColumnDefinition loginCol) {
288
289        // add default remove action
290        CmsListDefaultAction removeAction = new CmsListDefaultAction(LIST_DEFACTION_REMOVE);
291        removeAction.setName(Messages.get().container(Messages.GUI_USERS_LIST_DEFACTION_REMOVE_NAME_0));
292        removeAction.setHelpText(Messages.get().container(Messages.GUI_USERS_LIST_DEFACTION_REMOVE_HELP_0));
293        loginCol.addDefaultAction(removeAction);
294        // keep the id
295        m_removeActionIds.add(removeAction.getId());
296    }
297
298    /**
299     * @see org.opencms.workplace.tools.accounts.A_CmsGroupUsersList#setIconAction(org.opencms.workplace.list.CmsListColumnDefinition)
300     */
301    @Override
302    protected void setIconAction(CmsListColumnDefinition iconCol) {
303
304        CmsListDirectAction iconAction = new CmsListDefaultAction(LIST_ACTION_ICON) {
305
306            /**
307             * @see org.opencms.workplace.tools.I_CmsHtmlIconButton#getIconPath()
308             */
309            @Override
310            public String getIconPath() {
311
312                return ((A_CmsGroupUsersList)getWp()).getIconPath(getItem());
313            }
314        };
315        iconAction.setName(Messages.get().container(Messages.GUI_USERS_LIST_INGROUP_NAME_0));
316        iconAction.setHelpText(Messages.get().container(Messages.GUI_USERS_LIST_INGROUP_HELP_0));
317        iconAction.setIconPath(A_CmsUsersList.PATH_BUTTONS + "user.png");
318        iconAction.setEnabled(false);
319        iconCol.addDirectAction(iconAction);
320    }
321
322    /**
323     * @see org.opencms.workplace.list.A_CmsListDialog#setMultiActions(org.opencms.workplace.list.CmsListMetadata)
324     */
325    @Override
326    protected void setMultiActions(CmsListMetadata metadata) {
327
328        // add remove multi action
329        CmsListMultiAction removeMultiAction = new CmsListMultiAction(LIST_MACTION_REMOVE);
330        removeMultiAction.setName(Messages.get().container(Messages.GUI_USERS_LIST_MACTION_REMOVE_NAME_0));
331        removeMultiAction.setHelpText(Messages.get().container(Messages.GUI_USERS_LIST_MACTION_REMOVE_HELP_0));
332        removeMultiAction.setConfirmationMessage(
333            Messages.get().container(Messages.GUI_USERS_LIST_MACTION_REMOVE_CONF_0));
334        removeMultiAction.setIconPath(ICON_MULTI_MINUS);
335        metadata.addMultiAction(removeMultiAction);
336    }
337
338    /**
339     * @see org.opencms.workplace.tools.accounts.A_CmsGroupUsersList#setStateActionCol(org.opencms.workplace.list.CmsListMetadata)
340     */
341    @Override
342    protected void setStateActionCol(CmsListMetadata metadata) {
343
344        // create column for state change
345        CmsListColumnDefinition stateCol = new CmsListColumnDefinition(LIST_COLUMN_STATE);
346        stateCol.setName(Messages.get().container(Messages.GUI_USERS_LIST_COLS_STATE_0));
347        stateCol.setHelpText(Messages.get().container(Messages.GUI_USERS_LIST_COLS_STATE_HELP_0));
348        stateCol.setWidth("20");
349        stateCol.setAlign(CmsListColumnAlignEnum.ALIGN_CENTER);
350        stateCol.setSorteable(false);
351        // add remove action
352        CmsListDirectAction stateAction = new CmsListDirectAction(LIST_ACTION_REMOVE);
353        stateAction.setName(Messages.get().container(Messages.GUI_USERS_LIST_DEFACTION_REMOVE_NAME_0));
354        stateAction.setHelpText(Messages.get().container(Messages.GUI_USERS_LIST_DEFACTION_REMOVE_HELP_0));
355        stateAction.setIconPath(ICON_MINUS);
356        stateCol.addDirectAction(stateAction);
357        // add it to the list definition
358        metadata.addColumn(stateCol);
359        // keep the id
360        m_removeActionIds.add(stateAction.getId());
361    }
362
363    /**
364     * @see org.opencms.workplace.tools.accounts.A_CmsGroupUsersList#validateParamaters()
365     */
366    @Override
367    protected void validateParamaters() throws Exception {
368
369        super.validateParamaters();
370        if (getCms().readGroup(new CmsUUID(getParamGroupid())).isVirtual()) {
371            throw new Exception();
372        }
373    }
374}