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.CmsRuntimeException;
033import org.opencms.util.CmsStringUtil;
034import org.opencms.util.CmsUUID;
035import org.opencms.workplace.CmsDialog;
036import org.opencms.workplace.CmsWorkplaceSettings;
037import org.opencms.workplace.list.CmsHtmlList;
038
039import java.io.IOException;
040import java.util.HashMap;
041import java.util.Iterator;
042import java.util.List;
043import java.util.Map;
044
045import javax.servlet.ServletException;
046import javax.servlet.http.HttpServletRequest;
047import javax.servlet.http.HttpServletResponse;
048import javax.servlet.jsp.JspException;
049import javax.servlet.jsp.PageContext;
050
051/**
052 * User dependencies list view including delete and transfer functionality. <p>
053 *
054 * Displays the dependencies of a user or a list of user.<p>
055 *
056 * @since 6.0.0
057 */
058public class CmsUserDependenciesList extends CmsUserPrincipalDependenciesList {
059
060    /** Value for the delete action. */
061    public static final int ACTION_DELETE = 121;
062
063    /** Value for the transfer action. */
064    public static final int ACTION_TRANSFER = 122;
065
066    /** Request parameter value for the delete action. */
067    public static final String DELETE_ACTION = "delete";
068
069    /** Request parameter name for the user id, could be a list of ids. */
070    public static final String PARAM_USERID = "userid";
071
072    /** Path to the list buttons. */
073    public static final String PATH_BUTTONS = "tools/accounts/buttons/";
074
075    /** Request parameter value for the transfer action. */
076    public static final String TRANSFER_ACTION = "transfer";
077
078    /** Stores the value of the user name, could be a list of names. */
079    private String m_userName;
080
081    /**
082     * Public constructor.<p>
083     *
084     * @param jsp an initialized JSP action element
085     */
086    public CmsUserDependenciesList(CmsJspActionElement jsp) {
087
088        this(LIST_ID, jsp);
089        m_showAttributes = true;
090    }
091
092    /**
093     * Public constructor with JSP variables.<p>
094     *
095     * @param context the JSP page context
096     * @param req the JSP request
097     * @param res the JSP response
098     */
099    public CmsUserDependenciesList(PageContext context, HttpServletRequest req, HttpServletResponse res) {
100
101        this(new CmsJspActionElement(context, req, res));
102        m_showAttributes = true;
103    }
104
105    /**
106     * Protected constructor.<p>
107     *
108     * @param listId the id of the specialized list
109     * @param jsp an initialized JSP action element
110     */
111    protected CmsUserDependenciesList(String listId, CmsJspActionElement jsp) {
112
113        super(listId, jsp);
114        m_showAttributes = true;
115    }
116
117    /**
118     * @see org.opencms.workplace.list.A_CmsListDialog#actionDialog()
119     */
120    @Override
121    public void actionDialog() throws JspException, ServletException, IOException {
122
123        switch (getAction()) {
124            case ACTION_DELETE:
125                Iterator it = CmsStringUtil.splitAsList(getUserName(), CmsHtmlList.ITEM_SEPARATOR, true).iterator();
126                while (it.hasNext()) {
127                    String name = (String)it.next();
128                    try {
129                        getCms().deleteUser(name);
130                    } catch (CmsException e) {
131                        throw new CmsRuntimeException(e.getMessageContainer(), e);
132                    }
133                }
134                setAction(ACTION_CANCEL);
135                actionCloseDialog();
136                break;
137            case ACTION_TRANSFER:
138                Map params = new HashMap();
139                // set action parameter to initial dialog call
140                params.put(CmsDialog.PARAM_ACTION, CmsDialog.DIALOG_INITIAL);
141                // forward to the select replacement screen
142                params.put(PARAM_USERID, getParamUserid());
143                getToolManager().jspForwardPage(
144                    this,
145                    getJsp().getRequestContext().getFolderUri() + "user_transfer.jsp",
146                    params);
147                break;
148
149            default:
150                super.actionDialog();
151        }
152    }
153
154    /**
155     * @see org.opencms.workplace.list.A_CmsListDialog#defaultActionHtmlContent()
156     */
157    @Override
158    public String defaultActionHtmlContent() {
159
160        if (getList().getTotalSize() > 0) {
161            return super.defaultActionHtmlContent();
162        }
163        return "";
164    }
165
166    /**
167     * Returns the user Name.<p>
168     *
169     * @return the user Name
170     */
171    public String getUserName() {
172
173        return m_userName;
174    }
175
176    /**
177     * @see org.opencms.workplace.list.A_CmsListDialog#customHtmlEnd()
178     */
179    @Override
180    protected String customHtmlEnd() {
181
182        StringBuffer result = new StringBuffer(512);
183        result.append(super.customHtmlEnd());
184        result.append("<form name='actions' method='post' action='");
185        result.append(getDialogRealUri());
186        result.append("' class='nomargin' onsubmit=\"return submitAction('ok', null, 'actions');\">\n");
187        result.append(allParamsAsHidden());
188        result.append(dialogButtonRow(HTML_START));
189        result.append("<input name='");
190        result.append(DELETE_ACTION);
191        result.append("' type='button' value='");
192        result.append(Messages.get().container(Messages.GUI_DEPENDENCIES_BUTTON_DELETE_0).key(getLocale()));
193        result.append("' onclick=\"submitAction('");
194        result.append(DELETE_ACTION);
195        result.append("', form);\" class='dialogbutton'>\n");
196        if (getList().getTotalSize() > 0) {
197            result.append("<input name='");
198            result.append(TRANSFER_ACTION);
199            result.append("' type='button' value='");
200            result.append(Messages.get().container(Messages.GUI_DEPENDENCIES_BUTTON_TRANSFER_0).key(getLocale()));
201            result.append("' onclick=\"submitAction('");
202            result.append(TRANSFER_ACTION);
203            result.append("', form);\" class='dialogbutton'>\n");
204        }
205        dialogButtonsHtml(result, BUTTON_CANCEL, "");
206        result.append(dialogButtonRow(HTML_END));
207        result.append("</form>\n");
208        return result.toString();
209    }
210
211    /**
212     * @see org.opencms.workplace.list.A_CmsListDialog#customHtmlStart()
213     */
214    @Override
215    protected String customHtmlStart() {
216
217        StringBuffer result = new StringBuffer(512);
218        result.append(dialogBlockStart(key(Messages.GUI_USER_DEPENDENCIES_NOTICE_0)));
219        result.append("\n");
220        if (getCurrentToolPath().indexOf("/edit/") < 0) {
221            result.append(key(Messages.GUI_USER_DEPENDENCIES_SELECTED_USERS_0));
222            result.append(":<br>\n");
223            List users = CmsStringUtil.splitAsList(getUserName(), CmsHtmlList.ITEM_SEPARATOR, true);
224            result.append("<ul>\n");
225            Iterator it = users.iterator();
226            while (it.hasNext()) {
227                String name = (String)it.next();
228                result.append("<li>");
229                result.append(name);
230                result.append("</li>\n");
231            }
232            result.append("</ul>\n");
233        }
234        if (getList().getTotalSize() > 0) {
235            result.append(key(Messages.GUI_USER_DEPENDENCIES_NOTICE_TEXT_0));
236        } else {
237            result.append(key(Messages.GUI_USER_DEPENDENCIES_DELETE_0));
238        }
239        result.append(dialogBlockEnd());
240        return result.toString();
241    }
242
243    /**
244     * @see org.opencms.workplace.list.A_CmsListDialog#fillDetails(java.lang.String)
245     */
246    @Override
247    protected void fillDetails(String detailId) {
248
249        // no-op
250    }
251
252    /**
253     * @see org.opencms.workplace.list.A_CmsListDialog#initWorkplaceRequestValues(org.opencms.workplace.CmsWorkplaceSettings, javax.servlet.http.HttpServletRequest)
254     */
255    @Override
256    protected void initWorkplaceRequestValues(CmsWorkplaceSettings settings, HttpServletRequest request) {
257
258        super.initWorkplaceRequestValues(settings, request);
259        if (DELETE_ACTION.equals(getParamAction())) {
260            setAction(ACTION_DELETE);
261        } else if (TRANSFER_ACTION.equals(getParamAction())) {
262            setAction(ACTION_TRANSFER);
263        }
264    }
265
266    /**
267     * @see org.opencms.workplace.list.A_CmsListDialog#validateParamaters()
268     */
269    @Override
270    protected void validateParamaters() throws Exception {
271
272        // test the needed parameters
273        m_userName = "";
274        Iterator itUsers = CmsStringUtil.splitAsList(getParamUserid(), CmsHtmlList.ITEM_SEPARATOR, true).iterator();
275        while (itUsers.hasNext()) {
276            CmsUUID id = new CmsUUID(itUsers.next().toString());
277            m_userName += getCms().readUser(id).getName();
278            if (itUsers.hasNext()) {
279                m_userName += CmsHtmlList.ITEM_SEPARATOR;
280            }
281        }
282    }
283}