001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.kernel.search;
016    
017    import com.liferay.portal.kernel.search.util.SearchUtil;
018    import com.liferay.portal.kernel.util.ArrayUtil;
019    import com.liferay.portal.kernel.util.HtmlUtil;
020    import com.liferay.portal.kernel.util.LocaleThreadLocal;
021    import com.liferay.portal.kernel.util.StringPool;
022    import com.liferay.portal.kernel.util.StringUtil;
023    import com.liferay.portal.kernel.util.Validator;
024    
025    import java.util.Locale;
026    
027    import javax.portlet.PortletURL;
028    
029    /**
030     * @author Brian Wing Shun Chan
031     * @author Ryan Park
032     * @author Tibor Lipusz
033     */
034    public class Summary {
035    
036            public Summary(
037                    Locale locale, String title, String content, PortletURL portletURL) {
038    
039                    _locale = locale;
040                    _title = title;
041                    _content = content;
042                    _portletURL = portletURL;
043            }
044    
045            public Summary(String title, String content, PortletURL portletURL) {
046                    this(
047                            LocaleThreadLocal.getThemeDisplayLocale(), title, content,
048                            portletURL);
049            }
050    
051            public String getContent() {
052                    if (Validator.isNull(_content)) {
053                            return StringPool.BLANK;
054                    }
055    
056                    return _content;
057            }
058    
059            public String getHighlightedContent() {
060                    return _escapeAndHighlight(_content);
061            }
062    
063            public String getHighlightedTitle() {
064                    return _escapeAndHighlight(_title);
065            }
066    
067            public Locale getLocale() {
068                    return _locale;
069            }
070    
071            public int getMaxContentLength() {
072                    return _maxContentLength;
073            }
074    
075            public PortletURL getPortletURL() {
076                    return _portletURL;
077            }
078    
079            public String[] getQueryTerms() {
080                    return _queryTerms;
081            }
082    
083            public String getTitle() {
084                    if (Validator.isNull(_title)) {
085                            return StringPool.BLANK;
086                    }
087    
088                    return _title;
089            }
090    
091            public boolean isHighlight() {
092                    return _highlight;
093            }
094    
095            public void setContent(String content) {
096                    _content = content;
097    
098                    if ((_content != null) && (_maxContentLength > 0) &&
099                            (_content.length() > _maxContentLength)) {
100    
101                            _content = StringUtil.shorten(_content, _maxContentLength);
102                    }
103            }
104    
105            public void setHighlight(boolean highlight) {
106                    _highlight = highlight;
107            }
108    
109            public void setLocale(Locale locale) {
110                    _locale = locale;
111            }
112    
113            public void setMaxContentLength(int maxContentLength) {
114                    _maxContentLength = maxContentLength;
115    
116                    setContent(_content);
117            }
118    
119            public void setPortletURL(PortletURL portletURL) {
120                    _portletURL = portletURL;
121            }
122    
123            public void setQueryTerms(String[] queryTerms) {
124                    if (ArrayUtil.isEmpty(queryTerms)) {
125                            return;
126                    }
127    
128                    _queryTerms = queryTerms;
129            }
130    
131            public void setTitle(String title) {
132                    _title = title;
133            }
134    
135            private String _escapeAndHighlight(String text) {
136                    if (!_highlight || Validator.isNull(text) ||
137                            ArrayUtil.isEmpty(_queryTerms)) {
138    
139                            return HtmlUtil.escape(text);
140                    }
141    
142                    text = SearchUtil.highlight(
143                            text, _queryTerms, _ESCAPE_SAFE_HIGHLIGHTS[0],
144                            _ESCAPE_SAFE_HIGHLIGHTS[1]);
145    
146                    text = HtmlUtil.escape(text);
147    
148                    return StringUtil.replace(
149                            text, _ESCAPE_SAFE_HIGHLIGHTS, SearchUtil.HIGHLIGHTS);
150            }
151    
152            private static final String[] _ESCAPE_SAFE_HIGHLIGHTS =
153                    {"[@HIGHLIGHT1@]", "[@HIGHLIGHT2@]"};
154    
155            private String _content;
156            private boolean _highlight;
157            private Locale _locale;
158            private int _maxContentLength;
159            private PortletURL _portletURL;
160            private String[] _queryTerms;
161            private String _title;
162    
163    }