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.CmsUser;
031import org.opencms.file.CmsUserSearchParameters;
032import org.opencms.file.CmsUserSearchParameters.SortKey;
033import org.opencms.jsp.CmsJspActionElement;
034import org.opencms.main.CmsException;
035import org.opencms.main.OpenCms;
036import org.opencms.security.CmsPrincipal;
037import org.opencms.workplace.list.CmsListColumnDefinition;
038import org.opencms.workplace.list.CmsListDirectAction;
039import org.opencms.workplace.list.CmsListItem;
040import org.opencms.workplace.list.CmsListMetadata;
041import org.opencms.workplace.list.CmsListOrderEnum;
042import org.opencms.workplace.list.CmsListState;
043
044import java.util.List;
045
046import javax.servlet.http.HttpServletRequest;
047import javax.servlet.http.HttpServletResponse;
048import javax.servlet.jsp.PageContext;
049
050import com.google.common.collect.Lists;
051
052/**
053 * Main system user account management view.<p>
054 *
055 * @since 6.0.0
056 */
057public class CmsUsersList extends A_CmsUsersList {
058
059    /** list id constant. */
060    public static final String LIST_ID = "lsu";
061
062    /**
063     * Public constructor.<p>
064     *
065     * @param jsp an initialized JSP action element
066     */
067    public CmsUsersList(CmsJspActionElement jsp) {
068
069        this(jsp, false);
070    }
071
072    /**
073     * Public constructor.<p>
074     *
075     * @param jsp an initialized JSP action element
076     * @param lazy the lazy flag
077     */
078    public CmsUsersList(CmsJspActionElement jsp, boolean lazy) {
079
080        super(jsp, LIST_ID, Messages.get().container(Messages.GUI_USERS_LIST_NAME_0), lazy);
081    }
082
083    /**
084     * Public constructor with JSP variables.<p>
085     *
086     * @param context the JSP page context
087     * @param req the JSP request
088     * @param res the JSP response
089     */
090    public CmsUsersList(PageContext context, HttpServletRequest req, HttpServletResponse res) {
091
092        this(new CmsJspActionElement(context, req, res), false);
093    }
094
095    /**
096     * Public constructor with JSP variables.<p>
097     *
098     * @param context the JSP page context
099     * @param req the JSP request
100     * @param res the JSP response
101     */
102    public CmsUsersList(PageContext context, HttpServletRequest req, HttpServletResponse res, boolean lazy) {
103
104        this(new CmsJspActionElement(context, req, res), lazy);
105    }
106
107    /**
108     * @see org.opencms.workplace.tools.accounts.A_CmsUsersList#getGroupIcon()
109     */
110    @Override
111    protected String getGroupIcon() {
112
113        return PATH_BUTTONS + "group.png";
114    }
115
116    /**
117     * @see org.opencms.workplace.tools.accounts.A_CmsUsersList#getListItems()
118     */
119    @Override
120    protected List<CmsListItem> getListItems() throws CmsException {
121
122        if (!m_lazy) {
123            return super.getListItems();
124        } else {
125            CmsUserSearchParameters params = getSearchParams();
126            List<CmsUser> users = OpenCms.getOrgUnitManager().searchUsers(getCms(), params);
127            int count = (int)OpenCms.getOrgUnitManager().countUsers(getCms(), params);
128            getList().setSize(count);
129            List<CmsListItem> result = Lists.newArrayList();
130            for (CmsUser user : users) {
131                CmsListItem item = makeListItemForUser(user);
132                result.add(item);
133            }
134            return result;
135        }
136    }
137
138    /**
139     * Gets the search parameters.<p>
140     *
141     * @return the search parameters
142     *
143     * @throws CmsException if something goes wrong
144     */
145    protected CmsUserSearchParameters getSearchParams() throws CmsException {
146
147        CmsListState state = getListState();
148        CmsUserSearchParameters params = new CmsUserSearchParameters();
149        String searchFilter = state.getFilter();
150        params.setSearchFilter(searchFilter);
151        params.setFilterCore(true);
152        params.setPaging(getList().getMaxItemsPerPage(), state.getPage());
153        params.setSorting(getSortKey(state.getColumn()), state.getOrder().equals(CmsListOrderEnum.ORDER_ASCENDING));
154        params.setOrganizationalUnit(OpenCms.getOrgUnitManager().readOrganizationalUnit(getCms(), getParamOufqn()));
155        params.setRecursiveOrgUnits(false);
156        return params;
157    }
158
159    /**
160     * Gets the sort key for a column.<p>
161     *
162     * @param column a column
163     * @return the sort key
164     */
165    protected SortKey getSortKey(String column) {
166
167        if (column == null) {
168            return null;
169        }
170        if (column.equals(LIST_COLUMN_ENABLED)) {
171            return SortKey.activated;
172        } else if (column.equals(LIST_COLUMN_LASTLOGIN)) {
173            return SortKey.lastLogin;
174        } else if (column.equals(LIST_COLUMN_DISPLAY)) {
175            return SortKey.loginName;
176        } else if (column.equals(LIST_COLUMN_NAME)) {
177            return SortKey.fullName;
178        } else if (column.equals(LIST_COLUMN_EMAIL)) {
179            return SortKey.email;
180        }
181        return null;
182    }
183
184    /**
185     * @see org.opencms.workplace.tools.accounts.A_CmsUsersList#getUsers()
186     */
187    @Override
188    protected List<CmsUser> getUsers() throws CmsException {
189
190        return CmsPrincipal.filterCoreUsers(OpenCms.getOrgUnitManager().getUsers(getCms(), getParamOufqn(), false));
191    }
192
193    /**
194     * @see org.opencms.workplace.tools.accounts.A_CmsUsersList#readUser(java.lang.String)
195     */
196    @Override
197    protected CmsUser readUser(String name) throws CmsException {
198
199        return getCms().readUser(name);
200    }
201
202    /**
203     * @see org.opencms.workplace.tools.accounts.A_CmsUsersList#setColumns(org.opencms.workplace.list.CmsListMetadata)
204     */
205    @Override
206    protected void setColumns(CmsListMetadata metadata) {
207
208        if (m_lazy) {
209            metadata.setSelfManaged(true);
210        }
211        super.setColumns(metadata);
212        metadata.getColumnDefinition(LIST_COLUMN_ENABLED).setVisible(true);
213        metadata.getColumnDefinition(LIST_COLUMN_ACTIVATE).setVisible(false);
214
215    }
216
217    /**
218     * @see org.opencms.workplace.tools.accounts.A_CmsUsersList#setDeleteAction(org.opencms.workplace.list.CmsListColumnDefinition)
219     */
220    @Override
221    protected void setDeleteAction(CmsListColumnDefinition deleteCol) {
222
223        CmsListDirectAction deleteAction = new CmsListDirectAction(LIST_ACTION_DELETE);
224        deleteAction.setName(Messages.get().container(Messages.GUI_USERS_LIST_ACTION_DELETE_NAME_0));
225        deleteAction.setHelpText(Messages.get().container(Messages.GUI_USERS_LIST_ACTION_DELETE_HELP_0));
226        deleteAction.setIconPath(ICON_DELETE);
227        deleteCol.addDirectAction(deleteAction);
228    }
229
230    /**
231     * @see org.opencms.workplace.tools.accounts.A_CmsUsersList#setEditAction(org.opencms.workplace.list.CmsListColumnDefinition)
232     */
233    @Override
234    protected void setEditAction(CmsListColumnDefinition editCol) {
235
236        CmsListDirectAction editAction = new CmsListDirectAction(LIST_ACTION_EDIT);
237        editAction.setName(Messages.get().container(Messages.GUI_USERS_LIST_ACTION_EDIT_NAME_0));
238        editAction.setHelpText(Messages.get().container(Messages.GUI_USERS_LIST_ACTION_EDIT_HELP_0));
239        editAction.setIconPath(PATH_BUTTONS + "user.png");
240        editCol.addDirectAction(editAction);
241    }
242
243}