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.ui.dialogs;
029
030import org.opencms.file.CmsObject;
031import org.opencms.file.CmsResource;
032import org.opencms.file.CmsResourceFilter;
033import org.opencms.main.CmsLog;
034import org.opencms.main.OpenCms;
035import org.opencms.security.CmsRole;
036import org.opencms.security.CmsRoleViolationException;
037import org.opencms.ui.A_CmsUI;
038import org.opencms.ui.CmsVaadinUtils;
039import org.opencms.ui.I_CmsDialogContext.ContextType;
040import org.opencms.ui.actions.I_CmsWorkplaceAction;
041import org.opencms.ui.apps.CmsEditorConfiguration;
042import org.opencms.ui.apps.CmsPageEditorConfiguration;
043import org.opencms.ui.apps.CmsSitemapEditorConfiguration;
044import org.opencms.ui.apps.Messages;
045import org.opencms.ui.components.CmsErrorDialog;
046import org.opencms.ui.dialogs.permissions.CmsPrincipalSelectDialog;
047import org.opencms.util.CmsStringUtil;
048import org.opencms.util.CmsUUID;
049
050import java.util.ArrayList;
051import java.util.Collection;
052import java.util.Collections;
053import java.util.List;
054import java.util.Locale;
055
056import org.apache.commons.logging.Log;
057
058import com.vaadin.annotations.Theme;
059import com.vaadin.server.VaadinRequest;
060
061/**
062 * Separate UI for VAADIN based dialog embedded into a GWT module.<p>
063 */
064@Theme("opencms")
065public class CmsEmbeddedDialogsUI extends A_CmsUI {
066
067    /** The dialogs path fragment. */
068    public static final String DIALOGS_PATH = "dialogs/";
069
070    /** Logger instance for this class. */
071    private static final Log LOG = CmsLog.getLog(CmsEmbeddedDialogsUI.class);
072
073    /** The serial version id. */
074    private static final long serialVersionUID = 1201184887611215370L;
075
076    /**
077     * The dialog context of the currently opened dialog.<p>
078     */
079    CmsEmbeddedDialogContext m_currentContext;
080
081    /**
082     * Returns the context path for embedded dialogs.<p>
083     *
084     * @return the context path for embedded dialogs
085     */
086    public static String getEmbeddedDialogsContextPath() {
087
088        return CmsStringUtil.joinPaths(OpenCms.getSystemInfo().getWorkplaceContext(), DIALOGS_PATH);
089    }
090
091    /**
092     * @see com.vaadin.ui.AbstractComponent#getLocale()
093     */
094    @Override
095    public Locale getLocale() {
096
097        CmsObject cms = getCmsObject();
098        return OpenCms.getWorkplaceManager().getWorkplaceLocale(cms);
099    }
100
101    /**
102     * @see org.opencms.ui.A_CmsUI#reload()
103     */
104    @Override
105    public void reload() {
106
107        if (m_currentContext != null) {
108            m_currentContext.reload();
109        }
110    }
111
112    /**
113     * @see org.opencms.ui.A_CmsUI#init(com.vaadin.server.VaadinRequest)
114     */
115    @Override
116    protected void init(VaadinRequest request) {
117
118        super.init(request);
119        Throwable t = null;
120        String errorMessage = null;
121        try {
122            OpenCms.getRoleManager().checkRole(getCmsObject(), CmsRole.ELEMENT_AUTHOR);
123            if (CmsPrincipalSelectDialog.DIALOG_ID.equals(getDialogId(request))) {
124                //TODO implement a generic way to open dialogs other than workplace actions
125                m_currentContext = new CmsEmbeddedDialogContext(CmsPrincipalSelectDialog.DIALOG_ID, null, null);
126                CmsPrincipalSelectDialog.openEmbeddedDialog(m_currentContext, request.getParameterMap());
127            } else {
128                try {
129                    String resources = request.getParameter("resources");
130                    List<CmsResource> resourceList;
131                    if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(resources)) {
132                        resourceList = new ArrayList<CmsResource>();
133                        String[] resIds = resources.split(";");
134                        for (int i = 0; i < resIds.length; i++) {
135                            if (CmsUUID.isValidUUID(resIds[i])) {
136                                resourceList.add(
137                                    getCmsObject().readResource(new CmsUUID(resIds[i]), CmsResourceFilter.ALL));
138                            }
139
140                        }
141                    } else {
142                        resourceList = Collections.<CmsResource> emptyList();
143                    }
144                    String typeParam = request.getParameter("contextType");
145                    boolean isEditor = Boolean.parseBoolean(request.getParameter("editor"));
146
147                    ContextType type;
148                    String appId = "";
149                    try {
150                        type = ContextType.valueOf(typeParam);
151                        if (ContextType.containerpageToolbar.equals(type)) {
152                            appId = CmsPageEditorConfiguration.APP_ID;
153                        } else if (ContextType.sitemapToolbar.equals(type)) {
154                            appId = CmsSitemapEditorConfiguration.APP_ID;
155                        } else if (isEditor) {
156                            appId = CmsEditorConfiguration.APP_ID;
157                        }
158                    } catch (Exception e) {
159                        type = ContextType.appToolbar;
160                        LOG.error("Could not parse context type parameter " + typeParam);
161                    }
162
163                    m_currentContext = new CmsEmbeddedDialogContext(appId, type, resourceList);
164                    I_CmsWorkplaceAction action = getAction(request);
165                    if (action.isActive(m_currentContext)) {
166                        action.executeAction(m_currentContext);
167                    } else {
168                        errorMessage = CmsVaadinUtils.getMessageText(Messages.GUI_WORKPLACE_ACCESS_DENIED_TITLE_0);
169                    }
170                } catch (Throwable e) {
171                    t = e;
172                    errorMessage = CmsVaadinUtils.getMessageText(
173                        org.opencms.ui.dialogs.Messages.ERR_DAILOG_INSTANTIATION_FAILED_1,
174                        request.getPathInfo());
175                }
176            }
177        } catch (CmsRoleViolationException ex) {
178            t = ex;
179            errorMessage = CmsVaadinUtils.getMessageText(Messages.GUI_WORKPLACE_ACCESS_DENIED_TITLE_0);
180        }
181        if (errorMessage != null) {
182            CmsErrorDialog.showErrorDialog(errorMessage, t, new Runnable() {
183
184                public void run() {
185
186                    m_currentContext = new CmsEmbeddedDialogContext("", null, Collections.<CmsResource> emptyList());
187                    m_currentContext.finish((Collection<CmsUUID>)null);
188                }
189            });
190        }
191    }
192
193    /**
194     * Returns the dialog action matching the given request.<p>
195     *
196     * @param request the request
197     *
198     * @return the dialog action
199     *
200     * @throws Exception in case instantiating the action fails
201     */
202    private I_CmsWorkplaceAction getAction(VaadinRequest request) throws Exception {
203
204        String dialogId = getDialogId(request);
205        @SuppressWarnings("unchecked")
206        Class<I_CmsWorkplaceAction> actionClass = (Class<I_CmsWorkplaceAction>)getClass().getClassLoader().loadClass(
207            dialogId);
208        return actionClass.newInstance();
209    }
210
211    /**
212     * Returns the dialog id extracted from the requested path.<p>
213     *
214     * @param request the request
215     *
216     * @return the id
217     */
218    private String getDialogId(VaadinRequest request) {
219
220        String path = request.getPathInfo();
221        // remove the leading slash
222        return path != null ? path.substring(1) : null;
223    }
224}