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