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 GmbH & Co. KG, 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.file.CmsObject;
031import org.opencms.file.CmsPropertyDefinition;
032import org.opencms.file.CmsResource;
033import org.opencms.i18n.CmsEncoder;
034import org.opencms.i18n.CmsMessages;
035import org.opencms.json.JSONException;
036import org.opencms.json.JSONObject;
037import org.opencms.main.CmsException;
038import org.opencms.main.CmsLog;
039import org.opencms.main.OpenCms;
040import org.opencms.util.CmsStringUtil;
041import org.opencms.xml.content.I_CmsXmlContentHandler.DisplayType;
042import org.opencms.xml.types.A_CmsXmlContentValue;
043
044import java.util.List;
045import java.util.Locale;
046import java.util.Set;
047
048import org.apache.commons.logging.Log;
049
050/**
051 * Provides a display only widget, for use on a widget dialog.<p>
052 *
053 * @since 6.0.0
054 */
055public class CmsLocationPickerWidget extends A_CmsWidget implements I_CmsADEWidget {
056
057    /** The default widget configuration. */
058    private static final String DEFAULT_CONFIG = "{\"edit\":[\"map\", \"address\", \"coords\", \"size\", \"zoom\", \"type\", \"mode\"]}";
059
060    /** Key post fix, so you can display different help text if used a "normal" widget, and a display widget. */
061    private static final String DISABLED_POSTFIX = ".disabled";
062
063    /** The configuration key for the Google maps API key. */
064    public static final String CONFIG_API_KEY = "apiKey";
065
066    /** The configuration key for the Google maps API key. */
067    public static final String CONFIG_API_KEY_MESSAGE = "apiKeyMessage";
068
069    /** The logger instance for this class. */
070    private static final Log LOG = CmsLog.getLog(CmsLocationPickerWidget.class);
071
072    /**
073     * Creates a new input widget.<p>
074     */
075    public CmsLocationPickerWidget() {
076
077        // empty constructor is required for class registration
078        this("");
079    }
080
081    /**
082     * Creates a new input widget with the given configuration.<p>
083     *
084     * @param configuration the configuration to use
085     */
086    public CmsLocationPickerWidget(String configuration) {
087
088        super(configuration);
089    }
090
091    /**
092     * @see org.opencms.widgets.I_CmsADEWidget#getConfiguration(org.opencms.file.CmsObject, org.opencms.xml.types.A_CmsXmlContentValue, org.opencms.i18n.CmsMessages, org.opencms.file.CmsResource, java.util.Locale)
093     */
094    public String getConfiguration(
095        CmsObject cms,
096        A_CmsXmlContentValue schemaType,
097        CmsMessages messages,
098        CmsResource resource,
099        Locale contentLocale) {
100
101        String config = getConfiguration();
102        if (CmsStringUtil.isEmptyOrWhitespaceOnly(config)) {
103            config = DEFAULT_CONFIG;
104        } else {
105            if (!config.startsWith("{")) {
106                config = "{" + config + "}";
107            }
108        }
109        try {
110            // make sure the configuration is a parsable JSON string
111            JSONObject conf = new JSONObject(config);
112            String keyMessage = Messages.get().getBundle().key(Messages.LOG_API_KEY_FROM_CONFIG_0);
113            String apiKey = conf.has(CONFIG_API_KEY) ? conf.getString(CONFIG_API_KEY) : "";
114            String sitePath = null;
115            if (CmsStringUtil.isEmptyOrWhitespaceOnly(apiKey)) {
116                if (resource.getStructureId().isNullUUID()) {
117                    sitePath = "/";
118                } else {
119                    sitePath = cms.getSitePath(resource);
120                }
121                try {
122                    apiKey = cms.readPropertyObject(
123                        sitePath,
124                        CmsPropertyDefinition.PROPERTY_GOOGLE_API_KEY_WORKPLACE,
125                        true).getValue();
126                    keyMessage = Messages.get().getBundle().key(
127                        Messages.LOG_API_KEY_FROM_PROPERTY_1,
128                        CmsPropertyDefinition.PROPERTY_GOOGLE_API_KEY_WORKPLACE);
129                    if (CmsStringUtil.isEmptyOrWhitespaceOnly(apiKey)) {
130                        apiKey = cms.readPropertyObject(
131                            sitePath,
132                            CmsPropertyDefinition.PROPERTY_GOOGLE_API_KEY,
133                            true).getValue();
134                        keyMessage = Messages.get().getBundle().key(
135                            Messages.LOG_API_KEY_FROM_PROPERTY_1,
136                            CmsPropertyDefinition.PROPERTY_GOOGLE_API_KEY);
137                    }
138                } catch (CmsException e) {
139                    LOG.error(e.getLocalizedMessage(), e);
140                }
141            }
142            if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(apiKey)) {
143                apiKey = apiKey.trim();
144                conf.put(CONFIG_API_KEY, apiKey);
145                conf.put(CONFIG_API_KEY_MESSAGE, keyMessage);
146                if (LOG.isDebugEnabled()) {
147                    if (sitePath != null) {
148                        keyMessage += Messages.get().getBundle().key(
149                            Messages.LOG_API_KEY_FOR_PATH_1,
150                            cms.getRequestContext().addSiteRoot(sitePath));
151                    }
152                    LOG.debug(keyMessage);
153                }
154            }
155
156            config = conf.toString();
157        } catch (JSONException e) {
158            config = DEFAULT_CONFIG;
159            LOG.error(e.getLocalizedMessage(), e);
160        }
161
162        return config;
163    }
164
165    /**
166     * @see org.opencms.widgets.I_CmsADEWidget#getCssResourceLinks(org.opencms.file.CmsObject)
167     */
168    public List<String> getCssResourceLinks(CmsObject cms) {
169
170        return null;
171    }
172
173    /**
174     * @see org.opencms.widgets.I_CmsADEWidget#getDefaultDisplayType()
175     */
176    public DisplayType getDefaultDisplayType() {
177
178        return DisplayType.singleline;
179    }
180
181    /**
182     * @see org.opencms.widgets.I_CmsWidget#getDialogWidget(org.opencms.file.CmsObject, org.opencms.widgets.I_CmsWidgetDialog, org.opencms.widgets.I_CmsWidgetParameter)
183     */
184    public String getDialogWidget(CmsObject cms, I_CmsWidgetDialog widgetDialog, I_CmsWidgetParameter param) {
185
186        String value = param.getStringValue(cms);
187        String localizedValue = value;
188        if (CmsStringUtil.TRUE.equalsIgnoreCase(value) || CmsStringUtil.FALSE.equalsIgnoreCase(value)) {
189            boolean booleanValue = Boolean.valueOf(value).booleanValue();
190            if (booleanValue) {
191                localizedValue = Messages.get().getBundle(widgetDialog.getLocale()).key(Messages.GUI_LABEL_TRUE_0);
192            } else {
193                localizedValue = Messages.get().getBundle(widgetDialog.getLocale()).key(Messages.GUI_LABEL_FALSE_0);
194            }
195        }
196
197        String id = param.getId();
198        StringBuffer result = new StringBuffer(16);
199        result.append("<td class=\"xmlTd\">");
200        result.append("<span class=\"xmlInput textInput\" style=\"border: 0px solid black;\">");
201        if (CmsStringUtil.isNotEmpty(getConfiguration())) {
202            result.append(getConfiguration());
203        } else {
204            result.append(localizedValue);
205        }
206        result.append("</span>");
207        result.append("<input type=\"hidden\"");
208        result.append(" name=\"");
209        result.append(id);
210        result.append("\" id=\"");
211        result.append(id);
212        result.append("\" value=\"");
213        result.append(CmsEncoder.escapeXml(value));
214        result.append("\">");
215        result.append("</td>");
216
217        return result.toString();
218    }
219
220    /**
221     * @see org.opencms.widgets.A_CmsWidget#getHelpBubble(org.opencms.file.CmsObject, org.opencms.widgets.I_CmsWidgetDialog, org.opencms.widgets.I_CmsWidgetParameter)
222     */
223    @Override
224    public String getHelpBubble(CmsObject cms, I_CmsWidgetDialog widgetDialog, I_CmsWidgetParameter param) {
225
226        StringBuffer result = new StringBuffer(128);
227        String locKey = getDisabledHelpKey(param);
228        String locValue = widgetDialog.getMessages().key(locKey, true);
229        if (locValue == null) {
230            // there was no help message found for this key, so return a spacer cell
231            return widgetDialog.dialogHorizontalSpacer(16);
232        } else {
233            result.append("<td>");
234            result.append("<img name=\"img");
235            result.append(locKey);
236            result.append("\" id=\"img");
237            result.append(locKey);
238            result.append("\" src=\"");
239            result.append(OpenCms.getLinkManager().substituteLink(cms, "/system/workplace/resources/commons/help.png"));
240            result.append("\" alt=\"\" border=\"0\"");
241            if (widgetDialog.useNewStyle()) {
242                result.append(getJsHelpMouseHandler(widgetDialog, locKey, null));
243            } else {
244                result.append(
245                    getJsHelpMouseHandler(
246                        widgetDialog,
247                        locKey,
248                        CmsEncoder.escape(locValue, cms.getRequestContext().getEncoding())));
249            }
250            result.append("></td>");
251            return result.toString();
252        }
253    }
254
255    /**
256     * @see org.opencms.widgets.A_CmsWidget#getHelpText(org.opencms.widgets.I_CmsWidgetDialog, org.opencms.widgets.I_CmsWidgetParameter)
257     */
258    @Override
259    public String getHelpText(I_CmsWidgetDialog widgetDialog, I_CmsWidgetParameter param) {
260
261        String helpId = getDisabledHelpKey(param);
262        Set<String> helpIdsShown = widgetDialog.getHelpMessageIds();
263        if (helpIdsShown.contains(helpId)) {
264            // help hey has already been included in output
265            return "";
266        }
267        helpIdsShown.add(helpId);
268
269        // calculate the key
270        String locValue = widgetDialog.getMessages().key(helpId, true);
271        if (locValue == null) {
272            // there was no help message found for this key, so return an empty string
273            return "";
274        } else {
275            if (widgetDialog.useNewStyle()) {
276                StringBuffer result = new StringBuffer(128);
277                result.append("<div class=\"help\" id=\"help");
278                result.append(helpId);
279                result.append("\"");
280                result.append(getJsHelpMouseHandler(widgetDialog, helpId, helpId));
281                result.append(">");
282                result.append(locValue);
283                result.append("</div>\n");
284                return result.toString();
285            } else {
286                return "";
287            }
288        }
289    }
290
291    /**
292     * @see org.opencms.widgets.I_CmsADEWidget#getInitCall()
293     */
294    public String getInitCall() {
295
296        return null;
297    }
298
299    /**
300     * @see org.opencms.widgets.I_CmsADEWidget#getJavaScriptResourceLinks(org.opencms.file.CmsObject)
301     */
302    public List<String> getJavaScriptResourceLinks(CmsObject cms) {
303
304        return null;
305    }
306
307    /**
308     * @see org.opencms.widgets.I_CmsADEWidget#getWidgetName()
309     */
310    public String getWidgetName() {
311
312        return CmsLocationPickerWidget.class.getName();
313    }
314
315    /**
316     * @see org.opencms.widgets.I_CmsADEWidget#isInternal()
317     */
318    public boolean isInternal() {
319
320        return true;
321    }
322
323    /**
324     * @see org.opencms.widgets.I_CmsWidget#newInstance()
325     */
326    public I_CmsWidget newInstance() {
327
328        return new CmsLocationPickerWidget(getConfiguration());
329    }
330
331    /**
332     * Returns the localized help key for the provided widget parameter.<p>
333     *
334     * @param param the widget parameter to return the localized help key for
335     *
336     * @return the localized help key for the provided widget parameter
337     */
338    private String getDisabledHelpKey(I_CmsWidgetParameter param) {
339
340        StringBuffer result = new StringBuffer(64);
341        result.append(LABEL_PREFIX);
342        result.append(param.getKey());
343        result.append(HELP_POSTFIX);
344        result.append(DISABLED_POSTFIX);
345        return result.toString();
346    }
347}