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.gwt.shared.I_CmsAutoBeanFactory;
034import org.opencms.gwt.shared.I_CmsEmbeddedDialogInfo;
035import org.opencms.main.CmsLog;
036import org.opencms.main.OpenCms;
037import org.opencms.security.CmsRole;
038import org.opencms.security.CmsRoleViolationException;
039import org.opencms.ui.A_CmsUI;
040import org.opencms.ui.CmsVaadinUtils;
041import org.opencms.ui.I_CmsDialogContext.ContextType;
042import org.opencms.ui.actions.I_CmsWorkplaceAction;
043import org.opencms.ui.apps.CmsEditorConfiguration;
044import org.opencms.ui.apps.CmsPageEditorConfiguration;
045import org.opencms.ui.apps.CmsSitemapEditorConfiguration;
046import org.opencms.ui.apps.Messages;
047import org.opencms.ui.components.CmsErrorDialog;
048import org.opencms.ui.components.extensions.CmsEmbeddedDialogExtension;
049import org.opencms.ui.dialogs.permissions.CmsPrincipalSelectDialog;
050import org.opencms.ui.shared.rpc.I_CmsEmbeddingServerRpc;
051import org.opencms.util.CmsStringUtil;
052import org.opencms.util.CmsUUID;
053
054import java.util.ArrayList;
055import java.util.Collection;
056import java.util.Collections;
057import java.util.List;
058import java.util.Locale;
059
060import org.apache.commons.logging.Log;
061
062import com.google.web.bindery.autobean.shared.AutoBeanCodex;
063import com.google.web.bindery.autobean.vm.AutoBeanFactorySource;
064import com.vaadin.annotations.Theme;
065import com.vaadin.server.VaadinRequest;
066
067/**
068 * Separate UI for VAADIN based dialog embedded into a GWT module.<p>
069 */
070@Theme("opencms")
071public class CmsEmbeddedDialogsUI extends A_CmsUI implements I_CmsEmbeddingServerRpc {
072
073    /** The dialogs path fragment. */
074    public static final String DIALOGS_PATH = "dialogs/";
075
076    /** Logger instance for this class. */
077    private static final Log LOG = CmsLog.getLog(CmsEmbeddedDialogsUI.class);
078
079    /** The serial version id. */
080    private static final long serialVersionUID = 1201184887611215370L;
081
082    /** The auto bean factory for the dialog configuration. */
083    private static I_CmsAutoBeanFactory m_beanFactory = AutoBeanFactorySource.create(I_CmsAutoBeanFactory.class);
084
085    /**
086     * The dialog context of the currently opened dialog.<p>
087     */
088    CmsEmbeddedDialogContext m_currentContext;
089
090    /** The extension used to communicate with the client. */
091    protected CmsEmbeddedDialogExtension m_extension;
092
093    /**
094     * Returns the context path for embedded dialogs.<p>
095     *
096     * @return the context path for embedded dialogs
097     */
098    public static String getEmbeddedDialogsContextPath() {
099
100        return CmsStringUtil.joinPaths(OpenCms.getSystemInfo().getWorkplaceContext(), DIALOGS_PATH);
101    }
102
103    /**
104     * @see com.vaadin.ui.AbstractComponent#getLocale()
105     */
106    @Override
107    public Locale getLocale() {
108
109        CmsObject cms = getCmsObject();
110        return OpenCms.getWorkplaceManager().getWorkplaceLocale(cms);
111    }
112
113    /**
114     * @see org.opencms.ui.shared.rpc.I_CmsEmbeddingServerRpc#loadDialog(java.lang.String)
115     */
116    public void loadDialog(String dialogInfo) {
117
118        Throwable t = null;
119        String errorMessage = null;
120        I_CmsEmbeddedDialogInfo info = AutoBeanCodex.decode(
121            m_beanFactory,
122            I_CmsEmbeddedDialogInfo.class,
123            dialogInfo).as();
124
125        try {
126            OpenCms.getRoleManager().checkRole(getCmsObject(), CmsRole.ELEMENT_AUTHOR);
127            if (CmsPrincipalSelectDialog.DIALOG_ID.equals(info.getDialogId())) {
128                m_currentContext = new CmsEmbeddedDialogContext(
129                    CmsPrincipalSelectDialog.DIALOG_ID,
130                    m_extension,
131                    null,
132                    null,
133                    info.getParameters());
134                CmsPrincipalSelectDialog.openEmbeddedDialogV2(m_currentContext, info.getParameters(), true);
135            } else {
136                try {
137                    List<String> resources = info.getStructureIds();
138                    List<CmsResource> resourceList = new ArrayList<>();
139                    for (String uuid : resources) {
140                        if (CmsUUID.isValidUUID(uuid)) {
141                            resourceList.add(getCmsObject().readResource(new CmsUUID(uuid), CmsResourceFilter.ALL));
142                        }
143
144                    }
145                    String typeParam = info.getContextType();
146                    boolean isEditor = Boolean.parseBoolean(info.getParameters().get("editor"));
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(
164                        appId,
165                        m_extension,
166                        type,
167                        resourceList,
168                        info.getParameters());
169                    I_CmsWorkplaceAction action = getAction(info.getDialogId());
170                    if (action.isActive(m_currentContext)) {
171                        action.executeAction(m_currentContext);
172                    } else {
173                        errorMessage = CmsVaadinUtils.getMessageText(Messages.GUI_WORKPLACE_ACCESS_DENIED_TITLE_0);
174                    }
175                } catch (Throwable e) {
176                    t = e;
177                    errorMessage = CmsVaadinUtils.getMessageText(
178                        org.opencms.ui.dialogs.Messages.ERR_DAILOG_INSTANTIATION_FAILED_1,
179                        info.getDialogId());
180                }
181            }
182        } catch (CmsRoleViolationException ex) {
183            t = ex;
184            errorMessage = CmsVaadinUtils.getMessageText(Messages.GUI_WORKPLACE_ACCESS_DENIED_TITLE_0);
185        }
186        if (errorMessage != null) {
187            CmsErrorDialog.showErrorDialog(errorMessage, t, new Runnable() {
188
189                public void run() {
190
191                    m_currentContext = new CmsEmbeddedDialogContext(
192                        "",
193                        m_extension,
194                        null,
195                        Collections.<CmsResource> emptyList(),
196                        info.getParameters());
197                    m_currentContext.finish((Collection<CmsUUID>)null);
198                }
199            });
200        }
201
202    }
203
204    /**
205     * @see org.opencms.ui.A_CmsUI#reload()
206     */
207    @Override
208    public void reload() {
209
210        if (m_currentContext != null) {
211            m_currentContext.reload();
212        }
213    }
214
215    /**
216     * @see org.opencms.ui.A_CmsUI#init(com.vaadin.server.VaadinRequest)
217     */
218    @Override
219    protected void init(VaadinRequest request) {
220
221        super.init(request);
222        try {
223            OpenCms.getRoleManager().checkRole(getCmsObject(), CmsRole.ELEMENT_AUTHOR);
224        } catch (Exception e) {
225            throw new RuntimeException(e);
226        }
227        m_extension = new CmsEmbeddedDialogExtension(this);
228        m_extension.getClientRPC().initServerRpc();
229    }
230
231    /**
232     * Returns the dialog action for a given dialog id.
233     *
234     * @param dialogId the dialog id
235     *
236     * @return the dialog action
237     *
238     * @throws Exception in case instantiating the action fails
239     */
240    private I_CmsWorkplaceAction getAction(String dialogId) throws Exception {
241
242        @SuppressWarnings("unchecked")
243        Class<I_CmsWorkplaceAction> actionClass = (Class<I_CmsWorkplaceAction>)getClass().getClassLoader().loadClass(
244            dialogId);
245        return actionClass.newInstance();
246    }
247
248}