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.i18n.CmsMessageContainer;
032import org.opencms.jsp.CmsJspActionElement;
033import org.opencms.main.CmsException;
034import org.opencms.security.CmsOrganizationalUnit;
035import org.opencms.util.CmsUUID;
036import org.opencms.workplace.list.A_CmsListDialog;
037import org.opencms.workplace.list.CmsListColumnAlignEnum;
038import org.opencms.workplace.list.CmsListColumnDefinition;
039import org.opencms.workplace.list.CmsListIndependentAction;
040import org.opencms.workplace.list.CmsListItem;
041import org.opencms.workplace.list.CmsListItemDetails;
042import org.opencms.workplace.list.CmsListItemDetailsFormatter;
043import org.opencms.workplace.list.CmsListMetadata;
044import org.opencms.workplace.list.CmsListOrderEnum;
045
046import java.io.IOException;
047import java.util.ArrayList;
048import java.util.Iterator;
049import java.util.List;
050import java.util.Map;
051
052import javax.servlet.ServletException;
053import javax.servlet.jsp.JspException;
054
055/**
056 * Generalized user groups view.<p>
057 *
058 * @since 6.0.0
059 */
060public abstract class A_CmsGroupUsersList extends A_CmsListDialog {
061
062    /** list action id constant. */
063    public static final String LIST_ACTION_ICON = "ai";
064
065    /** list action id constant. */
066    public static final String LIST_ACTION_STATE = "as";
067
068    /** list column id constant. */
069    public static final String LIST_COLUMN_FULLNAME = "cf";
070
071    /** list column id constant. */
072    public static final String LIST_COLUMN_ICON = "ci";
073
074    /** list column id constant. */
075    public static final String LIST_COLUMN_LOGIN = "cl";
076
077    /** list column id constant. */
078    public static final String LIST_COLUMN_NAME = "cn";
079
080    /** list column id constant. */
081    public static final String LIST_COLUMN_ORGUNIT = "co";
082
083    /** list column id constant. */
084    public static final String LIST_COLUMN_STATE = "cs";
085
086    /** list item detail id constant. */
087    public static final String LIST_DETAIL_OTHEROU = "doo";
088
089    /** Cached value. */
090    private Boolean m_hasUsersInOtherOus;
091
092    /** Stores the value of the request parameter for the user id. */
093    private String m_paramGroupid;
094
095    /** Stores the value of the request parameter for the user name. */
096    private String m_paramGroupname;
097
098    /** Stores the value of the request parameter for the organizational unit fqn. */
099    private String m_paramOufqn;
100
101    /**
102     * Public constructor.<p>
103     *
104     * @param jsp an initialized JSP action element
105     * @param listId the id of the list
106     * @param listName the name of the list
107     * @param searchable searchable flag
108     */
109    protected A_CmsGroupUsersList(
110        CmsJspActionElement jsp,
111        String listId,
112        CmsMessageContainer listName,
113        boolean searchable) {
114
115        this(jsp, listId, listName, searchable, false);
116    }
117
118    /**
119     * Public constructor.<p>
120     *
121     * @param jsp an initialized JSP action element
122     * @param listId the id of the list
123     * @param listName the name of the list
124     * @param searchable searchable flag
125     * @param lazy the lazy flag
126     */
127    protected A_CmsGroupUsersList(
128        CmsJspActionElement jsp,
129        String listId,
130        CmsMessageContainer listName,
131        boolean searchable,
132        boolean lazy) {
133
134        super(
135            jsp,
136            listId,
137            listName,
138            LIST_COLUMN_LOGIN,
139            CmsListOrderEnum.ORDER_ASCENDING,
140            searchable ? LIST_COLUMN_NAME : null,
141            lazy);
142    }
143
144    /**
145     * @see org.opencms.workplace.list.A_CmsListDialog#actionDialog()
146     */
147    @Override
148    public void actionDialog() throws JspException, ServletException, IOException {
149
150        updateGroupList();
151        super.actionDialog();
152    }
153
154    /**
155     * Returns the user id parameter value.<p>
156     *
157     * @return the user id parameter value
158     */
159    public String getParamGroupid() {
160
161        return m_paramGroupid;
162    }
163
164    /**
165     * Returns the Group name parameter.<p>
166     *
167     * @return the Group name parameter
168     */
169    public String getParamGroupname() {
170
171        return m_paramGroupname;
172    }
173
174    /**
175     * Returns the right icon path for the given list item.<p>
176     *
177     * @param item the list item to get the icon path for
178     *
179     * @return the icon path for the given role
180     */
181    public String getIconPath(CmsListItem item) {
182
183        try {
184            CmsUser user = getCms().readUser((String)item.get(LIST_COLUMN_LOGIN));
185            if (user.getOuFqn().equals(getParamOufqn())) {
186                return A_CmsUsersList.PATH_BUTTONS + "user.png";
187            } else {
188                return A_CmsUsersList.PATH_BUTTONS + "user_other_ou.png";
189            }
190        } catch (CmsException e) {
191            return A_CmsUsersList.PATH_BUTTONS + "user.png";
192        }
193    }
194
195    /**
196     * Returns the organizational unit fqn parameter value.<p>
197     *
198     * @return the organizational unit fqn parameter value
199     */
200    public String getParamOufqn() {
201
202        return m_paramOufqn;
203    }
204
205    /**
206     * Returns true if the list of users has users of other organizational units.<p>
207     *
208     * @return <code>true</code> if the list of users has users of other organizational units
209     */
210    public boolean hasUsersInOtherOus() {
211
212        if (m_lazy) {
213            // if we use database-side paging, we have to assume that there may be users from other OUs
214            return true;
215        }
216        if (m_hasUsersInOtherOus == null) {
217            // lazy initialization
218            m_hasUsersInOtherOus = Boolean.FALSE;
219            try {
220                Iterator<CmsUser> itUsers = getUsers(true).iterator();
221                while (itUsers.hasNext()) {
222                    CmsUser user = itUsers.next();
223                    if (!user.getOuFqn().equals(getParamOufqn())) {
224                        m_hasUsersInOtherOus = Boolean.TRUE;
225                        break;
226                    }
227                }
228            } catch (Exception e) {
229                // ignore
230            }
231        }
232        return m_hasUsersInOtherOus.booleanValue();
233    }
234
235    /**
236     * Makes a list item for a given user.<p>
237     *
238     * @param user the user
239     *
240     * @return the list item
241     */
242    protected CmsListItem makeListItemForUser(CmsUser user) {
243
244        CmsListItem item = getList().newItem(user.getId().toString());
245        setUserData(user, item);
246        return item;
247    }
248
249    /**
250     * Sets the user id parameter value.<p>
251     *
252     * @param userId the user id parameter value
253     */
254    public void setParamGroupid(String userId) {
255
256        m_paramGroupid = userId;
257    }
258
259    /**
260     * Sets the organizational unit fqn parameter value.<p>
261     *
262     * @param ouFqn the organizational unit fqn parameter value
263     */
264    public void setParamOufqn(String ouFqn) {
265
266        if (ouFqn == null) {
267            ouFqn = "";
268        }
269        m_paramOufqn = ouFqn;
270    }
271
272    /**
273     * Updates the main user list.<p>
274     */
275    public void updateGroupList() {
276
277        Map<?, ?> objects = (Map<?, ?>)getSettings().getListObject();
278        if (objects != null) {
279            objects.remove(CmsGroupsList.class.getName());
280            objects.remove(A_CmsUsersList.class.getName());
281        }
282    }
283
284    /**
285     * @see org.opencms.workplace.list.A_CmsListDialog#fillDetails(java.lang.String)
286     */
287    @Override
288    protected void fillDetails(String detailId) {
289
290        // noop
291    }
292
293    /**
294     * Checks whether users of other OUs should be shown.<p>
295     *
296     * @return true if users of other OUs should be shown
297     */
298    protected boolean hasOuDetail() {
299
300        CmsListMetadata meta = getList().getMetadata();
301        CmsListItemDetails detail = meta.getItemDetailDefinition(LIST_DETAIL_OTHEROU);
302        return (detail != null) && detail.isVisible();
303    }
304
305    /**
306     * @see org.opencms.workplace.list.A_CmsListDialog#getListItems()
307     */
308    @Override
309    protected List<CmsListItem> getListItems() throws CmsException {
310
311        List<CmsListItem> ret = new ArrayList<CmsListItem>();
312
313        boolean withOtherOus = hasOuDetail() && hasUsersInOtherOus();
314        // get content
315        Iterator<CmsUser> itUsers = getUsers(withOtherOus).iterator();
316        while (itUsers.hasNext()) {
317            CmsUser user = itUsers.next();
318            CmsListItem item = makeListItem(user);
319            ret.add(item);
320        }
321        return ret;
322    }
323
324    /**
325     * Makes a list item from a user.<p>
326     *
327     * @param user a user
328     *
329     * @return a list item
330     */
331    protected CmsListItem makeListItem(CmsUser user) {
332
333        CmsListItem item = getList().newItem(user.getId().toString());
334        setUserData(user, item);
335        return item;
336    }
337
338    /**
339     * Sets all needed data of the user into the list item object.<p>
340     *
341     * @param user the user to set the data for
342     * @param item the list item object to set the data into
343     */
344    protected void setUserData(CmsUser user, CmsListItem item) {
345
346        item.set(LIST_COLUMN_LOGIN, user.getName());
347        item.set(LIST_COLUMN_NAME, user.getSimpleName());
348        item.set(LIST_COLUMN_ORGUNIT, CmsOrganizationalUnit.SEPARATOR + user.getOuFqn());
349        item.set(LIST_COLUMN_FULLNAME, user.getFullName());
350    }
351
352    /**
353     * Returns a list of users to display.<p>
354     *
355     * @param withOtherOus if not set only users of the current ou should be returned
356     *
357     * @return a list of <code><{@link CmsUser}</code>s
358     *
359     * @throws CmsException if something goes wrong
360     */
361    protected abstract List<CmsUser> getUsers(boolean withOtherOus) throws CmsException;
362
363    /**
364     * @see org.opencms.workplace.list.A_CmsListDialog#initializeDetail(java.lang.String)
365     */
366    @Override
367    protected void initializeDetail(String detailId) {
368
369        super.initializeDetail(detailId);
370        if (detailId.equals(LIST_DETAIL_OTHEROU)) {
371            boolean visible = hasUsersInOtherOus()
372                && getList().getMetadata().getItemDetailDefinition(LIST_DETAIL_OTHEROU).isVisible();
373            getList().getMetadata().getColumnDefinition(LIST_COLUMN_ORGUNIT).setVisible(visible);
374            getList().getMetadata().getColumnDefinition(LIST_COLUMN_ORGUNIT).setPrintable(visible);
375        }
376    }
377
378    /**
379     * @see org.opencms.workplace.CmsWorkplace#initMessages()
380     */
381    @Override
382    protected void initMessages() {
383
384        // add specific dialog resource bundle
385        addMessages(Messages.get().getBundleName());
386        // add default resource bundles
387        super.initMessages();
388    }
389
390    /**
391     * @see org.opencms.workplace.list.A_CmsListDialog#setColumns(org.opencms.workplace.list.CmsListMetadata)
392     */
393    @Override
394    protected void setColumns(CmsListMetadata metadata) {
395
396        // create column for icon display
397        CmsListColumnDefinition iconCol = new CmsListColumnDefinition(LIST_COLUMN_ICON);
398        iconCol.setName(Messages.get().container(Messages.GUI_USERS_LIST_COLS_ICON_0));
399        iconCol.setHelpText(Messages.get().container(Messages.GUI_USERS_LIST_COLS_ICON_HELP_0));
400        iconCol.setWidth("20");
401        iconCol.setAlign(CmsListColumnAlignEnum.ALIGN_CENTER);
402        iconCol.setSorteable(false);
403        // set icon action
404        setIconAction(iconCol);
405        // add it to the list definition
406        metadata.addColumn(iconCol);
407
408        setStateActionCol(metadata);
409
410        // create column for login
411        CmsListColumnDefinition loginCol = new CmsListColumnDefinition(LIST_COLUMN_LOGIN);
412        loginCol.setVisible(false);
413        // add it to the list definition
414        metadata.addColumn(loginCol);
415
416        // create column for name
417        CmsListColumnDefinition nameCol = new CmsListColumnDefinition(LIST_COLUMN_NAME);
418        nameCol.setName(Messages.get().container(Messages.GUI_USERS_LIST_COLS_LOGIN_0));
419        nameCol.setWidth("35%");
420        setDefaultAction(nameCol);
421        // add it to the list definition
422        metadata.addColumn(nameCol);
423
424        // create column for orgunit
425        CmsListColumnDefinition orgunitCol = new CmsListColumnDefinition(LIST_COLUMN_ORGUNIT);
426        orgunitCol.setName(Messages.get().container(Messages.GUI_USERS_LIST_COLS_ORGUNIT_0));
427        orgunitCol.setVisible(false);
428        // add it to the list definition
429        metadata.addColumn(orgunitCol);
430
431        // create column for fullname
432        CmsListColumnDefinition fullnameCol = new CmsListColumnDefinition(LIST_COLUMN_FULLNAME);
433        fullnameCol.setName(Messages.get().container(Messages.GUI_USERS_LIST_COLS_FULLNAME_0));
434        fullnameCol.setWidth("65%");
435        fullnameCol.setTextWrapping(true);
436        // add it to the list definition
437        metadata.addColumn(fullnameCol);
438    }
439
440    /**
441     * Sets the optional login default action.<p>
442     *
443     * @param loginCol the login column
444     */
445    protected abstract void setDefaultAction(CmsListColumnDefinition loginCol);
446
447    /**
448     * Sets the needed icon action(s).<p>
449     *
450     * @param iconCol the list column for edition.
451     */
452    protected abstract void setIconAction(CmsListColumnDefinition iconCol);
453
454    /**
455     * @see org.opencms.workplace.list.A_CmsListDialog#setIndependentActions(org.opencms.workplace.list.CmsListMetadata)
456     */
457    @Override
458    protected void setIndependentActions(CmsListMetadata metadata) {
459
460        // add other ou button
461        CmsListItemDetails otherOuDetails = new CmsListItemDetails(LIST_DETAIL_OTHEROU);
462        otherOuDetails.setVisible(false);
463        otherOuDetails.setHideAction(new CmsListIndependentAction(LIST_DETAIL_OTHEROU) {
464
465            /**
466             * @see org.opencms.workplace.tools.A_CmsHtmlIconButton#getIconPath()
467             */
468            @Override
469            public String getIconPath() {
470
471                return A_CmsListDialog.ICON_DETAILS_HIDE;
472            }
473
474            /**
475             * @see org.opencms.workplace.tools.A_CmsHtmlIconButton#isVisible()
476             */
477            @Override
478            public boolean isVisible() {
479
480                return ((A_CmsGroupUsersList)getWp()).hasUsersInOtherOus();
481            }
482        });
483        otherOuDetails.setShowAction(new CmsListIndependentAction(LIST_DETAIL_OTHEROU) {
484
485            /**
486             * @see org.opencms.workplace.tools.A_CmsHtmlIconButton#getIconPath()
487             */
488            @Override
489            public String getIconPath() {
490
491                return A_CmsListDialog.ICON_DETAILS_SHOW;
492            }
493
494            /**
495             * @see org.opencms.workplace.tools.A_CmsHtmlIconButton#isVisible()
496             */
497            @Override
498            public boolean isVisible() {
499
500                return ((A_CmsGroupUsersList)getWp()).hasUsersInOtherOus();
501            }
502        });
503        otherOuDetails.setShowActionName(Messages.get().container(Messages.GUI_USERS_DETAIL_SHOW_OTHEROU_NAME_0));
504        otherOuDetails.setShowActionHelpText(Messages.get().container(Messages.GUI_USERS_DETAIL_SHOW_OTHEROU_HELP_0));
505        otherOuDetails.setHideActionName(Messages.get().container(Messages.GUI_USERS_DETAIL_HIDE_OTHEROU_NAME_0));
506        otherOuDetails.setHideActionHelpText(Messages.get().container(Messages.GUI_USERS_DETAIL_HIDE_OTHEROU_HELP_0));
507        otherOuDetails.setName(Messages.get().container(Messages.GUI_USERS_DETAIL_OTHEROU_NAME_0));
508        otherOuDetails.setFormatter(
509            new CmsListItemDetailsFormatter(Messages.get().container(Messages.GUI_USERS_DETAIL_OTHEROU_NAME_0)));
510        metadata.addItemDetails(otherOuDetails);
511    }
512
513    /**
514     * Sets the optional state change action column.<p>
515     *
516     * @param metadata the list metadata object
517     */
518    protected abstract void setStateActionCol(CmsListMetadata metadata);
519
520    /**
521     * @see org.opencms.workplace.list.A_CmsListDialog#validateParamaters()
522     */
523    @Override
524    protected void validateParamaters() throws Exception {
525
526        // test the needed parameters
527        m_paramGroupname = getCms().readGroup(new CmsUUID(getParamGroupid())).getName();
528    }
529}