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.CmsFile;
031import org.opencms.file.CmsResource;
032import org.opencms.i18n.CmsEncoder;
033import org.opencms.jsp.CmsJspActionElement;
034import org.opencms.security.CmsPermissionSet;
035import org.opencms.util.CmsStringUtil;
036import org.opencms.workplace.CmsDialog;
037import org.opencms.workplace.CmsWorkplaceSettings;
038
039import javax.servlet.http.HttpServletRequest;
040import javax.servlet.http.HttpServletResponse;
041import javax.servlet.jsp.JspException;
042import javax.servlet.jsp.PageContext;
043
044/**
045 * The edit pointer dialog changes the link target of a pointer resource.<p>
046 *
047 * The following files use this class:
048 * <ul>
049 * <li>/commons/editpointer.jsp
050 * </ul>
051 * <p>
052 *
053 * @since 6.0.0
054 */
055public class CmsEditPointer extends CmsDialog {
056
057    /** The dialog type.<p> */
058    public static final String DIALOG_TYPE = "newlink";
059
060    /** Request parameter name for the link target.<p> */
061    public static final String PARAM_LINKTARGET = "linktarget";
062
063    /** Stores the value of the link target.<p> */
064    private String m_paramLinkTarget;
065
066    /**
067     * Public constructor with JSP action element.<p>
068     *
069     * @param jsp an initialized JSP action element
070     */
071    public CmsEditPointer(CmsJspActionElement jsp) {
072
073        super(jsp);
074    }
075
076    /**
077     * Public constructor with JSP variables.<p>
078     *
079     * @param context the JSP page context
080     * @param req the JSP request
081     * @param res the JSP response
082     */
083    public CmsEditPointer(PageContext context, HttpServletRequest req, HttpServletResponse res) {
084
085        this(new CmsJspActionElement(context, req, res));
086    }
087
088    /**
089     * Changes the link target of the pointer.<p>
090     *
091     * @throws JspException if inclusion of error dialog fails
092     */
093    public void actionChangeLinkTarget() throws JspException {
094
095        try {
096            // check the resource lock state
097            checkLock(getParamResource());
098            // change the link target
099            CmsFile editFile = getCms().readFile(getParamResource());
100            editFile.setContents(getParamLinkTarget().getBytes());
101            getCms().writeFile(editFile);
102            // close the dialog window
103            actionCloseDialog();
104        } catch (Throwable e) {
105            // error changing link target, show error dialog
106            setParamMessage(Messages.get().getBundle(getLocale()).key(Messages.ERR_CHANGE_LINK_TARGET_0));
107            includeErrorpage(this, e);
108        }
109    }
110
111    /**
112     * Returns the old link target value of the pointer resource to edit.<p>
113     *
114     * @return the old link target value
115     * @throws JspException if problems including sub-elements occur
116     *
117     */
118    public String getOldTargetValue() throws JspException {
119
120        String linkTarget = "";
121        if (CmsStringUtil.isEmpty(getParamLinkTarget())) {
122            // this is the initial dialog call, get link target value
123            try {
124                // get pointer contents
125                CmsFile file = getCms().readFile(getParamResource());
126                linkTarget = new String(file.getContents());
127            } catch (Throwable e1) {
128                // error reading file, show error dialog
129                setParamMessage(
130                    Messages.get().getBundle(getLocale()).key(Messages.ERR_GET_LINK_TARGET_1, getParamResource()));
131                includeErrorpage(this, e1);
132
133            }
134        }
135        return CmsEncoder.escapeXml(linkTarget);
136    }
137
138    /**
139     * Returns the link target request parameter value.<p>
140     *
141     * @return the link target request parameter value
142     */
143    public String getParamLinkTarget() {
144
145        return m_paramLinkTarget;
146    }
147
148    /**
149     * Sets the link target request parameter value.<p>
150     *
151     * @param linkTarget the link target request parameter value
152     */
153    public void setParamLinkTarget(String linkTarget) {
154
155        m_paramLinkTarget = linkTarget;
156    }
157
158    /**
159     * @see org.opencms.workplace.CmsWorkplace#initWorkplaceRequestValues(org.opencms.workplace.CmsWorkplaceSettings, javax.servlet.http.HttpServletRequest)
160     */
161    @Override
162    protected void initWorkplaceRequestValues(CmsWorkplaceSettings settings, HttpServletRequest request) {
163
164        // fill the parameter values in the get/set methods
165        fillParamValues(request);
166
167        // check the required permissions to edit the pointer
168        if (!checkResourcePermissions(CmsPermissionSet.ACCESS_WRITE, false)) {
169            // no write permissions for the resource, set cancel action to close dialog
170            setParamAction(DIALOG_CANCEL);
171        }
172
173        // set the dialog type
174        setParamDialogtype(DIALOG_TYPE);
175        // set the action for the JSP switch
176        if (DIALOG_OK.equals(getParamAction())) {
177            // ok button pressed, change link target
178            setAction(ACTION_OK);
179        } else if (DIALOG_CANCEL.equals(getParamAction())) {
180            // cancel button pressed
181            setAction(ACTION_CANCEL);
182        } else {
183            // first call of dialog
184            setAction(ACTION_DEFAULT);
185            // build title for change link target dialog
186            setParamTitle(key(Messages.GUI_CHLINK_1, new Object[] {CmsResource.getName(getParamResource())}));
187        }
188    }
189
190}