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