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.ade.containerpage.shared;
029
030import org.opencms.gwt.shared.CmsAdditionalInfoBean;
031import org.opencms.gwt.shared.CmsTemplateContextInfo;
032import org.opencms.util.CmsStringUtil;
033import org.opencms.xml.content.CmsXmlContentProperty;
034
035import java.util.ArrayList;
036import java.util.Collections;
037import java.util.List;
038import java.util.Map;
039import java.util.Map.Entry;
040import java.util.Set;
041
042/**
043 * Bean holding all element information including it's formatted contents.<p>
044 *
045 * @since 8.0.0
046 */
047public class CmsContainerElementData extends CmsContainerElement {
048
049    /** The contents by container type. */
050    private Map<String, String> m_contents;
051
052    /** The group-container description. */
053    private String m_description;
054
055    /** Dragging an element may require changing its settings, but this changes the id since it is computed from the settings. In the DND case this field contains the client id of the element with the changed settings. */
056    private String m_dndId;
057
058    /** The formatter configurations by container. */
059    private Map<String, Map<String, CmsFormatterConfig>> m_formatters;
060
061    /** The inheritance infos off all sub-items. */
062    private List<CmsInheritanceInfo> m_inheritanceInfos = new ArrayList<CmsInheritanceInfo>();
063
064    /** The inheritance name. */
065    private String m_inheritanceName;
066
067    /** Indicates whether this element is used as a group. */
068    private boolean m_isGroup;
069
070    /** The last user modifying the element. */
071    private String m_lastModifiedByUser;
072
073    /** The date of last modification. */
074    private long m_lastModifiedDate;
075
076    /** The time this element was loaded. */
077    private long m_loadTime;
078
079    /** The element navText property. */
080    private String m_navText;
081
082    /** The settings for this container entry. */
083    private Map<String, String> m_settings;
084
085    /** The contained sub-item id's. */
086    private List<String> m_subItems = new ArrayList<String>();
087
088    /** The title. */
089    private String m_title;
090
091    /** The supported container types of a group-container. */
092    private Set<String> m_types;
093
094    /**
095     * Returns the contents.<p>
096     *
097     * @return the contents
098     */
099    public Map<String, String> getContents() {
100
101        return m_contents;
102    }
103
104    /**
105     * Returns the required css resources.<p>
106     *
107     * @param containerName the current container name
108     *
109     * @return the required css resources
110     */
111    public Set<String> getCssResources(String containerName) {
112
113        CmsFormatterConfig formatterConfig = getFormatterConfig(containerName);
114        return formatterConfig != null ? formatterConfig.getCssResources() : Collections.<String> emptySet();
115    }
116
117    /**
118     * Returns the description.<p>
119     *
120     * @return the description
121     */
122    public String getDescription() {
123
124        return m_description;
125    }
126
127    /**
128     * Dragging an element may require changing its settings, but this changes the id since it is computed from the settings. In the DND case this method returns the client id of the element with the changed settings.<p>
129     *
130     * @return the drag and drop element id, or null if it isn't available or needed
131     */
132    public String getDndId() {
133
134        return m_dndId;
135    }
136
137    /**
138     * Returns the individual element settings formated with nice-names to be used as additional-info.<p>
139     *
140     * @param containerId the container id
141     *
142     * @return the settings list
143     */
144    public List<CmsAdditionalInfoBean> getFormatedIndividualSettings(String containerId) {
145
146        List<CmsAdditionalInfoBean> result = new ArrayList<CmsAdditionalInfoBean>();
147        CmsFormatterConfig config = getFormatterConfig(containerId);
148        if ((m_settings != null) && (config != null)) {
149            for (Entry<String, String> settingEntry : m_settings.entrySet()) {
150                String settingKey = settingEntry.getKey();
151                if (config.getSettingConfig().containsKey(settingEntry.getKey())) {
152                    String niceName = config.getSettingConfig().get(settingEntry.getKey()).getNiceName();
153                    if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(
154                        config.getSettingConfig().get(settingEntry.getKey()).getNiceName())) {
155                        settingKey = niceName;
156                    }
157                }
158                result.add(new CmsAdditionalInfoBean(settingKey, settingEntry.getValue(), null));
159            }
160        }
161        return result;
162    }
163
164    /**
165     * Returns the current formatter configuration.<p>
166     *
167     * @param containerName the current container name
168     *
169     * @return the current formatter configuration
170     */
171    public CmsFormatterConfig getFormatterConfig(String containerName) {
172
173        CmsFormatterConfig formatterConfig = null;
174        if (m_formatters != null) {
175            String formatterId = getSettings().get(CmsFormatterConfig.getSettingsKeyForContainer(containerName));
176            if ((formatterId != null)
177                && getFormatters().containsKey(containerName)
178                && getFormatters().get(containerName).containsKey(formatterId)) {
179                // if the settings contain the formatter id, use the matching config
180                formatterConfig = getFormatters().get(containerName).get(formatterId);
181            } else if (getFormatters().containsKey(containerName) && !getFormatters().get(containerName).isEmpty()) {
182                // otherwise use the first entry for the given container
183                formatterConfig = getFormatters().get(containerName).values().iterator().next();
184            }
185        }
186        return formatterConfig;
187    }
188
189    /**
190     * Returns the formatter configurations.<p>
191     *
192     * @return the formatter configurations
193     */
194    public Map<String, Map<String, CmsFormatterConfig>> getFormatters() {
195
196        return m_formatters;
197    }
198
199    /**
200     * Returns the inheritance infos off all sub-items.<p>
201     *
202     * @return the inheritance infos off all sub-items.
203     */
204    public List<CmsInheritanceInfo> getInheritanceInfos() {
205
206        if (isInheritContainer()) {
207            return m_inheritanceInfos;
208        } else {
209            throw new UnsupportedOperationException("Only inherit containers have inheritance infos");
210        }
211    }
212
213    /**
214     * Returns the inheritance name.<p>
215     *
216     * @return the inheritance name
217     */
218    public String getInheritanceName() {
219
220        return m_inheritanceName;
221    }
222
223    /**
224     * Returns the last modifying user.<p>
225     *
226     * @return the last modifying user
227     */
228    public String getLastModifiedByUser() {
229
230        return m_lastModifiedByUser;
231    }
232
233    /**
234     * Returns the last modification date.<p>
235     *
236     * @return the last modification date
237     */
238    public long getLastModifiedDate() {
239
240        return m_lastModifiedDate;
241    }
242
243    /**
244     * Returns the load time.<p>
245     *
246     * @return the load time
247     */
248    public long getLoadTime() {
249
250        return m_loadTime;
251    }
252
253    /**
254     * Returns the navText.<p>
255     *
256     * @return the navText
257     */
258    public String getNavText() {
259
260        return m_navText;
261    }
262
263    /**
264     * Gets the setting configuration for this container element.<p>
265     *
266     * @param containerName the current container name
267     *
268     * @return the setting configuration map
269     */
270    public Map<String, CmsXmlContentProperty> getSettingConfig(String containerName) {
271
272        CmsFormatterConfig formatterConfig = getFormatterConfig(containerName);
273        return formatterConfig != null
274        ? formatterConfig.getSettingConfig()
275        : Collections.<String, CmsXmlContentProperty> emptyMap();
276    }
277
278    /**
279     * Returns the settings for this container element.<p>
280     *
281     * @return a map of settings
282     */
283    public Map<String, String> getSettings() {
284
285        return m_settings;
286    }
287
288    /**
289     * Returns the sub-items.<p>
290     *
291     * @return the sub-items
292     */
293    public List<String> getSubItems() {
294
295        if (isGroupContainer()) {
296            return m_subItems;
297        } else if (isInheritContainer()) {
298            List<String> result = new ArrayList<String>();
299            for (CmsInheritanceInfo info : m_inheritanceInfos) {
300                if (info.isVisible()) {
301                    result.add(info.getClientId());
302                }
303            }
304            return result;
305        } else {
306            throw new UnsupportedOperationException("Only group or inherit containers have sub-items");
307        }
308    }
309
310    /**
311     * Returns the supported container types.<p>
312     *
313     * @return the supported container types
314     */
315    @Override
316    public String getTitle() {
317
318        return m_title;
319    }
320
321    /**
322     * If this element represents an element group, this method will return the supported container type.<p>
323     *
324     * @return the supported container types
325     */
326    public Set<String> getTypes() {
327
328        return m_types;
329    }
330
331    /**
332     * Returns if there are alternative formatters available for the given container.<p>
333     *
334     * @param containerName the container name
335     *
336     * @return <code>true</code> if there are alternative formatters available for the given container
337     */
338    public boolean hasAlternativeFormatters(String containerName) {
339
340        return (m_formatters.get(containerName) != null) && (m_formatters.get(containerName).size() > 1);
341    }
342
343    /**
344     * @see org.opencms.ade.containerpage.shared.CmsContainerElement#hasSettings(java.lang.String)
345     */
346    @Override
347    public boolean hasSettings(String containerId) {
348
349        // in case formatter info is not available, do the simple check
350        if ((m_formatters == null) || m_formatters.isEmpty()) {
351            return super.hasSettings(containerId);
352        } else {
353            CmsFormatterConfig config = getFormatterConfig(containerId);
354            return (config != null) && (!config.getSettingConfig().isEmpty() || hasAlternativeFormatters(containerId));
355        }
356    }
357
358    /**
359     * Returns if this element is used as a group.<p>
360     *
361     * @return if this element is used as a group
362     */
363    public boolean isGroup() {
364
365        return m_isGroup;
366    }
367
368    /**
369     * Returns true if the element should be shown in the current template context.<p>
370     *
371     * @param currentContext the current template context
372     *
373     * @return true if the element should be shown
374     */
375    public boolean isShowInContext(String currentContext) {
376
377        if ((m_settings == null)
378            || !m_settings.containsKey(CmsTemplateContextInfo.SETTING)
379            || CmsStringUtil.isEmptyOrWhitespaceOnly(m_settings.get(CmsTemplateContextInfo.SETTING))) {
380            return true;
381        }
382        return CmsStringUtil.splitAsList(m_settings.get(CmsTemplateContextInfo.SETTING), "|").contains(currentContext);
383    }
384
385    /**
386     * Sets the contents.<p>
387     *
388     * @param contents the contents to set
389     */
390    public void setContents(Map<String, String> contents) {
391
392        m_contents = contents;
393    }
394
395    /**
396     * Sets the description.<p>
397     *
398     * @param description the description to set
399     */
400    public void setDescription(String description) {
401
402        m_description = description;
403    }
404
405    /**
406     * During dragging and dropping in the container page editor, it may be required to substitute a different element for the element being dragged. This sets the id of the element to substitute.<p>
407     *
408     * @param dndId the drag and drop replacement element's client id
409     */
410    public void setDndId(String dndId) {
411
412        m_dndId = dndId;
413    }
414
415    /**
416     * Sets the formatter configurations.<p>
417     *
418     * @param formatters the formatter configurations to set
419     */
420    public void setFormatters(Map<String, Map<String, CmsFormatterConfig>> formatters) {
421
422        m_formatters = formatters;
423    }
424
425    /**
426     * Sets if this element is used as a group.<p>
427     *
428     * @param isGroup if this element is used as a group
429     */
430    public void setGroup(boolean isGroup) {
431
432        m_isGroup = isGroup;
433    }
434
435    /**
436     * Sets the inheritance infos.<p>
437     *
438     * @param inheritanceInfos the inheritance infos to set
439     */
440    public void setInheritanceInfos(List<CmsInheritanceInfo> inheritanceInfos) {
441
442        m_inheritanceInfos = inheritanceInfos;
443    }
444
445    /**
446     * Sets the inheritance name.<p>
447     *
448     * @param inheritanceName the inheritance name to set
449     */
450    public void setInheritanceName(String inheritanceName) {
451
452        m_inheritanceName = inheritanceName;
453    }
454
455    /**
456     * Sets the modifying userdByUser.<p>
457     *
458     * @param lastModifiedByUser the last modifying user to set
459     */
460    public void setLastModifiedByUser(String lastModifiedByUser) {
461
462        m_lastModifiedByUser = lastModifiedByUser;
463    }
464
465    /**
466     * Sets the last modification date.<p>
467     *
468     * @param lastModifiedDate the last modification date to set
469     */
470    public void setLastModifiedDate(long lastModifiedDate) {
471
472        m_lastModifiedDate = lastModifiedDate;
473    }
474
475    /**
476     * Sets the load time.<p>
477     *
478     * @param loadTime the load time to set
479     */
480    public void setLoadTime(long loadTime) {
481
482        m_loadTime = loadTime;
483    }
484
485    /**
486     * Sets the navText.<p>
487     *
488     * @param navText the navText to set
489     */
490    public void setNavText(String navText) {
491
492        m_navText = navText;
493    }
494
495    /**
496     * Sets the settings for this container element.<p>
497     *
498     * @param settings the new settings
499     */
500    public void setSettings(Map<String, String> settings) {
501
502        m_settings = settings;
503    }
504
505    /**
506     * Sets the sub-items.<p>
507     *
508     * @param subItems the sub-items to set
509     */
510    public void setSubItems(List<String> subItems) {
511
512        m_subItems = subItems;
513    }
514
515    /**
516     * Sets the title.<p>
517     *
518     * @param title the title to set
519     */
520    @Override
521    public void setTitle(String title) {
522
523        m_title = title;
524    }
525
526    /**
527     * Sets the supported container types.<p>
528     *
529     * @param types the supported container types to set
530     */
531    public void setTypes(Set<String> types) {
532
533        m_types = types;
534    }
535
536    /**
537     * @see java.lang.Object#toString()
538     */
539    @Override
540    public String toString() {
541
542        StringBuffer result = new StringBuffer();
543        result.append("Title: ").append(getTitle()).append("  File: ").append(getSitePath()).append(
544            "  ClientId: ").append(getClientId());
545        return result.toString();
546    }
547
548}