001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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 AssetEntryQuery(AssetEntryQuery assetEntryQuery) {
043                    setAllCategoryIds(assetEntryQuery.getAllCategoryIds());
044                    setAllTagIds(assetEntryQuery.getAllTagIds());
045                    setAnyCategoryIds(assetEntryQuery.getAnyCategoryIds());
046                    setAnyTagIds(assetEntryQuery.getAnyTagIds());
047                    setClassNameIds(assetEntryQuery.getClassNameIds());
048                    setEnd(assetEntryQuery.getEnd());
049                    setExcludeZeroViewCount(assetEntryQuery.isExcludeZeroViewCount());
050                    setExpirationDate(assetEntryQuery.getExpirationDate());
051                    setGroupIds(assetEntryQuery.getGroupIds());
052                    setNotAllCategoryIds(assetEntryQuery.getNotAllCategoryIds());
053                    setNotAllTagIds(assetEntryQuery.getNotAllTagIds());
054                    setNotAnyCategoryIds(assetEntryQuery.getNotAnyCategoryIds());
055                    setNotAnyTagIds(assetEntryQuery.getNotAnyTagIds());
056                    setOrderByCol1(assetEntryQuery.getOrderByCol1());
057                    setOrderByCol2(assetEntryQuery.getOrderByCol2());
058                    setOrderByType1(assetEntryQuery.getOrderByType1());
059                    setOrderByType2(assetEntryQuery.getOrderByType2());
060                    setPublishDate(assetEntryQuery.getPublishDate());
061                    setStart(assetEntryQuery.getStart());
062                    setVisible(assetEntryQuery.isVisible());
063            }
064    
065            public static String[] ORDER_BY_COLUMNS = new String[] {
066                    "title", "createDate", "modifiedDate", "publishDate", "expirationDate",
067                    "priority", "viewCount", "ratings"
068            };
069    
070            public static String checkOrderByCol(String orderByCol) {
071                    if (orderByCol == null) {
072                            return ORDER_BY_COLUMNS[2];
073                    }
074    
075                    for (String curOrderByCol : ORDER_BY_COLUMNS) {
076                            if (orderByCol.equals(curOrderByCol)) {
077                                    return orderByCol;
078                            }
079                    }
080    
081                    return ORDER_BY_COLUMNS[2];
082            }
083    
084            public static String checkOrderByType(String orderByType) {
085                    if ((orderByType == null) || orderByType.equalsIgnoreCase("DESC")) {
086                            return "DESC";
087                    }
088                    else {
089                            return "ASC";
090                    }
091            }
092    
093            public AssetEntryQuery() {
094                    Date now = new Date();
095    
096                    _expirationDate = now;
097                    _publishDate = now;
098            }
099    
100            public AssetEntryQuery(
101                            long[] classNameIds, SearchContainer<?> searchContainer)
102                    throws PortalException, SystemException {
103    
104                    this();
105    
106                    setClassNameIds(classNameIds);
107                    _start = searchContainer.getStart();
108                    _end = searchContainer.getEnd();
109    
110                    if (Validator.isNotNull(searchContainer.getOrderByCol())) {
111                            setOrderByCol1(searchContainer.getOrderByCol());
112                            setOrderByType1(searchContainer.getOrderByType());
113                    }
114    
115                    PortletRequest portletRequest = searchContainer.getPortletRequest();
116    
117                    ThemeDisplay themeDisplay = (ThemeDisplay)portletRequest.getAttribute(
118                            WebKeys.THEME_DISPLAY);
119    
120                    _groupIds = new long[] {themeDisplay.getScopeGroupId()};
121    
122                    long categoryId = ParamUtil.getLong(portletRequest, "categoryId");
123    
124                    if (categoryId > 0) {
125                            _allCategoryIds = new long[] {categoryId};
126                    }
127    
128                    String tagName = ParamUtil.getString(portletRequest, "tag");
129    
130                    if (Validator.isNotNull(tagName)) {
131                            _allTagIds = AssetTagLocalServiceUtil.getTagIds(
132                                    themeDisplay.getParentGroupId(), new String[] {tagName});
133                    }
134            }
135    
136            public AssetEntryQuery(String className, SearchContainer<?> searchContainer)
137                    throws PortalException, SystemException {
138    
139                    this(
140                            new long[] {PortalUtil.getClassNameId(className)}, searchContainer);
141            }
142    
143            public long[] getAllCategoryIds() {
144                    return _allCategoryIds;
145            }
146    
147            public long[] getAllLeftAndRightCategoryIds() {
148                    return _getLeftAndRightCategoryIds(_allCategoryIds);
149            }
150    
151            public long[] getAllTagIds() {
152                    return _allTagIds;
153            }
154    
155            public long[] getAnyCategoryIds() {
156                    return _anyCategoryIds;
157            }
158    
159            public long[] getAnyLeftAndRightCategoryIds() {
160                    return _getLeftAndRightCategoryIds(_anyCategoryIds);
161            }
162    
163            public long[] getAnyTagIds() {
164                    return _anyTagIds;
165            }
166    
167            public long[] getClassNameIds() {
168                    return _classNameIds;
169            }
170    
171            public int getEnd() {
172                    return _end;
173            }
174    
175            public Date getExpirationDate() {
176                    return _expirationDate;
177            }
178    
179            public long[] getGroupIds() {
180                    return _groupIds;
181            }
182    
183            public long[] getNotAllCategoryIds() {
184                    return _notAllCategoryIds;
185            }
186    
187            public long[] getNotAllLeftAndRightCategoryIds() {
188                    return _getLeftAndRightCategoryIds(_notAllCategoryIds);
189            }
190    
191            public long[] getNotAllTagIds() {
192                    return _notAllTagIds;
193            }
194    
195            public long[] getNotAnyCategoryIds() {
196                    return _notAnyCategoryIds;
197            }
198    
199            public long[] getNotAnyLeftAndRightCategoryIds() {
200                    return _getLeftAndRightCategoryIds(_notAnyCategoryIds);
201            }
202    
203            public long[] getNotAnyTagIds() {
204                    return _notAnyTagIds;
205            }
206    
207            public String getOrderByCol1() {
208                    return checkOrderByCol(_orderByCol1);
209            }
210    
211            public String getOrderByCol2() {
212                    return checkOrderByCol(_orderByCol2);
213            }
214    
215            public String getOrderByType1() {
216                    return checkOrderByType(_orderByType1);
217            }
218    
219            public String getOrderByType2() {
220                    return checkOrderByType(_orderByType2);
221            }
222    
223            public Date getPublishDate() {
224                    return _publishDate;
225            }
226    
227            public int getStart() {
228                    return _start;
229            }
230    
231            public boolean isExcludeZeroViewCount() {
232                    return _excludeZeroViewCount;
233            }
234    
235            public Boolean isVisible() {
236                    return _visible;
237            }
238    
239            public void setAllCategoryIds(long[] allCategoryIds) {
240                    _allCategoryIds = allCategoryIds;
241            }
242    
243            public void setAllTagIds(long[] allTagIds) {
244                    _allTagIds = allTagIds;
245            }
246    
247            public void setAnyCategoryIds(long[] anyCategoryIds) {
248                    _anyCategoryIds = anyCategoryIds;
249            }
250    
251            public void setAnyTagIds(long[] anyTagIds) {
252                    _anyTagIds = anyTagIds;
253            }
254    
255            public void setClassName(String className) {
256                    long classNameId = PortalUtil.getClassNameId(className);
257    
258                    _classNameIds = new long[] {classNameId};
259            }
260    
261            public void setClassNameIds(long[] classNameIds) {
262                    _classNameIds = classNameIds;
263            }
264    
265            public void setEnd(int end) {
266                    _end = end;
267            }
268    
269            public void setExcludeZeroViewCount(boolean excludeZeroViewCount) {
270                    _excludeZeroViewCount = excludeZeroViewCount;
271            }
272    
273            public void setExpirationDate(Date expirationDate) {
274                    _expirationDate = expirationDate;
275            }
276    
277            public void setGroupIds(long[] groupIds) {
278                    _groupIds = groupIds;
279            }
280    
281            public void setNotAllCategoryIds(long[] notAllCategoryIds) {
282                    _notAllCategoryIds = notAllCategoryIds;
283            }
284    
285            public void setNotAllTagIds(long[] notAllTagIds) {
286                    _notAllTagIds = notAllTagIds;
287            }
288    
289            public void setNotAnyCategoryIds(long[] notAnyCategoryIds) {
290                    _notAnyCategoryIds = notAnyCategoryIds;
291            }
292    
293            public void setNotAnyTagIds(long[] notAnyTagIds) {
294                    _notAnyTagIds = notAnyTagIds;
295            }
296    
297            public void setOrderByCol1(String orderByCol1) {
298                    _orderByCol1 = orderByCol1;
299            }
300    
301            public void setOrderByCol2(String orderByCol2) {
302                    _orderByCol2 = orderByCol2;
303            }
304    
305            public void setOrderByType1(String orderByType1) {
306                    _orderByType1 = orderByType1;
307            }
308    
309            public void setOrderByType2(String orderByType2) {
310                    _orderByType2 = orderByType2;
311            }
312    
313            public void setPublishDate(Date publishDate) {
314                    _publishDate = publishDate;
315            }
316    
317            public void setStart(int start) {
318                    _start = start;
319            }
320    
321            public void setVisible(Boolean visible) {
322                    _visible = visible;
323            }
324    
325            private long[] _getLeftAndRightCategoryIds(long[] categoryIds) {
326                    long[] leftRightIds = new long[categoryIds.length * 3];
327    
328                    for (int i = 0; i < categoryIds.length; i++) {
329                            long categoryId = categoryIds[i];
330    
331                            try {
332                                    AssetCategory category =
333                                            AssetCategoryLocalServiceUtil.getCategory(categoryId);
334    
335                                    leftRightIds[3 * i] = category.getGroupId();
336                                    leftRightIds[3 * i + 1] = category.getLeftCategoryId();
337                                    leftRightIds[3 * i + 2] = category.getRightCategoryId();
338                            }
339                            catch (Exception e) {
340                                    _log.warn("Error retrieving category " + categoryId);
341                            }
342                    }
343    
344                    return leftRightIds;
345            }
346    
347            private static Log _log = LogFactoryUtil.getLog(AssetEntryQuery.class);
348    
349            private long[] _allCategoryIds = new long[0];
350            private long[] _allTagIds = new long[0];
351            private long[] _anyCategoryIds = new long[0];
352            private long[] _anyTagIds = new long[0];
353            private long[] _classNameIds = new long[0];
354            private int _end = QueryUtil.ALL_POS;
355            private boolean _excludeZeroViewCount;
356            private Date _expirationDate;
357            private long[] _groupIds = new long[0];
358            private long[] _notAllCategoryIds = new long[0];
359            private long[] _notAllTagIds = new long[0];
360            private long[] _notAnyCategoryIds = new long[0];
361            private long[] _notAnyTagIds = new long[0];
362            private String _orderByCol1;
363            private String _orderByCol2;
364            private String _orderByType1;
365            private String _orderByType2;
366            private Date _publishDate;
367            private int _start = QueryUtil.ALL_POS;
368            private Boolean _visible = Boolean.TRUE;
369    
370    }