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