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.editors;
029
030import org.opencms.configuration.CmsDefaultUserSettings;
031import org.opencms.file.CmsResourceFilter;
032import org.opencms.i18n.CmsEncoder;
033import org.opencms.jsp.CmsJspActionElement;
034import org.opencms.main.OpenCms;
035import org.opencms.staticexport.CmsLinkManager;
036import org.opencms.ui.CmsVaadinUtils;
037import org.opencms.util.CmsStringUtil;
038import org.opencms.workplace.CmsDialog;
039import org.opencms.workplace.CmsWorkplace;
040
041import java.io.IOException;
042
043import javax.servlet.jsp.JspException;
044
045/**
046 * Provides a method to perform a user defined action when editing a page.<p>
047 *
048 * @since 6.0.0
049 */
050public class CmsEditorActionDefault implements I_CmsEditorActionHandler {
051
052    /**
053     * Default constructor needed for editor action handler implementation.<p>
054     */
055    public CmsEditorActionDefault() {
056
057        // empty constructor
058    }
059
060    /**
061     * @see org.opencms.workplace.editors.I_CmsEditorActionHandler#editorAction(org.opencms.workplace.editors.CmsEditor, org.opencms.jsp.CmsJspActionElement)
062     */
063    public void editorAction(CmsEditor editor, CmsJspActionElement jsp) throws IOException, JspException {
064
065        // save the edited content
066        editor.actionSave();
067        // delete temporary file and unlock resource in direct edit mode
068        editor.actionClear(true);
069        // create the publish link to redirect to
070        String publishLink = jsp.link(CmsWorkplace.PATH_DIALOGS + "publishresource.jsp");
071        // define the parameters which are necessary for publishing the resource
072        StringBuffer params = new StringBuffer(256);
073        params.append("?").append(CmsDialog.PARAM_RESOURCE).append("=").append(editor.getParamResource());
074        params.append("&").append(CmsDialog.PARAM_ACTION).append("=").append(CmsDialog.DIALOG_CONFIRMED);
075        params.append("&").append(CmsWorkplace.PARAM_DIRECTPUBLISH).append("=").append(CmsStringUtil.TRUE);
076        params.append("&").append(CmsWorkplace.PARAM_PUBLISHSIBLINGS).append("=").append(
077            editor.getSettings().getUserSettings().getDialogPublishSiblings());
078        // set the related resources option
079        String pubRelated = CmsStringUtil.TRUE;
080        if (OpenCms.getWorkplaceManager().getDefaultUserSettings().getPublishRelatedResources() == CmsDefaultUserSettings.PUBLISH_RELATED_RESOURCES_MODE_FALSE) {
081            pubRelated = CmsStringUtil.FALSE;
082        }
083        params.append("&").append(CmsWorkplace.PARAM_RELATEDRESOURCES).append("=").append(pubRelated);
084        params.append("&").append(CmsDialog.PARAM_TITLE).append("=");
085        params.append(
086            CmsEncoder.escapeWBlanks(
087                editor.key(Messages.GUI_MESSAGEBOX_TITLE_PUBLISHRESOURCE_0) + ": " + editor.getParamResource(),
088                CmsEncoder.ENCODING_UTF_8));
089        params.append("&").append(CmsDialog.PARAM_REDIRECT).append("=").append(CmsStringUtil.TRUE);
090        params.append("&").append(CmsDialog.PARAM_CLOSELINK).append("=");
091        if (Boolean.valueOf(editor.getParamDirectedit()).booleanValue()) {
092            String linkTarget;
093            if (!"".equals(editor.getParamBacklink())) {
094                linkTarget = jsp.link(editor.getParamBacklink());
095            } else {
096                linkTarget = jsp.link(editor.getParamResource());
097            }
098            // append the parameters and the report "ok" button action to the link
099            publishLink += params.toString() + CmsEncoder.escapeWBlanks(linkTarget, CmsEncoder.ENCODING_UTF_8);
100        } else {
101            // check for links to the new workplace
102            if (CmsLinkManager.isWorkplaceLink(editor.getParamBacklink())) {
103                publishLink += params.toString()
104                    + CmsEncoder.escapeWBlanks(jsp.link(editor.getParamBacklink()), CmsEncoder.ENCODING_UTF_8);
105            } else {
106                publishLink += params.toString()
107                    + CmsEncoder.escapeWBlanks(CmsVaadinUtils.getWorkplaceLink(), CmsEncoder.ENCODING_UTF_8);
108            }
109
110        }
111        // redirect to the publish dialog with all necessary parameters
112        jsp.getResponse().sendRedirect(publishLink);
113    }
114
115    /**
116     * @see org.opencms.workplace.editors.I_CmsEditorActionHandler#getButtonName()
117     */
118    public String getButtonName() {
119
120        return Messages.GUI_EXPLORER_CONTEXT_PUBLISH_0;
121    }
122
123    /**
124     * @see org.opencms.workplace.editors.I_CmsEditorActionHandler#getButtonUrl(CmsJspActionElement, java.lang.String)
125     */
126    public String getButtonUrl(CmsJspActionElement jsp, String resourceName) {
127
128        // get the button image
129        String button = CmsWorkplace.VFS_PATH_RESOURCES + "buttons/publish.png";
130        if (!isButtonActive(jsp, resourceName)) {
131            // show disabled button if not active
132            button = CmsWorkplace.VFS_PATH_RESOURCES + "buttons/publish_in.png";
133        }
134        return jsp.link(button);
135    }
136
137    /**
138     * @see org.opencms.workplace.editors.I_CmsEditorActionHandler#isButtonActive(CmsJspActionElement, java.lang.String)
139     */
140    public boolean isButtonActive(CmsJspActionElement jsp, String resourceName) {
141
142        try {
143            OpenCms.getPublishManager().getPublishList(
144                jsp.getCmsObject(),
145                jsp.getCmsObject().readResource(resourceName, CmsResourceFilter.IGNORE_EXPIRATION),
146                false);
147            return true;
148        } catch (Exception e) {
149            return false;
150        }
151    }
152}