001    /**
002     * Copyright (c) 2000-2010 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.asset.service.persistence;
016    
017    import com.liferay.portal.kernel.dao.orm.QueryUtil;
018    import com.liferay.portal.kernel.dao.search.SearchContainer;
019    import com.liferay.portal.kernel.exception.PortalException;
020    import com.liferay.portal.kernel.exception.SystemException;
021    import com.liferay.portal.kernel.log.Log;
022    import com.liferay.portal.kernel.log.LogFactoryUtil;
023    import com.liferay.portal.kernel.util.ParamUtil;
024    import com.liferay.portal.kernel.util.Validator;
025    import com.liferay.portal.kernel.util.WebKeys;
026    import com.liferay.portal.theme.ThemeDisplay;
027    import com.liferay.portal.util.PortalUtil;
028    import com.liferay.portlet.asset.model.AssetCategory;
029    import com.liferay.portlet.asset.service.AssetCategoryLocalServiceUtil;
030    import com.liferay.portlet.asset.service.AssetTagLocalServiceUtil;
031    
032    import java.util.Date;
033    
034    import javax.portlet.PortletRequest;
035    
036    /**
037     * @author Brian Wing Shun Chan
038     * @author Jorge Ferrer
039     */
040    public class AssetEntryQuery {
041    
042            public static String[] ORDER_BY_COLUMNS = new String[] {
043                    "title", "createDate", "modifiedDate", "publishDate", "expirationDate",
044                    "priority", "viewCount"
045            };
046    
047            public static String checkOrderByCol(String orderByCol) {
048                    if (orderByCol == null) {
049                            return ORDER_BY_COLUMNS[2];
050                    }
051    
052                    for (String curOrderByCol : ORDER_BY_COLUMNS) {
053                            if (orderByCol.equals(curOrderByCol)) {
054                                    return orderByCol;
055                            }
056                    }
057    
058                    return ORDER_BY_COLUMNS[2];
059            }
060    
061            public static String checkOrderByType(String orderByType) {
062                    if ((orderByType == null) || orderByType.equalsIgnoreCase("DESC")) {
063                            return "DESC";
064                    }
065                    else {
066                            return "ASC";
067                    }
068            }
069    
070            public AssetEntryQuery() {
071                    Date now = new Date();
072    
073                    _expirationDate = now;
074                    _publishDate = now;
075            }
076    
077            public AssetEntryQuery(
078                            long[] classNameIds, SearchContainer<?> searchContainer)
079                    throws PortalException, SystemException {
080    
081                    this();
082    
083                    setClassNameIds(classNameIds);
084                    _start = searchContainer.getStart();
085                    _end = searchContainer.getEnd();
086    
087                    if (Validator.isNotNull(searchContainer.getOrderByCol())) {
088                            setOrderByCol1(searchContainer.getOrderByCol());
089                            setOrderByType1(searchContainer.getOrderByType());
090                    }
091    
092                    PortletRequest portletRequest = searchContainer.getPortletRequest();
093    
094                    ThemeDisplay themeDisplay = (ThemeDisplay)portletRequest.getAttribute(
095                            WebKeys.THEME_DISPLAY);
096    
097                    _groupIds = new long[] {themeDisplay.getScopeGroupId()};
098    
099                    long categoryId = ParamUtil.getLong(portletRequest, "categoryId");
100    
101                    if (categoryId > 0) {
102                            _allCategoryIds = new long[] {categoryId};
103                    }
104    
105                    String tagName = ParamUtil.getString(portletRequest, "tag");
106    
107                    if (Validator.isNotNull(tagName)) {
108                            _allTagIds = AssetTagLocalServiceUtil.getTagIds(
109                                    themeDisplay.getParentGroupId(), new String[] {tagName});
110                    }
111            }
112    
113            public AssetEntryQuery(String className, SearchContainer<?> searchContainer)
114                    throws PortalException, SystemException {
115    
116                    this(
117                            new long[] {PortalUtil.getClassNameId(className)}, searchContainer);
118            }
119    
120            public long[] getAllCategoryIds() {
121                    return _allCategoryIds;
122            }
123    
124            public long[] getAllLeftAndRightCategoryIds() {
125                    return _getLeftAndRightCategoryIds(_allCategoryIds);
126            }
127    
128            public long[] getAllTagIds() {
129                    return _allTagIds;
130            }
131    
132            public long[] getAnyCategoryIds() {
133                    return _anyCategoryIds;
134            }
135    
136            public long[] getAnyLeftAndRightCategoryIds() {
137                    return _getLeftAndRightCategoryIds(_anyCategoryIds);
138            }
139    
140            public long[] getAnyTagIds() {
141                    return _anyTagIds;
142            }
143    
144            public long[] getClassNameIds() {
145                    return _classNameIds;
146            }
147    
148            public int getEnd() {
149                    return _end;
150            }
151    
152            public Date getExpirationDate() {
153                    return _expirationDate;
154            }
155    
156            public long[] getGroupIds() {
157                    return _groupIds;
158            }
159    
160            public long[] getNotAllCategoryIds() {
161                    return _notAllCategoryIds;
162            }
163    
164            public long[] getNotAllLeftAndRightCategoryIds() {
165                    return _getLeftAndRightCategoryIds(_notAllCategoryIds);
166            }
167    
168            public long[] getNotAllTagIds() {
169                    return _notAllTagIds;
170            }
171    
172            public long[] getNotAnyCategoryIds() {
173                    return _notAnyCategoryIds;
174            }
175    
176            public long[] getNotAnyLeftAndRightCategoryIds() {
177                    return _getLeftAndRightCategoryIds(_notAnyCategoryIds);
178            }
179    
180            public long[] getNotAnyTagIds() {
181                    return _notAnyTagIds;
182            }
183    
184            public String getOrderByCol1() {
185                    return checkOrderByCol(_orderByCol1);
186            }
187    
188            public String getOrderByCol2() {
189                    return checkOrderByCol(_orderByCol2);
190            }
191    
192            public String getOrderByType1() {
193                    return checkOrderByType(_orderByType1);
194            }
195    
196            public String getOrderByType2() {
197                    return checkOrderByType(_orderByType2);
198            }
199    
200            public Date getPublishDate() {
201                    return _publishDate;
202            }
203    
204            public int getStart() {
205                    return _start;
206            }
207    
208            public boolean isExcludeZeroViewCount() {
209                    return _excludeZeroViewCount;
210            }
211    
212            public Boolean isVisible() {
213                    return _visible;
214            }
215    
216            public void setAllCategoryIds(long[] allCategoryIds) {
217                    _allCategoryIds = allCategoryIds;
218            }
219    
220            public void setAllTagIds(long[] allTagIds) {
221                    _allTagIds = allTagIds;
222            }
223    
224            public void setAnyCategoryIds(long[] anyCategoryIds) {
225                    _anyCategoryIds = anyCategoryIds;
226            }
227    
228            public void setAnyTagIds(long[] anyTagIds) {
229                    _anyTagIds = anyTagIds;
230            }
231    
232            public void setClassName(String className) {
233                    long classNameId = PortalUtil.getClassNameId(className);
234    
235                    _classNameIds = new long[] {classNameId};
236            }
237    
238            public void setClassNameIds(long[] classNameIds) {
239                    _classNameIds = classNameIds;
240            }
241    
242            public void setEnd(int end) {
243                    _end = end;
244            }
245    
246            public void setExcludeZeroViewCount(boolean excludeZeroViewCount) {
247                    _excludeZeroViewCount = excludeZeroViewCount;
248            }
249    
250            public void setExpirationDate(Date expirationDate) {
251                    _expirationDate = expirationDate;
252            }
253    
254            public void setGroupIds(long[] groupIds) {
255                    _groupIds = groupIds;
256            }
257    
258            public void setNotAllCategoryIds(long[] notAllCategoryIds) {
259                    _notAllCategoryIds = notAllCategoryIds;
260            }
261    
262            public void setNotAllTagIds(long[] notAllTagIds) {
263                    _notAllTagIds = notAllTagIds;
264            }
265    
266            public void setNotAnyCategoryIds(long[] notAnyCategoryIds) {
267                    _notAnyCategoryIds = notAnyCategoryIds;
268            }
269    
270            public void setNotAnyTagIds(long[] notAnyTagIds) {
271                    _notAnyTagIds = notAnyTagIds;
272            }
273    
274            public void setOrderByCol1(String orderByCol1) {
275                    _orderByCol1 = orderByCol1;
276            }
277    
278            public void setOrderByCol2(String orderByCol2) {
279                    _orderByCol2 = orderByCol2;
280            }
281    
282            public void setOrderByType1(String orderByType1) {
283                    _orderByType1 = orderByType1;
284            }
285    
286            public void setOrderByType2(String orderByType2) {
287                    _orderByType2 = orderByType2;
288            }
289    
290            public void setPublishDate(Date publishDate) {
291                    _publishDate = publishDate;
292            }
293    
294            public void setStart(int start) {
295                    _start = start;
296            }
297    
298            public void setVisible(Boolean visible) {
299                    _visible = visible;
300            }
301    
302            private long[] _getLeftAndRightCategoryIds(long[] categoryIds) {
303                    long[] leftRightIds = new long[categoryIds.length * 2];
304    
305                    for (int i = 0; i < categoryIds.length; i++) {
306                            long categoryId = categoryIds[i];
307    
308                            try {
309                                    AssetCategory category =
310                                            AssetCategoryLocalServiceUtil.getCategory(categoryId);
311    
312                                    leftRightIds[2 * i] = category.getLeftCategoryId();
313                                    leftRightIds[2 * i + 1] = category.getRightCategoryId();
314                            }
315                            catch (Exception e) {
316                                    _log.warn("Error retrieving category " + categoryId);
317                            }
318                    }
319    
320                    return leftRightIds;
321            }
322    
323            private static Log _log = LogFactoryUtil.getLog(AssetEntryQuery.class);
324    
325            private long[] _allCategoryIds = new long[0];
326            private long[] _allTagIds = new long[0];
327            private long[] _anyCategoryIds = new long[0];
328            private long[] _anyTagIds = new long[0];
329            private long[] _classNameIds = new long[0];
330            private int _end = QueryUtil.ALL_POS;
331            private boolean _excludeZeroViewCount;
332            private Date _expirationDate;
333            private long[] _groupIds = new long[0];
334            private long[] _notAllCategoryIds = new long[0];
335            private long[] _notAllTagIds = new long[0];
336            private long[] _notAnyCategoryIds = new long[0];
337            private long[] _notAnyTagIds = new long[0];
338            private String _orderByCol1;
339            private String _orderByCol2;
340            private String _orderByType1;
341            private String _orderByType2;
342            private Date _publishDate;
343            private int _start = QueryUtil.ALL_POS;
344            private Boolean _visible = Boolean.TRUE;
345    
346    }