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.commons;
029
030import org.opencms.file.CmsResource;
031import org.opencms.file.CmsResourceFilter;
032import org.opencms.jsp.CmsJspActionElement;
033import org.opencms.main.CmsException;
034import org.opencms.main.CmsLog;
035import org.opencms.workplace.CmsDialog;
036import org.opencms.workplace.CmsWorkplaceSettings;
037
038import java.util.Iterator;
039import java.util.List;
040
041import javax.servlet.http.HttpServletRequest;
042import javax.servlet.http.HttpServletResponse;
043import javax.servlet.jsp.JspException;
044import javax.servlet.jsp.PageContext;
045
046import org.apache.commons.logging.Log;
047
048/**
049 * Provides methods for the copy to project dialog.<p>
050 *
051 * The following files use this class:
052 * <ul>
053 * <li>/commons/copytoproject.jsp
054 * </ul>
055 * <p>
056 *
057 * @since 6.0.0
058 */
059public class CmsCopyToProject extends CmsDialog {
060
061    /** Value for the action: copy the resource to current project. */
062    public static final int ACTION_COPYTOPROJECT = 100;
063
064    /** The dialog type. */
065    public static final String DIALOG_TYPE = "copytoproject";
066
067    /** The log object for this class. */
068    private static final Log LOG = CmsLog.getLog(CmsCopyToProject.class);
069
070    /**
071     * Public constructor with JSP action element.<p>
072     *
073     * @param jsp an initialized JSP action element
074     */
075    public CmsCopyToProject(CmsJspActionElement jsp) {
076
077        super(jsp);
078    }
079
080    /**
081     * Public constructor with JSP variables.<p>
082     *
083     * @param context the JSP page context
084     * @param req the JSP request
085     * @param res the JSP response
086     */
087    public CmsCopyToProject(PageContext context, HttpServletRequest req, HttpServletResponse res) {
088
089        this(new CmsJspActionElement(context, req, res));
090    }
091
092    /**
093     * Performs the copy to project action, will be called by the JSP page.<p>
094     *
095     * @throws JspException if problems including sub-elements occur
096     */
097    public void actionCopyToProject() throws JspException {
098
099        // save initialized instance of this class in request attribute for included sub-elements
100        getJsp().getRequest().setAttribute(SESSION_WORKPLACE_CLASS, this);
101        try {
102            // copy the resource to the current project
103            getCms().copyResourceToProject(getParamResource());
104            // close the dialog
105            actionCloseDialog();
106        } catch (Throwable e) {
107            // error copying resource to project, include error page
108            includeErrorpage(this, e);
109        }
110    }
111
112    /**
113     * Returns the HTML containing project information and confirmation question for the JSP.<p>
114     *
115     * @return the HTML containing project information and confirmation question for the JSP
116     */
117    public String buildProjectInformation() {
118
119        StringBuffer result = new StringBuffer(32);
120
121        try {
122            String[] localizedObject = new String[] {getCms().getRequestContext().getCurrentProject().getName()};
123            List<String> resources = getCms().readProjectResources(getCms().getRequestContext().getCurrentProject());
124            Iterator<String> i = resources.iterator();
125            result.append(dialogBlockStart(key(Messages.GUI_COPYTOPROJECT_RESOURCES_0)));
126            if (resources.size() > 0) {
127                // at least one resource in current project
128                result.append(key(Messages.GUI_COPYTOPROJECT_PART_1, localizedObject));
129                result.append("<ul style=\"margin-top: 3px; margin-bottom: 3px;\">\n");
130                String siteRoot = getCms().getRequestContext().getSiteRoot();
131                while (i.hasNext()) {
132                    // create resource list
133                    String resName = i.next();
134                    if (resName.startsWith(siteRoot)) {
135                        // remove current site root from resource name
136                        resName = resName.substring(siteRoot.length());
137                    }
138                    result.append("\t<li>");
139                    result.append(resName);
140                    result.append("</li>\n");
141                }
142                result.append("</ul>\n");
143            } else {
144                // no resources in current project
145                result.append(key(Messages.GUI_COPYTOPROJECT_NOPART_1, localizedObject));
146            }
147            result.append(dialogBlockEnd());
148            result.append(dialogSpacer());
149        } catch (CmsException e) {
150            // error reading project resources, should not happen
151            if (LOG.isInfoEnabled()) {
152                LOG.info(e.getLocalizedMessage());
153            }
154        }
155
156        // determine resource name to show
157        String resName = getParamResource();
158        try {
159            CmsResource res = getCms().readResource(getParamResource(), CmsResourceFilter.ALL);
160            if (res.isFolder() && !resName.endsWith("/")) {
161                resName += "/";
162            }
163        } catch (CmsException e) {
164            // error reading resource, should not happen
165            if (LOG.isInfoEnabled()) {
166                LOG.info(e.getLocalizedMessage());
167            }
168        }
169        // show confirmation question
170        String[] localizedObject = new String[] {resName, getCms().getRequestContext().getCurrentProject().getName()};
171        result.append(key(Messages.GUI_COPYTOPROJECT_PROJECT_CONFIRMATION_2, localizedObject));
172        return result.toString();
173    }
174
175    /**
176     * @see org.opencms.workplace.CmsWorkplace#initWorkplaceRequestValues(org.opencms.workplace.CmsWorkplaceSettings, javax.servlet.http.HttpServletRequest)
177     */
178    @Override
179    protected void initWorkplaceRequestValues(CmsWorkplaceSettings settings, HttpServletRequest request) {
180
181        // fill the parameter values in the get/set methods
182        fillParamValues(request);
183        // set the dialog type
184        setParamDialogtype(DIALOG_TYPE);
185        // set the action for the JSP switch
186        if (DIALOG_TYPE.equals(getParamAction())) {
187            setAction(ACTION_COPYTOPROJECT);
188        } else if (DIALOG_CANCEL.equals(getParamAction())) {
189            setAction(ACTION_CANCEL);
190        } else {
191            setAction(ACTION_DEFAULT);
192            // build title for copy to project dialog
193            setParamTitle(key(Messages.GUI_COPYTOPROJECT_TITLE_0));
194        }
195    }
196
197}