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.detailpage;
029
030import org.opencms.ade.configuration.CmsADEManager;
031import org.opencms.util.CmsUUID;
032import org.opencms.xml.containerpage.CmsXmlDynamicFunctionHandler;
033
034import java.io.Serializable;
035
036/**
037 * Data bean containing the information for a detail page.<p>
038 *
039 * @since 8.0.0
040 */
041public class CmsDetailPageInfo implements Serializable {
042
043    /** The prefix for dynamic function detail page types. */
044    public static final String FUNCTION_PREFIX = "function@";
045
046    /** A string used to separate the type from the qualifier in the sitemap configuration. */
047    public static final String QUALIFIER_SEPARATOR = "|";
048
049    /** ID for serialization. */
050    private static final long serialVersionUID = 7714334294682534900L;
051
052    /** The resource icon style classes. */
053    private String m_iconClasses;
054
055    /** The id of the detail page. */
056    private CmsUUID m_id;
057
058    /** Flag used to distinguish inherited detail pages from ones defined in the current sitemap config. */
059    private boolean m_inherited;
060
061    /** Optional string that indicates when this detail page should be used. */
062    private String m_qualifier;
063
064    /** The resource type which the detail page should display. */
065    private String m_type;
066
067    /** The original URI of the detail page (for debugging purposes only). */
068    private String m_uri;
069
070    /**
071     * Creates a new detail page info bean.<p>
072     *
073     * @param id the id of the detail page
074     * @param uri the original URI of the page
075     * @param type the resource type for which the detail page is used
076     * @param qualifier an optional string that indicates when the detail page should be used
077     * @param iconClasses the resource icon style classes
078     */
079    public CmsDetailPageInfo(CmsUUID id, String uri, String type, String qualifier, String iconClasses) {
080
081        m_id = id;
082        m_type = type;
083        if ((m_type != null) && (m_type.indexOf(QUALIFIER_SEPARATOR) != -1)) {
084            throw new RuntimeException(
085                "Error: Qualifier separator '"
086                    + QUALIFIER_SEPARATOR
087                    + "' may not be part of detail page type: "
088                    + type);
089        }
090        m_qualifier = qualifier;
091        m_uri = uri;
092        m_iconClasses = iconClasses;
093    }
094
095    /**
096     * Empty default constructor for serialization.<p>
097     */
098    protected CmsDetailPageInfo() {
099
100        // for serialization
101    }
102
103    /**
104     * Removes the prefix for dynamic functions from a detail page type name.<p>
105     *
106     * @param name the detail page type name
107     *
108     * @return the detail page type name withotu the function prefix
109     */
110    public static String removeFunctionPrefix(String name) {
111
112        return name.replaceFirst("^" + FUNCTION_PREFIX, "");
113    }
114
115    /**
116     * Creates a copy of this entry, but sets the 'inherited' flag to true in the copy.<p>
117     *
118     * @return the copy of this entry
119     */
120    public CmsDetailPageInfo copyAsInherited() {
121
122        CmsDetailPageInfo result = new CmsDetailPageInfo(m_id, m_uri, m_type, m_qualifier, m_iconClasses);
123        result.m_inherited = true;
124        return result;
125    }
126
127    /**
128     * @see java.lang.Object#equals(java.lang.Object)
129     */
130    @Override
131    public boolean equals(Object obj) {
132
133        boolean result = false;
134        if (obj instanceof CmsDetailPageInfo) {
135            CmsDetailPageInfo info = (CmsDetailPageInfo)obj;
136            result = toString().equals(info.toString());
137        }
138        return result;
139    }
140
141    /**
142     * Gets the type name to display to the user.<p>
143     *
144     * @return the type name to display
145     */
146    public String getDisplayType() {
147
148        return m_type != null ? removeFunctionPrefix(m_type) : "";
149    }
150
151    /**
152     * Returns the resource icon style classes.<p>
153     *
154     * @return the resource icon style classes
155     **/
156    public String getIconClasses() {
157
158        return m_iconClasses;
159    }
160
161    /**
162     * Returns the resource type name for the icon to display.<p>
163     *
164     * @return the icon resource type
165     */
166    public String getIconType() {
167
168        if (m_type.startsWith(FUNCTION_PREFIX)) {
169            return CmsXmlDynamicFunctionHandler.TYPE_FUNCTION;
170        } else {
171            return m_type;
172        }
173    }
174
175    /**
176     * Returns the id of the detail page.<p>
177     *
178     * @return the id of the detail page
179     */
180    public CmsUUID getId() {
181
182        return m_id;
183    }
184
185    /**
186     * Gets the type including the qualifier (if set).
187     *
188     * <p>This is the same format as used in the detail page type field in the sitemap configuration.
189     *
190     * @return the qualified type
191     */
192    public String getQualifiedType() {
193
194        if (m_qualifier != null) {
195            return getType() + CmsDetailPageInfo.QUALIFIER_SEPARATOR + getQualifier();
196        } else {
197            return getType();
198        }
199    }
200
201    /**
202     * Gets the qualifier string.
203     *
204     * <p>The qualifier is an optional (i.e. possibly null) string that indicates when this detail page should be used.
205     *
206     * @return the qualifier string
207     */
208    public String getQualifier() {
209
210        return m_qualifier;
211    }
212
213    /**
214     * Returns the type for which the detail page is used.<p>
215     *
216     * @return the type for which the detail page is used
217     */
218    public String getType() {
219
220        return m_type;
221    }
222
223    /**
224     * The original URI for the detail page.<p>
225     *
226     * @return the original URI for the detail page
227     */
228    public String getUri() {
229
230        return m_uri;
231    }
232
233    /**
234     * @see java.lang.Object#hashCode()
235     */
236    @Override
237    public int hashCode() {
238
239        return toString().hashCode();
240    }
241
242    /**
243     * Checks if this detail page has the default detail page type.
244     *
245     * @return true if this is a default detail page
246     */
247    public boolean isDefaultDetailPage() {
248
249        return CmsADEManager.DEFAULT_DETAILPAGE_TYPE.equals(getType());
250    }
251
252    /**
253     * Returns true if the detail page entry is inherited from a parent sitemap.<p>
254     *
255     * @return true if the detail page entry is inherited from a parent sitemap
256     */
257    public boolean isInherited() {
258
259        return m_inherited;
260    }
261
262    /**
263     * @see java.lang.Object#toString()
264     */
265    @Override
266    public String toString() {
267
268        return "" + m_type + QUALIFIER_SEPARATOR + m_qualifier + ":" + m_id + m_uri;
269    }
270}