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.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        this("");
058    }
059
060    /**
061     * Creates a new instance.<p>
062     *
063     * @param config the widget configuration
064     */
065    public CmsDataViewWidget(String config) {
066        m_config = config;
067    }
068
069    /**
070     * @see org.opencms.widgets.I_CmsComplexWidget#configure(java.lang.String)
071     */
072    public I_CmsComplexWidget configure(String configuration) {
073
074        validateConfiguration(configuration);
075        return new CmsDataViewWidget(configuration);
076    }
077
078    /**
079     * @see org.opencms.widgets.I_CmsComplexWidget#getName()
080     */
081    public String getName() {
082
083        return "dataview";
084    }
085
086    /**
087     * @see org.opencms.widgets.I_CmsComplexWidget#getWidgetData(org.opencms.file.CmsObject)
088     */
089    public CmsComplexWidgetData getWidgetData(CmsObject cms) {
090
091        String configToUse = m_config;
092        try {
093            JSONObject json = new JSONObject(m_config);
094            String icon = json.optString(CmsDataViewConstants.CONFIG_ICON);
095            if (icon != null) {
096                String iconLink = OpenCms.getLinkManager().substituteLinkForUnknownTarget(cms, icon);
097                json.put(CmsDataViewConstants.CONFIG_ICON, iconLink);
098                configToUse = json.toString();
099            }
100        } catch (Exception e) {
101            LOG.error(e.getLocalizedMessage(), e);
102        }
103
104        return new CmsComplexWidgetData(CmsDataViewConstants.RENDERER_ID, configToUse, null);
105    }
106
107    /**
108     * Validates the configuration.<p>
109     *
110     * @param configuration the configuration
111     */
112    public void validateConfiguration(String configuration) {
113
114        try {
115            JSONObject json = new JSONObject(configuration);
116            String className = json.optString(CmsDataViewConstants.CONFIG_VIEW_CLASS);
117
118            Class<?> cls = Class.forName(className, false, getClass().getClassLoader());
119            if (!I_CmsDataView.class.isAssignableFrom(cls)) {
120                throw new IllegalArgumentException(
121                    "Class " + cls.getName() + " does not implement " + I_CmsDataView.class.getName());
122            }
123        } catch (Exception e) {
124            throw new CmsWidgetConfigurationException(e);
125        }
126
127    }
128
129}