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 (int i = 0; i < ORDER_BY_COLUMNS.length; i++) {
053                            if (orderByCol.equals(ORDER_BY_COLUMNS[i])) {
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                    PortletRequest portletRequest = searchContainer.getPortletRequest();
088    
089                    ThemeDisplay themeDisplay = (ThemeDisplay)portletRequest.getAttribute(
090                            WebKeys.THEME_DISPLAY);
091    
092                    _groupIds = new long[] {themeDisplay.getScopeGroupId()};
093    
094                    long categoryId = ParamUtil.getLong(portletRequest, "categoryId");
095    
096                    if (categoryId > 0) {
097                            _allCategoryIds = new long[] {categoryId};
098                    }
099    
100                    String tagName = ParamUtil.getString(portletRequest, "tag");
101    
102                    if (Validator.isNotNull(tagName)) {
103                            _allTagIds = AssetTagLocalServiceUtil.getTagIds(
104                                    _groupIds[0], new String[] {tagName});
105                    }
106            }
107    
108            public AssetEntryQuery(String className, SearchContainer<?> searchContainer)
109                    throws PortalException, SystemException {
110    
111                    this(
112                            new long[] {PortalUtil.getClassNameId(className)}, searchContainer);
113            }
114    
115            public long[] getAllCategoryIds() {
116                    return _allCategoryIds;
117            }
118    
119            public long[] getAllLeftAndRightCategoryIds() {
120                    return _getLeftAndRightCategoryIds(_allCategoryIds);
121            }
122    
123            public long[] getAllTagIds() {
124                    return _allTagIds;
125            }
126    
127            public long[] getAnyCategoryIds() {
128                    return _anyCategoryIds;
129            }
130    
131            public long[] getAnyLeftAndRightCategoryIds() {
132                    return _getLeftAndRightCategoryIds(_anyCategoryIds);
133            }
134    
135            public long[] getAnyTagIds() {
136                    return _anyTagIds;
137            }
138    
139            public long[] getClassNameIds() {
140                    return _classNameIds;
141            }
142    
143            public int getEnd() {
144                    return _end;
145            }
146    
147            public Date getExpirationDate() {
148                    return _expirationDate;
149            }
150    
151            public long[] getGroupIds() {
152                    return _groupIds;
153            }
154    
155            public long[] getNotAllCategoryIds() {
156                    return _notAllCategoryIds;
157            }
158    
159            public long[] getNotAllLeftAndRightCategoryIds() {
160                    return _getLeftAndRightCategoryIds(_notAllCategoryIds);
161            }
162    
163            public long[] getNotAllTagIds() {
164                    return _notAllTagIds;
165            }
166    
167            public long[] getNotAnyCategoryIds() {
168                    return _notAnyCategoryIds;
169            }
170    
171            public long[] getNotAnyLeftAndRightCategoryIds() {
172                    return _getLeftAndRightCategoryIds(_notAnyCategoryIds);
173            }
174    
175            public long[] getNotAnyTagIds() {
176                    return _notAnyTagIds;
177            }
178    
179            public String getOrderByCol1() {
180                    return checkOrderByCol(_orderByCol1);
181            }
182    
183            public String getOrderByCol2() {
184                    return checkOrderByCol(_orderByCol2);
185            }
186    
187            public String getOrderByType1() {
188                    return checkOrderByType(_orderByType1);
189            }
190    
191            public String getOrderByType2() {
192                    return checkOrderByType(_orderByType2);
193            }
194    
195            public Date getPublishDate() {
196                    return _publishDate;
197            }
198    
199            public int getStart() {
200                    return _start;
201            }
202    
203            public boolean isExcludeZeroViewCount() {
204                    return _excludeZeroViewCount;
205            }
206    
207            public Boolean isVisible() {
208                    return _visible;
209            }
210    
211            public void setAllCategoryIds(long[] allCategoryIds) {
212                    _allCategoryIds = allCategoryIds;
213            }
214    
215            public void setAllTagIds(long[] allTagIds) {
216                    _allTagIds = allTagIds;
217            }
218    
219            public void setAnyCategoryIds(long[] anyCategoryIds) {
220                    _anyCategoryIds = anyCategoryIds;
221            }
222    
223            public void setAnyTagIds(long[] anyTagIds) {
224                    _anyTagIds = anyTagIds;
225            }
226    
227            public void setClassName(String className) {
228                    long classNameId = PortalUtil.getClassNameId(className);
229    
230                    _classNameIds = new long[] {classNameId};
231            }
232    
233            public void setClassNameIds(long[] classNameIds) {
234                    _classNameIds = classNameIds;
235            }
236    
237            public void setEnd(int end) {
238                    _end = end;
239            }
240    
241            public void setExcludeZeroViewCount(boolean excludeZeroViewCount) {
242                    _excludeZeroViewCount = excludeZeroViewCount;
243            }
244    
245            public void setExpirationDate(Date expirationDate) {
246                    _expirationDate = expirationDate;
247            }
248    
249            public void setGroupIds(long[] groupIds) {
250                    _groupIds = groupIds;
251            }
252    
253            public void setNotAllCategoryIds(long[] notAllCategoryIds) {
254                    _notAllCategoryIds = notAllCategoryIds;
255            }
256    
257            public void setNotAllTagIds(long[] notAllTagIds) {
258                    _notAllTagIds = notAllTagIds;
259            }
260    
261            public void setNotAnyCategoryIds(long[] notAnyCategoryIds) {
262                    _notAnyCategoryIds = notAnyCategoryIds;
263            }
264    
265            public void setNotAnyTagIds(long[] notAnyTagIds) {
266                    _notAnyTagIds = notAnyTagIds;
267            }
268    
269            public void setOrderByCol1(String orderByCol1) {
270                    _orderByCol1 = orderByCol1;
271            }
272    
273            public void setOrderByCol2(String orderByCol2) {
274                    _orderByCol2 = orderByCol2;
275            }
276    
277            public void setOrderByType1(String orderByType1) {
278                    _orderByType1 = orderByType1;
279            }
280    
281            public void setOrderByType2(String orderByType2) {
282                    _orderByType2 = orderByType2;
283            }
284    
285            public void setPublishDate(Date publishDate) {
286                    _publishDate = publishDate;
287            }
288    
289            public void setStart(int start) {
290                    _start = start;
291            }
292    
293            public void setVisible(Boolean visible) {
294                    _visible = visible;
295            }
296    
297            private long[] _getLeftAndRightCategoryIds(long[] categoryIds) {
298                    long[] leftRightIds = new long[categoryIds.length * 2];
299    
300                    for (int i = 0; i < categoryIds.length; i++) {
301                            long categoryId = categoryIds[i];
302    
303                            try {
304                                    AssetCategory category =
305                                            AssetCategoryLocalServiceUtil.getCategory(categoryId);
306    
307                                    leftRightIds[2 * i] = category.getLeftCategoryId();
308                                    leftRightIds[2 * i + 1] = category.getRightCategoryId();
309                            }
310                            catch (Exception e) {
311                                    _log.warn("Error retrieving category " + categoryId);
312                            }
313                    }
314    
315                    return leftRightIds;
316            }
317    
318            private static Log _log = LogFactoryUtil.getLog(AssetEntryQuery.class);
319    
320            private long[] _allCategoryIds = new long[0];
321            private long[] _allTagIds = new long[0];
322            private long[] _anyCategoryIds = new long[0];
323            private long[] _anyTagIds = new long[0];
324            private long[] _classNameIds = new long[0];
325            private int _end = QueryUtil.ALL_POS;
326            private boolean _excludeZeroViewCount;
327            private Date _expirationDate;
328            private long[] _groupIds = new long[0];
329            private long[] _notAllCategoryIds = new long[0];
330            private long[] _notAllTagIds = new long[0];
331            private long[] _notAnyCategoryIds = new long[0];
332            private long[] _notAnyTagIds = new long[0];
333            private String _orderByCol1;
334            private String _orderByCol2;
335            private String _orderByType1;
336            private String _orderByType2;
337            private Date _publishDate;
338            private int _start = QueryUtil.ALL_POS;
339            private Boolean _visible = Boolean.TRUE;
340    
341    }