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.CmsResource;
031import org.opencms.file.types.CmsResourceTypeFolderExtended;
032import org.opencms.file.types.CmsResourceTypeImage;
033import org.opencms.file.types.I_CmsResourceType;
034import org.opencms.json.JSONException;
035import org.opencms.json.JSONObject;
036import org.opencms.jsp.CmsJspActionElement;
037import org.opencms.loader.CmsImageLoader;
038import org.opencms.loader.CmsImageScaler;
039import org.opencms.loader.CmsLoaderException;
040import org.opencms.main.CmsLog;
041import org.opencms.main.OpenCms;
042import org.opencms.util.CmsStringUtil;
043
044import java.awt.Color;
045
046import javax.servlet.http.HttpServletRequest;
047import javax.servlet.http.HttpServletResponse;
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 image gallery dialog
054 * used in the XML content editors, WYSIWYG editors and context menu.<p>
055 *
056 * @since 7.5.0
057 */
058public class CmsAjaxImageGallery extends A_CmsAjaxGallery {
059
060    /** Type name of the image gallery. */
061    public static final String GALLERYTYPE_NAME = "imagegallery";
062
063    /** The uri suffix for the gallery start page. */
064    public static final String OPEN_URI_SUFFIX = GALLERYTYPE_NAME + "/index.jsp";
065
066    /** Request parameter name for the format name. */
067    public static final String PARAM_FORMATNAME = "formatname";
068
069    /** Request parameter name for the format value. */
070    public static final String PARAM_FORMATVALUE = "formatvalue";
071
072    /** Request parameter name for the input field hash id. */
073    public static final String PARAM_HASHID = "hashid";
074
075    /** Request parameter name for the image height. */
076    public static final String PARAM_IMGHEIGHT = "imgheight";
077
078    /** Request parameter name for the image width. */
079    public static final String PARAM_IMGWIDTH = "imgwidth";
080
081    /** Request parameter name for the image scale parameters. */
082    public static final String PARAM_SCALE = "scale";
083
084    /** Request parameter name for the use formats flag. */
085    public static final String PARAM_USEFORMATS = "useformats";
086
087    /** Property definition name for the Copyright property. */
088    public static final String PARAM_WIDGETMODE = "widgetmode";
089
090    /** Property definition name for the Copyright property. */
091    public static final String PROPERTY_COPYRIGHT = "Copyright";
092
093    /** The log object for this class. */
094    private static final Log LOG = CmsLog.getLog(CmsAjaxImageGallery.class);
095
096    /** The default image scaling parameters for the gallery preview. */
097    private CmsImageScaler m_defaultScaleParams;
098
099    /** The resource type id of this gallery instance. */
100    private int m_galleryTypeId;
101
102    /**
103     * Public empty constructor, required for {@link A_CmsAjaxGallery#createInstance(String, CmsJspActionElement)}.<p>
104     */
105    public CmsAjaxImageGallery() {
106
107        // noop
108    }
109
110    /**
111     * Public constructor with JSP action element.<p>
112     *
113     * @param jsp an initialized JSP action element
114     */
115    public CmsAjaxImageGallery(CmsJspActionElement jsp) {
116
117        super(jsp);
118    }
119
120    /**
121     * Public constructor with JSP variables.<p>
122     *
123     * @param context the JSP page context
124     * @param req the JSP request
125     * @param res the JSP response
126     */
127    public CmsAjaxImageGallery(PageContext context, HttpServletRequest req, HttpServletResponse res) {
128
129        this(new CmsJspActionElement(context, req, res));
130    }
131
132    /**
133     * Returns the default image scaling parameters for the gallery preview.<p>
134     *
135     * @return the default image scaling parameters for the gallery preview
136     */
137    public CmsImageScaler getDefaultScaleParams() {
138
139        return m_defaultScaleParams;
140    }
141
142    /**
143     * @see org.opencms.workplace.galleries.A_CmsAjaxGallery#getGalleryItemsTypeId()
144     */
145    @Override
146    public int getGalleryItemsTypeId() {
147
148        int imageId;
149        try {
150            imageId = OpenCms.getResourceManager().getResourceType(
151                CmsResourceTypeImage.getStaticTypeName()).getTypeId();
152        } catch (CmsLoaderException e1) {
153            // should really never happen
154            LOG.warn(e1.getLocalizedMessage(), e1);
155            imageId = CmsResourceTypeImage.getStaticTypeId();
156        }
157        return imageId;
158    }
159
160    /**
161     * @see org.opencms.workplace.galleries.A_CmsAjaxGallery#getGalleryTypeId()
162     */
163    @Override
164    public int getGalleryTypeId() {
165
166        try {
167            m_galleryTypeId = OpenCms.getResourceManager().getResourceType(GALLERYTYPE_NAME).getTypeId();
168        } catch (CmsLoaderException e) {
169            // resource type not found, log error
170            if (LOG.isErrorEnabled()) {
171                LOG.error(e.getLocalizedMessage(), e);
172            }
173        }
174        return m_galleryTypeId;
175    }
176
177    /**
178     * @see org.opencms.workplace.galleries.A_CmsAjaxGallery#getGalleryTypeName()
179     */
180    @Override
181    public String getGalleryTypeName() {
182
183        return GALLERYTYPE_NAME;
184    }
185
186    /**
187     * Initializes the default image scaling parameters for the gallery preview.<p>
188     *
189     * @see org.opencms.workplace.galleries.A_CmsAjaxGallery#init()
190     */
191    @Override
192    public void init() {
193
194        if (CmsImageLoader.isEnabled()) {
195            try {
196                //reads the optional parameters for the gallery from the XML configuration
197                I_CmsResourceType type = OpenCms.getResourceManager().getResourceType(GALLERYTYPE_NAME);
198                if (type instanceof CmsResourceTypeFolderExtended) {
199                    m_galleryTypeParams = ((CmsResourceTypeFolderExtended)type).getFolderClassParams();
200                } else {
201                    m_galleryTypeParams = null;
202                }
203            } catch (CmsLoaderException e) {
204                if (LOG.isErrorEnabled()) {
205                    LOG.error(e.getLocalizedMessage(), e);
206                }
207            }
208            m_defaultScaleParams = new CmsImageScaler(m_galleryTypeParams);
209            if (!m_defaultScaleParams.isValid()) {
210                // no valid parameters have been provided, use defaults
211                m_defaultScaleParams.setType(0);
212                m_defaultScaleParams.setPosition(0);
213                m_defaultScaleParams.setWidth(120);
214                m_defaultScaleParams.setHeight(90);
215                m_defaultScaleParams.setColor(new Color(221, 221, 221));
216            }
217        } else {
218            m_defaultScaleParams = null;
219        }
220    }
221
222    /**
223     * Fills the JSON object with the specific information used for image resource type.<p>
224     *
225     * <ul>
226     * <li><code>scalepath</code>: scaling parameters.</li>
227     * <li><code>width</code>: image width.</li>
228     * <li><code>height</code>: image height.</li>
229     * <li><code>id</code>: image ID.</li>
230     * <li><code>type</code>: image type.</li>
231     * <li><code>hash</code>: image structure id hash code.</li>
232     * <li><code>copyright</code>: image copyright.</li>
233     * </ul>
234     *
235     * @see org.opencms.workplace.galleries.A_CmsAjaxGallery#buildJsonItemSpecificPart(JSONObject jsonObj, CmsResource res, String sitePath)
236     */
237    @Override
238    protected void buildJsonItemSpecificPart(JSONObject jsonObj, CmsResource res, String sitePath) {
239
240        CmsImageScaler scaler = new CmsImageScaler(getCms(), res);
241        try {
242            String scaleParams = "";
243            // 1: if scaling is disabled, the scale parameters might be null!
244            if (getDefaultScaleParams() != null) {
245                scaleParams = getDefaultScaleParams().toRequestParam();
246            }
247            jsonObj.put("scalepath", getJsp().link(sitePath + scaleParams));
248            // 2: image width
249            if (scaler.isValid()) {
250                jsonObj.put("width", scaler.getWidth());
251            } else {
252                jsonObj.put("width", -1);
253            }
254            // 3: image height
255            if (scaler.isValid()) {
256                jsonObj.put("height", scaler.getHeight());
257            } else {
258                jsonObj.put("height", -1);
259            }
260            // 4: image ID
261            jsonObj.put("id", res.getStructureId());
262            // 5: image type (gif, jpg, etc.)
263            String type = "";
264            int dotIndex = res.getName().lastIndexOf('.');
265            if (dotIndex != -1) {
266                type = res.getName().substring(dotIndex + 1).toLowerCase();
267            }
268            jsonObj.put("type", type);
269            // 6: image structure id hash code
270            jsonObj.put("hash", res.getStructureId().hashCode());
271            // 7: image copyright
272            String copyright = getJsp().property(PROPERTY_COPYRIGHT, sitePath, "");
273            jsonObj.put("copyright", CmsStringUtil.escapeJavaScript(copyright));
274
275        } catch (JSONException e) {
276            if (LOG.isErrorEnabled()) {
277                LOG.error(e.getLocalizedMessage(), e);
278            }
279        }
280    }
281}