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, 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.galleries;
029
030import org.opencms.file.CmsFile;
031import org.opencms.file.CmsResource;
032import org.opencms.file.types.CmsResourceTypePointer;
033import org.opencms.json.JSONException;
034import org.opencms.json.JSONObject;
035import org.opencms.jsp.CmsJspActionElement;
036import org.opencms.loader.CmsLoaderException;
037import org.opencms.lock.CmsLock;
038import org.opencms.main.CmsException;
039import org.opencms.main.CmsLog;
040import org.opencms.main.OpenCms;
041import org.opencms.util.CmsStringUtil;
042
043import java.io.IOException;
044
045import javax.servlet.http.HttpServletRequest;
046import javax.servlet.http.HttpServletResponse;
047import javax.servlet.jsp.JspWriter;
048import javax.servlet.jsp.PageContext;
049
050import org.apache.commons.logging.Log;
051
052/**
053 * Provides the specific constants, members and helper methods to generate the content of the external link gallery dialog
054 * used in the XML content editors, WYSIWYG editors and context menu.<p>
055 *
056 * @since 7.5.0
057 */
058
059public class CmsAjaxLinkGallery extends A_CmsAjaxGallery {
060
061    /** Type name of the link gallery. */
062    public static final String GALLERYTYPE_NAME = "linkgallery";
063
064    /** The uri suffix for the gallery start page. */
065    public static final String OPEN_URI_SUFFIX = GALLERYTYPE_NAME + "/index.jsp";
066
067    /** The log object for this class. */
068    private static final Log LOG = CmsLog.getLog(CmsAjaxLinkGallery.class);
069
070    /** The resource type id of this gallery instance. */
071    private int m_galleryTypeId;
072
073    /**
074     * Public empty constructor, required for {@link A_CmsAjaxGallery#createInstance(String, CmsJspActionElement)}.<p>
075     */
076    public CmsAjaxLinkGallery() {
077
078        // noop
079    }
080
081    /**
082     * Public constructor with JSP action element.<p>
083     *
084     * @param jsp an initialized JSP action element
085     */
086    public CmsAjaxLinkGallery(CmsJspActionElement jsp) {
087
088        super(jsp);
089
090    }
091
092    /**
093     * Public constructor with JSP variables.<p>
094     *
095     * @param context the JSP page context
096     * @param req the JSP request
097     * @param res the JSP response
098     */
099    public CmsAjaxLinkGallery(PageContext context, HttpServletRequest req, HttpServletResponse res) {
100
101        this(new CmsJspActionElement(context, req, res));
102
103    }
104
105    /**
106     * @see org.opencms.workplace.galleries.A_CmsAjaxGallery#getGalleryItemsTypeId()
107     */
108    @Override
109    public int getGalleryItemsTypeId() {
110
111        int pointerId;
112        try {
113            pointerId = OpenCms.getResourceManager().getResourceType(
114                CmsResourceTypePointer.getStaticTypeName()).getTypeId();
115        } catch (CmsLoaderException e) {
116            // should not never ever happen
117            pointerId = CmsResourceTypePointer.getStaticTypeId();
118        }
119        return pointerId;
120    }
121
122    /**
123     * @see org.opencms.workplace.galleries.A_CmsAjaxGallery#getGalleryTypeId()
124     */
125    @Override
126    public int getGalleryTypeId() {
127
128        try {
129            m_galleryTypeId = OpenCms.getResourceManager().getResourceType(GALLERYTYPE_NAME).getTypeId();
130        } catch (CmsLoaderException e) {
131            // resource type not found, log error
132            if (LOG.isErrorEnabled()) {
133                LOG.error(e.getLocalizedMessage(), e);
134            }
135        }
136        return m_galleryTypeId;
137    }
138
139    /**
140     * @see org.opencms.workplace.galleries.A_CmsAjaxGallery#getGalleryTypeName()
141     */
142    @Override
143    public String getGalleryTypeName() {
144
145        return GALLERYTYPE_NAME;
146    }
147
148    /**
149     * Fills the JSON object with the specific information used for pointer file resource type.<p>
150     *
151     * <ul>
152     * <li><code>pointer</code>: the content of the pointer resource. This could be an external or internal link.</li>
153     * </ul>
154     *
155     * @see org.opencms.workplace.galleries.A_CmsAjaxGallery#buildJsonItemSpecificPart(JSONObject jsonObj, CmsResource res, String sitePath)
156     *
157     */
158    @Override
159    protected void buildJsonItemSpecificPart(JSONObject jsonObj, CmsResource res, String sitePath) {
160
161        // file target
162        String pointer;
163        try {
164            pointer = new String(getCms().readFile(res).getContents());
165            if (CmsStringUtil.isEmptyOrWhitespaceOnly(pointer)) {
166                pointer = getJsp().link(getCms().getSitePath(res));
167            }
168            jsonObj.append("pointer", pointer);
169        } catch (CmsException e) {
170            // reading the resource or property value failed
171            LOG.error(e.getLocalizedMessage(), e);
172        } catch (JSONException e) {
173            if (LOG.isErrorEnabled()) {
174                LOG.error(e.getLocalizedMessage(), e);
175            }
176        }
177
178    }
179
180    /**
181     * Writes the current link into the pointer resource. <p>
182     *
183     * @see org.opencms.workplace.galleries.CmsAjaxLinkGallery#changeItemLinkUrl(String)
184     *
185     * @param itemUrl the pointer resource to change the link of
186     *
187     */
188    @Override
189    protected void changeItemLinkUrl(String itemUrl) {
190
191        try {
192            JspWriter out = getJsp().getJspContext().getOut();
193            if (getCms().existsResource(itemUrl)) {
194                try {
195                    writePointerLink(getCms().readResource(itemUrl));
196                    out.print(buildJsonItemObject(getCms().readResource(itemUrl)));
197                } catch (CmsException e) {
198                    // can not happen in theory, because we used existsResource() before...
199                }
200            } else {
201                out.print(RETURNVALUE_NONE);
202            }
203        } catch (IOException e) {
204            if (LOG.isErrorEnabled()) {
205                LOG.error(e.getLocalizedMessage(), e);
206            }
207        }
208
209    }
210
211    /**
212     * Writes the current link into the pointer resource. <p>
213     *
214     * @param res the pointer resource to change the link of
215     *
216     * @throws CmsException if sth. goes wrong
217     */
218    private void writePointerLink(CmsResource res) throws CmsException {
219
220        String resPath = getCms().getSitePath(res);
221        String currentPropertyValue = getParamPropertyValue();
222        boolean locked = true;
223        CmsLock lock = getCms().getLock(res);
224        if (lock.isUnlocked()) {
225            // lock resource before operation
226            getCms().lockResource(resPath);
227            locked = false;
228        }
229        CmsFile file = getCms().readFile(res);
230        file.setContents(currentPropertyValue.getBytes());
231        checkLock(getCms().getSitePath(res));
232        getCms().writeFile(file);
233        if (!locked) {
234            // unlock the resource
235            getCms().unlockResource(resPath);
236        }
237    }
238}