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.util.CmsUUID;
040import org.opencms.workplace.CmsDialog;
041import org.opencms.workplace.list.CmsListColumnDefinition;
042import org.opencms.workplace.list.CmsListDefaultAction;
043import org.opencms.workplace.list.CmsListDirectAction;
044import org.opencms.workplace.list.CmsListItem;
045import org.opencms.workplace.list.CmsListMetadata;
046import org.opencms.workplace.list.CmsListOrderEnum;
047import org.opencms.workplace.list.CmsListState;
048
049import java.io.IOException;
050import java.util.HashMap;
051import java.util.List;
052import java.util.Map;
053
054import javax.servlet.ServletException;
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 * User groups overview view.<p>
063 *
064 * @since 6.0.0
065 */
066public class CmsShowGroupUsersList extends A_CmsGroupUsersList {
067
068    /** list action id constant. */
069    public static final String LIST_ACTION_EDIT = "ae";
070
071    /** list column id constant. */
072    public static final String LIST_COLUMN_LASTLOGIN = "cl";
073
074    /** list id constant. */
075    public static final String LIST_ID = "lsgu";
076
077    /**
078     * Public constructor.<p>
079     *
080     * @param jsp an initialized JSP action element
081     */
082    public CmsShowGroupUsersList(CmsJspActionElement jsp) {
083
084        this(jsp, LIST_ID);
085    }
086
087    /**
088     * Public constructor.<p>
089     *
090     * @param jsp an initialized JSP action element
091     * @param lazy <code>true</code> for lazy initialization
092     */
093    public CmsShowGroupUsersList(CmsJspActionElement jsp, boolean lazy) {
094
095        this(jsp, LIST_ID, lazy);
096    }
097
098    /**
099     * Public constructor with JSP variables.<p>
100     *
101     * @param context the JSP page context
102     * @param req the JSP request
103     * @param res the JSP response
104     */
105    public CmsShowGroupUsersList(PageContext context, HttpServletRequest req, HttpServletResponse res) {
106
107        this(new CmsJspActionElement(context, req, res));
108    }
109
110    /**
111     * Public constructor with JSP variables.<p>
112     *
113     * @param context the JSP page context
114     * @param req the JSP request
115     * @param res the JSP response
116     * @param lazy the lazy flag
117     */
118    public CmsShowGroupUsersList(PageContext context, HttpServletRequest req, HttpServletResponse res, boolean lazy) {
119
120        this(new CmsJspActionElement(context, req, res), lazy);
121    }
122
123    /**
124     * Protected constructor.<p>
125     * @param jsp an initialized JSP action element
126     * @param listId the id of the specialized list
127     */
128    protected CmsShowGroupUsersList(CmsJspActionElement jsp, String listId) {
129
130        super(jsp, listId, Messages.get().container(Messages.GUI_GROUPUSERS_LIST_NAME_0), false);
131    }
132
133    /**
134     * Protected constructor.<p>
135     * @param jsp an initialized JSP action element
136     * @param listId the id of the specialized list
137     * @param lazy the lazy flag
138     */
139    protected CmsShowGroupUsersList(CmsJspActionElement jsp, String listId, boolean lazy) {
140
141        super(jsp, listId, Messages.get().container(Messages.GUI_GROUPUSERS_LIST_NAME_0), false, lazy);
142    }
143
144    /**
145     * @see org.opencms.workplace.list.A_CmsListDialog#defaultActionHtmlStart()
146     */
147    @Override
148    public String defaultActionHtmlStart() {
149
150        return getList().listJs() + dialogContentStart(getParamTitle());
151    }
152
153    /**
154     * @see org.opencms.workplace.list.A_CmsListDialog#executeListMultiActions()
155     */
156    @Override
157    public void executeListMultiActions() throws CmsRuntimeException {
158
159        throwListUnsupportedActionException();
160    }
161
162    /**
163     * @see org.opencms.workplace.list.A_CmsListDialog#executeListSingleActions()
164     */
165    @Override
166    public void executeListSingleActions() throws IOException, ServletException {
167
168        String userId = getSelectedItem().getId();
169
170        Map<String, String[]> params = new HashMap<String, String[]>();
171        params.put(CmsDialog.PARAM_ACTION, new String[] {CmsDialog.DIALOG_INITIAL});
172        params.put(A_CmsEditUserDialog.PARAM_USERID, new String[] {userId});
173        params.put(A_CmsOrgUnitDialog.PARAM_OUFQN, new String[] {getParamOufqn()});
174
175        if (getParamListAction().equals(LIST_ACTION_EDIT)) {
176            getToolManager().jspForwardTool(
177                this,
178                getCurrentToolPath().substring(0, getCurrentToolPath().indexOf('/', 2)) + "/orgunit/users/edit/user",
179                params);
180        } else {
181            throwListUnsupportedActionException();
182        }
183    }
184
185    /**
186     * @see org.opencms.workplace.tools.accounts.A_CmsGroupUsersList#getListItems()
187     */
188    @Override
189    protected List<CmsListItem> getListItems() throws CmsException {
190
191        if (!m_lazy) {
192            return super.getListItems();
193        } else {
194            CmsUserSearchParameters params = getSearchParams();
195            List<CmsUser> users = OpenCms.getOrgUnitManager().searchUsers(getCms(), params);
196            int count = (int)OpenCms.getOrgUnitManager().countUsers(getCms(), params);
197            getList().setSize(count);
198            List<CmsListItem> result = Lists.newArrayList();
199            for (CmsUser user : users) {
200                CmsListItem item = makeListItem(user);
201                result.add(item);
202            }
203            return result;
204        }
205    }
206
207    /**
208     * Gets the search parameters.<p>
209     *
210     * @return the search parameters
211     *
212     * @throws CmsException if something goes wrong
213     */
214    protected CmsUserSearchParameters getSearchParams() throws CmsException {
215
216        CmsListState state = getListState();
217        CmsUserSearchParameters params = new CmsUserSearchParameters();
218        String searchFilter = state.getFilter();
219        params.setSearchFilter(searchFilter);
220        if (!hasOuDetail()) {
221            params.setOrganizationalUnit(OpenCms.getOrgUnitManager().readOrganizationalUnit(getCms(), getParamOufqn()));
222        }
223        params.setPaging(getList().getMaxItemsPerPage(), state.getPage());
224        params.setSorting(getSortKey(state.getColumn()), state.getOrder().equals(CmsListOrderEnum.ORDER_ASCENDING));
225        CmsGroup group = getCms().readGroup(new CmsUUID(getParamGroupid()));
226        params.setGroup(group);
227        params.setFilterByGroupOu(false);
228        return params;
229    }
230
231    /**
232     * Gets the sort key for a column.<p>
233     *
234     * @param column a column
235     * @return the sort key
236     */
237    protected SortKey getSortKey(String column) {
238
239        if (column == null) {
240            return null;
241        }
242        if (column.equals(LIST_COLUMN_FULLNAME)) {
243            return SortKey.fullName;
244        } else if (column.equals(LIST_COLUMN_NAME)) {
245            return SortKey.loginName;
246        }
247        return null;
248    }
249
250    /**
251     * @see org.opencms.workplace.tools.accounts.A_CmsGroupUsersList#getUsers(boolean)
252     */
253    @Override
254    protected List<CmsUser> getUsers(boolean withOtherOus) throws CmsException {
255
256        return getCms().getUsersOfGroup(getParamGroupname(), withOtherOus);
257    }
258
259    /**
260     * @see org.opencms.workplace.tools.accounts.A_CmsGroupUsersList#setColumns(org.opencms.workplace.list.CmsListMetadata)
261     */
262    @Override
263    protected void setColumns(CmsListMetadata meta) {
264
265        if (m_lazy) {
266            meta.setSelfManaged(true);
267        }
268        super.setColumns(meta);
269    }
270
271    /**
272     * @see org.opencms.workplace.tools.accounts.A_CmsGroupUsersList#setDefaultAction(org.opencms.workplace.list.CmsListColumnDefinition)
273     */
274    @Override
275    protected void setDefaultAction(CmsListColumnDefinition loginCol) {
276
277        CmsListDefaultAction editAction = new CmsListDefaultAction(LIST_ACTION_EDIT) {
278
279            /**
280             * @see org.opencms.workplace.tools.A_CmsHtmlIconButton#isEnabled()
281             */
282            @Override
283            public boolean isEnabled() {
284
285                return getItem().get(LIST_COLUMN_ORGUNIT).equals(
286                    CmsOrganizationalUnit.SEPARATOR + ((CmsShowGroupUsersList)getWp()).getParamOufqn());
287            }
288        };
289        editAction.setName(Messages.get().container(Messages.GUI_USERS_LIST_ACTION_EDIT_NAME_0));
290        editAction.setHelpText(Messages.get().container(Messages.GUI_USERS_LIST_ACTION_EDIT_HELP_0));
291        loginCol.addDefaultAction(editAction);
292    }
293
294    /**
295     * @see org.opencms.workplace.tools.accounts.A_CmsGroupUsersList#setIconAction(org.opencms.workplace.list.CmsListColumnDefinition)
296     */
297    @Override
298    protected void setIconAction(CmsListColumnDefinition iconCol) {
299
300        CmsListDirectAction iconAction = new CmsListDirectAction(LIST_ACTION_ICON) {
301
302            /**
303             * @see org.opencms.workplace.tools.I_CmsHtmlIconButton#getIconPath()
304             */
305            @Override
306            public String getIconPath() {
307
308                return ((A_CmsGroupUsersList)getWp()).getIconPath(getItem());
309            }
310        };
311        iconAction.setName(Messages.get().container(Messages.GUI_USERS_LIST_INGROUP_NAME_0));
312        iconAction.setHelpText(Messages.get().container(Messages.GUI_USERS_LIST_INGROUP_HELP_0));
313        iconAction.setIconPath(A_CmsUsersList.PATH_BUTTONS + "user.png");
314        iconAction.setEnabled(false);
315        iconCol.addDirectAction(iconAction);
316    }
317
318    /**
319     * @see org.opencms.workplace.list.A_CmsListDialog#setMultiActions(org.opencms.workplace.list.CmsListMetadata)
320     */
321    @Override
322    protected void setMultiActions(CmsListMetadata metadata) {
323
324        // noop
325    }
326
327    /**
328     * @see org.opencms.workplace.tools.accounts.A_CmsGroupUsersList#setStateActionCol(org.opencms.workplace.list.CmsListMetadata)
329     */
330    @Override
331    protected void setStateActionCol(CmsListMetadata metadata) {
332
333        // no-op
334    }
335}