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.CmsResource;
032import org.opencms.file.CmsUser;
033import org.opencms.i18n.CmsMessageContainer;
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.security.CmsRole;
040import org.opencms.workplace.CmsDialog;
041import org.opencms.workplace.list.A_CmsListDialog;
042import org.opencms.workplace.list.CmsListColumnAlignEnum;
043import org.opencms.workplace.list.CmsListColumnDefinition;
044import org.opencms.workplace.list.CmsListDefaultAction;
045import org.opencms.workplace.list.CmsListDirectAction;
046import org.opencms.workplace.list.CmsListItem;
047import org.opencms.workplace.list.CmsListItemDetails;
048import org.opencms.workplace.list.CmsListItemDetailsFormatter;
049import org.opencms.workplace.list.CmsListMetadata;
050import org.opencms.workplace.list.CmsListOrderEnum;
051import org.opencms.workplace.list.CmsListSearchAction;
052
053import java.io.IOException;
054import java.util.ArrayList;
055import java.util.HashMap;
056import java.util.Iterator;
057import java.util.List;
058import java.util.Map;
059
060import javax.servlet.ServletException;
061
062/**
063 * Main organization unit management view.<p>
064 *
065 * @since 6.5.6
066 */
067public abstract class A_CmsOrgUnitsList extends A_CmsListDialog {
068
069    /** list action id constant. */
070    public static final String LIST_ACTION_DEACTIVE = "add";
071
072    /** list action id constant. */
073    public static final String LIST_ACTION_DELETE = "ad";
074
075    /** list action id constant. */
076    public static final String LIST_ACTION_EDIT = "ae";
077
078    /** list action id constant. */
079    public static final String LIST_ACTION_GROUP = "ag";
080
081    /** list action id constant. */
082    public static final String LIST_ACTION_USER = "au";
083
084    /** list column id constant. */
085    public static final String LIST_COLUMN_ADMIN = "ca";
086
087    /** list column id constant. */
088    public static final String LIST_COLUMN_DELETE = "cd";
089
090    /** list column id constant. */
091    public static final String LIST_COLUMN_DESCRIPTION = "cb";
092
093    /** list column id constant. */
094    public static final String LIST_COLUMN_EDIT = "ce";
095
096    /** list column id constant. */
097    public static final String LIST_COLUMN_GROUP = "cg";
098
099    /** list column id constant. */
100    public static final String LIST_COLUMN_NAME = "cn";
101
102    /** list column id constant. */
103    public static final String LIST_COLUMN_USER = "cu";
104
105    /** list column id constant. */
106    public static final String LIST_COLUMN_WEBUSER = "cw";
107
108    /** list action id constant. */
109    public static final String LIST_DEFACTION_OVERVIEW = "do";
110
111    /** list item detail id constant. */
112    public static final String LIST_DETAIL_GROUPS = "dg";
113
114    /** list item detail id constant. */
115    public static final String LIST_DETAIL_RESOURCES = "dr";
116
117    /** list item detail id constant. */
118    public static final String LIST_DETAIL_USERS = "du";
119
120    /** list action id constant. */
121    public static final String LIST_MACTION_DELETE = "md";
122
123    /** Path to the list buttons. */
124    public static final String PATH_BUTTONS = "tools/accounts/buttons/";
125
126    /** Cached list of OUs. */
127    private List<CmsOrganizationalUnit> m_ous;
128
129    /**
130     * Public constructor.<p>
131     *
132     * @param jsp an initialized JSP action element
133     * @param listId the id of the list
134     * @param listName the list name
135     */
136    public A_CmsOrgUnitsList(CmsJspActionElement jsp, String listId, CmsMessageContainer listName) {
137
138        super(jsp, listId, listName, LIST_COLUMN_NAME, CmsListOrderEnum.ORDER_ASCENDING, null);
139    }
140
141    /**
142     * @see org.opencms.workplace.list.A_CmsListDialog#executeListMultiActions()
143     */
144    @Override
145    public void executeListMultiActions() {
146
147        Iterator<CmsListItem> itItems = getSelectedItems().iterator();
148
149        if (getParamListAction().equals(LIST_MACTION_DELETE)) {
150            while (itItems.hasNext()) {
151                CmsListItem item = itItems.next();
152                String ouFqn = item.get(LIST_COLUMN_NAME).toString();
153                try {
154                    OpenCms.getOrgUnitManager().deleteOrganizationalUnit(getCms(), ouFqn.substring(1));
155                } catch (CmsException e) {
156                    throw new CmsRuntimeException(Messages.get().container(Messages.ERR_DELETE_ORGUNIT_1, ouFqn), e);
157                }
158            }
159        } else {
160            throwListUnsupportedActionException();
161        }
162        listSave();
163    }
164
165    /**
166     * @see org.opencms.workplace.list.A_CmsListDialog#executeListSingleActions()
167     */
168    @Override
169    public void executeListSingleActions() throws IOException, ServletException {
170
171        String ouFqn = getSelectedItem().get(LIST_COLUMN_NAME).toString();
172        Map<String, String[]> params = new HashMap<String, String[]>();
173        params.put(A_CmsOrgUnitDialog.PARAM_OUFQN, new String[] {ouFqn.substring(1)});
174        params.put(CmsDialog.PARAM_ACTION, new String[] {CmsDialog.DIALOG_INITIAL});
175        if (getParamListAction().equals(LIST_ACTION_EDIT)) {
176            // forward to the edit user screen
177            getToolManager().jspForwardTool(this, "/accounts/orgunit/mgmt/edit", params);
178        } else if (getParamListAction().equals(LIST_ACTION_USER)) {
179            // forward to the edit user screen
180            getToolManager().jspForwardTool(this, "/accounts/orgunit/users", params);
181        } else if (getParamListAction().equals(LIST_ACTION_GROUP)) {
182            // forward to the edit user screen
183            getToolManager().jspForwardTool(this, "/accounts/orgunit/groups", params);
184        } else if (getParamListAction().equals(LIST_ACTION_DELETE)) {
185            // forward to the edit user screen
186            getToolManager().jspForwardTool(this, "/accounts/orgunit/mgmt/delete", params);
187        } else if (getParamListAction().equals(LIST_DEFACTION_OVERVIEW)) {
188            // forward to the edit user screen
189            getToolManager().jspForwardTool(this, "/accounts/orgunit", params);
190        } else {
191            throwListUnsupportedActionException();
192        }
193        listSave();
194    }
195
196    /**
197     * Returns the path of the edit icon.<p>
198     *
199     * @return the path of the edit icon
200     */
201    public String getEditIcon() {
202
203        return PATH_BUTTONS + "orgunit.png";
204    }
205
206    /**
207     * Returns the path of the group icon.<p>
208     *
209     * @return the path of the group icon
210     */
211    public String getGroupIcon() {
212
213        return PATH_BUTTONS + "group.png";
214    }
215
216    /**
217     * Returns the path of the user icon.<p>
218     *
219     * @return the path of the user icon
220     */
221    public String getUserIcon() {
222
223        return PATH_BUTTONS + "user.png";
224    }
225
226    /**
227     * @see org.opencms.workplace.list.A_CmsListDialog#fillDetails(java.lang.String)
228     */
229    @Override
230    protected void fillDetails(String detailId) {
231
232        // get content
233        List<CmsListItem> orgUnits = getList().getAllContent();
234        Iterator<CmsListItem> itOrgUnits = orgUnits.iterator();
235        while (itOrgUnits.hasNext()) {
236            CmsListItem item = itOrgUnits.next();
237            String ouFqn = item.get(LIST_COLUMN_NAME).toString();
238            StringBuffer html = new StringBuffer(512);
239            try {
240                if (detailId.equals(LIST_DETAIL_USERS)) {
241                    List<CmsUser> usersOrgUnit = OpenCms.getOrgUnitManager().getUsers(getCms(), ouFqn, false);
242                    Iterator<CmsUser> itUsersOrgUnit = usersOrgUnit.iterator();
243                    while (itUsersOrgUnit.hasNext()) {
244                        CmsUser user = itUsersOrgUnit.next();
245                        html.append(user.getFullName());
246                        if (itUsersOrgUnit.hasNext()) {
247                            html.append("<br>");
248                        }
249                        html.append("\n");
250                    }
251                } else if (detailId.equals(LIST_DETAIL_GROUPS)) {
252                    List<CmsGroup> groupsOrgUnit = OpenCms.getOrgUnitManager().getGroups(getCms(), ouFqn, false);
253                    Iterator<CmsGroup> itGroupsOrgUnit = groupsOrgUnit.iterator();
254                    while (itGroupsOrgUnit.hasNext()) {
255                        CmsGroup group = itGroupsOrgUnit.next();
256                        String niceGroupName = OpenCms.getWorkplaceManager().translateGroupName(group.getName(), false);
257                        html.append(niceGroupName);
258                        if (itGroupsOrgUnit.hasNext()) {
259                            html.append("<br>");
260                        }
261                        html.append("\n");
262                    }
263                } else if (detailId.equals(LIST_DETAIL_RESOURCES)) {
264                    List<CmsResource> resourcesOrgUnit = OpenCms.getOrgUnitManager().getResourcesForOrganizationalUnit(
265                        getCms(),
266                        ouFqn);
267                    Iterator<CmsResource> itResourcesOrgUnit = resourcesOrgUnit.iterator();
268                    while (itResourcesOrgUnit.hasNext()) {
269                        CmsResource resource = itResourcesOrgUnit.next();
270                        html.append(resource.getRootPath());
271                        if (itResourcesOrgUnit.hasNext()) {
272                            html.append("<br>");
273                        }
274                        html.append("\n");
275                    }
276                } else {
277                    continue;
278                }
279            } catch (Exception e) {
280                // noop
281            }
282            item.set(detailId, html.toString());
283        }
284    }
285
286    /**
287     * @see org.opencms.workplace.list.A_CmsListDialog#getListItems()
288     */
289    @Override
290    protected List<CmsListItem> getListItems() throws CmsException {
291
292        List<CmsListItem> ret = new ArrayList<CmsListItem>();
293        List<CmsOrganizationalUnit> orgUnits = getOrgUnits();
294        Iterator<CmsOrganizationalUnit> itOrgUnits = orgUnits.iterator();
295        while (itOrgUnits.hasNext()) {
296            CmsOrganizationalUnit childOrgUnit = itOrgUnits.next();
297            CmsListItem item = getList().newItem(childOrgUnit.getName());
298            item.set(LIST_COLUMN_NAME, CmsOrganizationalUnit.SEPARATOR + childOrgUnit.getName());
299            item.set(LIST_COLUMN_DESCRIPTION, childOrgUnit.getDescription(getLocale()));
300            item.set(LIST_COLUMN_ADMIN, Boolean.valueOf(
301                OpenCms.getRoleManager().hasRole(getCms(), CmsRole.ADMINISTRATOR.forOrgUnit(childOrgUnit.getName()))));
302            item.set(LIST_COLUMN_WEBUSER, Boolean.valueOf(childOrgUnit.hasFlagWebuser()));
303            ret.add(item);
304        }
305        return ret;
306    }
307
308    /**
309     * Returns the organizational units to display.<p>
310     *
311     * @return the organizational units
312     *
313     * @throws CmsException if something goes wrong
314     */
315    protected List<CmsOrganizationalUnit> getOrgUnits() throws CmsException {
316
317        if (m_ous == null) {
318            m_ous = OpenCms.getRoleManager().getOrgUnitsForRole(getCms(), CmsRole.ACCOUNT_MANAGER.forOrgUnit(""), true);
319        }
320        return m_ous;
321    }
322
323    /**
324     * @see org.opencms.workplace.list.A_CmsListDialog#setColumns(org.opencms.workplace.list.CmsListMetadata)
325     */
326    @Override
327    protected void setColumns(CmsListMetadata metadata) {
328
329        // create column for edit
330        CmsListColumnDefinition editCol = new CmsListColumnDefinition(LIST_COLUMN_EDIT);
331        editCol.setName(Messages.get().container(Messages.GUI_ORGUNITS_LIST_COLS_EDIT_0));
332        editCol.setHelpText(Messages.get().container(Messages.GUI_ORGUNITS_LIST_COLS_EDIT_HELP_0));
333        editCol.setWidth("20");
334        editCol.setAlign(CmsListColumnAlignEnum.ALIGN_CENTER);
335        editCol.setSorteable(false);
336        // add edit action
337        CmsListDirectAction editAction = new CmsListDirectAction(LIST_ACTION_EDIT) {
338
339            /**
340             * @see org.opencms.workplace.tools.A_CmsHtmlIconButton#getHelpText()
341             */
342            @Override
343            public CmsMessageContainer getHelpText() {
344
345                if (!isEnabled()) {
346                    return Messages.get().container(Messages.GUI_ORGUNIT_ADMIN_TOOL_DISABLED_EDIT_HELP_0);
347                }
348                return super.getHelpText();
349            }
350
351            /**
352             * @see org.opencms.workplace.tools.A_CmsHtmlIconButton#getIconPath()
353             */
354            @Override
355            public String getIconPath() {
356
357                if (getItem() != null) {
358                    if (((Boolean)getItem().get(LIST_COLUMN_WEBUSER)).booleanValue()) {
359                        return PATH_BUTTONS + "webuser_ou.png";
360                    }
361                }
362                return super.getIconPath();
363            }
364
365            /**
366             * @see org.opencms.workplace.tools.A_CmsHtmlIconButton#isEnabled()
367             */
368            @Override
369            public boolean isEnabled() {
370
371                if (getItem() != null) {
372                    return ((Boolean)getItem().get(LIST_COLUMN_ADMIN)).booleanValue();
373                }
374                return super.isVisible();
375            }
376        };
377        editAction.setName(Messages.get().container(Messages.GUI_ORGUNITS_LIST_ACTION_EDIT_NAME_0));
378        editAction.setHelpText(Messages.get().container(Messages.GUI_ORGUNITS_LIST_COLS_EDIT_HELP_0));
379        editAction.setIconPath(getEditIcon());
380        editCol.addDirectAction(editAction);
381        // add it to the list definition
382        metadata.addColumn(editCol);
383
384        // create column for user
385        CmsListColumnDefinition userCol = new CmsListColumnDefinition(LIST_COLUMN_USER);
386        userCol.setName(Messages.get().container(Messages.GUI_ORGUNITS_LIST_COLS_USER_0));
387        userCol.setHelpText(Messages.get().container(Messages.GUI_ORGUNITS_LIST_COLS_USER_HELP_0));
388        userCol.setWidth("20");
389        userCol.setAlign(CmsListColumnAlignEnum.ALIGN_CENTER);
390        userCol.setSorteable(false);
391        // add user action
392        CmsListDirectAction userAction = new CmsListDirectAction(LIST_ACTION_USER);
393        userAction.setName(Messages.get().container(Messages.GUI_ORGUNITS_LIST_ACTION_USER_NAME_0));
394        userAction.setHelpText(Messages.get().container(Messages.GUI_ORGUNITS_LIST_COLS_USER_HELP_0));
395        userAction.setIconPath(getUserIcon());
396        userCol.addDirectAction(userAction);
397        // add it to the list definition
398        metadata.addColumn(userCol);
399
400        // create column for group
401        CmsListColumnDefinition groupCol = new CmsListColumnDefinition(LIST_COLUMN_GROUP);
402        groupCol.setName(Messages.get().container(Messages.GUI_ORGUNITS_LIST_COLS_GROUP_0));
403        groupCol.setHelpText(Messages.get().container(Messages.GUI_ORGUNITS_LIST_COLS_GROUP_HELP_0));
404        groupCol.setWidth("20");
405        groupCol.setAlign(CmsListColumnAlignEnum.ALIGN_CENTER);
406        groupCol.setSorteable(false);
407        // add group action
408        CmsListDirectAction groupAction = new CmsListDirectAction(LIST_ACTION_GROUP);
409        groupAction.setName(Messages.get().container(Messages.GUI_ORGUNITS_LIST_ACTION_GROUP_NAME_0));
410        groupAction.setHelpText(Messages.get().container(Messages.GUI_ORGUNITS_LIST_COLS_GROUP_HELP_0));
411        groupAction.setIconPath(getGroupIcon());
412        groupCol.addDirectAction(groupAction);
413        // add it to the list definition
414        metadata.addColumn(groupCol);
415
416        // create column for description
417        CmsListColumnDefinition descCol = new CmsListColumnDefinition(LIST_COLUMN_DESCRIPTION);
418        descCol.setName(Messages.get().container(Messages.GUI_ORGUNITS_LIST_COLS_DESCRIPTION_0));
419        descCol.setWidth("60%");
420        descCol.setSorteable(true);
421        // create default overview action
422        CmsListDefaultAction defOverviewAction = new CmsListDefaultAction(LIST_DEFACTION_OVERVIEW);
423        defOverviewAction.setName(Messages.get().container(Messages.GUI_ORGUNITS_LIST_DEFACTION_OVERVIEW_NAME_0));
424        defOverviewAction.setHelpText(Messages.get().container(Messages.GUI_ORGUNITS_LIST_DEFACTION_OVERVIEW_HELP_0));
425        descCol.addDefaultAction(defOverviewAction);
426        // add it to the list definition
427        metadata.addColumn(descCol);
428
429        // create column for name / path
430        CmsListColumnDefinition nameCol = new CmsListColumnDefinition(LIST_COLUMN_NAME);
431        nameCol.setName(Messages.get().container(Messages.GUI_ORGUNITS_LIST_COLS_NAME_0));
432        nameCol.setWidth("40%");
433        // add it to the list definition
434        metadata.addColumn(nameCol);
435
436        // create column for manageable flag
437        CmsListColumnDefinition adminCol = new CmsListColumnDefinition(LIST_COLUMN_ADMIN);
438        adminCol.setVisible(false);
439        metadata.addColumn(adminCol);
440
441        // create column for webuser flag
442        CmsListColumnDefinition webuserCol = new CmsListColumnDefinition(LIST_COLUMN_WEBUSER);
443        webuserCol.setVisible(false);
444        metadata.addColumn(webuserCol);
445    }
446
447    /**
448     * @see org.opencms.workplace.list.A_CmsListDialog#setIndependentActions(org.opencms.workplace.list.CmsListMetadata)
449     */
450    @Override
451    protected void setIndependentActions(CmsListMetadata metadata) {
452
453        // add users details
454        CmsListItemDetails usersDetails = new CmsListItemDetails(LIST_DETAIL_USERS);
455        usersDetails.setAtColumn(LIST_COLUMN_DESCRIPTION);
456        usersDetails.setVisible(false);
457        usersDetails.setShowActionName(Messages.get().container(Messages.GUI_ORGUNITS_DETAIL_SHOW_USERS_NAME_0));
458        usersDetails.setShowActionHelpText(Messages.get().container(Messages.GUI_ORGUNITS_DETAIL_SHOW_USERS_HELP_0));
459        usersDetails.setHideActionName(Messages.get().container(Messages.GUI_ORGUNITS_DETAIL_HIDE_USERS_NAME_0));
460        usersDetails.setHideActionHelpText(Messages.get().container(Messages.GUI_ORGUNITS_DETAIL_HIDE_USERS_HELP_0));
461        usersDetails.setName(Messages.get().container(Messages.GUI_ORGUNITS_DETAIL_USERS_NAME_0));
462        usersDetails.setFormatter(
463            new CmsListItemDetailsFormatter(Messages.get().container(Messages.GUI_ORGUNITS_DETAIL_USERS_NAME_0)));
464        metadata.addItemDetails(usersDetails);
465
466        // add groups details
467        CmsListItemDetails groupsDetails = new CmsListItemDetails(LIST_DETAIL_GROUPS);
468        groupsDetails.setAtColumn(LIST_COLUMN_DESCRIPTION);
469        groupsDetails.setVisible(false);
470        groupsDetails.setShowActionName(Messages.get().container(Messages.GUI_ORGUNITS_DETAIL_SHOW_GROUPS_NAME_0));
471        groupsDetails.setShowActionHelpText(Messages.get().container(Messages.GUI_ORGUNITS_DETAIL_SHOW_GROUPS_HELP_0));
472        groupsDetails.setHideActionName(Messages.get().container(Messages.GUI_ORGUNITS_DETAIL_HIDE_GROUPS_NAME_0));
473        groupsDetails.setHideActionHelpText(Messages.get().container(Messages.GUI_ORGUNITS_DETAIL_HIDE_GROUPS_HELP_0));
474        groupsDetails.setName(Messages.get().container(Messages.GUI_ORGUNITS_DETAIL_GROUPS_NAME_0));
475        groupsDetails.setFormatter(
476            new CmsListItemDetailsFormatter(Messages.get().container(Messages.GUI_ORGUNITS_DETAIL_GROUPS_NAME_0)));
477        metadata.addItemDetails(groupsDetails);
478
479        // add resources details
480        CmsListItemDetails resourcesDetails = new CmsListItemDetails(LIST_DETAIL_RESOURCES);
481        resourcesDetails.setAtColumn(LIST_COLUMN_DESCRIPTION);
482        resourcesDetails.setVisible(false);
483        resourcesDetails.setShowActionName(
484            Messages.get().container(Messages.GUI_ORGUNITS_DETAIL_SHOW_RESOURCES_NAME_0));
485        resourcesDetails.setShowActionHelpText(
486            Messages.get().container(Messages.GUI_ORGUNITS_DETAIL_SHOW_RESOURCES_HELP_0));
487        resourcesDetails.setHideActionName(
488            Messages.get().container(Messages.GUI_ORGUNITS_DETAIL_HIDE_RESOURCES_NAME_0));
489        resourcesDetails.setHideActionHelpText(
490            Messages.get().container(Messages.GUI_ORGUNITS_DETAIL_HIDE_RESOURCES_HELP_0));
491        resourcesDetails.setName(Messages.get().container(Messages.GUI_ORGUNITS_DETAIL_RESOURCES_NAME_0));
492        resourcesDetails.setFormatter(
493            new CmsListItemDetailsFormatter(Messages.get().container(Messages.GUI_ORGUNITS_DETAIL_RESOURCES_NAME_0)));
494        metadata.addItemDetails(resourcesDetails);
495
496        // makes the list searchable
497        CmsListSearchAction searchAction = new CmsListSearchAction(metadata.getColumnDefinition(LIST_COLUMN_NAME));
498        searchAction.addColumn(metadata.getColumnDefinition(LIST_COLUMN_DESCRIPTION));
499        metadata.setSearchAction(searchAction);
500    }
501
502    /**
503     * @see org.opencms.workplace.list.A_CmsListDialog#setMultiActions(org.opencms.workplace.list.CmsListMetadata)
504     */
505    @Override
506    protected void setMultiActions(CmsListMetadata metadata) {
507
508        // noop
509    }
510
511}