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.search.result;
029
030import org.opencms.file.CmsObject;
031import org.opencms.i18n.CmsLocaleManager;
032import org.opencms.jsp.util.CmsJspContentAccessBean;
033import org.opencms.search.CmsSearchResource;
034import org.opencms.util.CmsCollectionsGenericWrapper;
035
036import java.util.Date;
037import java.util.List;
038import java.util.Map;
039
040import org.apache.commons.collections.Transformer;
041
042/** JSP EL friendly wrapper class for a single search result (document). */
043public class CmsSearchResourceBean implements I_CmsSearchResourceBean {
044
045    /** The result as returned normally. */
046    final CmsSearchResource m_searchResource;
047    /** Lazy map to access the String fields of the document. */
048    private Map<String, String> m_stringfields;
049    /** Lazy map to access the Date fields of the document. */
050    private Map<String, Date> m_datefields;
051    /** Lazy map to access the multi-valued String fields of the document. */
052    private Map<String, List<String>> m_multivaluedfields;
053    /** Lazy map to access content in different locales. */
054    private Map<String, CmsJspContentAccessBean> m_localizedContent;
055    /** Cms object. */
056    final CmsObject m_cmsObject;
057
058    /** Constructor taking the search resource to wrap.
059     * @param searchResource The search resource to wrap.
060     * @param cms The Cms object, used to read resources.
061     */
062    public CmsSearchResourceBean(final CmsSearchResource searchResource, final CmsObject cms) {
063
064        m_searchResource = searchResource;
065        m_cmsObject = cms;
066    }
067
068    /**
069     * @see org.opencms.jsp.search.result.I_CmsSearchResourceBean#getDateFields()
070     */
071    @Override
072    public Map<String, Date> getDateFields() {
073
074        if (m_datefields == null) {
075            m_datefields = CmsCollectionsGenericWrapper.createLazyMap(new Transformer() {
076
077                @Override
078                public Object transform(final Object fieldName) {
079
080                    return getSearchResource().getDateField(fieldName.toString());
081                }
082            });
083        }
084        return m_datefields;
085    }
086
087    /**
088     * @see org.opencms.jsp.search.result.I_CmsSearchResourceBean#getFields()
089     */
090    @Override
091    public Map<String, String> getFields() {
092
093        if (m_stringfields == null) {
094            m_stringfields = CmsCollectionsGenericWrapper.createLazyMap(new Transformer() {
095
096                @Override
097                public Object transform(final Object fieldName) {
098
099                    return getSearchResource().getField(fieldName.toString());
100                }
101            });
102        }
103        return m_stringfields;
104    }
105
106    /**
107     * @see org.opencms.jsp.search.result.I_CmsSearchResourceBean#getMultiValuedFields()
108     */
109    @Override
110    public Map<String, List<String>> getMultiValuedFields() {
111
112        if (m_multivaluedfields == null) {
113            m_multivaluedfields = CmsCollectionsGenericWrapper.createLazyMap(new Transformer() {
114
115                @Override
116                public Object transform(final Object fieldName) {
117
118                    return getSearchResource().getMultivaluedField(fieldName.toString());
119                }
120            });
121
122        }
123        return m_multivaluedfields;
124    }
125
126    /**
127     * @see org.opencms.jsp.search.result.I_CmsSearchResourceBean#getSearchResource()
128     */
129    @Override
130    public CmsSearchResource getSearchResource() {
131
132        return m_searchResource;
133    }
134
135    /**
136     * @see org.opencms.jsp.search.result.I_CmsSearchResourceBean#getXmlContent()
137     */
138    @Override
139    public CmsJspContentAccessBean getXmlContent() {
140
141        CmsJspContentAccessBean accessBean = null;
142        try {
143            accessBean = new CmsJspContentAccessBean(m_cmsObject, m_searchResource);
144        } catch (@SuppressWarnings("unused") Exception e) {
145            // do nothing - simply could not read content;
146        }
147        return accessBean;
148    }
149
150    /**
151     * @see org.opencms.jsp.search.result.I_CmsSearchResourceBean#getXmlContentInLocale()
152     */
153    public Map<String, CmsJspContentAccessBean> getXmlContentInLocale() {
154
155        if (m_localizedContent == null) {
156            m_localizedContent = CmsCollectionsGenericWrapper.createLazyMap(new Transformer() {
157
158                @Override
159                public Object transform(final Object locale) {
160
161                    CmsJspContentAccessBean accessBean = null;
162                    try {
163                        accessBean = new CmsJspContentAccessBean(
164                            m_cmsObject,
165                            CmsLocaleManager.getLocale((String)locale),
166                            m_searchResource);
167                    } catch (@SuppressWarnings("unused") Exception e) {
168                        // simply return null
169                    }
170                    return accessBean;
171                }
172            });
173
174        }
175        return m_localizedContent;
176    }
177}