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.galleries;
029
030import org.opencms.file.CmsResource;
031import org.opencms.i18n.CmsMessageContainer;
032import org.opencms.json.JSONException;
033import org.opencms.json.JSONObject;
034import org.opencms.jsp.CmsJspActionElement;
035import org.opencms.main.CmsException;
036import org.opencms.main.CmsLog;
037import org.opencms.main.CmsRuntimeException;
038import org.opencms.main.OpenCms;
039import org.opencms.workplace.CmsDialog;
040import org.opencms.workplace.CmsWorkplaceSettings;
041
042import javax.servlet.http.HttpServletRequest;
043import javax.servlet.http.HttpServletResponse;
044import javax.servlet.jsp.PageContext;
045
046import org.apache.commons.logging.Log;
047
048/**
049 * Provides methods for open gallery dialog.<p>
050 *
051 * The following files use this class:
052 * <ul>
053 * <li>/commons/opengallery.jsp
054 * </ul>
055 * <p>
056 *
057 * @since 6.0.0
058 */
059public class CmsOpenGallery extends CmsDialog {
060
061    /** The dialog type. */
062    public static final String DIALOG_TYPE = "opengallery";
063
064    /** The log object for this class. */
065    private static final Log LOG = CmsLog.getLog(CmsOpenGallery.class);
066
067    /**
068     * Public constructor with JSP action element.<p>
069     *
070     * @param jsp an initialized JSP action element
071     */
072    public CmsOpenGallery(CmsJspActionElement jsp) {
073
074        super(jsp);
075    }
076
077    /**
078     * Public constructor with JSP variables.<p>
079     *
080     * @param context the JSP page context
081     * @param req the JSP request
082     * @param res the JSP response
083     */
084    public CmsOpenGallery(PageContext context, HttpServletRequest req, HttpServletResponse res) {
085
086        this(new CmsJspActionElement(context, req, res));
087    }
088
089    /**
090     * Generates a javascript window open for the requested gallery type.<p>
091     *
092     * @return a javascript window open for the requested gallery type
093     */
094    public String openGallery() {
095
096        StringBuffer jsOpener = new StringBuffer(32);
097        String galleryType = null;
098        try {
099            CmsResource res = getCms().readResource(getParamResource());
100            if (res != null) {
101                // get gallery path
102                String galleryPath = getParamResource();
103                if (!galleryPath.endsWith("/")) {
104                    galleryPath += "/";
105                }
106                // get the matching gallery type name
107                galleryType = OpenCms.getResourceManager().getResourceType(res.getTypeId()).getTypeName();
108                StringBuffer galleryUri = new StringBuffer(256);
109                galleryUri.append(A_CmsAjaxGallery.PATH_GALLERIES);
110                String width = "650";
111                String height = "700";
112                // path to the gallery dialog with the required request parameters
113                galleryUri.append(galleryType);
114                galleryUri.append("/index.jsp?");
115                galleryUri.append(A_CmsAjaxGallery.PARAM_DIALOGMODE);
116                galleryUri.append("=");
117                galleryUri.append(A_CmsAjaxGallery.MODE_VIEW);
118                galleryUri.append("&");
119                galleryUri.append(A_CmsAjaxGallery.PARAM_PARAMS);
120                galleryUri.append("=");
121                JSONObject jsonObj = new JSONObject();
122                try {
123                    jsonObj.putOpt(A_CmsAjaxGallery.PARAM_STARTUPFOLDER, galleryPath);
124                    jsonObj.putOpt(A_CmsAjaxGallery.PARAM_STARTUPTYPE, A_CmsAjaxGallery.LISTMODE_GALLERY);
125                } catch (JSONException e) {
126                    // ignore, because it should not happen!
127                }
128                galleryUri.append(jsonObj.toString());
129                // open new gallery dialog
130                jsOpener.append("window.open('");
131                jsOpener.append(getJsp().link(galleryUri.toString()));
132
133                jsOpener.append("', '");
134                jsOpener.append(galleryType);
135                jsOpener.append("','width=");
136                jsOpener.append(width);
137                jsOpener.append(", height=");
138                jsOpener.append(height);
139                jsOpener.append(", resizable=yes, top=100, left=270, status=yes');");
140            }
141        } catch (CmsException e) {
142            // requested type is not configured
143            CmsMessageContainer message = Messages.get().container(Messages.ERR_OPEN_GALLERY_1, galleryType);
144            LOG.error(message.key(), e);
145            throw new CmsRuntimeException(message, e);
146        }
147
148        return jsOpener.toString();
149    }
150
151    /**
152     * @see org.opencms.workplace.CmsWorkplace#initWorkplaceRequestValues(org.opencms.workplace.CmsWorkplaceSettings, javax.servlet.http.HttpServletRequest)
153     */
154    @Override
155    protected void initWorkplaceRequestValues(CmsWorkplaceSettings settings, HttpServletRequest request) {
156
157        // fill the parameter values in the get/set methods
158        fillParamValues(request);
159        // set the dialog type
160        setParamDialogtype(DIALOG_TYPE);
161    }
162}