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.jsp.CmsJspActionElement;
031import org.opencms.main.CmsException;
032import org.opencms.main.OpenCms;
033import org.opencms.security.CmsRole;
034import org.opencms.widgets.CmsDisplayWidget;
035import org.opencms.workplace.CmsWidgetDialog;
036import org.opencms.workplace.CmsWidgetDialogParameter;
037
038import javax.servlet.http.HttpServletRequest;
039import javax.servlet.http.HttpServletResponse;
040import javax.servlet.jsp.PageContext;
041
042/**
043 * The role overview widget dialog.<p>
044 *
045 * @since 6.5.6
046 */
047public class CmsRoleOverviewDialog extends CmsWidgetDialog {
048
049    /** localized messages Keys prefix. */
050    public static final String KEY_PREFIX = "role";
051
052    /** Defines which pages are valid for this dialog. */
053    public static final String[] PAGES = {"page1"};
054
055    /** The role object that is viewed on this dialog. */
056    protected CmsRole m_role;
057
058    /** Stores the value of the request parameter for the organizational unit. */
059    private String m_paramOufqn;
060
061    /** Stores the value of the request parameter for the role name. */
062    private String m_paramRole;
063
064    /**
065     * Public constructor with JSP action element.<p>
066     *
067     * @param jsp an initialized JSP action element
068     */
069    public CmsRoleOverviewDialog(CmsJspActionElement jsp) {
070
071        super(jsp);
072
073    }
074
075    /**
076     * Public constructor with JSP variables.<p>
077     *
078     * @param context the JSP page context
079     * @param req the JSP request
080     * @param res the JSP response
081     */
082    public CmsRoleOverviewDialog(PageContext context, HttpServletRequest req, HttpServletResponse res) {
083
084        this(new CmsJspActionElement(context, req, res));
085    }
086
087    /**
088     * @see org.opencms.workplace.CmsWidgetDialog#actionCommit()
089     */
090    @Override
091    public void actionCommit() {
092
093        // noop
094    }
095
096    /**
097     * Returns a String inculding all parent roles of the role object.<p>
098     *
099     * @return a String inculding all parent roles of the role object
100     */
101    public String getDependency() {
102
103        String dependency = "";
104        CmsRole role = m_role;
105        while (role.getParentRole() != null) {
106            dependency = dependency + role.getParentRole().getName(getCms().getRequestContext().getLocale());
107            role = role.getParentRole();
108            if (role.getParentRole() != null) {
109                dependency = dependency + ", ";
110            }
111        }
112        return dependency;
113    }
114
115    /**
116     * Returns the localized description of the role object.<p>
117     *
118     * @return the localized description of the role object
119     */
120    public String getDescription() {
121
122        return m_role.getDescription(getCms().getRequestContext().getLocale());
123    }
124
125    /**
126     * Returns the localized name of the role object.<p>
127     *
128     * @return the localized name of the role object
129     */
130    public String getName() {
131
132        return m_role.getName(getCms().getRequestContext().getLocale());
133    }
134
135    /**
136     * Returns the organizational unit parameter value.<p>
137     *
138     * @return the organizational unit parameter value
139     */
140    public String getParamOufqn() {
141
142        return m_paramOufqn;
143    }
144
145    /**
146     * Returns the role name parameter value.<p>
147     *
148     * @return the role name parameter value
149     */
150    public String getParamRole() {
151
152        return m_paramRole;
153    }
154
155    /**
156     * This method is needed only for displaying reasons.<p>
157     *
158     * @param dependency nothing to do with this parameter
159     */
160    public void setDependency(String dependency) {
161
162        // nothing will be done here, just to avoid warnings
163        dependency.length();
164    }
165
166    /**
167     * This method is needed only for displaying reasons.<p>
168     *
169     * @param description nothing to do with this parameter
170     */
171    public void setDescription(String description) {
172
173        // nothing will be done here, just to avoid warnings
174        description.length();
175    }
176
177    /**
178     * This method is needed only for displaying reasons.<p>
179     *
180     * @param name nothing to do with this parameter
181     */
182    public void setName(String name) {
183
184        // nothing will be done here, just to avoid warnings
185        name.length();
186    }
187
188    /**
189     * Sets the user organizational unit value.<p>
190     *
191     * @param ouFqn the organizational unit parameter value
192     */
193    public void setParamOufqn(String ouFqn) {
194
195        if (ouFqn == null) {
196            ouFqn = "";
197        }
198        m_paramOufqn = ouFqn;
199    }
200
201    /**
202     * Sets the role name value.<p>
203     *
204     * @param role the role name parameter value
205     */
206    public void setParamRole(String role) {
207
208        m_paramRole = role;
209    }
210
211    /**
212     * Creates the dialog HTML for all defined widgets of the named dialog (page).<p>
213     *
214     * This overwrites the method from the super class to create a layout variation for the widgets.<p>
215     *
216     * @param dialog the dialog (page) to get the HTML for
217     * @return the dialog HTML for all defined widgets of the named dialog (page)
218     */
219    @Override
220    protected String createDialogHtml(String dialog) {
221
222        StringBuffer result = new StringBuffer(1024);
223
224        // create widget table
225        result.append(createWidgetTableStart());
226
227        // show error header once if there were validation errors
228        result.append(createWidgetErrorHeader());
229
230        if (dialog.equals(PAGES[0])) {
231            // create the widgets for the first dialog page
232            result.append(dialogBlockStart(key(Messages.GUI_ROLE_OVERVIEW_LABEL_IDENTIFICATION_BLOCK_0)));
233            result.append(createWidgetTableStart());
234            result.append(createDialogRowsHtml(0, 2));
235            result.append(createWidgetTableEnd());
236            result.append(dialogBlockEnd());
237        }
238
239        // close widget table
240        result.append(createWidgetTableEnd());
241
242        return result.toString();
243    }
244
245    /**
246     * @see org.opencms.workplace.CmsWidgetDialog#defaultActionHtmlEnd()
247     */
248    @Override
249    protected String defaultActionHtmlEnd() {
250
251        return "";
252    }
253
254    /**
255     * @see org.opencms.workplace.CmsWidgetDialog#defineWidgets()
256     */
257    @Override
258    protected void defineWidgets() {
259
260        // initialize the user object to use for the dialog
261        initRoleObject();
262
263        setKeyPrefix(KEY_PREFIX);
264
265        // widgets to display
266        addWidget(new CmsWidgetDialogParameter(this, "name", PAGES[0], new CmsDisplayWidget()));
267        addWidget(new CmsWidgetDialogParameter(this, "dependency", PAGES[0], new CmsDisplayWidget()));
268        addWidget(new CmsWidgetDialogParameter(this, "description", PAGES[0], new CmsDisplayWidget()));
269    }
270
271    /**
272     * @see org.opencms.workplace.CmsWidgetDialog#getPageArray()
273     */
274    @Override
275    protected String[] getPageArray() {
276
277        return PAGES;
278    }
279
280    /**
281     * Initializes the group object.<p>
282     */
283    protected void initRoleObject() {
284
285        try {
286            m_role = CmsRole.valueOf(getCms().readGroup(m_paramRole));
287        } catch (CmsException e) {
288            // noop
289        }
290    }
291
292    /**
293     * @see org.opencms.workplace.CmsWidgetDialog#validateParamaters()
294     */
295    @Override
296    protected void validateParamaters() throws Exception {
297
298        // test the needed parameters
299        OpenCms.getRoleManager().checkRole(getCms(), CmsRole.ACCOUNT_MANAGER.forOrgUnit(getParamOufqn()));
300        CmsRole.valueOf(getCms().readGroup(getParamRole())).getGroupName();
301    }
302}