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.projects;
029
030import org.opencms.db.CmsPublishList;
031import org.opencms.file.CmsProject;
032import org.opencms.jsp.CmsJspActionElement;
033import org.opencms.main.CmsException;
034import org.opencms.main.CmsRuntimeException;
035import org.opencms.main.OpenCms;
036import org.opencms.report.CmsHtmlReport;
037import org.opencms.util.CmsStringUtil;
038import org.opencms.util.CmsUUID;
039import org.opencms.workplace.CmsReport;
040import org.opencms.workplace.CmsWorkplaceSettings;
041import org.opencms.workplace.commons.Messages;
042import org.opencms.workplace.threads.CmsRelationsValidatorThread;
043
044import javax.servlet.http.HttpServletRequest;
045import javax.servlet.http.HttpServletResponse;
046import javax.servlet.jsp.JspException;
047import javax.servlet.jsp.PageContext;
048
049/**
050 * Provides a report for publishing a project.<p>
051 *
052 * @since 6.0.0
053 */
054public class CmsPublishProjectReport extends CmsReport {
055
056    /** Request parameter name for the project id. */
057    public static final String PARAM_PROJECTID = "projectid";
058
059    /** list of project id. */
060    private String m_paramProjectid;
061
062    /**
063     * Public constructor with JSP action element.<p>
064     *
065     * @param jsp an initialized JSP action element
066     */
067    public CmsPublishProjectReport(CmsJspActionElement jsp) {
068
069        super(jsp);
070    }
071
072    /**
073     * Public constructor with JSP variables.<p>
074     *
075     * @param context the JSP page context
076     * @param req the JSP request
077     * @param res the JSP response
078     */
079    public CmsPublishProjectReport(PageContext context, HttpServletRequest req, HttpServletResponse res) {
080
081        this(new CmsJspActionElement(context, req, res));
082    }
083
084    /**
085     * Performs the dialog actions depending on the initialized action.<p>
086     *
087     * @throws JspException if dialog actions fail
088     */
089    public void displayReport() throws JspException {
090
091        // save initialized instance of this class in request attribute for included sub-elements
092        getJsp().getRequest().setAttribute(SESSION_WORKPLACE_CLASS, this);
093        switch (getAction()) {
094            case ACTION_REPORT_END:
095                actionCloseDialog();
096                break;
097            case ACTION_CANCEL:
098                actionCloseDialog();
099                break;
100            case ACTION_REPORT_UPDATE:
101                setParamAction(REPORT_UPDATE);
102                getJsp().include(FILE_REPORT_OUTPUT);
103                break;
104            case ACTION_REPORT_BEGIN:
105            case ACTION_CONFIRMED:
106            case ACTION_DEFAULT:
107            default:
108                try {
109                    if (getCms().readProject(
110                        new CmsUUID(getParamProjectid())).getType() == CmsProject.PROJECT_TYPE_TEMPORARY) {
111                        // set the flag that this is a temporary project
112                        setParamRefreshWorkplace(CmsStringUtil.TRUE);
113                    }
114                } catch (Exception e) {
115                    // ignore
116                }
117
118                if (getParamProjectid() == null) {
119                    return;
120                }
121
122                CmsPublishList list = null;
123                try {
124                    CmsProject currentProject = getCms().getRequestContext().getCurrentProject();
125                    getCms().getRequestContext().setCurrentProject(
126                        getCms().readProject(new CmsUUID(getParamProjectid())));
127                    list = OpenCms.getPublishManager().getPublishList(getCms());
128                    getCms().getRequestContext().setCurrentProject(currentProject);
129                } catch (CmsException e) {
130                    throw new CmsRuntimeException(e.getMessageContainer(), e);
131                }
132
133                // start validation check
134                startValidationThread(list);
135                getJsp().include(FILE_REPORT_OUTPUT);
136        }
137    }
138
139    /**
140     * Gets the project id parameter.<p>
141     *
142     * @return the project id parameter
143     */
144    public String getParamProjectid() {
145
146        return m_paramProjectid;
147    }
148
149    /**
150     * Sets the project id parameter.<p>
151     *
152     * @param projectId the project id parameter
153     */
154    public void setParamProjectid(String projectId) {
155
156        m_paramProjectid = projectId;
157    }
158
159    /**
160     * @see org.opencms.workplace.CmsWorkplace#initWorkplaceRequestValues(org.opencms.workplace.CmsWorkplaceSettings, javax.servlet.http.HttpServletRequest)
161     */
162    @Override
163    protected void initWorkplaceRequestValues(CmsWorkplaceSettings settings, HttpServletRequest request) {
164
165        // fill the parameter values in the get/set methods
166        fillParamValues(request);
167
168        if (REPORT_UPDATE.equals(getParamAction())) {
169            setAction(ACTION_REPORT_UPDATE);
170        } else if (REPORT_BEGIN.equals(getParamAction())) {
171            setAction(ACTION_REPORT_BEGIN);
172        } else if (REPORT_END.equals(getParamAction())) {
173            if (Boolean.valueOf(getParamThreadHasNext()).booleanValue()) {
174                // after the link check start the publish thread
175                startPublishThread();
176            } else {
177                // ends the publish thread
178                setAction(ACTION_REPORT_END);
179            }
180        } else if (DIALOG_CANCEL.equals(getParamAction())) {
181            setAction(ACTION_CANCEL);
182        } else {
183            // set the default action
184            setAction(ACTION_DEFAULT);
185        }
186    }
187
188    /**
189     * Starts the publish thread for the project.<p>
190     */
191    private void startPublishThread() {
192
193        // create a publish thread from the current publish list
194        CmsPublishList publishList = getSettings().getPublishList();
195        try {
196            OpenCms.getPublishManager().publishProject(
197                getCms(),
198                new CmsHtmlReport(getLocale(), getCms().getRequestContext().getSiteRoot()),
199                publishList);
200        } catch (CmsException e) {
201            throw new CmsRuntimeException(e.getMessageContainer());
202        }
203        setParamAction(REPORT_END);
204        setAction(ACTION_REPORT_END);
205        setParamThreadHasNext(CmsStringUtil.FALSE);
206    }
207
208    /**
209     * Starts the link validation thread for the project.<p>
210     *
211     * @param publishList the list of resources to publish
212     *
213     * @throws JspException if something goes wrong
214     */
215    private void startValidationThread(CmsPublishList publishList) throws JspException {
216
217        try {
218            CmsRelationsValidatorThread thread = new CmsRelationsValidatorThread(getCms(), publishList, getSettings());
219            thread.start();
220
221            setParamThread(thread.getUUID().toString());
222            setParamThreadHasNext(CmsStringUtil.TRUE);
223
224            setParamAction(REPORT_BEGIN);
225
226            // set the key name for the continue checkbox
227            setParamReportContinueKey(Messages.GUI_PUBLISH_CONTINUE_BROKEN_LINKS_0);
228        } catch (Throwable e) {
229            // error while link validation, show error screen
230            includeErrorpage(this, e);
231        }
232    }
233}