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.ui.sitemap;
029
030import org.opencms.ade.sitemap.shared.CmsClientSitemapEntry;
031import org.opencms.file.CmsObject;
032import org.opencms.file.CmsProperty;
033import org.opencms.file.CmsPropertyDefinition;
034import org.opencms.file.CmsResource;
035import org.opencms.file.CmsResourceFilter;
036import org.opencms.file.types.CmsResourceTypeXmlContainerPage;
037import org.opencms.i18n.CmsLocaleGroup;
038import org.opencms.i18n.CmsLocaleManager;
039import org.opencms.main.CmsException;
040import org.opencms.main.CmsLog;
041import org.opencms.util.CmsUUID;
042
043import java.util.Collection;
044import java.util.Locale;
045
046import org.apache.commons.logging.Log;
047
048/**
049 * Represents the data of a sitemap tree node.
050 */
051public class CmsSitemapTreeNodeData {
052
053    /** The log instance for this class. */
054    @SuppressWarnings("unused")
055    private static final Log LOG = CmsLog.getLog(CmsSitemapTreeNodeData.class);
056
057    /** The client sitemap entry. */
058    private CmsClientSitemapEntry m_entry;
059
060    /** Indicates whether we have definitely no children. */
061    private boolean m_hasNoChildren;
062
063    /** Sitmap entry is copyable (ie has the 'Copy page' option). */
064    private boolean m_isCopyable;
065
066    /** Flag indicating whether the linked resource is directly linked. */
067    private boolean m_isDirectLink;
068
069    /** The linked resource. */
070    private CmsResource m_linkedResource;
071
072    /** String containing locales for which no translations should be provided, read from locale.notranslation property. */
073    private String m_noTranslation;
074
075    /** The other locale. */
076    private Locale m_otherLocale;
077
078    /** The entry resource. */
079    private CmsResource m_resource;
080
081    /**
082     * Creates a new instance.<p>
083     *
084     * @param mainLocale the main locale
085     * @param otherLocale the other locale
086     */
087    public CmsSitemapTreeNodeData(Locale mainLocale, Locale otherLocale) {
088        m_otherLocale = otherLocale;
089    }
090
091    /**
092     * Gets the client sitemap entry.<p>
093     *
094     * @return the client sitemap entry
095     */
096    public CmsClientSitemapEntry getClientEntry() {
097
098        return m_entry;
099    }
100
101    /**
102     * Gets the linked resource.<p>
103     *
104     * @return the linked resource
105     */
106    public CmsResource getLinkedResource() {
107
108        return m_linkedResource;
109    }
110
111    /**
112     * Gets the sitemap entry resource.<p>
113     *
114     * @return the resource
115     */
116    public CmsResource getResource() {
117
118        return m_resource;
119
120    }
121
122    /**
123     * Returns true if the node definitely has no children to load.<p>
124     *
125     * @return true if there are definitely no children to load
126     */
127    public boolean hasNoChildren() {
128
129        return m_hasNoChildren;
130    }
131
132    /**
133     * Initializes the bean.<p>
134     *
135     * @param cms the CMS context to use
136     *
137     * @throws CmsException if something goes wrong
138     */
139    public void initialize(CmsObject cms) throws CmsException {
140
141        CmsUUID id = m_entry.getId();
142        CmsResource resource = cms.readResource(id, CmsResourceFilter.IGNORE_EXPIRATION);
143        m_resource = resource;
144        CmsResource defaultFile = resource;
145        if (resource.isFolder()) {
146            defaultFile = cms.readDefaultFile(resource, CmsResourceFilter.IGNORE_EXPIRATION);
147        }
148        CmsLocaleGroup localeGroup = cms.getLocaleGroupService().readLocaleGroup(defaultFile);
149        CmsResource primary = localeGroup.getPrimaryResource();
150        CmsProperty noTranslationProp = cms.readPropertyObject(
151            primary,
152            CmsPropertyDefinition.PROPERTY_LOCALE_NOTRANSLATION,
153            false);
154        m_noTranslation = noTranslationProp.getValue();
155        CmsUUID defaultFileId = (defaultFile != null) ? defaultFile.getStructureId() : null;
156        m_isCopyable = (defaultFile != null) && CmsResourceTypeXmlContainerPage.isContainerPage(defaultFile);
157        Collection<CmsResource> resourcesForTargetLocale = localeGroup.getResourcesForLocale(m_otherLocale);
158        if (!resourcesForTargetLocale.isEmpty()) {
159            m_linkedResource = resourcesForTargetLocale.iterator().next();
160            if (primary.getStructureId().equals(m_resource.getStructureId())
161                || primary.getStructureId().equals(defaultFileId)
162                || primary.getStructureId().equals(m_linkedResource.getStructureId())) {
163                m_isDirectLink = true;
164            }
165        }
166    }
167
168    /**
169     * Returns true if the 'Copy page' function should be offered for this entry.<p>
170     *
171     * @return true if the 'copy pgae' function should be available
172     */
173    public boolean isCopyable() {
174
175        return m_isCopyable;
176    }
177
178    /**
179     * Returns true if the linked resource is directly linked.<p>
180     *
181     * @return true if the linked resource is directly linked
182     */
183    public boolean isDirectLink() {
184
185        return m_isDirectLink;
186
187    }
188
189    /**
190     * Returns true if this sitemap entry has a linked resource.<p>
191     *
192     * @return true if there is a linked resource
193     */
194    public boolean isLinked() {
195
196        return m_linkedResource != null;
197    }
198
199    /**
200     * Checks if this entry is marked as 'do not translate' for the given locale .<p>
201     *
202     * @param locale the locale
203     * @return true if the 'do not translate' mark for the given locale is set
204     */
205    public boolean isMarkedNoTranslation(Locale locale) {
206
207        if (m_noTranslation != null) {
208            return CmsLocaleManager.getLocales(m_noTranslation).contains(locale);
209        }
210        return false;
211    }
212
213    /**
214     * Sets the client entry.<p>
215     *
216     * @param entry the client entry
217     */
218    public void setClientEntry(CmsClientSitemapEntry entry) {
219
220        m_entry = entry;
221    }
222
223    /**
224     * Sets the 'has no children' flag.<p>
225     *
226     * @param b the new value
227     */
228    public void setHasNoChildren(boolean b) {
229
230        m_hasNoChildren = b;
231
232    }
233
234}