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.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( 080 CmsDataViewParamEncoder.decodeString( 081 context.getParameters().get(CmsDataViewConstants.PARAM_CONFIG))); 082 m_callback = json.optString(CmsDataViewConstants.PARAM_CALLBACK); 083 m_callbackArg = json.optString(CmsDataViewConstants.PARAM_CALLBACK_ARG); 084 m_viewClass = json.optString(CmsDataViewConstants.CONFIG_VIEW_CLASS); 085 m_viewArg = json.optString(CmsDataViewConstants.CONFIG_VIEW_ARG); 086 m_multiSelect = json.optString(CmsDataViewConstants.CONFIG_MULTI_SELECT); 087 } catch (JSONException e) { 088 LOG.error(e.getLocalizedMessage(), e); 089 } 090 } 091 } 092 093 /** 094 * Creates the data view instance.<p> 095 * 096 * @param cms the CMS context 097 * @param locale the locale 098 * @return the new data view instance 099 */ 100 public I_CmsDataView createViewInstance(CmsObject cms, Locale locale) { 101 102 try { 103 Class<?> cls = Class.forName(m_viewClass); 104 Object viewObj = cls.newInstance(); 105 I_CmsDataView dataView = (I_CmsDataView)viewObj; 106 dataView.initialize(cms, m_viewArg, locale); 107 return dataView; 108 } catch (Exception e) { 109 LOG.error(e.getLocalizedMessage(), e); 110 return null; 111 } 112 } 113 114 /** 115 * Return true if the list should have multi-selection enabled.<p> 116 * 117 * @return true if multi-selection should be enabled 118 */ 119 public boolean isMultiSelect() { 120 121 return Boolean.parseBoolean(m_multiSelect); 122 } 123 124 /** 125 * Creates the script which calls the callback with the result.<p> 126 * 127 * @param result the list of result data items 128 * @return the script to call the callback 129 */ 130 public String prepareCallbackScript(List<I_CmsDataViewItem> result) { 131 132 try { 133 if (CmsStringUtil.isEmptyOrWhitespaceOnly(m_callbackArg)) { 134 m_callbackArg = "{}"; 135 } 136 JSONObject obj = new JSONObject(m_callbackArg); 137 JSONArray selection = new JSONArray(); 138 for (I_CmsDataViewItem item : result) { 139 JSONObject singleResult = new JSONObject(); 140 singleResult.put(CmsDataViewConstants.FIELD_ID, item.getId()); 141 singleResult.put(CmsDataViewConstants.FIELD_TITLE, item.getTitle()); 142 singleResult.put(CmsDataViewConstants.FIELD_DESCRIPTION, item.getDescription()); 143 singleResult.put(CmsDataViewConstants.FIELD_DATA, item.getData()); 144 selection.put(singleResult); 145 } 146 obj.put(CmsDataViewConstants.KEY_RESULT, selection); 147 String jsonString = obj.toString(); 148 return "parent." + m_callback + "(" + jsonString + ")"; 149 } catch (Exception e) { 150 return null; 151 } 152 153 } 154 155}