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.portlet.journal.search;
016    
017    import com.liferay.portal.kernel.dao.search.SearchContainer;
018    import com.liferay.portal.kernel.log.Log;
019    import com.liferay.portal.kernel.log.LogFactoryUtil;
020    import com.liferay.portal.kernel.util.JavaConstants;
021    import com.liferay.portal.kernel.util.OrderByComparator;
022    import com.liferay.portal.kernel.util.ParamUtil;
023    import com.liferay.portal.kernel.util.StringPool;
024    import com.liferay.portal.kernel.util.Validator;
025    import com.liferay.portal.kernel.workflow.WorkflowConstants;
026    import com.liferay.portal.util.PortletKeys;
027    import com.liferay.portlet.PortalPreferences;
028    import com.liferay.portlet.PortletPreferencesFactoryUtil;
029    import com.liferay.portlet.journal.model.JournalArticle;
030    import com.liferay.portlet.journal.util.JournalUtil;
031    
032    import java.util.ArrayList;
033    import java.util.HashMap;
034    import java.util.List;
035    import java.util.Map;
036    
037    import javax.portlet.PortletConfig;
038    import javax.portlet.PortletRequest;
039    import javax.portlet.PortletURL;
040    
041    /**
042     * @author Brian Wing Shun Chan
043     */
044    public class ArticleSearch extends SearchContainer<JournalArticle> {
045    
046            public static List<String> headerNames = new ArrayList<String>();
047            public static Map<String, String> orderableHeaders =
048                    new HashMap<String, String>();
049    
050            static {
051                    headerNames.add("id");
052                    headerNames.add("title");
053                    headerNames.add("status");
054                    //headerNames.add("version");
055                    headerNames.add("modified-date");
056                    headerNames.add("display-date");
057                    headerNames.add("author");
058                    headerNames.add("structure");
059                    headerNames.add(StringPool.BLANK);
060    
061                    //orderableHeaders.put("id", "id");
062                    //orderableHeaders.put("title", "title");
063                    //orderableHeaders.put("version", "version");
064                    orderableHeaders.put("modified-date", "modified-date");
065                    orderableHeaders.put("display-date", "display-date");
066            }
067    
068            public ArticleSearch(
069                    PortletRequest portletRequest, int cur, int delta,
070                    PortletURL iteratorURL) {
071    
072                    super(
073                            portletRequest, new ArticleDisplayTerms(portletRequest),
074                            new ArticleSearchTerms(portletRequest), DEFAULT_CUR_PARAM, cur,
075                            delta, iteratorURL, headerNames, null);
076    
077                    PortletConfig portletConfig =
078                            (PortletConfig)portletRequest.getAttribute(
079                                    JavaConstants.JAVAX_PORTLET_CONFIG);
080    
081                    ArticleDisplayTerms displayTerms =
082                            (ArticleDisplayTerms)getDisplayTerms();
083                    ArticleSearchTerms searchTerms = (ArticleSearchTerms)getSearchTerms();
084    
085                    String portletName = portletConfig.getPortletName();
086    
087                    if (!portletName.equals(PortletKeys.JOURNAL)) {
088                            displayTerms.setStatus(WorkflowConstants.STATUS_APPROVED);
089                            searchTerms.setStatus(WorkflowConstants.STATUS_APPROVED);
090                    }
091    
092                    iteratorURL.setParameter(
093                            ArticleDisplayTerms.ARTICLE_ID, displayTerms.getArticleId());
094                    iteratorURL.setParameter(
095                            ArticleDisplayTerms.CONTENT, displayTerms.getContent());
096                    iteratorURL.setParameter(
097                            ArticleDisplayTerms.DESCRIPTION, displayTerms.getDescription());
098                    iteratorURL.setParameter(
099                            ArticleDisplayTerms.DDM_STRUCTURE_KEY,
100                            displayTerms.getDDMStructureKey());
101                    iteratorURL.setParameter(
102                            ArticleDisplayTerms.DDM_TEMPLATE_KEY,
103                            displayTerms.getDDMTemplateKey());
104                    iteratorURL.setParameter(
105                            ArticleDisplayTerms.FOLDER_ID,
106                            String.valueOf(displayTerms.getFolderId()));
107                    iteratorURL.setParameter(
108                            ArticleDisplayTerms.GROUP_ID,
109                            String.valueOf(displayTerms.getGroupId()));
110                    iteratorURL.setParameter(
111                            ArticleDisplayTerms.NAVIGATION, displayTerms.getNavigation());
112                    iteratorURL.setParameter(
113                            ArticleDisplayTerms.STATUS,
114                            String.valueOf(displayTerms.getStatus()));
115                    iteratorURL.setParameter(
116                            ArticleDisplayTerms.TITLE, displayTerms.getTitle());
117                    iteratorURL.setParameter(
118                            ArticleDisplayTerms.VERSION,
119                            String.valueOf(displayTerms.getVersion()));
120    
121                    try {
122                            PortalPreferences preferences =
123                                    PortletPreferencesFactoryUtil.getPortalPreferences(
124                                            portletRequest);
125    
126                            String orderByCol = ParamUtil.getString(
127                                    portletRequest, "orderByCol");
128                            String orderByType = ParamUtil.getString(
129                                    portletRequest, "orderByType");
130    
131                            if (Validator.isNotNull(orderByCol) &&
132                                    Validator.isNotNull(orderByType)) {
133    
134                                    preferences.setValue(
135                                            PortletKeys.JOURNAL, "articles-order-by-col", orderByCol);
136                                    preferences.setValue(
137                                            PortletKeys.JOURNAL, "articles-order-by-type", orderByType);
138                            }
139                            else {
140                                    orderByCol = preferences.getValue(
141                                            PortletKeys.JOURNAL, "articles-order-by-col", "id");
142                                    orderByType = preferences.getValue(
143                                            PortletKeys.JOURNAL, "articles-order-by-type", "asc");
144                            }
145    
146                            OrderByComparator<JournalArticle> orderByComparator =
147                                    JournalUtil.getArticleOrderByComparator(
148                                            orderByCol, orderByType);
149    
150                            setOrderableHeaders(orderableHeaders);
151                            setOrderByCol(orderByCol);
152                            setOrderByType(orderByType);
153                            setOrderByComparator(orderByComparator);
154                    }
155                    catch (Exception e) {
156                            _log.error(e);
157                    }
158            }
159    
160            public ArticleSearch(
161                    PortletRequest portletRequest, PortletURL iteratorURL) {
162    
163                    this(portletRequest, 0, DEFAULT_DELTA, iteratorURL);
164            }
165    
166            private static final Log _log = LogFactoryUtil.getLog(ArticleSearch.class);
167    
168    }