001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH & Co. KG (https://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: https://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: https://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.widgets;
029
030import org.opencms.ade.contenteditor.shared.CmsComplexWidgetData;
031import org.opencms.file.CmsObject;
032import org.opencms.gwt.shared.CmsDataViewConstants;
033import org.opencms.json.JSONObject;
034import org.opencms.main.CmsLog;
035import org.opencms.main.OpenCms;
036import org.opencms.widgets.dataview.I_CmsDataView;
037
038import org.apache.commons.logging.Log;
039
040/**
041 * Complex widget for opening selecting data from a data source implementing the I_CmsDataView interface.<p>
042 *
043 * This widget can only be used
044 */
045public class CmsDataViewWidget implements I_CmsComplexWidget {
046
047    /** Logger instance for this class. */
048    private static final Log LOG = CmsLog.getLog(CmsDataViewWidget.class);
049
050    /** The widget configuration. */
051    private String m_config;
052
053    /**
054     * Default constructor.<p>
055     */
056    public CmsDataViewWidget() {
057
058        this("");
059    }
060
061    /**
062     * Creates a new instance.<p>
063     *
064     * @param config the widget configuration
065     */
066    public CmsDataViewWidget(String config) {
067
068        m_config = config;
069    }
070
071    /**
072     * @see org.opencms.widgets.I_CmsComplexWidget#configure(java.lang.String)
073     */
074    public I_CmsComplexWidget configure(String configuration) {
075
076        validateConfiguration(configuration);
077        return new CmsDataViewWidget(configuration);
078    }
079
080    /**
081     * @see org.opencms.widgets.I_CmsComplexWidget#getName()
082     */
083    public String getName() {
084
085        return "dataview";
086    }
087
088    /**
089     * @see org.opencms.widgets.I_CmsComplexWidget#getWidgetData(org.opencms.file.CmsObject)
090     */
091    public CmsComplexWidgetData getWidgetData(CmsObject cms) {
092
093        String configToUse = m_config;
094        try {
095            JSONObject json = new JSONObject(m_config);
096            String icon = json.optString(CmsDataViewConstants.CONFIG_ICON);
097            if (icon != null) {
098                String iconLink = OpenCms.getLinkManager().substituteLinkForUnknownTarget(cms, icon);
099                json.put(CmsDataViewConstants.CONFIG_ICON, iconLink);
100                configToUse = json.toString();
101            }
102        } catch (Exception e) {
103            LOG.error(e.getLocalizedMessage(), e);
104        }
105
106        return new CmsComplexWidgetData(CmsDataViewConstants.RENDERER_ID, configToUse, null);
107    }
108
109    /**
110     * Validates the configuration.<p>
111     *
112     * @param configuration the configuration
113     */
114    public void validateConfiguration(String configuration) {
115
116        try {
117            JSONObject json = new JSONObject(configuration);
118            String className = json.optString(CmsDataViewConstants.CONFIG_VIEW_CLASS);
119
120            Class<?> cls = Class.forName(className, false, getClass().getClassLoader());
121            if (!I_CmsDataView.class.isAssignableFrom(cls)) {
122                throw new IllegalArgumentException(
123                    "Class " + cls.getName() + " does not implement " + I_CmsDataView.class.getName());
124            }
125        } catch (Exception e) {
126            throw new CmsWidgetConfigurationException(e);
127        }
128
129    }
130
131}