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.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.util.CmsStringUtil;
037import org.opencms.widgets.CmsSelectWidgetOption;
038import org.opencms.workplace.CmsWidgetDialog;
039
040import java.io.IOException;
041import java.util.ArrayList;
042import java.util.Collections;
043import java.util.Comparator;
044import java.util.Iterator;
045import java.util.List;
046
047import javax.servlet.ServletException;
048import javax.servlet.http.HttpServletRequest;
049import javax.servlet.http.HttpServletResponse;
050import javax.servlet.jsp.PageContext;
051
052/**
053 * Abstract dialog class to import and export user data.<p>
054 *
055 * @since 6.7.1
056 */
057public abstract class A_CmsUserDataImexportDialog extends CmsWidgetDialog {
058
059    /** Defines which pages are valid for this dialog. */
060    public static final String[] PAGES = {"page1"};
061
062    /** List of groups. */
063    private List<CmsGroup> m_groups;
064
065    /** Stores the value of the request parameter for the organizational unit fqn. */
066    private String m_paramOufqn;
067
068    /** List of roles. */
069    private List<CmsRole> m_roles;
070
071    /**
072     * Public constructor with JSP action element.<p>
073     *
074     * @param jsp an initialized JSP action element
075     */
076    public A_CmsUserDataImexportDialog(CmsJspActionElement jsp) {
077
078        super(jsp);
079    }
080
081    /**
082     * Public constructor with JSP variables.<p>
083     *
084     * @param context the JSP page context
085     * @param req the JSP request
086     * @param res the JSP response
087     */
088    public A_CmsUserDataImexportDialog(PageContext context, HttpServletRequest req, HttpServletResponse res) {
089
090        this(new CmsJspActionElement(context, req, res));
091    }
092
093    /**
094     * @see org.opencms.workplace.CmsWidgetDialog#actionCommit()
095     */
096    @Override
097    public abstract void actionCommit() throws IOException, ServletException;
098
099    /**
100     * Returns the list of groups.<p>
101     *
102     * @return the list of groups
103     */
104    public List<CmsGroup> getGroups() {
105
106        return m_groups;
107    }
108
109    /**
110     * Returns the organizational unit fqn parameter value.<p>
111     *
112     * @return the organizational unit fqn parameter value
113     */
114    public String getParamOufqn() {
115
116        return m_paramOufqn;
117    }
118
119    /**
120     * Returns the list of roles to export.<p>
121     *
122     * @return the list of roles to export
123     */
124    public List<CmsRole> getRoles() {
125
126        return m_roles;
127    }
128
129    /**
130     * Sets the groups list.<p>
131     *
132     * @param groups the groups list
133     */
134    public void setGroups(List<CmsGroup> groups) {
135
136        m_groups = groups;
137    }
138
139    /**
140     * Sets the organizational unit fqn parameter value.<p>
141     *
142     * @param ouFqn the organizational unit fqn parameter value
143     */
144    public void setParamOufqn(String ouFqn) {
145
146        if (ouFqn == null) {
147            ouFqn = "";
148        }
149        m_paramOufqn = ouFqn;
150    }
151
152    /**
153     * Sets the roles list.<p>
154     *
155     * @param roles the roles list
156     */
157    public void setRoles(List<CmsRole> roles) {
158
159        m_roles = roles;
160    }
161
162    /**
163     * @see org.opencms.workplace.CmsWidgetDialog#defineWidgets()
164     */
165    @Override
166    protected abstract void defineWidgets();
167
168    /**
169     * @see org.opencms.workplace.CmsWidgetDialog#getPageArray()
170     */
171    @Override
172    protected String[] getPageArray() {
173
174        return PAGES;
175    }
176
177    /**
178     * Returns the role names to show in the select box.<p>
179     *
180     * @return the role names to show in the select box
181     */
182    protected List<CmsSelectWidgetOption> getSelectRoles() {
183
184        List<CmsSelectWidgetOption> retVal = new ArrayList<CmsSelectWidgetOption>();
185
186        try {
187            boolean inRootOu = CmsStringUtil.isEmptyOrWhitespaceOnly(getParamOufqn())
188                || CmsOrganizationalUnit.SEPARATOR.equals(getParamOufqn());
189            List<CmsRole> roles = OpenCms.getRoleManager().getRolesOfUser(
190                getCms(),
191                getCms().getRequestContext().getCurrentUser().getName(),
192                getParamOufqn(),
193                false,
194                false,
195                false);
196            Iterator<CmsRole> itRoles = roles.iterator();
197            while (itRoles.hasNext()) {
198                CmsRole role = itRoles.next();
199                if (role.isOrganizationalUnitIndependent() && !inRootOu) {
200                    continue;
201                }
202                retVal.add(new CmsSelectWidgetOption(role.getGroupName(), false, role.getName(getLocale())));
203            }
204        } catch (CmsException e) {
205            // noop
206        }
207        Collections.sort(retVal, new Comparator<CmsSelectWidgetOption>() {
208
209            public int compare(CmsSelectWidgetOption arg0, CmsSelectWidgetOption arg1) {
210
211                return arg0.getOption().compareTo(arg1.getOption());
212            }
213
214        });
215        return retVal;
216    }
217}