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