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