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.file.CmsObject;
031import org.opencms.file.CmsProject;
032import org.opencms.gwt.shared.CmsGwtConstants;
033import org.opencms.main.CmsException;
034import org.opencms.main.CmsLog;
035import org.opencms.ui.A_CmsUI;
036import org.opencms.ui.components.CmsErrorDialog;
037import org.opencms.ui.components.CmsExtendedSiteSelector.SiteSelectorOption;
038import org.opencms.ui.dialogs.CmsEmbeddedDialogContext;
039import org.opencms.ui.dialogs.CmsSiteSelectDialog;
040import org.opencms.ui.favorites.CmsFavoriteEntry.Type;
041import org.opencms.util.CmsStringUtil;
042import org.opencms.util.CmsUUID;
043
044import java.util.ArrayList;
045import java.util.Optional;
046
047import org.apache.commons.logging.Log;
048
049import com.vaadin.server.VaadinRequest;
050import com.vaadin.ui.Component;
051
052/**
053 * Favorite dialog context for the case where the dialog is opened from the page editor,
054 * in an iframe.
055 */
056public class CmsPageEditorFavoriteContext implements I_CmsFavoriteContext {
057
058    /** Logger instance for this class. */
059    private static final Log LOG = CmsLog.getLog(CmsPageEditorFavoriteContext.class);
060
061    /** The dialog context. */
062    private CmsEmbeddedDialogContext m_dialogContext;
063
064    /** The current favorite location. */
065    private CmsFavoriteEntry m_entry;
066
067    /**
068     * Creates a new instance.
069     *
070     * @param context the embedded dialog context
071     * @param req the current request
072     */
073    public CmsPageEditorFavoriteContext(CmsEmbeddedDialogContext context, VaadinRequest req) {
074
075        CmsUUID detailId = toUuid(req.getParameter(CmsGwtConstants.Favorites.PARAM_DETAIL));
076        CmsUUID pageId = toUuid(req.getParameter(CmsGwtConstants.Favorites.PARAM_PAGE));
077        CmsUUID project = toUuid(req.getParameter(CmsGwtConstants.Favorites.PARAM_PROJECT));
078        String siteRoot = req.getParameter(CmsGwtConstants.Favorites.PARAM_SITE);
079        CmsFavoriteEntry entry = new CmsFavoriteEntry();
080        entry.setDetailId(detailId);
081        entry.setProjectId(project);
082        entry.setSiteRoot(siteRoot);
083        entry.setStructureId(pageId);
084        entry.setType(Type.page);
085        m_entry = entry;
086        m_dialogContext = context;
087    }
088
089    /**
090     * Converts string to UUID and returns it, or null if the conversion is not possible.
091     *
092     * @param uuid the potential UUID string
093     * @return the UUID, or null if conversion is not possible
094     */
095    private static CmsUUID toUuid(String uuid) {
096
097        if ("null".equals(uuid) || CmsStringUtil.isEmpty(uuid)) {
098            return null;
099        }
100        return new CmsUUID(uuid);
101
102    }
103
104    /**
105     * @see org.opencms.ui.favorites.I_CmsFavoriteContext#changeProject(org.opencms.util.CmsUUID)
106     */
107    public void changeProject(CmsUUID value) {
108
109        CmsObject cms = A_CmsUI.getCmsObject();
110        try {
111            CmsProject project = cms.readProject(value);
112            close();
113            A_CmsUI.get().changeProject(project);
114            m_dialogContext.finish(project, null);
115        } catch (CmsException e) {
116            LOG.error(e.getLocalizedMessage(), e);
117            CmsErrorDialog.showErrorDialog(e);
118        }
119
120    }
121
122    /**
123     * @see org.opencms.ui.favorites.I_CmsFavoriteContext#changeSite(org.opencms.ui.components.CmsExtendedSiteSelector.SiteSelectorOption)
124     */
125    public void changeSite(SiteSelectorOption value) {
126
127        CmsSiteSelectDialog.changeSite(m_dialogContext, value);
128    }
129
130    /**
131     * @see org.opencms.ui.favorites.I_CmsFavoriteContext#close()
132     */
133    public void close() {
134
135        m_dialogContext.finish(new ArrayList<>());
136
137    }
138
139    /**
140     * @see org.opencms.ui.favorites.I_CmsFavoriteContext#getFavoriteForCurrentLocation()
141     */
142    public Optional<CmsFavoriteEntry> getFavoriteForCurrentLocation() {
143
144        return Optional.ofNullable(m_entry);
145    }
146
147    /**
148     * @see org.opencms.ui.favorites.I_CmsFavoriteContext#openFavorite(org.opencms.ui.favorites.CmsFavoriteEntry)
149     */
150    public void openFavorite(CmsFavoriteEntry entry) {
151
152        try {
153            String url = entry.updateContextAndGetFavoriteUrl(A_CmsUI.getCmsObject());
154            m_dialogContext.leavePage(url);
155        } catch (CmsException e) {
156            CmsErrorDialog.showErrorDialog(e);
157        }
158    }
159
160    /**
161     * @see org.opencms.ui.favorites.I_CmsFavoriteContext#setDialog(com.vaadin.ui.Component)
162     */
163    public void setDialog(Component component) {
164
165        // not needed
166    }
167
168}