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.workplace.CmsDialog;
036import org.opencms.workplace.list.CmsListColumnAlignEnum;
037import org.opencms.workplace.list.CmsListColumnDefinition;
038import org.opencms.workplace.list.CmsListDefaultAction;
039import org.opencms.workplace.list.CmsListDirectAction;
040import org.opencms.workplace.list.CmsListMetadata;
041
042import java.io.IOException;
043import java.util.HashMap;
044import java.util.List;
045import java.util.Map;
046
047import javax.servlet.ServletException;
048import javax.servlet.http.HttpServletRequest;
049import javax.servlet.http.HttpServletResponse;
050import javax.servlet.jsp.PageContext;
051
052/**
053 * Admin organization unit management view.<p>
054 *
055 * @since 6.5.6
056 */
057public class CmsOrgUnitsAdminList extends A_CmsOrgUnitsList {
058
059    /** list id constant. */
060    public static final String LIST_ID = "lsoua";
061
062    /** list action id constant. */
063    protected static final String LIST_ACTION_OVERVIEW = "ao";
064
065    /** list column id constant. */
066    protected static final String LIST_COLUMN_OVERVIEW = "co";
067
068    /**
069     * Public constructor.<p>
070     *
071     * @param jsp an initialized JSP action element
072     */
073    public CmsOrgUnitsAdminList(CmsJspActionElement jsp) {
074
075        super(jsp, LIST_ID, Messages.get().container(Messages.GUI_ADMINORGUNITS_LIST_NAME_0));
076    }
077
078    /**
079     * Public constructor with JSP variables.<p>
080     *
081     * @param context the JSP page context
082     * @param req the JSP request
083     * @param res the JSP response
084     */
085    public CmsOrgUnitsAdminList(PageContext context, HttpServletRequest req, HttpServletResponse res) {
086
087        this(new CmsJspActionElement(context, req, res));
088    }
089
090    /**
091     *
092     * @see org.opencms.workplace.list.A_CmsListDialog#defaultActionHtml()
093     */
094    @Override
095    public String defaultActionHtml() {
096
097        if ((getList() != null) && getList().getAllContent().isEmpty()) {
098            // TODO: check the need for this
099            refreshList();
100        }
101        StringBuffer result = new StringBuffer(2048);
102        result.append(defaultActionHtmlStart());
103        result.append(customHtmlStart());
104        try {
105
106            if (hasMoreAdminOUs()) {
107                result.append(defaultActionHtmlContent());
108            }
109        } catch (CmsException e) {
110            // noop
111        }
112        result.append(customHtmlEnd());
113        result.append(defaultActionHtmlEnd());
114        return result.toString();
115    }
116
117    /**
118     * @see org.opencms.workplace.list.A_CmsListDialog#executeListSingleActions()
119     */
120    @Override
121    public void executeListSingleActions() throws IOException, ServletException {
122
123        String ouFqn = getSelectedItem().get(LIST_COLUMN_NAME).toString();
124        if (ouFqn == null) {
125            ouFqn = "";
126        }
127        Map<String, String[]> params = new HashMap<String, String[]>();
128        params.put(A_CmsOrgUnitDialog.PARAM_OUFQN, new String[] {ouFqn.substring(1)});
129        params.put(CmsDialog.PARAM_ACTION, new String[] {CmsDialog.DIALOG_INITIAL});
130        if (getParamListAction().equals(LIST_ACTION_OVERVIEW)) {
131            // forward to the edit user screen
132            getToolManager().jspForwardTool(this, getCurrentToolPath() + "/orgunit", params);
133        } else if (getParamListAction().equals(LIST_ACTION_USER)) {
134            // forward to the edit user screen
135            getToolManager().jspForwardTool(this, getUsersToolPath(), params);
136        } else if (getParamListAction().equals(LIST_ACTION_GROUP)) {
137            // forward to the edit user screen
138            getToolManager().jspForwardTool(this, getGroupsToolPath(), params);
139        } else if (getParamListAction().equals(LIST_DEFACTION_OVERVIEW)) {
140            // forward to the edit user screen
141            getToolManager().jspForwardTool(this, getCurrentToolPath() + "/orgunit", params);
142        } else {
143            throwListUnsupportedActionException();
144        }
145        listSave();
146    }
147
148    /**
149     * Performs a forward to the overview of the single organizational unit the current user
150     * is allowed to administrate.<p>
151     *
152     * @throws ServletException in case of errors during forwarding
153     * @throws IOException in case of errors during forwarding
154     * @throws CmsException in case of errors during getting orgunits
155     */
156    public void forwardToSingleAdminOU() throws ServletException, IOException, CmsException {
157
158        List<CmsOrganizationalUnit> orgUnits = getOrgUnits();
159
160        if (orgUnits.isEmpty()) {
161            OpenCms.getWorkplaceManager().getToolManager().jspForwardTool(this, "/", null);
162            return;
163        }
164
165        Map<String, String[]> params = new HashMap<String, String[]>();
166        params.put(A_CmsOrgUnitDialog.PARAM_OUFQN, new String[] {orgUnits.get(0).getName()});
167        params.put(CmsDialog.PARAM_ACTION, new String[] {CmsDialog.DIALOG_INITIAL});
168
169        OpenCms.getWorkplaceManager().getToolManager().jspForwardTool(this, getForwardToolPath(), params);
170    }
171
172    /**
173     * Returns the path of the overview icon.<p>
174     *
175     * @return the path of the overview icon
176     */
177    public String getOverviewIcon() {
178
179        return PATH_BUTTONS + "orgunit.png";
180    }
181
182    /**
183     * Checks if the user has more then one organizational unit to administrate.<p>
184     *
185     * @return true if the user has more then then one organizational unit to administrate
186     *         otherwise false
187     * @throws CmsException if the organizational units can not be read
188     */
189    public boolean hasMoreAdminOUs() throws CmsException {
190
191        List<CmsOrganizationalUnit> orgUnits = getOrgUnits();
192
193        if (orgUnits == null) {
194            return false;
195        }
196        if (orgUnits.size() <= 1) {
197            return false;
198        }
199        return true;
200    }
201
202    /**
203     * Returns the tool path to forward if there is only one single organizational unit.<p>
204     *
205     * @return the tool path to forward
206     */
207    protected String getForwardToolPath() {
208
209        return "/accounts/orgunit";
210    }
211
212    /**
213     * Returns the tool path of the groups management tool.<p>
214     *
215     * @return the tool path of the groups management tool
216     */
217    protected String getGroupsToolPath() {
218
219        return getCurrentToolPath() + "/orgunit/groups";
220    }
221
222    /**
223     * Returns the tool path of the users management tool.<p>
224     *
225     * @return the tool path of the users management tool
226     */
227    protected String getUsersToolPath() {
228
229        return getCurrentToolPath() + "/orgunit/users";
230    }
231
232    /**
233     * @see org.opencms.workplace.list.A_CmsListDialog#setColumns(org.opencms.workplace.list.CmsListMetadata)
234     */
235    @Override
236    protected void setColumns(CmsListMetadata metadata) {
237
238        // create column for overview
239        CmsListColumnDefinition overviewCol = new CmsListColumnDefinition(LIST_COLUMN_OVERVIEW);
240        overviewCol.setName(Messages.get().container(Messages.GUI_ORGUNITS_LIST_COLS_OVERVIEW_0));
241        overviewCol.setHelpText(Messages.get().container(Messages.GUI_ORGUNITS_LIST_COLS_OVERVIEW_HELP_0));
242        overviewCol.setWidth("20");
243        overviewCol.setAlign(CmsListColumnAlignEnum.ALIGN_CENTER);
244        overviewCol.setSorteable(false);
245        // add overview action
246        CmsListDirectAction overviewAction = new CmsListDirectAction(LIST_ACTION_OVERVIEW) {
247
248            /**
249             * @see org.opencms.workplace.tools.A_CmsHtmlIconButton#getIconPath()
250             */
251            @Override
252            public String getIconPath() {
253
254                if (getItem() != null) {
255                    if (((Boolean)getItem().get(LIST_COLUMN_WEBUSER)).booleanValue()) {
256                        return PATH_BUTTONS + "webuser_ou.png";
257                    }
258                }
259                return super.getIconPath();
260            }
261
262            /**
263             * @see org.opencms.workplace.tools.A_CmsHtmlIconButton#getName()
264             */
265            @Override
266            public CmsMessageContainer getName() {
267
268                if (getItem() != null) {
269                    if (((Boolean)getItem().get(LIST_COLUMN_WEBUSER)).booleanValue()) {
270                        return Messages.get().container(Messages.GUI_WEBOUS_LIST_ACTION_OVERVIEW_NAME_0);
271                    }
272                }
273                return super.getName();
274            }
275        };
276        overviewAction.setName(Messages.get().container(Messages.GUI_ORGUNITS_LIST_ACTION_OVERVIEW_NAME_0));
277        overviewAction.setHelpText(Messages.get().container(Messages.GUI_ORGUNITS_LIST_COLS_OVERVIEW_HELP_0));
278        overviewAction.setIconPath(getOverviewIcon());
279        overviewCol.addDirectAction(overviewAction);
280        // add it to the list definition
281        metadata.addColumn(overviewCol);
282
283        // create column for user
284        CmsListColumnDefinition userCol = new CmsListColumnDefinition(LIST_COLUMN_USER);
285        userCol.setName(Messages.get().container(Messages.GUI_ORGUNITS_LIST_COLS_USER_0));
286        userCol.setHelpText(Messages.get().container(Messages.GUI_ORGUNITS_LIST_COLS_USER_HELP_0));
287        userCol.setWidth("20");
288        userCol.setAlign(CmsListColumnAlignEnum.ALIGN_CENTER);
289        userCol.setSorteable(false);
290        // add user action
291        CmsListDirectAction userAction = new CmsListDirectAction(LIST_ACTION_USER);
292        userAction.setName(Messages.get().container(Messages.GUI_ORGUNITS_LIST_ACTION_USER_NAME_0));
293        userAction.setHelpText(Messages.get().container(Messages.GUI_ORGUNITS_LIST_COLS_USER_HELP_0));
294        userAction.setIconPath(getUserIcon());
295        userCol.addDirectAction(userAction);
296        // add it to the list definition
297        metadata.addColumn(userCol);
298
299        // create column for group
300        CmsListColumnDefinition groupCol = new CmsListColumnDefinition(LIST_COLUMN_GROUP);
301        groupCol.setName(Messages.get().container(Messages.GUI_ORGUNITS_LIST_COLS_GROUP_0));
302        groupCol.setHelpText(Messages.get().container(Messages.GUI_ORGUNITS_LIST_COLS_GROUP_HELP_0));
303        groupCol.setWidth("20");
304        groupCol.setAlign(CmsListColumnAlignEnum.ALIGN_CENTER);
305        groupCol.setSorteable(false);
306        // add group action
307        CmsListDirectAction groupAction = new CmsListDirectAction(LIST_ACTION_GROUP);
308        groupAction.setName(Messages.get().container(Messages.GUI_ORGUNITS_LIST_ACTION_GROUP_NAME_0));
309        groupAction.setHelpText(Messages.get().container(Messages.GUI_ORGUNITS_LIST_COLS_GROUP_HELP_0));
310        groupAction.setIconPath(getGroupIcon());
311        groupCol.addDirectAction(groupAction);
312        // add it to the list definition
313        metadata.addColumn(groupCol);
314
315        // create column for description
316        CmsListColumnDefinition descCol = new CmsListColumnDefinition(LIST_COLUMN_DESCRIPTION);
317        descCol.setName(Messages.get().container(Messages.GUI_ORGUNITS_LIST_COLS_DESCRIPTION_0));
318        descCol.setWidth("60%");
319        descCol.setSorteable(true);
320        // create default overview action
321        CmsListDefaultAction defOverviewAction = new CmsListDefaultAction(LIST_DEFACTION_OVERVIEW);
322        defOverviewAction.setName(Messages.get().container(Messages.GUI_ORGUNITS_LIST_DEFACTION_OVERVIEW_NAME_0));
323        defOverviewAction.setHelpText(Messages.get().container(Messages.GUI_ORGUNITS_LIST_COLS_OVERVIEW_HELP_0));
324        descCol.addDefaultAction(defOverviewAction);
325        // add it to the list definition
326        metadata.addColumn(descCol);
327
328        // create column for name / path
329        CmsListColumnDefinition nameCol = new CmsListColumnDefinition(LIST_COLUMN_NAME);
330        nameCol.setName(Messages.get().container(Messages.GUI_ORGUNITS_LIST_COLS_NAME_0));
331        nameCol.setWidth("40%");
332        nameCol.setSorteable(true);
333        // add it to the list definition
334        metadata.addColumn(nameCol);
335
336        // create column for manageable flag
337        CmsListColumnDefinition adminCol = new CmsListColumnDefinition(LIST_COLUMN_ADMIN);
338        adminCol.setVisible(false);
339        metadata.addColumn(adminCol);
340
341        // create column for webuser flag
342        CmsListColumnDefinition webuserCol = new CmsListColumnDefinition(LIST_COLUMN_WEBUSER);
343        webuserCol.setVisible(false);
344        metadata.addColumn(webuserCol);
345    }
346
347    /**
348     * @see org.opencms.workplace.list.A_CmsListDialog#validateParamaters()
349     */
350    @Override
351    protected void validateParamaters() throws Exception {
352
353        if (Boolean.parseBoolean(getParamForce())) {
354            if (!hasMoreAdminOUs()) {
355                // jump one level higher
356                throw new Exception();
357            }
358        }
359    }
360}