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.favorites;
029
030import org.opencms.gwt.shared.CmsGwtConstants;
031import org.opencms.main.CmsException;
032import org.opencms.ui.A_CmsUI;
033import org.opencms.ui.components.CmsErrorDialog;
034import org.opencms.ui.dialogs.CmsEmbeddedDialogContext;
035import org.opencms.ui.favorites.CmsFavoriteEntry.Type;
036import org.opencms.util.CmsStringUtil;
037import org.opencms.util.CmsUUID;
038
039import java.util.ArrayList;
040import java.util.Optional;
041
042import com.vaadin.server.VaadinRequest;
043import com.vaadin.ui.Component;
044
045/**
046 * Favorite dialog context for the case where the dialog is opened from the page editor,
047 * in an iframe.
048 */
049public class CmsPageEditorFavoriteContext implements I_CmsFavoriteContext {
050
051    /** The dialog context. */
052    private CmsEmbeddedDialogContext m_dialogContext;
053
054    /** The current favorite location. */
055    private CmsFavoriteEntry m_entry;
056
057    /**
058     * Creates a new instance.
059     *
060     * @param context the embedded dialog context
061     * @param req the current request
062     */
063    public CmsPageEditorFavoriteContext(CmsEmbeddedDialogContext context, VaadinRequest req) {
064
065        CmsUUID detailId = toUuid(req.getParameter(CmsGwtConstants.Favorites.PARAM_DETAIL));
066        CmsUUID pageId = toUuid(req.getParameter(CmsGwtConstants.Favorites.PARAM_PAGE));
067        CmsUUID project = toUuid(req.getParameter(CmsGwtConstants.Favorites.PARAM_PROJECT));
068        String siteRoot = req.getParameter(CmsGwtConstants.Favorites.PARAM_SITE);
069        CmsFavoriteEntry entry = new CmsFavoriteEntry();
070        entry.setDetailId(detailId);
071        entry.setProjectId(project);
072        entry.setSiteRoot(siteRoot);
073        entry.setStructureId(pageId);
074        entry.setType(Type.page);
075        m_entry = entry;
076        m_dialogContext = context;
077    }
078
079    /**
080     * Converts string to UUID and returns it, or null if the conversion is not possible.
081     *
082     * @param uuid the potential UUID string
083     * @return the UUID, or null if conversion is not possible
084     */
085    private static CmsUUID toUuid(String uuid) {
086
087        if ("null".equals(uuid) || CmsStringUtil.isEmpty(uuid)) {
088            return null;
089        }
090        return new CmsUUID(uuid);
091
092    }
093
094    /**
095     * @see org.opencms.ui.favorites.I_CmsFavoriteContext#close()
096     */
097    public void close() {
098
099        m_dialogContext.finish(new ArrayList<>());
100
101    }
102
103    /**
104     * @see org.opencms.ui.favorites.I_CmsFavoriteContext#getFavoriteForCurrentLocation()
105     */
106    public Optional<CmsFavoriteEntry> getFavoriteForCurrentLocation() {
107
108        return Optional.ofNullable(m_entry);
109    }
110
111    /**
112     * @see org.opencms.ui.favorites.I_CmsFavoriteContext#openFavorite(org.opencms.ui.favorites.CmsFavoriteEntry)
113     */
114    public void openFavorite(CmsFavoriteEntry entry) {
115
116        try {
117            String url = entry.updateContextAndGetFavoriteUrl(A_CmsUI.getCmsObject());
118            m_dialogContext.leavePage(url);
119        } catch (CmsException e) {
120            CmsErrorDialog.showErrorDialog(e);
121        }
122    }
123
124    /**
125     * @see org.opencms.ui.favorites.I_CmsFavoriteContext#setDialog(com.vaadin.ui.Component)
126     */
127    public void setDialog(Component component) {
128        // not needed
129    }
130
131}