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.i18n.CmsMessageContainer;
032import org.opencms.jsp.CmsJspActionElement;
033import org.opencms.main.CmsException;
034import org.opencms.main.OpenCms;
035import org.opencms.security.CmsOrganizationalUnit;
036import org.opencms.util.CmsUUID;
037import org.opencms.workplace.list.A_CmsListDialog;
038import org.opencms.workplace.list.CmsListColumnAlignEnum;
039import org.opencms.workplace.list.CmsListColumnDefinition;
040import org.opencms.workplace.list.CmsListIndependentAction;
041import org.opencms.workplace.list.CmsListItem;
042import org.opencms.workplace.list.CmsListItemDetails;
043import org.opencms.workplace.list.CmsListItemDetailsFormatter;
044import org.opencms.workplace.list.CmsListMetadata;
045import org.opencms.workplace.list.CmsListOrderEnum;
046
047import java.util.ArrayList;
048import java.util.Iterator;
049import java.util.List;
050
051/**
052 * Generalized user groups view.<p>
053 *
054 * @since 6.0.0
055 */
056public abstract class A_CmsUserGroupsList extends A_CmsListDialog {
057
058    /** list action id constant. */
059    public static final String LIST_ACTION_ICON = "ai";
060
061    /** list action id constant. */
062    public static final String LIST_ACTION_ICON_DIRECT = "aid";
063
064    /** list action id constant. */
065    public static final String LIST_ACTION_ICON_INDIRECT = "aii";
066
067    /** list action id constant. */
068    public static final String LIST_ACTION_STATE_DIRECT = "asd";
069
070    /** list action id constant. */
071    public static final String LIST_ACTION_STATE_INDIRECT = "asi";
072
073    /** list column id constant. */
074    public static final String LIST_COLUMN_DESCRIPTION = "cd";
075
076    /** list column id constant. */
077    public static final String LIST_COLUMN_DISPLAY = "cdn";
078
079    /** list column id constant. */
080    public static final String LIST_COLUMN_ICON = "ci";
081
082    /** list column id constant. */
083    public static final String LIST_COLUMN_NAME = "cn";
084
085    /** list column id constant. */
086    public static final String LIST_COLUMN_ORGUNIT = "co";
087
088    /** list column id constant. */
089    public static final String LIST_COLUMN_STATE = "cs";
090
091    /** list item detail id constant. */
092    public static final String LIST_DETAIL_OTHEROU = "doo";
093
094    /** Cached value. */
095    private Boolean m_hasGroupsInOtherOus;
096
097    /** Stores the value of the request parameter for the organizational unit. */
098    private String m_paramOufqn;
099
100    /** Stores the value of the request parameter for the user id. */
101    private String m_paramUserid;
102
103    /** Stores the value of the request parameter for the user name. */
104    private String m_paramUsername;
105
106    /**
107     * Public constructor.<p>
108     *
109     * @param jsp an initialized JSP action element
110     * @param listId the id of the list
111     * @param listName the name of the list
112     * @param searchable searchable flag
113     */
114    protected A_CmsUserGroupsList(
115        CmsJspActionElement jsp,
116        String listId,
117        CmsMessageContainer listName,
118        boolean searchable) {
119
120        super(
121            jsp,
122            listId,
123            listName,
124            LIST_COLUMN_DISPLAY,
125            CmsListOrderEnum.ORDER_ASCENDING,
126            searchable ? LIST_COLUMN_DISPLAY : null);
127    }
128
129    /**
130     * Returns the organizational unit parameter value.<p>
131     *
132     * @return the organizational unit parameter value
133     */
134    public String getParamOufqn() {
135
136        return m_paramOufqn;
137    }
138
139    /**
140     * Returns the user id parameter value.<p>
141     *
142     * @return the user id parameter value
143     */
144    public String getParamUserid() {
145
146        return m_paramUserid;
147    }
148
149    /**
150     * Returns the User name parameter value.<p>
151     *
152     * @return the User name parameter value
153     */
154    public String getParamUsername() {
155
156        return m_paramUsername;
157    }
158
159    /**
160     * Returns if the list of groups has groups of other organizational units.<p>
161     *
162     * @return if the list of groups has groups of other organizational units
163     */
164    public boolean hasGroupsInOtherOus() {
165
166        if (m_hasGroupsInOtherOus == null) {
167            // lazzy initialization
168            m_hasGroupsInOtherOus = Boolean.FALSE;
169            try {
170                List<CmsGroup> groups = getGroups(true);
171                Iterator<CmsGroup> itGroups = groups.iterator();
172                while (itGroups.hasNext()) {
173                    CmsGroup group = itGroups.next();
174                    if (!group.getOuFqn().equals(getParamOufqn())) {
175                        m_hasGroupsInOtherOus = Boolean.TRUE;
176                        break;
177                    }
178                }
179            } catch (Exception e) {
180                // ignore
181            }
182        }
183        return m_hasGroupsInOtherOus.booleanValue();
184    }
185
186    /**
187     * Sets the user organizational unit value.<p>
188     *
189     * @param ouFqn the organizational unit parameter value
190     */
191    public void setParamOufqn(String ouFqn) {
192
193        if (ouFqn == null) {
194            ouFqn = "";
195        }
196        m_paramOufqn = ouFqn;
197    }
198
199    /**
200     * Sets the user id parameter value.<p>
201     *
202     * @param userId the user id parameter value
203     */
204    public void setParamUserid(String userId) {
205
206        m_paramUserid = userId;
207    }
208
209    /**
210     * Sets the User name parameter value.<p>
211     *
212     * @param username the username to set
213     */
214    public void setParamUsername(String username) {
215
216        m_paramUsername = username;
217    }
218
219    /**
220     * @see org.opencms.workplace.list.A_CmsListDialog#fillDetails(java.lang.String)
221     */
222    @Override
223    protected void fillDetails(String detailId) {
224
225        // noop
226    }
227
228    /**
229     * Returns a list of groups to display.<p>
230     *
231     * @param withOtherOus if not set only groups of the current ou should be returned
232     *
233     * @return a list of <code><{@link CmsGroup}</code>s
234     *
235     * @throws CmsException if something goes wrong
236     */
237    protected abstract List<CmsGroup> getGroups(boolean withOtherOus) throws CmsException;
238
239    /**
240     * @see org.opencms.workplace.list.A_CmsListDialog#getListItems()
241     */
242    @Override
243    protected List<CmsListItem> getListItems() throws CmsException {
244
245        CmsListItemDetails details = getList().getMetadata().getItemDetailDefinition(LIST_DETAIL_OTHEROU);
246        boolean withOtherOus = hasGroupsInOtherOus() && (details != null) && details.isVisible();
247        List<CmsListItem> ret = new ArrayList<CmsListItem>();
248        // get content
249        List<CmsGroup> groups = getGroups(withOtherOus);
250        Iterator<CmsGroup> itGroups = groups.iterator();
251        while (itGroups.hasNext()) {
252            CmsGroup group = itGroups.next();
253            CmsListItem item = getList().newItem(group.getId().toString());
254            item.set(LIST_COLUMN_NAME, group.getName());
255            item.set(LIST_COLUMN_DISPLAY, OpenCms.getWorkplaceManager().translateGroupName(group.getName(), false));
256            item.set(LIST_COLUMN_DESCRIPTION, group.getDescription(getLocale()));
257            item.set(LIST_COLUMN_ORGUNIT, CmsOrganizationalUnit.SEPARATOR + group.getOuFqn());
258            ret.add(item);
259        }
260        return ret;
261    }
262
263    /**
264     * @see org.opencms.workplace.list.A_CmsListDialog#initializeDetail(java.lang.String)
265     */
266    @Override
267    protected void initializeDetail(String detailId) {
268
269        super.initializeDetail(detailId);
270        if (detailId.equals(LIST_DETAIL_OTHEROU)) {
271            boolean visible = hasGroupsInOtherOus()
272                && getList().getMetadata().getItemDetailDefinition(LIST_DETAIL_OTHEROU).isVisible();
273            getList().getMetadata().getColumnDefinition(LIST_COLUMN_ORGUNIT).setVisible(visible);
274            getList().getMetadata().getColumnDefinition(LIST_COLUMN_ORGUNIT).setPrintable(visible);
275        }
276    }
277
278    /**
279     * @see org.opencms.workplace.CmsWorkplace#initMessages()
280     */
281    @Override
282    protected void initMessages() {
283
284        // add specific dialog resource bundle
285        addMessages(Messages.get().getBundleName());
286        // add default resource bundles
287        super.initMessages();
288    }
289
290    /**
291     * @see org.opencms.workplace.list.A_CmsListDialog#setColumns(org.opencms.workplace.list.CmsListMetadata)
292     */
293    @Override
294    protected void setColumns(CmsListMetadata metadata) {
295
296        // create column for icon display
297        CmsListColumnDefinition iconCol = new CmsListColumnDefinition(LIST_COLUMN_ICON);
298        iconCol.setName(Messages.get().container(Messages.GUI_GROUPS_LIST_COLS_ICON_0));
299        iconCol.setHelpText(Messages.get().container(Messages.GUI_GROUPS_LIST_COLS_ICON_HELP_0));
300        iconCol.setWidth("20");
301        iconCol.setAlign(CmsListColumnAlignEnum.ALIGN_CENTER);
302        iconCol.setSorteable(false);
303        // add icon actions
304        setIconAction(iconCol);
305        // add it to the list definition
306        metadata.addColumn(iconCol);
307        // add state column and actions
308        setStateActionCol(metadata);
309
310        CmsListColumnDefinition nameCol = new CmsListColumnDefinition(LIST_COLUMN_NAME);
311        nameCol.setVisible(false);
312        metadata.addColumn(nameCol);
313
314        // create column for display name
315        CmsListColumnDefinition displayCol = new CmsListColumnDefinition(LIST_COLUMN_DISPLAY);
316        displayCol.setName(Messages.get().container(Messages.GUI_GROUPS_LIST_COLS_NAME_0));
317        displayCol.setWidth("35%");
318        // add default actions
319        setDefaultAction(displayCol);
320        // add it to the list definition
321        metadata.addColumn(displayCol);
322
323        // create column for orgunit
324        CmsListColumnDefinition orgunitCol = new CmsListColumnDefinition(LIST_COLUMN_ORGUNIT);
325        orgunitCol.setName(Messages.get().container(Messages.GUI_USERS_LIST_COLS_ORGUNIT_0));
326        orgunitCol.setVisible(false);
327        // add it to the list definition
328        metadata.addColumn(orgunitCol);
329
330        // create column for description
331        CmsListColumnDefinition descCol = new CmsListColumnDefinition(LIST_COLUMN_DESCRIPTION);
332        descCol.setName(Messages.get().container(Messages.GUI_GROUPS_LIST_COLS_DESCRIPTION_0));
333        descCol.setWidth("65%");
334        descCol.setTextWrapping(true);
335        // add it to the list definition
336        metadata.addColumn(descCol);
337    }
338
339    /**
340     * Sets the optional login default action.<p>
341     *
342     * @param nameCol the group name column
343     */
344    protected abstract void setDefaultAction(CmsListColumnDefinition nameCol);
345
346    /**
347     * Sets the needed icon action(s).<p>
348     *
349     * @param iconCol the list column for edition.
350     */
351    protected abstract void setIconAction(CmsListColumnDefinition iconCol);
352
353    /**
354     * @see org.opencms.workplace.list.A_CmsListDialog#setIndependentActions(org.opencms.workplace.list.CmsListMetadata)
355     */
356    @Override
357    protected void setIndependentActions(CmsListMetadata metadata) {
358
359        // add user address details
360        CmsListItemDetails otherOuDetails = new CmsListItemDetails(LIST_DETAIL_OTHEROU);
361        otherOuDetails.setHideAction(new CmsListIndependentAction(LIST_DETAIL_OTHEROU) {
362
363            /**
364             * @see org.opencms.workplace.tools.A_CmsHtmlIconButton#getIconPath()
365             */
366            @Override
367            public String getIconPath() {
368
369                return A_CmsListDialog.ICON_DETAILS_HIDE;
370            }
371
372            /**
373             * @see org.opencms.workplace.tools.A_CmsHtmlIconButton#isVisible()
374             */
375            @Override
376            public boolean isVisible() {
377
378                return ((A_CmsUserGroupsList)getWp()).hasGroupsInOtherOus();
379            }
380        });
381        otherOuDetails.setShowAction(new CmsListIndependentAction(LIST_DETAIL_OTHEROU) {
382
383            /**
384             * @see org.opencms.workplace.tools.A_CmsHtmlIconButton#getIconPath()
385             */
386            @Override
387            public String getIconPath() {
388
389                return A_CmsListDialog.ICON_DETAILS_SHOW;
390            }
391
392            /**
393             * @see org.opencms.workplace.tools.A_CmsHtmlIconButton#isVisible()
394             */
395            @Override
396            public boolean isVisible() {
397
398                return ((A_CmsUserGroupsList)getWp()).hasGroupsInOtherOus();
399            }
400        });
401        otherOuDetails.setShowActionName(Messages.get().container(Messages.GUI_GROUPS_DETAIL_SHOW_OTHEROU_NAME_0));
402        otherOuDetails.setShowActionHelpText(Messages.get().container(Messages.GUI_GROUPS_DETAIL_SHOW_OTHEROU_HELP_0));
403        otherOuDetails.setHideActionName(Messages.get().container(Messages.GUI_GROUPS_DETAIL_HIDE_OTHEROU_NAME_0));
404        otherOuDetails.setHideActionHelpText(Messages.get().container(Messages.GUI_GROUPS_DETAIL_HIDE_OTHEROU_HELP_0));
405        otherOuDetails.setName(Messages.get().container(Messages.GUI_GROUPS_DETAIL_OTHEROU_NAME_0));
406        otherOuDetails.setFormatter(
407            new CmsListItemDetailsFormatter(Messages.get().container(Messages.GUI_GROUPS_DETAIL_OTHEROU_NAME_0)));
408        otherOuDetails.setVisible(true);
409        metadata.addItemDetails(otherOuDetails);
410    }
411
412    /**
413     * Sets the optional state change action column.<p>
414     *
415     * @param metadata the list metadata object
416     */
417    protected abstract void setStateActionCol(CmsListMetadata metadata);
418
419    /**
420     * @see org.opencms.workplace.list.A_CmsListDialog#validateParamaters()
421     */
422    @Override
423    protected void validateParamaters() throws Exception {
424
425        // test the needed parameters
426        m_paramUsername = getCms().readUser(new CmsUUID(getParamUserid())).getName();
427    }
428}