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