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