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.i18n.CmsMessageContainer;
031import org.opencms.jsp.CmsJspActionElement;
032import org.opencms.main.CmsException;
033import org.opencms.main.OpenCms;
034import org.opencms.security.CmsOrganizationalUnit;
035import org.opencms.security.CmsRole;
036import org.opencms.workplace.list.A_CmsListDialog;
037import org.opencms.workplace.list.CmsListColumnDefinition;
038import org.opencms.workplace.list.CmsListDefaultAction;
039import org.opencms.workplace.list.CmsListDirectAction;
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;
045import org.opencms.workplace.list.I_CmsListFormatter;
046
047import java.util.ArrayList;
048import java.util.HashMap;
049import java.util.Iterator;
050import java.util.List;
051import java.util.Locale;
052import java.util.Map;
053
054/**
055 * User roles overview view.<p>
056 *
057 * @since 6.5.6
058 */
059public abstract class A_CmsRolesList extends A_CmsListDialog {
060
061    /** list action id constant. */
062    public static final String LIST_ACTION_ICON = "ai";
063
064    /** list column id constant. */
065    public static final String LIST_COLUMN_DEPENDENCY = "cd";
066
067    /** list column id constant. */
068    public static final String LIST_COLUMN_GROUP_NAME = "cgn";
069
070    /** list column id constant. */
071    public static final String LIST_COLUMN_HIDDEN_NAME = "chn";
072
073    /** list column id constant. */
074    public static final String LIST_COLUMN_ICON = "ci";
075
076    /** list column id constant. */
077    public static final String LIST_COLUMN_NAME = "cn";
078
079    /** list item detail id constant. */
080    public static final String LIST_DETAIL_DESCRIPTION = "dd";
081
082    /** list item detail id constant. */
083    public static final String LIST_DETAIL_PATH = "dp";
084
085    /** Path to the list buttons. */
086    public static final String PATH_BUTTONS = "tools/accounts/buttons/";
087
088    /** Stores the value of the request parameter for the organizational unit. */
089    private String m_paramOufqn;
090
091    /**
092     * Public constructor.<p>
093     *
094     * @param jsp an initialized JSP action element
095     * @param listId the id of the list
096     * @param listName the name of the list
097     */
098    protected A_CmsRolesList(CmsJspActionElement jsp, String listId, CmsMessageContainer listName) {
099
100        super(jsp, listId, listName, LIST_COLUMN_HIDDEN_NAME, CmsListOrderEnum.ORDER_ASCENDING, null);
101    }
102
103    /**
104     * Returns the right icon path for the given list item.<p>
105     *
106     * @param item the list item to get the icon path for
107     *
108     * @return the icon path for the given role
109     */
110    public abstract String getIconPath(CmsListItem item);
111
112    /**
113     * Returns the organizational unit parameter value.<p>
114     *
115     * @return the organizational unit parameter value
116     */
117    public String getParamOufqn() {
118
119        return m_paramOufqn;
120    }
121
122    /**
123     * Sets the user organizational unit value.<p>
124     *
125     * @param ouFqn the organizational unit parameter value
126     */
127    public void setParamOufqn(String ouFqn) {
128
129        if (ouFqn == null) {
130            ouFqn = "";
131        }
132        m_paramOufqn = ouFqn;
133    }
134
135    /**
136     * @see org.opencms.workplace.list.A_CmsListDialog#fillDetails(java.lang.String)
137     */
138    @Override
139    protected void fillDetails(String detailId) {
140
141        // get content
142        List<CmsListItem> roles = getList().getAllContent();
143        Iterator<CmsListItem> itRoles = roles.iterator();
144        while (itRoles.hasNext()) {
145            CmsListItem item = itRoles.next();
146            String roleName = item.get(LIST_COLUMN_GROUP_NAME).toString();
147            StringBuffer html = new StringBuffer(512);
148            try {
149                if (detailId.equals(LIST_DETAIL_PATH)) {
150                    html.append(
151                        OpenCms.getOrgUnitManager().readOrganizationalUnit(
152                            getCms(),
153                            CmsOrganizationalUnit.getParentFqn(roleName)).getDisplayName(getLocale()));
154                } else if (detailId.equals(LIST_DETAIL_DESCRIPTION)) {
155                    CmsRole role = CmsRole.valueOf(getCms().readGroup(roleName));
156                    html.append(role.getDescription(getCms().getRequestContext().getLocale()));
157                } else {
158                    continue;
159                }
160            } catch (Exception e) {
161                // noop
162            }
163            item.set(detailId, html.toString());
164        }
165    }
166
167    /**
168     * @see org.opencms.workplace.list.A_CmsListDialog#getListItems()
169     */
170    @Override
171    protected List<CmsListItem> getListItems() throws CmsException {
172
173        List<CmsListItem> ret = new ArrayList<CmsListItem>();
174        List<CmsRole> roles = getRoles();
175        Locale locale = getCms().getRequestContext().getLocale();
176        Map<String, String> dependencies = new HashMap<String, String>();
177        for (CmsRole role : roles) {
178            for (CmsRole child : role.getChildren(true)) {
179                String deps = dependencies.get(child.getRoleName());
180                if (deps == null) {
181                    deps = "";
182                } else {
183                    deps += ", ";
184                }
185                deps += role.getName(locale);
186                dependencies.put(child.getRoleName(), deps);
187            }
188        }
189        for (CmsRole role : roles) {
190            CmsListItem item = getList().newItem(role.getGroupName());
191
192            item.set(LIST_COLUMN_NAME, role.getName(locale));
193            String dependency = dependencies.get(role.getRoleName());
194            if (dependency == null) {
195                dependency = "";
196            }
197            item.set(LIST_COLUMN_DEPENDENCY, dependency);
198            item.set(LIST_COLUMN_HIDDEN_NAME, "" + (1000 + dependency.length()));
199            item.set(LIST_COLUMN_GROUP_NAME, role.getGroupName());
200            ret.add(item);
201        }
202        return ret;
203    }
204
205    /**
206     * Returns all roles to display.<p>
207     *
208     * @return a list of {@link CmsRole} objects
209     *
210     * @throws CmsException if something goes wrong
211     */
212    protected abstract List<CmsRole> getRoles() throws CmsException;
213
214    /**
215     * Returns if the organizational unit details button should be displayed.<p>
216     *
217     * @return if the organizational unit details button should be displayed
218     */
219    protected boolean includeOuDetails() {
220
221        return true;
222    }
223
224    /**
225     * @see org.opencms.workplace.list.A_CmsListDialog#setColumns(org.opencms.workplace.list.CmsListMetadata)
226     */
227    @Override
228    protected void setColumns(CmsListMetadata metadata) {
229
230        // create column for icon display
231        CmsListColumnDefinition iconCol = new CmsListColumnDefinition(LIST_COLUMN_ICON);
232        iconCol.setName(Messages.get().container(Messages.GUI_ROLEEDIT_LIST_COLS_ICON_0));
233        iconCol.setHelpText(Messages.get().container(Messages.GUI_ROLEEDIT_LIST_COLS_ICON_HELP_0));
234        iconCol.setWidth("1%");
235        iconCol.setSorteable(false);
236
237        // adds a role icon
238        CmsListDirectAction dirAction = new CmsListDefaultAction(LIST_ACTION_ICON) {
239
240            /**
241             * @see org.opencms.workplace.tools.A_CmsHtmlIconButton#getIconPath()
242             */
243            @Override
244            public String getIconPath() {
245
246                return ((A_CmsRolesList)getWp()).getIconPath(getItem());
247            }
248
249        };
250        dirAction.setName(Messages.get().container(Messages.GUI_ROLEEDIT_LIST_ICON_NAME_0));
251        dirAction.setHelpText(Messages.get().container(Messages.GUI_ROLEEDIT_LIST_ICON_HELP_0));
252        dirAction.setIconPath(PATH_BUTTONS + "role.png");
253        dirAction.setEnabled(false);
254        iconCol.addDirectAction(dirAction);
255        // add it to the list definition
256        metadata.addColumn(iconCol);
257
258        // create column for name
259        CmsListColumnDefinition nameCol = new CmsListColumnDefinition(LIST_COLUMN_NAME);
260        nameCol.setName(Messages.get().container(Messages.GUI_ROLEEDIT_LIST_COLS_NAME_0));
261        nameCol.setWidth("40%");
262        // add it to the list definition
263        metadata.addColumn(nameCol);
264
265        // create column for path
266        CmsListColumnDefinition depCol = new CmsListColumnDefinition(LIST_COLUMN_DEPENDENCY);
267        depCol.setName(Messages.get().container(Messages.GUI_ROLEEDIT_LIST_COLS_DEPENDENCY_0));
268        depCol.setWidth("60%");
269        depCol.setTextWrapping(true);
270        // add it to the list definition
271        metadata.addColumn(depCol);
272
273        // create column for hidden name
274        CmsListColumnDefinition hideNameCol = new CmsListColumnDefinition(LIST_COLUMN_HIDDEN_NAME);
275        hideNameCol.setSorteable(true);
276        hideNameCol.setVisible(false);
277        // add it to the list definition
278        metadata.addColumn(hideNameCol);
279        hideNameCol.setPrintable(false);
280
281        // create column for group name
282        CmsListColumnDefinition groupNameCol = new CmsListColumnDefinition(LIST_COLUMN_GROUP_NAME);
283        groupNameCol.setVisible(false);
284        // add it to the list definition
285        metadata.addColumn(groupNameCol);
286        groupNameCol.setPrintable(false);
287    }
288
289    /**
290     * @see org.opencms.workplace.list.A_CmsListDialog#setIndependentActions(org.opencms.workplace.list.CmsListMetadata)
291     */
292    @Override
293    protected void setIndependentActions(CmsListMetadata metadata) {
294
295        // add description details
296        CmsListItemDetails descriptionDetails = new CmsListItemDetails(LIST_DETAIL_DESCRIPTION);
297        descriptionDetails.setAtColumn(LIST_COLUMN_NAME);
298        descriptionDetails.setVisible(true);
299        descriptionDetails.setShowActionName(
300            Messages.get().container(Messages.GUI_ROLEEDIT_DETAIL_SHOW_DESCRIPTION_NAME_0));
301        descriptionDetails.setShowActionHelpText(
302            Messages.get().container(Messages.GUI_ROLEEDIT_DETAIL_SHOW_DESCRIPTION_HELP_0));
303        descriptionDetails.setHideActionName(
304            Messages.get().container(Messages.GUI_ROLEEDIT_DETAIL_HIDE_DESCRIPTION_NAME_0));
305        descriptionDetails.setHideActionHelpText(
306            Messages.get().container(Messages.GUI_ROLEEDIT_DETAIL_HIDE_DESCRIPTION_HELP_0));
307        descriptionDetails.setName(Messages.get().container(Messages.GUI_ROLEEDIT_DETAIL_DESCRIPTION_NAME_0));
308        descriptionDetails.setFormatter(new I_CmsListFormatter() {
309
310            /**
311             * @see org.opencms.workplace.list.I_CmsListFormatter#format(java.lang.Object, java.util.Locale)
312             */
313            public String format(Object data, Locale locale) {
314
315                StringBuffer html = new StringBuffer(512);
316                html.append("<table border='0' cellspacing='0' cellpadding='0'>\n");
317                html.append("\t<tr>\n");
318                html.append("\t\t<td style='white-space:normal;' >\n");
319                html.append("\t\t\t");
320                html.append(data == null ? "" : data);
321                html.append("\n");
322                html.append("\t\t</td>\n");
323                html.append("\t</tr>\n");
324                html.append("</table>\n");
325                return html.toString();
326            }
327        });
328        metadata.addItemDetails(descriptionDetails);
329
330        if (includeOuDetails()) {
331            // add role path
332            CmsListItemDetails pathDetails = new CmsListItemDetails(LIST_DETAIL_PATH);
333            pathDetails.setAtColumn(LIST_COLUMN_NAME);
334            pathDetails.setVisible(false);
335            pathDetails.setShowActionName(Messages.get().container(Messages.GUI_ROLES_DETAIL_SHOW_PATH_NAME_0));
336            pathDetails.setShowActionHelpText(Messages.get().container(Messages.GUI_ROLES_DETAIL_SHOW_PATH_HELP_0));
337            pathDetails.setHideActionName(Messages.get().container(Messages.GUI_ROLES_DETAIL_HIDE_PATH_NAME_0));
338            pathDetails.setHideActionHelpText(Messages.get().container(Messages.GUI_ROLES_DETAIL_HIDE_PATH_HELP_0));
339            pathDetails.setName(Messages.get().container(Messages.GUI_ROLES_DETAIL_PATH_NAME_0));
340            pathDetails.setFormatter(
341                new CmsListItemDetailsFormatter(Messages.get().container(Messages.GUI_ROLES_DETAIL_PATH_NAME_0)));
342            metadata.addItemDetails(pathDetails);
343        }
344    }
345
346    /**
347     * @see org.opencms.workplace.list.A_CmsListDialog#validateParamaters()
348     */
349    @Override
350    protected void validateParamaters() throws Exception {
351
352        OpenCms.getRoleManager().checkRole(getCms(), CmsRole.ACCOUNT_MANAGER.forOrgUnit(getParamOufqn()));
353    }
354}