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.list;
029
030import org.opencms.i18n.CmsEncoder;
031import org.opencms.i18n.CmsMessages;
032import org.opencms.jsp.CmsJspActionElement;
033import org.opencms.main.CmsException;
034import org.opencms.main.CmsLog;
035import org.opencms.util.CmsCollectionsGenericWrapper;
036import org.opencms.util.CmsRequestUtil;
037import org.opencms.util.CmsStringUtil;
038import org.opencms.workplace.CmsWorkplaceSettings;
039import org.opencms.workplace.explorer.CmsExplorer;
040import org.opencms.workplace.tools.A_CmsHtmlIconButton;
041import org.opencms.workplace.tools.CmsExplorerDialog;
042import org.opencms.workplace.tools.CmsHtmlIconButtonStyleEnum;
043import org.opencms.workplace.tools.CmsTool;
044import org.opencms.workplace.tools.CmsToolDialog;
045import org.opencms.workplace.tools.CmsToolMacroResolver;
046import org.opencms.workplace.tools.CmsToolManager;
047
048import java.io.IOException;
049import java.util.Collection;
050import java.util.Collections;
051import java.util.HashMap;
052import java.util.Iterator;
053import java.util.Map;
054
055import javax.servlet.ServletException;
056import javax.servlet.http.HttpServletRequest;
057import javax.servlet.http.HttpServletResponse;
058import javax.servlet.jsp.JspWriter;
059import javax.servlet.jsp.PageContext;
060
061import org.apache.commons.logging.Log;
062
063/**
064 * Explorer dialog for the project files view.<p>
065 *
066 * @since 6.0.0
067 */
068public class CmsListExplorerFrameset extends CmsExplorerDialog {
069
070    /** Page parameter name. */
071    public static final String PARAM_PAGE = "explorer_page";
072
073    /** Title uri parameter name. */
074    public static final String PARAM_TITLE_URI = "title_uri";
075
076    /** The log object for this class. */
077    private static final Log LOG = CmsLog.getLog(CmsListExplorerFrameset.class);
078
079    /**
080     * Public constructor with JSP action element.<p>
081     *
082     * @param jsp an initialized JSP action element
083     */
084    public CmsListExplorerFrameset(CmsJspActionElement jsp) {
085
086        super(jsp);
087    }
088
089    /**
090     * Public constructor with JSP variables.<p>
091     *
092     * @param context the JSP page context
093     * @param req the JSP request
094     * @param res the JSP response
095     */
096    public CmsListExplorerFrameset(PageContext context, HttpServletRequest req, HttpServletResponse res) {
097
098        this(new CmsJspActionElement(context, req, res));
099    }
100
101    /**
102     * Generates the dialog starting html code.<p>
103     *
104     * @return html code
105     */
106    public String defaultActionHtml() {
107
108        String params = allParamsAsRequest();
109        String titleUri = CmsEncoder.escapeXml(getJsp().getRequest().getParameter(PARAM_TITLE_URI));
110        if (CmsStringUtil.isEmptyOrWhitespaceOnly(titleUri)) {
111            titleUri = CmsToolManager.ADMINVIEW_ROOT_LOCATION + "/list-title.jsp";
112        }
113        String titleSrc = getFrameSource("tool_title", getJsp().link(titleUri + "?" + params));
114        String contentSrc = getFrameSource(
115            "tool_content",
116            getJsp().link(CmsToolManager.ADMINVIEW_ROOT_LOCATION + "/list-explorer.jsp") + "?" + params);
117        StringBuffer html = new StringBuffer(1024);
118        html.append("<!DOCTYPE html>\n");
119        html.append("<html>\n");
120        html.append("\t<head>\n");
121        html.append("\t\t<meta HTTP-EQUIV=\"Content-Type\" CONTENT=\"text/html; charset=\"").append(
122            getEncoding()).append("\">\n");
123        String uplink = "/opencms_a/opencms/system/workplace/views/admin/admin-main.jsp?path=/projects/files&action=initial&projectid=fa9f561f-be30-11e2-bac3-21ebf444feef&showexplorer=true";
124        html.append(
125            "<script >var uplink = \""
126                + CmsStringUtil.escapeJavaScript(uplink)
127                + "\";</script>\n");
128        html.append("\t\t<title>\n");
129        html.append(
130            "\t\t\t").append(
131                key(
132                    org.opencms.workplace.Messages.GUI_LABEL_WPTITLE_1,
133                    new Object[] {getSettings().getUser().getFullName()})).append("\n");
134        html.append("\t\t</title>\n");
135        html.append("\t</head>\n");
136        html.append("\t<frameset rows='57,*' border='0' frameborder='0' framespacing='0'>\n");
137        html.append("\t\t<frame ").append(titleSrc).append(" frameborder='0' border='0' noresize scrolling='no'>\n");
138        html.append("\t\t<frame ").append(contentSrc).append(
139            " frameborder='0' border='0' noresize scrolling='auto' framespacing='0' marginheight='2' marginwidth='2' >\n");
140        html.append("\t</frameset>\n");
141        html.append("</html>\n");
142        return html.toString();
143    }
144
145    /**
146     * @see org.opencms.workplace.tools.CmsToolDialog#dialogTitle()
147     */
148    @Override
149    public String dialogTitle() {
150
151        StringBuffer html = new StringBuffer(512);
152        String toolPath = getCurrentToolPath();
153        String parentPath = getParentPath();
154        String rootKey = getToolManager().getCurrentRoot(this).getKey();
155        CmsTool parentTool = getToolManager().resolveAdminTool(rootKey, parentPath);
156        String upLevelLink = CmsToolManager.linkForToolPath(
157            getJsp(),
158            parentPath,
159            parentTool.getHandler().getParameters(this));
160        String listLevelLink = CmsToolManager.linkForToolPath(
161            getJsp(),
162            toolPath,
163            getToolManager().resolveAdminTool(rootKey, toolPath).getHandler().getParameters(this));
164        listLevelLink = CmsRequestUtil.appendParameter(
165            listLevelLink,
166            A_CmsListExplorerDialog.PARAM_SHOW_EXPLORER,
167            Boolean.FALSE.toString());
168        String parentName = getToolManager().resolveAdminTool(rootKey, parentPath).getHandler().getName();
169
170        html.append(getToolManager().generateNavBar(toolPath, this));
171
172        // check if page switch needed
173        int items = 0;
174        try {
175            items = getSettings().getCollector().getResults(getCms()).size();
176        } catch (CmsException e) {
177            // ignore
178            LOG.error(e.getLocalizedMessage(), e);
179        }
180
181        int size = (int)Math.ceil((double)items / getSettings().getUserSettings().getExplorerFileEntries());
182        // build title
183        html.append("<div class='screenTitle'>\n");
184        html.append("\t<table width='100%' cellspacing='0'>\n");
185        html.append("\t\t<tr>\n");
186        html.append("\t\t\t<td>\n");
187        html.append(getAdminTool().getHandler().getName());
188        html.append("\n\t\t\t</td>\n");
189        html.append("\t\t\t<td class='uplevel'>\n");
190        html.append("<form name='title-form' method='post' target='_parent' action='");
191        html.append(getJsp().link(A_CmsListExplorerDialog.PATH_EXPLORER_LIST)).append("'>\n");
192        html.append(getFormContent());
193        // if page switch needed
194        if (size > 1) {
195            html.append("<select name='").append(PARAM_PAGE);
196            html.append("' class='location' onchange='this.form.submit()'>\n");
197            html.append(
198                CmsHtmlList.htmlPageSelector(
199                    size,
200                    getSettings().getUserSettings().getExplorerFileEntries(),
201                    items,
202                    getSettings().getExplorerPage(),
203                    getLocale()));
204            html.append("</select>\n");
205        }
206        // list view button
207        CmsMessages messages = Messages.get().getBundle(getLocale());
208        html.append(A_CmsHtmlIconButton.defaultButtonHtml(
209            CmsHtmlIconButtonStyleEnum.SMALL_ICON_TEXT,
210            "id-list-switch",
211            messages.key(Messages.GUI_LIST_ACTION_LIST_SWITCH_NAME_0),
212            messages.key(Messages.GUI_LIST_ACTION_LIST_SWITCH_HELP_0),
213            true,
214            "list/list.png",
215            null,
216            "openPage('" + listLevelLink + "');"));
217        html.append("\n");
218        // uplevel button only if needed
219        if (!toolPath.equals(getParentPath())) {
220            html.append(A_CmsHtmlIconButton.defaultButtonHtml(
221                CmsHtmlIconButtonStyleEnum.SMALL_ICON_TEXT,
222                "id-up-level",
223                org.opencms.workplace.tools.Messages.get().getBundle(getLocale()).key(
224                    org.opencms.workplace.tools.Messages.GUI_ADMIN_VIEW_UPLEVEL_0),
225                parentName,
226                true,
227                "admin/images/up.png",
228                null,
229                "openPage('" + upLevelLink + "');"));
230        }
231        html.append("</form>");
232        html.append("\n\t\t\t</td>\n");
233        html.append("\t\t</tr>\n");
234        html.append("\t</table>\n");
235        html.append("</div>\n");
236
237        String code = html.toString().replaceAll("openPage\\('([^']+)'\\)", "openPageIn('$1', parent)");
238        return CmsToolMacroResolver.resolveMacros(code, this);
239    }
240
241    /**
242     * Performs the dialog actions depending on the initialized action and displays the dialog form.<p>
243     *
244     * @throws ServletException if forwarding explorer view fails
245     * @throws IOException if forwarding explorer view fails
246     */
247    public void displayDialog() throws IOException, ServletException {
248
249        getSettings().setExplorerMode(CmsExplorer.VIEW_LIST);
250        Map<String, String[]> addParams = new HashMap<String, String[]>();
251        HttpServletRequest req = getJsp().getRequest();
252        String addParamsVal = req.getParameter(CmsExplorer.PARAMETER_CONTEXTMENUPARAMS);
253        if (addParamsVal != null) {
254            addParams.put(CmsExplorer.PARAMETER_CONTEXTMENUPARAMS, new String[] {addParamsVal});
255        }
256        getToolManager().jspForwardPage(this, FILE_EXPLORER_FILELIST, addParams);
257    }
258
259    /**
260     * Validates the needed parameters and display the frameset.<p>
261     *
262     * @throws IOException in case of errros displaying to the required page
263     */
264    public void displayFrameSet() throws IOException {
265
266        if (getJsp().getRequest().getParameter(CmsListExplorerFrameset.PARAM_PAGE) != null) {
267            int page = Integer.parseInt(getJsp().getRequest().getParameter(CmsListExplorerFrameset.PARAM_PAGE));
268            getSettings().setExplorerPage(page);
269
270            if (getSettings().getCollector() instanceof I_CmsListResourceCollector) {
271                I_CmsListResourceCollector collector = (I_CmsListResourceCollector)getSettings().getCollector();
272                collector.setPage(page);
273            }
274        }
275        JspWriter out = getJsp().getJspContext().getOut();
276        out.print(defaultActionHtml());
277    }
278
279    /**
280     * @see org.opencms.workplace.CmsWorkplace#paramsAsHidden(java.util.Collection)
281     */
282    @Override
283    public String paramsAsHidden(Collection<String> excludes) {
284
285        StringBuffer result = new StringBuffer(512);
286        Map<String, String[]> params = CmsCollectionsGenericWrapper.map(getJsp().getRequest().getParameterMap());
287        params = new HashMap<String, String[]>(params);
288        params.remove(CmsListExplorerFrameset.PARAM_PAGE);
289        Iterator<Map.Entry<String, String[]>> it = params.entrySet().iterator();
290        while (it.hasNext()) {
291            Map.Entry<String, String[]> entry = it.next();
292            String param = entry.getKey();
293            if ((excludes == null) || (!excludes.contains(param))) {
294                String[] value = entry.getValue();
295                for (int i = 0; i < value.length; i++) {
296                    result.append("<input type=\"hidden\" name=\"");
297                    result.append(param);
298                    result.append("\" value=\"");
299                    result.append(CmsEncoder.encode(value[i], getCms().getRequestContext().getEncoding()));
300                    result.append("\">\n");
301                }
302            }
303        }
304        return result.toString();
305    }
306
307    /**
308     * Returns the form contents.<p>
309     *
310     * @return the form contents
311     */
312    protected String getFormContent() {
313
314        return paramsAsHidden(Collections.singleton(PARAM_PAGE));
315    }
316
317    /**
318     * @see org.opencms.workplace.CmsDialog#initWorkplaceRequestValues(org.opencms.workplace.CmsWorkplaceSettings, javax.servlet.http.HttpServletRequest)
319     */
320    @Override
321    protected void initWorkplaceRequestValues(CmsWorkplaceSettings settings, HttpServletRequest request) {
322
323        setParamStyle(CmsToolDialog.STYLE_NEW);
324        super.initWorkplaceRequestValues(settings, request);
325    }
326
327}