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.configuration;
029
030import org.opencms.ade.configuration.formatters.CmsFormatterChangeSet;
031import org.opencms.ade.detailpage.CmsDetailPageInfo;
032import org.opencms.file.CmsResource;
033import org.opencms.util.CmsUUID;
034
035import java.util.Collection;
036import java.util.Collections;
037import java.util.Comparator;
038import java.util.List;
039import java.util.Set;
040
041import com.google.common.collect.ComparisonChain;
042import com.google.common.collect.Lists;
043
044/**
045 * Represents a parsed sitemap or module configuration.<p>
046 *
047 * This is the internal representation stored in the cache. The configuration class
048 * which is actually returned by CmsADEManager, and which contains most of the logic
049 * related to sitemap configurations, is CmsADEConfigData.
050 */
051public class CmsADEConfigDataInternal {
052
053    /** The "create contents locally" flag. */
054    protected boolean m_createContentsLocally;
055    /** Should inherited model pages be discarded? */
056    protected boolean m_discardInheritedModelPages;
057    /** Should inherited properties be discard? */
058    protected boolean m_discardInheritedProperties;
059    /** Should inherited types be discarded? */
060    protected boolean m_discardInheritedTypes;
061
062    /** The configured formatter changes. */
063    protected CmsFormatterChangeSet m_formatterChangeSet = new CmsFormatterChangeSet();
064
065    /** True if this is a module configuration, not a normal sitemap configuration. */
066    protected boolean m_isModuleConfig;
067    /** The master configuration resource (possibly null). */
068    protected CmsResource m_masterConfig;
069    /** The base path of this configuration. */
070    private String m_basePath;
071    /** The list of configured function references. */
072    private List<CmsFunctionReference> m_functionReferences = Lists.newArrayList();
073
074    /** The internal detail page configuration. */
075    private List<CmsDetailPageInfo> m_ownDetailPages = Lists.newArrayList();
076
077    /** The internal model page entries. */
078    private List<CmsModelPageConfig> m_ownModelPageConfig = Lists.newArrayList();
079
080    /** The internal property configuration. */
081    private List<CmsPropertyConfig> m_ownPropertyConfigurations = Lists.newArrayList();
082
083    /** The internal resource type entries. */
084    private List<CmsResourceTypeConfig> m_ownResourceTypes = Lists.newArrayList();
085
086    /** The dynamic functions configured for this configuration level. */
087    private Set<CmsUUID> m_ownDynamicFunctions;
088
089    /** the dynamic functions available. */
090    private Set<CmsUUID> m_dynamicFunctions;
091
092    /** True if detail pages from this sitemap should be preferred when linking to contents inside this sitemap. */
093    private boolean m_preferDetailPagesForLocalContents;
094
095    /** The resource from which the configuration data was read. */
096    private CmsResource m_resource;
097
098    /**
099     * Creates a new configuration data instance.<p>
100    
101     * @param resource the resource from which this configuration data was read
102     * @param isModuleConfig true if this is a module configuration
103     * @param basePath the base path
104     * @param masterConfig the master configuration resource (possibly null)
105     * @param resourceTypeConfig the resource type configuration
106     * @param discardInheritedTypes the "discard inherited types" flag
107     * @param propertyConfig the property configuration
108     * @param discardInheritedProperties the "discard inherited properties" flag
109     * @param detailPageInfos the detail page configuration
110     * @param modelPages the model page configuration
111     * @param functionReferences the function reference configuration
112     * @param discardInheritedModelPages the "discard  inherited model pages" flag
113     * @param createContentsLocally the "create contents locally" flag
114     * @param preferDetailPagesForLocalContents the "preferDetailPagesForLocalContents" flag
115     * @param formatterChangeSet the formatter changes
116     * @param dynamicFunctions the dynamic functions available
117     */
118    public CmsADEConfigDataInternal(
119        CmsResource resource,
120        boolean isModuleConfig,
121        String basePath,
122        CmsResource masterConfig,
123        List<CmsResourceTypeConfig> resourceTypeConfig,
124        boolean discardInheritedTypes,
125        List<CmsPropertyConfig> propertyConfig,
126        boolean discardInheritedProperties,
127        List<CmsDetailPageInfo> detailPageInfos,
128        List<CmsModelPageConfig> modelPages,
129        List<CmsFunctionReference> functionReferences,
130        boolean discardInheritedModelPages,
131        boolean createContentsLocally,
132        boolean preferDetailPagesForLocalContents,
133        CmsFormatterChangeSet formatterChangeSet,
134        Set<CmsUUID> dynamicFunctions) {
135
136        m_resource = resource;
137        m_basePath = basePath;
138        m_ownResourceTypes = resourceTypeConfig;
139        m_ownPropertyConfigurations = propertyConfig;
140        m_ownModelPageConfig = modelPages;
141        m_ownDetailPages = detailPageInfos;
142        m_functionReferences = functionReferences;
143        m_isModuleConfig = isModuleConfig;
144        m_masterConfig = masterConfig;
145
146        m_discardInheritedTypes = discardInheritedTypes;
147        m_discardInheritedProperties = discardInheritedProperties;
148        m_discardInheritedModelPages = discardInheritedModelPages;
149        m_createContentsLocally = createContentsLocally;
150        m_preferDetailPagesForLocalContents = preferDetailPagesForLocalContents;
151        m_formatterChangeSet = formatterChangeSet;
152        m_ownDynamicFunctions = dynamicFunctions;
153        m_dynamicFunctions = dynamicFunctions;
154    }
155
156    /**
157     * Creates an empty configuration data object with a given base path.<p>
158     *
159     * @param basePath the base path
160     */
161    public CmsADEConfigDataInternal(String basePath) {
162
163        m_basePath = basePath;
164    }
165
166    /**
167     * Creates an empty configuration for a given base path.<p>
168     *
169     * @param basePath the base path
170     *
171     * @return the empty configuration object
172     */
173    public static CmsADEConfigDataInternal emptyConfiguration(String basePath) {
174
175        return new CmsADEConfigDataInternal(basePath);
176    }
177
178    /**
179     * Gets the base path.<p>
180     *
181     * @return the base path
182     */
183    public String getBasePath() {
184
185        return m_basePath;
186    }
187
188    /**
189     * Returns the restricted dynamic functions or <code>null</code>.<p>
190     *
191     * @return the dynamic functions
192     */
193    public Collection<CmsUUID> getDynamicFunctions() {
194
195        return m_dynamicFunctions == null ? null : Collections.unmodifiableSet(m_dynamicFunctions);
196    }
197
198    /**
199     * Gets the formatter change set.<p>
200     *
201     * @return the formatter change set.<p>
202     */
203    public CmsFormatterChangeSet getFormatterChangeSet() {
204
205        return m_formatterChangeSet;
206
207    }
208
209    /**
210     * Gets the dynamic function references.<p>
211     *
212     * @return the dynamic function references
213     */
214    public List<CmsFunctionReference> getFunctionReferences() {
215
216        return m_functionReferences;
217    }
218
219    /**
220     * Gets the master configuration resource (may be null).<p>
221     *
222     * @return the master configuration resource
223     */
224    public CmsResource getMasterConfig() {
225
226        return m_masterConfig;
227    }
228
229    /**
230     * Returns the ownDetailPages.<p>
231     *
232     * @return the ownDetailPages
233     */
234    public List<CmsDetailPageInfo> getOwnDetailPages() {
235
236        return m_ownDetailPages;
237    }
238
239    /**
240     * Returns the ownModelPageConfig.<p>
241     *
242     * @return the ownModelPageConfig
243     */
244    public List<CmsModelPageConfig> getOwnModelPageConfig() {
245
246        return m_ownModelPageConfig;
247    }
248
249    /**
250     * Returns the ownPropertyConfigurations.<p>
251     *
252     * @return the ownPropertyConfigurations
253     */
254    public List<CmsPropertyConfig> getOwnPropertyConfigurations() {
255
256        return m_ownPropertyConfigurations;
257    }
258
259    /**
260     * Gets the resource types defined in this configuration.<p>
261     *
262     * @return the resource type configurations
263     */
264    public List<CmsResourceTypeConfig> getOwnResourceTypes() {
265
266        return m_ownResourceTypes;
267    }
268
269    /**
270     * Returns the resource.<p>
271     *
272     * @return the resource
273     */
274    public CmsResource getResource() {
275
276        return m_resource;
277    }
278
279    /**
280     * Returns true if contents should be created in the sub-sitemap.<p>
281     *
282     * @return true if contents should be created in the sub-sitemap
283     */
284    public boolean isCreateContentsLocally() {
285
286        return m_createContentsLocally;
287    }
288
289    /**
290     * Returns true if inherited model pages should be discarded.<p>
291     *
292     * @return true if inherited model pages should be discarded.
293     */
294    public boolean isDiscardInheritedModelPages() {
295
296        return m_discardInheritedModelPages;
297    }
298
299    /**
300     * Returns true if inherited properties should be discarded.<p>
301     *
302     * @return true if inherited property configurations should be discardded.<p>
303     */
304    public boolean isDiscardInheritedProperties() {
305
306        return m_discardInheritedProperties;
307    }
308
309    /**
310     * Returns true if inherited types should be discarded.<p>
311     *
312     * @return true if inherited types should be discarded
313     */
314    public boolean isDiscardInheritedTypes() {
315
316        return m_discardInheritedTypes;
317    }
318
319    /**
320     * Returns the isModuleConfig.<p>
321     *
322     * @return the isModuleConfig
323     */
324    public boolean isModuleConfig() {
325
326        return m_isModuleConfig;
327    }
328
329    /**
330     * Returns true if detail pages from this sitemap should be preferred for creating links to detail contents located inside this sitemap.<p>
331     *
332     * @return true if detail pages from this sitemap should be preferred
333     */
334    public boolean isPreferDetailPagesForLocalContents() {
335
336        return m_preferDetailPagesForLocalContents;
337    }
338
339    /**
340     * Merges the parent's data into this object.<p>
341     *
342     * @param parent the parent configuration data
343     */
344    protected void mergeParent(CmsADEConfigDataInternal parent) {
345
346        List<CmsResourceTypeConfig> parentTypes = null;
347        if (parent != null) {
348            parentTypes = parent.m_ownResourceTypes;
349        } else {
350            parentTypes = Collections.emptyList();
351        }
352
353        List<CmsPropertyConfig> parentProperties = null;
354        if (parent != null) {
355            parentProperties = parent.m_ownPropertyConfigurations;
356        } else {
357            parentProperties = Collections.emptyList();
358        }
359
360        List<CmsModelPageConfig> parentModelPages = null;
361        if (parent != null) {
362            parentModelPages = parent.m_ownModelPageConfig;
363        } else {
364            parentModelPages = Collections.emptyList();
365        }
366
367        List<CmsFunctionReference> parentFunctionRefs = null;
368        if (parent != null) {
369            parentFunctionRefs = parent.m_functionReferences;
370        } else {
371            parentFunctionRefs = Collections.emptyList();
372        }
373
374        m_ownResourceTypes = CmsADEConfigData.combineConfigurationElements(parentTypes, m_ownResourceTypes, false);
375        m_ownPropertyConfigurations = CmsADEConfigData.combineConfigurationElements(
376            parentProperties,
377            m_ownPropertyConfigurations,
378            false);
379        m_ownModelPageConfig = CmsADEConfigData.combineConfigurationElements(
380            parentModelPages,
381            m_ownModelPageConfig,
382            false);
383        m_functionReferences = CmsADEConfigData.combineConfigurationElements(
384            parentFunctionRefs,
385            m_functionReferences,
386            false);
387        if ((parent != null) && (m_ownDynamicFunctions == null)) {
388            m_dynamicFunctions = parent.m_dynamicFunctions;
389        } else {
390            m_dynamicFunctions = m_ownDynamicFunctions;
391        }
392    }
393
394    /**
395     * Handle the ordering from the module configurations.<p>
396     */
397    protected void processModuleOrdering() {
398
399        Collections.sort(m_ownResourceTypes, new Comparator<CmsResourceTypeConfig>() {
400
401            public int compare(CmsResourceTypeConfig a, CmsResourceTypeConfig b) {
402
403                return ComparisonChain.start().compare(a.getOrder(), b.getOrder()).compare(
404                    a.getTypeName(),
405                    b.getTypeName()).result();
406            }
407        });
408
409        Collections.sort(m_ownPropertyConfigurations, new Comparator<CmsPropertyConfig>() {
410
411            public int compare(CmsPropertyConfig a, CmsPropertyConfig b) {
412
413                return ComparisonChain.start().compare(a.getOrder(), b.getOrder()).compare(
414                    a.getName(),
415                    b.getName()).result();
416            }
417        });
418
419        Collections.sort(m_functionReferences, new Comparator<CmsFunctionReference>() {
420
421            public int compare(CmsFunctionReference a, CmsFunctionReference b) {
422
423                return ComparisonChain.start().compare(a.getOrder(), b.getOrder()).compare(
424                    a.getName(),
425                    b.getName()).result();
426            }
427        });
428    }
429
430}