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