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