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