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.commons;
029
030import org.opencms.jsp.CmsJspActionElement;
031import org.opencms.main.CmsException;
032import org.opencms.main.OpenCms;
033import org.opencms.security.CmsOrganizationalUnit;
034import org.opencms.security.CmsRole;
035import org.opencms.workplace.list.A_CmsListDefaultJsAction;
036import org.opencms.workplace.list.A_CmsListDialog;
037import org.opencms.workplace.list.CmsListColumnAlignEnum;
038import org.opencms.workplace.list.CmsListColumnDefinition;
039import org.opencms.workplace.list.CmsListDefaultAction;
040import org.opencms.workplace.list.CmsListDirectAction;
041import org.opencms.workplace.list.CmsListItem;
042import org.opencms.workplace.list.CmsListMetadata;
043import org.opencms.workplace.list.CmsListOrderEnum;
044import org.opencms.workplace.list.CmsListSearchAction;
045import org.opencms.workplace.tools.CmsToolMacroResolver;
046
047import java.util.ArrayList;
048import java.util.Iterator;
049import java.util.List;
050
051import javax.servlet.http.HttpServletRequest;
052import javax.servlet.http.HttpServletResponse;
053import javax.servlet.jsp.PageContext;
054
055/**
056 * Organizational unit selection dialog.<p>
057 *
058 * @since 6.5.6
059 */
060public class CmsOrgUnitSelectionList 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_SELECT = "js";
067
068    /** list column id constant. */
069    public static final String LIST_COLUMN_DESCRIPTION = "cd";
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_PATH = "cp";
076
077    /** list id constant. */
078    public static final String LIST_ID = "lous";
079
080    /** Stores the value of the request parameter for the organizational unit fqn. */
081    private String m_paramOufqn;
082
083    /** Stores the value of the request parameter for the role group name to use to filter the selection. */
084    private String m_paramRole;
085
086    /**
087     * Public constructor.<p>
088     *
089     * @param jsp an initialized JSP action element
090     */
091    public CmsOrgUnitSelectionList(CmsJspActionElement jsp) {
092
093        super(
094            jsp,
095            LIST_ID,
096            Messages.get().container(Messages.GUI_ORGUNITSELECTION_LIST_NAME_0),
097            LIST_COLUMN_PATH,
098            CmsListOrderEnum.ORDER_ASCENDING,
099            null);
100    }
101
102    /**
103     * Public constructor with JSP variables.<p>
104     *
105     * @param context the JSP page context
106     * @param req the JSP request
107     * @param res the JSP response
108     */
109    public CmsOrgUnitSelectionList(PageContext context, HttpServletRequest req, HttpServletResponse res) {
110
111        this(new CmsJspActionElement(context, req, res));
112    }
113
114    /**
115     * @see org.opencms.workplace.tools.CmsToolDialog#dialogTitle()
116     */
117    @Override
118    public String dialogTitle() {
119
120        // build title
121        StringBuffer html = new StringBuffer(512);
122        html.append("<div class='screenTitle'>\n");
123        html.append("\t<table width='100%' cellspacing='0'>\n");
124        html.append("\t\t<tr>\n");
125        html.append("\t\t\t<td>\n");
126        html.append(key(Messages.GUI_ORGUNITSELECTION_INTRO_TITLE_0));
127        html.append("\n\t\t\t</td>");
128        html.append("\t\t</tr>\n");
129        html.append("\t</table>\n");
130        html.append("</div>\n");
131        return CmsToolMacroResolver.resolveMacros(html.toString(), this);
132    }
133
134    /**
135     * @see org.opencms.workplace.list.A_CmsListDialog#executeListMultiActions()
136     */
137    @Override
138    public void executeListMultiActions() {
139
140        throwListUnsupportedActionException();
141    }
142
143    /**
144     * @see org.opencms.workplace.list.A_CmsListDialog#executeListSingleActions()
145     */
146    @Override
147    public void executeListSingleActions() {
148
149        throwListUnsupportedActionException();
150    }
151
152    /**
153     * Returns the organizational unit fqn parameter value.<p>
154     *
155     * @return the organizational unit fqn parameter value
156     */
157    public String getParamOufqn() {
158
159        return m_paramOufqn;
160    }
161
162    /**
163     * Returns the value of the role parameter.<p>
164     *
165     * @return the value of the role parameter
166     */
167    public String getParamRole() {
168
169        return m_paramRole;
170    }
171
172    /**
173     * Sets the organizational unit fqn parameter value.<p>
174     *
175     * @param ouFqn the organizational unit fqn parameter value
176     */
177    public void setParamOufqn(String ouFqn) {
178
179        if (ouFqn == null) {
180            ouFqn = "";
181        }
182        m_paramOufqn = ouFqn;
183    }
184
185    /**
186     * Sets the value of the role parameter.<p>
187     *
188     * @param paramRole the value to set
189     */
190    public void setParamRole(String paramRole) {
191
192        m_paramRole = paramRole;
193    }
194
195    /**
196     * @see org.opencms.workplace.list.A_CmsListDialog#fillDetails(java.lang.String)
197     */
198    @Override
199    protected void fillDetails(String detailId) {
200
201        // noop
202    }
203
204    /**
205     * @see org.opencms.workplace.list.A_CmsListDialog#getListItems()
206     */
207    @Override
208    protected List<CmsListItem> getListItems() throws CmsException {
209
210        List<CmsListItem> ret = new ArrayList<CmsListItem>();
211        Iterator<CmsOrganizationalUnit> itOrgUnits = getOrgUnits().iterator();
212        while (itOrgUnits.hasNext()) {
213            CmsOrganizationalUnit orgUnit = itOrgUnits.next();
214            CmsListItem item = getList().newItem(orgUnit.getName());
215            item.set(LIST_COLUMN_DESCRIPTION, orgUnit.getDescription(getLocale()));
216            item.set(LIST_COLUMN_PATH, CmsOrganizationalUnit.SEPARATOR + orgUnit.getName());
217            ret.add(item);
218        }
219
220        return ret;
221    }
222
223    /**
224     * Returns the list of organizational units for selection.<p>
225     *
226     * @return a list of organizational units
227     *
228     * @throws CmsException if something goes wrong
229     */
230    protected List<CmsOrganizationalUnit> getOrgUnits() throws CmsException {
231
232        List<CmsOrganizationalUnit> ret = new ArrayList<CmsOrganizationalUnit>();
233        CmsRole role = null;
234        if (getParamRole() != null) {
235            role = CmsRole.valueOfGroupName(getParamRole());
236        }
237        String ou = getParamOufqn();
238        if (ou == null) {
239            ou = "";
240        }
241        if (role != null) {
242            ret.addAll(OpenCms.getRoleManager().getOrgUnitsForRole(getCms(), role.forOrgUnit(ou), true));
243        } else {
244            ret.addAll(OpenCms.getOrgUnitManager().getOrganizationalUnits(getCms(), ou, true));
245        }
246        return ret;
247    }
248
249    /**
250     * @see org.opencms.workplace.list.A_CmsListDialog#setColumns(org.opencms.workplace.list.CmsListMetadata)
251     */
252    @Override
253    protected void setColumns(CmsListMetadata metadata) {
254
255        // create column for icon display
256        CmsListColumnDefinition iconCol = new CmsListColumnDefinition(LIST_COLUMN_ICON);
257        iconCol.setName(Messages.get().container(Messages.GUI_ORGUNITSELECTION_LIST_COLS_ICON_0));
258        iconCol.setHelpText(Messages.get().container(Messages.GUI_ORGUNITSELECTION_LIST_COLS_ICON_HELP_0));
259        iconCol.setWidth("20");
260        iconCol.setAlign(CmsListColumnAlignEnum.ALIGN_CENTER);
261        iconCol.setSorteable(false);
262        // set icon action
263        CmsListDirectAction iconAction = new CmsListDirectAction(LIST_ACTION_ICON);
264        iconAction.setName(Messages.get().container(Messages.GUI_ORGUNITSELECTION_LIST_ICON_NAME_0));
265        iconAction.setHelpText(Messages.get().container(Messages.GUI_ORGUNITSELECTION_LIST_ICON_HELP_0));
266        iconAction.setIconPath("buttons/orgunit.png");
267        iconAction.setEnabled(false);
268        iconCol.addDirectAction(iconAction);
269        // add it to the list definition
270        metadata.addColumn(iconCol);
271
272        // create column for description
273        CmsListColumnDefinition descCol = new CmsListColumnDefinition(LIST_COLUMN_DESCRIPTION);
274        descCol.setName(Messages.get().container(Messages.GUI_ORGUNITSELECTION_LIST_COLS_DESCRIPTION_0));
275        descCol.setWidth("60%");
276        CmsListDefaultAction selectAction = new A_CmsListDefaultJsAction(LIST_ACTION_SELECT) {
277
278            /**
279             * @see org.opencms.workplace.list.A_CmsListDirectJsAction#jsCode()
280             */
281            @Override
282            public String jsCode() {
283
284                return "window.opener.setOrgUnitFormValue('"
285                    + getItem().get(LIST_COLUMN_PATH)
286                    + "'); window.opener.focus(); window.close();";
287            }
288        };
289        selectAction.setName(Messages.get().container(Messages.GUI_ORGUNITSELECTION_LIST_ACTION_SELECT_NAME_0));
290        selectAction.setHelpText(Messages.get().container(Messages.GUI_ORGUNITSELECTION_LIST_ACTION_SELECT_HELP_0));
291        descCol.addDefaultAction(selectAction);
292        // add it to the list definition
293        metadata.addColumn(descCol);
294
295        // create column for path
296        CmsListColumnDefinition pathCol = new CmsListColumnDefinition(LIST_COLUMN_PATH);
297        pathCol.setName(Messages.get().container(Messages.GUI_ORGUNITSELECTION_LIST_COLS_PATH_0));
298        pathCol.setWidth("40%");
299        pathCol.setTextWrapping(true);
300        // add it to the list definition
301        metadata.addColumn(pathCol);
302    }
303
304    /**
305     * @see org.opencms.workplace.list.A_CmsListDialog#setIndependentActions(org.opencms.workplace.list.CmsListMetadata)
306     */
307    @Override
308    protected void setIndependentActions(CmsListMetadata metadata) {
309
310        CmsListSearchAction searchAction = new CmsListSearchAction(metadata.getColumnDefinition(LIST_COLUMN_PATH));
311        searchAction.addColumn(metadata.getColumnDefinition(LIST_COLUMN_DESCRIPTION));
312        searchAction.setCaseInSensitive(true);
313        metadata.setSearchAction(searchAction);
314    }
315
316    /**
317     * @see org.opencms.workplace.list.A_CmsListDialog#setMultiActions(org.opencms.workplace.list.CmsListMetadata)
318     */
319    @Override
320    protected void setMultiActions(CmsListMetadata metadata) {
321
322        // no-op
323    }
324
325    /**
326     * @see org.opencms.workplace.list.A_CmsListDialog#validateParamaters()
327     */
328    @Override
329    protected void validateParamaters() throws Exception {
330
331        try {
332            OpenCms.getOrgUnitManager().readOrganizationalUnit(getCms(), getParamOufqn());
333        } catch (Exception e) {
334            setParamOufqn(null);
335        }
336    }
337}