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.jsp.util;
029
030import org.opencms.file.CmsObject;
031import org.opencms.file.CmsResource;
032
033import java.util.ArrayList;
034import java.util.List;
035import java.util.Locale;
036
037/**
038 * Allows JSP access to the results of a &lt;cms:contentload ... &gt; operation using the JSTL and EL.<p>
039 *
040 * @since 7.0.2
041 *
042 * @see org.opencms.jsp.CmsJspTagContentLoad
043 * @see CmsJspContentAccessBean
044 */
045public class CmsJspContentLoadBean {
046
047    /** The OpenCms context of the current user. */
048    protected CmsObject m_cms;
049
050    /** The List of results form the content loader. */
051    protected List<CmsJspContentAccessBean> m_content;
052
053    /** The selected locale for accessing entries from the XML content. */
054    protected Locale m_locale;
055
056    /**
057     * No argument constructor, required for a JavaBean.<p>
058     *
059     * You must call {@link #init(CmsObject, Locale, List)} and provide the
060     * required values when you use this constructor.<p>
061     *
062     * @see #init(CmsObject, Locale, List)
063     */
064    public CmsJspContentLoadBean() {
065
066        // must call init() manually later
067    }
068
069    /**
070     * Creates a new context bean using the OpenCms context of the current user.<p>
071     *
072     * The current request context locale is used.<p>
073     *
074     * @param cms the OpenCms context of the current user
075     * @param content the content to access, must contain Object of type {@link CmsResource}
076     */
077    public CmsJspContentLoadBean(CmsObject cms, List<CmsResource> content) {
078
079        this(cms, cms.getRequestContext().getLocale(), content);
080    }
081
082    /**
083     * Creates a new context bean using the OpenCms context of the current user with the given locale.<p>
084     *
085     * @param cms the OpenCms context of the current user
086     * @param locale the Locale to use when accessing the content
087     * @param content the content to access, must contain Object of type {@link CmsResource}
088     */
089    public CmsJspContentLoadBean(CmsObject cms, Locale locale, List<CmsResource> content) {
090
091        init(cms, locale, content);
092    }
093
094    /**
095     * Converts a list of {@link CmsResource} objects to a list of {@link CmsJspContentAccessBean} objects,
096     * using the current request context locale.<p>
097     *
098     * @param cms the current OpenCms user context
099     * @param resources a list of of {@link CmsResource} objects that should be converted
100     *
101     * @return a list of {@link CmsJspContentAccessBean} objects created from the given {@link CmsResource} objects
102     */
103    public static List<CmsJspContentAccessBean> convertResourceList(CmsObject cms, List<CmsResource> resources) {
104
105        return convertResourceList(cms, cms.getRequestContext().getLocale(), resources);
106    }
107
108    /**
109     * Converts a list of {@link CmsResource} objects to a list of {@link CmsJspContentAccessBean} objects,
110     * using the given locale.<p>
111     *
112     * @param cms the current OpenCms user context
113     * @param locale the default locale to use when accessing the content
114     * @param resources a list of of {@link CmsResource} objects that should be converted
115     *
116     * @return a list of {@link CmsJspContentAccessBean} objects created from the given {@link CmsResource} objects
117     */
118    public static List<CmsJspContentAccessBean> convertResourceList(
119        CmsObject cms,
120        Locale locale,
121        List<CmsResource> resources) {
122
123        List<CmsJspContentAccessBean> result = new ArrayList<CmsJspContentAccessBean>(resources.size());
124        for (int i = 0, size = resources.size(); i < size; i++) {
125            CmsResource res = resources.get(i);
126            result.add(new CmsJspContentAccessBean(cms, locale, res));
127        }
128        return result;
129    }
130
131    /**
132     * Returns the OpenCms user context this bean was initialized with.<p>
133     *
134     * @return the OpenCms user context this bean was initialized with
135     */
136    public CmsObject getCmsObject() {
137
138        return m_cms;
139    }
140
141    /**
142     * Returns a List of {@link CmsJspContentAccessBean} instances, which have been wrapped around
143     * the original {@link CmsResource} instances of the collector result.<p>
144     *
145     * @return a List of {@link CmsJspContentAccessBean} instances, which have been wrapped around
146     * the original {@link CmsResource} instances of the collector result.<p>
147     */
148    public List<CmsJspContentAccessBean> getContent() {
149
150        return m_content;
151    }
152
153    /**
154     * Returns the Locale this bean was initialized with.<p>
155     *
156     * @return the locale  this bean was initialized with
157     */
158    public Locale getLocale() {
159
160        return m_locale;
161    }
162
163    /**
164     * Initialize this instance.<p>
165     *
166     * @param cms the OpenCms context of the current user
167     * @param locale the Locale to use when accessing the content
168     * @param content the content to access, must contain Object of type {@link CmsResource}
169     */
170    public void init(CmsObject cms, Locale locale, List<CmsResource> content) {
171
172        m_cms = cms;
173        m_locale = locale;
174        m_content = convertResourceList(m_cms, m_locale, content);
175    }
176}