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