1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet.asset.service.persistence;
16  
17  import com.liferay.portal.kernel.dao.orm.QueryUtil;
18  import com.liferay.portal.kernel.dao.search.SearchContainer;
19  import com.liferay.portal.kernel.exception.PortalException;
20  import com.liferay.portal.kernel.exception.SystemException;
21  import com.liferay.portal.kernel.log.Log;
22  import com.liferay.portal.kernel.log.LogFactoryUtil;
23  import com.liferay.portal.kernel.util.ParamUtil;
24  import com.liferay.portal.kernel.util.Validator;
25  import com.liferay.portal.kernel.util.WebKeys;
26  import com.liferay.portal.theme.ThemeDisplay;
27  import com.liferay.portal.util.PortalUtil;
28  import com.liferay.portlet.asset.model.AssetCategory;
29  import com.liferay.portlet.asset.service.AssetCategoryLocalServiceUtil;
30  import com.liferay.portlet.asset.service.AssetTagLocalServiceUtil;
31  
32  import java.util.Date;
33  
34  import javax.portlet.PortletRequest;
35  
36  /**
37   * <a href="AssetEntryQuery.java.html"><b><i>View Source</i></b></a>
38   *
39   * @author Brian Wing Shun Chan
40   * @author Jorge Ferrer
41   */
42  public class AssetEntryQuery {
43  
44      public static String[] ORDER_BY_COLUMNS = new String[] {
45          "title", "createDate", "modifiedDate", "publishDate", "expirationDate",
46          "priority", "viewCount"
47      };
48  
49      public static String checkOrderByCol(String orderByCol) {
50          if (orderByCol == null) {
51              return ORDER_BY_COLUMNS[2];
52          }
53  
54          for (int i = 0; i < ORDER_BY_COLUMNS.length; i++) {
55              if (orderByCol.equals(ORDER_BY_COLUMNS[i])) {
56                  return orderByCol;
57              }
58          }
59  
60          return ORDER_BY_COLUMNS[2];
61      }
62  
63      public static String checkOrderByType(String orderByType) {
64          if ((orderByType == null) || orderByType.equalsIgnoreCase("DESC")) {
65              return "DESC";
66          }
67          else {
68              return "ASC";
69          }
70      }
71  
72      public AssetEntryQuery() {
73          Date now = new Date();
74  
75          _expirationDate = now;
76          _publishDate = now;
77      }
78  
79      public AssetEntryQuery(
80              long[] classNameIds, SearchContainer<?> searchContainer)
81          throws PortalException, SystemException {
82  
83          this();
84  
85          setClassNameIds(classNameIds);
86          _start = searchContainer.getStart();
87          _end = searchContainer.getEnd();
88  
89          PortletRequest portletRequest = searchContainer.getPortletRequest();
90  
91          ThemeDisplay themeDisplay = (ThemeDisplay)portletRequest.getAttribute(
92              WebKeys.THEME_DISPLAY);
93  
94          _groupIds = new long[] {themeDisplay.getScopeGroupId()};
95  
96          long categoryId = ParamUtil.getLong(portletRequest, "categoryId");
97  
98          if (categoryId > 0) {
99              _allCategoryIds = new long[] {categoryId};
100         }
101 
102         String tagName = ParamUtil.getString(portletRequest, "tag");
103 
104         if (Validator.isNotNull(tagName)) {
105             _allTagIds = AssetTagLocalServiceUtil.getTagIds(
106                 _groupIds[0], new String[] {tagName});
107         }
108     }
109 
110     public AssetEntryQuery(String className, SearchContainer<?> searchContainer)
111         throws PortalException, SystemException {
112 
113         this(
114             new long[] {PortalUtil.getClassNameId(className)}, searchContainer);
115     }
116 
117     public long[] getAllCategoryIds() {
118         return _allCategoryIds;
119     }
120 
121     public long[] getAllLeftAndRightCategoryIds() {
122         return _getLeftAndRightCategoryIds(_allCategoryIds);
123     }
124 
125     public long[] getAllTagIds() {
126         return _allTagIds;
127     }
128 
129     public long[] getAnyCategoryIds() {
130         return _anyCategoryIds;
131     }
132 
133     public long[] getAnyLeftAndRightCategoryIds() {
134         return _getLeftAndRightCategoryIds(_anyCategoryIds);
135     }
136 
137     public long[] getAnyTagIds() {
138         return _anyTagIds;
139     }
140 
141     public long[] getClassNameIds() {
142         return _classNameIds;
143     }
144 
145     public int getEnd() {
146         return _end;
147     }
148 
149     public Date getExpirationDate() {
150         return _expirationDate;
151     }
152 
153     public long[] getGroupIds() {
154         return _groupIds;
155     }
156 
157     public long[] getNotAllCategoryIds() {
158         return _notAllCategoryIds;
159     }
160 
161     public long[] getNotAllLeftAndRightCategoryIds() {
162         return _getLeftAndRightCategoryIds(_notAllCategoryIds);
163     }
164 
165     public long[] getNotAllTagIds() {
166         return _notAllTagIds;
167     }
168 
169     public long[] getNotAnyCategoryIds() {
170         return _notAnyCategoryIds;
171     }
172 
173     public long[] getNotAnyLeftAndRightCategoryIds() {
174         return _getLeftAndRightCategoryIds(_notAnyCategoryIds);
175     }
176 
177     public long[] getNotAnyTagIds() {
178         return _notAnyTagIds;
179     }
180 
181     public String getOrderByCol1() {
182         return checkOrderByCol(_orderByCol1);
183     }
184 
185     public String getOrderByCol2() {
186         return checkOrderByCol(_orderByCol2);
187     }
188 
189     public String getOrderByType1() {
190         return checkOrderByType(_orderByType1);
191     }
192 
193     public String getOrderByType2() {
194         return checkOrderByType(_orderByType2);
195     }
196 
197     public Date getPublishDate() {
198         return _publishDate;
199     }
200 
201     public int getStart() {
202         return _start;
203     }
204 
205     public boolean isExcludeZeroViewCount() {
206         return _excludeZeroViewCount;
207     }
208 
209     public Boolean isVisible() {
210         return _visible;
211     }
212 
213     public void setAllCategoryIds(long[] allCategoryIds) {
214         _allCategoryIds = allCategoryIds;
215     }
216 
217     public void setAllTagIds(long[] allTagIds) {
218         _allTagIds = allTagIds;
219     }
220 
221     public void setAnyCategoryIds(long[] anyCategoryIds) {
222         _anyCategoryIds = anyCategoryIds;
223     }
224 
225     public void setAnyTagIds(long[] anyTagIds) {
226         _anyTagIds = anyTagIds;
227     }
228 
229     public void setClassName(String className) {
230         long classNameId = PortalUtil.getClassNameId(className);
231 
232         _classNameIds = new long[] {classNameId};
233     }
234 
235     public void setClassNameIds(long[] classNameIds) {
236         _classNameIds = classNameIds;
237     }
238 
239     public void setEnd(int end) {
240         _end = end;
241     }
242 
243     public void setExcludeZeroViewCount(boolean excludeZeroViewCount) {
244         _excludeZeroViewCount = excludeZeroViewCount;
245     }
246 
247     public void setExpirationDate(Date expirationDate) {
248         _expirationDate = expirationDate;
249     }
250 
251     public void setGroupIds(long[] groupIds) {
252         _groupIds = groupIds;
253     }
254 
255     public void setNotAllCategoryIds(long[] notAllCategoryIds) {
256         _notAllCategoryIds = notAllCategoryIds;
257     }
258 
259     public void setNotAllTagIds(long[] notAllTagIds) {
260         _notAllTagIds = notAllTagIds;
261     }
262 
263     public void setNotAnyCategoryIds(long[] notAnyCategoryIds) {
264         _notAnyCategoryIds = notAnyCategoryIds;
265     }
266 
267     public void setNotAnyTagIds(long[] notAnyTagIds) {
268         _notAnyTagIds = notAnyTagIds;
269     }
270 
271     public void setOrderByCol1(String orderByCol1) {
272         _orderByCol1 = orderByCol1;
273     }
274 
275     public void setOrderByCol2(String orderByCol2) {
276         _orderByCol2 = orderByCol2;
277     }
278 
279     public void setOrderByType1(String orderByType1) {
280         _orderByType1 = orderByType1;
281     }
282 
283     public void setOrderByType2(String orderByType2) {
284         _orderByType2 = orderByType2;
285     }
286 
287     public void setPublishDate(Date publishDate) {
288         _publishDate = publishDate;
289     }
290 
291     public void setStart(int start) {
292         _start = start;
293     }
294 
295     public void setVisible(Boolean visible) {
296         _visible = visible;
297     }
298 
299     private long[] _getLeftAndRightCategoryIds(long[] categoryIds) {
300         long[] leftRightIds = new long[categoryIds.length * 2];
301 
302         for (int i = 0; i < categoryIds.length; i++) {
303             long categoryId = categoryIds[i];
304 
305             try {
306                 AssetCategory category =
307                     AssetCategoryLocalServiceUtil.getCategory(categoryId);
308 
309                 leftRightIds[2 * i] = category.getLeftCategoryId();
310                 leftRightIds[2 * i + 1] = category.getRightCategoryId();
311             }
312             catch (Exception e) {
313                 _log.warn("Error retrieving category " + categoryId);
314             }
315         }
316 
317         return leftRightIds;
318     }
319 
320     private static Log _log = LogFactoryUtil.getLog(AssetEntryQuery.class);
321 
322     private long[] _allCategoryIds = new long[0];
323     private long[] _allTagIds = new long[0];
324     private long[] _anyCategoryIds = new long[0];
325     private long[] _anyTagIds = new long[0];
326     private long[] _classNameIds = new long[0];
327     private int _end = QueryUtil.ALL_POS;
328     private boolean _excludeZeroViewCount;
329     private Date _expirationDate;
330     private long[] _groupIds = new long[0];
331     private long[] _notAllCategoryIds = new long[0];
332     private long[] _notAllTagIds = new long[0];
333     private long[] _notAnyCategoryIds = new long[0];
334     private long[] _notAnyTagIds = new long[0];
335     private String _orderByCol1;
336     private String _orderByCol2;
337     private String _orderByType1;
338     private String _orderByType2;
339     private Date _publishDate;
340     private int _start = QueryUtil.ALL_POS;
341     private Boolean _visible = Boolean.TRUE;
342 
343 }