1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.journal.action;
24  
25  import com.liferay.portal.kernel.dao.search.DAOParamUtil;
26  import com.liferay.portal.kernel.language.LanguageUtil;
27  import com.liferay.portal.kernel.log.Log;
28  import com.liferay.portal.kernel.log.LogFactoryUtil;
29  import com.liferay.portal.kernel.util.ContentTypes;
30  import com.liferay.portal.kernel.util.DateUtil;
31  import com.liferay.portal.kernel.util.GetterUtil;
32  import com.liferay.portal.kernel.util.OrderByComparator;
33  import com.liferay.portal.kernel.util.ParamUtil;
34  import com.liferay.portal.kernel.util.StringPool;
35  import com.liferay.portal.kernel.util.StringUtil;
36  import com.liferay.portal.kernel.util.Validator;
37  import com.liferay.portal.kernel.xml.Document;
38  import com.liferay.portal.kernel.xml.Element;
39  import com.liferay.portal.kernel.xml.SAXReaderUtil;
40  import com.liferay.portal.theme.ThemeDisplay;
41  import com.liferay.portal.util.PortalUtil;
42  import com.liferay.portal.util.WebKeys;
43  import com.liferay.portlet.journal.model.JournalArticle;
44  import com.liferay.portlet.journal.service.JournalArticleLocalServiceUtil;
45  import com.liferay.portlet.journal.util.JournalUtil;
46  import com.liferay.portlet.journal.util.comparator.ArticleDisplayDateComparator;
47  import com.liferay.portlet.journal.util.comparator.ArticleModifiedDateComparator;
48  import com.liferay.util.servlet.ServletResponseUtil;
49  
50  import java.text.DateFormat;
51  
52  import java.util.Date;
53  import java.util.List;
54  import java.util.Map;
55  
56  import javax.servlet.http.HttpServletRequest;
57  import javax.servlet.http.HttpServletResponse;
58  
59  import org.apache.struts.action.Action;
60  import org.apache.struts.action.ActionForm;
61  import org.apache.struts.action.ActionForward;
62  import org.apache.struts.action.ActionMapping;
63  
64  /**
65   * <a href="GetArticlesAction.java.html"><b><i>View Source</i></b></a>
66   *
67   * @author Raymond Augé
68   * @author Brian Wing Shun Chan
69   */
70  public class GetArticlesAction extends Action {
71  
72      public ActionForward execute(
73              ActionMapping mapping, ActionForm form, HttpServletRequest request,
74              HttpServletResponse response)
75          throws Exception {
76  
77          try {
78              List<JournalArticle> articles = getArticles(request);
79  
80              String fileName = null;
81              byte[] bytes = getContent(request, articles);
82  
83              ServletResponseUtil.sendFile(
84                  response, fileName, bytes, ContentTypes.TEXT_XML_UTF8);
85  
86              return null;
87          }
88          catch (Exception e) {
89              PortalUtil.sendError(e, request, response);
90  
91              return null;
92          }
93      }
94  
95      protected List<JournalArticle> getArticles(HttpServletRequest request)
96          throws Exception {
97  
98          long companyId = PortalUtil.getCompanyId(request);
99          long groupId = DAOParamUtil.getLong(request, "groupId");
100         String articleId = null;
101         Double version = null;
102         String title = null;
103         String description = null;
104         String content = null;
105         String type = DAOParamUtil.getString(request, "type");
106         String[] structureIds = StringUtil.split(
107             DAOParamUtil.getString(request, "structureId"));
108         String[] templateIds = StringUtil.split(
109             DAOParamUtil.getString(request, "templateId"));
110 
111         Date displayDateGT = null;
112 
113         String displayDateGTParam = ParamUtil.getString(
114             request, "displayDateGT");
115 
116         if (Validator.isNotNull(displayDateGTParam)) {
117             DateFormat displayDateGTFormat = DateUtil.getISOFormat(
118                 displayDateGTParam);
119 
120             displayDateGT = GetterUtil.getDate(
121                 displayDateGTParam, displayDateGTFormat);
122         }
123 
124         if (_log.isDebugEnabled()) {
125             _log.debug("displayDateGT is " + displayDateGT);
126         }
127 
128         Date displayDateLT = null;
129 
130         String displayDateLTParam = ParamUtil.getString(
131             request, "displayDateLT");
132 
133         if (Validator.isNotNull(displayDateLTParam)) {
134             DateFormat displayDateLTFormat = DateUtil.getISOFormat(
135                 displayDateLTParam);
136 
137             displayDateLT = GetterUtil.getDate(
138                 displayDateLTParam, displayDateLTFormat);
139         }
140 
141         if (displayDateLT == null) {
142             displayDateLT = new Date();
143         }
144 
145         if (_log.isDebugEnabled()) {
146             _log.debug("displayDateLT is " + displayDateLT);
147         }
148 
149         Boolean approved = Boolean.TRUE;
150         Boolean expired = Boolean.FALSE;
151         Date reviewDate = null;
152         boolean andOperator = true;
153         int start = 0;
154         int end = ParamUtil.getInteger(request, "delta", 5);
155         String orderBy = ParamUtil.getString(request, "orderBy");
156         String orderByCol = ParamUtil.getString(request, "orderByCol", orderBy);
157         String orderByType = ParamUtil.getString(request, "orderByType");
158         boolean orderByAsc = orderByType.equals("asc");
159 
160         OrderByComparator obc = new ArticleModifiedDateComparator(orderByAsc);
161 
162         if (orderByCol.equals("display-date")) {
163             obc = new ArticleDisplayDateComparator(orderByAsc);
164         }
165 
166         return JournalArticleLocalServiceUtil.search(
167             companyId, groupId, articleId, version, title, description, content,
168             type, structureIds, templateIds, displayDateGT, displayDateLT,
169             approved, expired, reviewDate, andOperator, start, end, obc);
170     }
171 
172     protected byte[] getContent(
173             HttpServletRequest request, List<JournalArticle> articles)
174         throws Exception {
175 
176         long groupId = ParamUtil.getLong(request, "groupId");
177 
178         String languageId = LanguageUtil.getLanguageId(request);
179 
180         ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
181             WebKeys.THEME_DISPLAY);
182 
183         Map<String, String> tokens = JournalUtil.getTokens(
184             groupId, themeDisplay);
185 
186         Document resultsDoc = SAXReaderUtil.createDocument(StringPool.UTF8);
187 
188         Element resultSetEl = resultsDoc.addElement("result-set");
189 
190         for (JournalArticle article : articles) {
191             Element resultEl = resultSetEl.addElement("result");
192 
193             Document articleDoc = SAXReaderUtil.read(
194                 article.getContentByLocale(languageId));
195 
196             resultEl.content().add(
197                 articleDoc.getRootElement().createCopy());
198 
199             resultEl = resultEl.element("root");
200 
201             JournalUtil.addAllReservedEls(resultEl, tokens, article);
202         }
203 
204         return JournalUtil.formatXML(resultsDoc).getBytes(StringPool.UTF8);
205     }
206 
207     private static Log _log = LogFactoryUtil.getLog(GetArticlesAction.class);
208 
209 }