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.dialogs.embedded;
029
030import org.opencms.file.CmsObject;
031import org.opencms.gwt.shared.CmsDataViewConstants;
032import org.opencms.gwt.shared.CmsDataViewParamEncoder;
033import org.opencms.json.JSONArray;
034import org.opencms.json.JSONException;
035import org.opencms.json.JSONObject;
036import org.opencms.main.CmsLog;
037import org.opencms.util.CmsRequestUtil;
038import org.opencms.util.CmsStringUtil;
039import org.opencms.widgets.dataview.I_CmsDataView;
040import org.opencms.widgets.dataview.I_CmsDataViewItem;
041
042import java.net.URI;
043import java.util.List;
044import java.util.Locale;
045
046import org.apache.commons.logging.Log;
047
048import com.google.common.collect.Multimap;
049
050/**
051 * Class representing the configuration passed to the Vaadin data view dialog by the client.<p>
052 */
053public class CmsDataViewParams {
054
055    /** Logger instance for this class. */
056    private static final Log LOG = CmsLog.getLog(CmsDataViewParams.class);
057
058    /** The callback to call with the selected items. */
059    private String m_callback;
060
061    /** Argument to pass back to the callback. */
062    private String m_callbackArg;
063
064    /** Value of multiselect option. */
065    private String m_multiSelect;
066
067    /** Value of class option. */
068    private String m_viewClass;
069
070    /** Configuration string for the data view class. */
071    private String m_viewArg;
072
073    /**
074     * Creates a new instance by parsing the query string of the given URI.<p>
075     *
076     * @param uri the URI from which to read the configuration
077     */
078    public CmsDataViewParams(URI uri) {
079        Multimap<String, String> params = CmsRequestUtil.getParameters(uri);
080        if (params.containsKey(CmsDataViewConstants.PARAM_CONFIG)) {
081            String encodedConfig = params.get(CmsDataViewConstants.PARAM_CONFIG).iterator().next();
082            try {
083                JSONObject json = new JSONObject(CmsDataViewParamEncoder.decodeString(encodedConfig));
084
085                m_callback = json.optString(CmsDataViewConstants.PARAM_CALLBACK);
086                m_callbackArg = json.optString(CmsDataViewConstants.PARAM_CALLBACK_ARG);
087                m_viewClass = json.optString(CmsDataViewConstants.CONFIG_VIEW_CLASS);
088                m_viewArg = json.optString(CmsDataViewConstants.CONFIG_VIEW_ARG);
089                m_multiSelect = json.optString(CmsDataViewConstants.CONFIG_MULTI_SELECT);
090            } catch (JSONException e) {
091                LOG.error(e.getLocalizedMessage(), e);
092            }
093        }
094    }
095
096    /**
097     * Creates the data view instance.<p>
098     *
099     * @param cms the CMS context
100     * @param locale the locale
101     * @return the new data view instance
102     */
103    public I_CmsDataView createViewInstance(CmsObject cms, Locale locale) {
104
105        try {
106            Class<?> cls = Class.forName(m_viewClass);
107            Object viewObj = cls.newInstance();
108            I_CmsDataView dataView = (I_CmsDataView)viewObj;
109            dataView.initialize(cms, m_viewArg, locale);
110            return dataView;
111        } catch (Exception e) {
112            LOG.error(e.getLocalizedMessage(), e);
113            return null;
114        }
115    }
116
117    /**
118     * Return true if the list should have multi-selection enabled.<p>
119     *
120     * @return true if multi-selection should be enabled
121     */
122    public boolean isMultiSelect() {
123
124        return Boolean.parseBoolean(m_multiSelect);
125    }
126
127    /**
128     * Creates the script which calls the callback with the result.<p>
129     *
130     * @param result the list of result data items
131     * @return the script to call the callback
132     */
133    public String prepareCallbackScript(List<I_CmsDataViewItem> result) {
134
135        try {
136            if (CmsStringUtil.isEmptyOrWhitespaceOnly(m_callbackArg)) {
137                m_callbackArg = "{}";
138            }
139            JSONObject obj = new JSONObject(m_callbackArg);
140            JSONArray selection = new JSONArray();
141            for (I_CmsDataViewItem item : result) {
142                JSONObject singleResult = new JSONObject();
143                singleResult.put(CmsDataViewConstants.FIELD_ID, item.getId());
144                singleResult.put(CmsDataViewConstants.FIELD_TITLE, item.getTitle());
145                singleResult.put(CmsDataViewConstants.FIELD_DESCRIPTION, item.getDescription());
146                singleResult.put(CmsDataViewConstants.FIELD_DATA, item.getData());
147                selection.put(singleResult);
148            }
149            obj.put(CmsDataViewConstants.KEY_RESULT, selection);
150            String jsonString = obj.toString();
151            return "parent." + m_callback + "(" + jsonString + ")";
152        } catch (Exception e) {
153            return null;
154        }
155
156    }
157
158}