001    /**
002     * Copyright (c) 2000-2012 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.StringBundler;
025    import com.liferay.portal.kernel.util.StringUtil;
026    import com.liferay.portal.kernel.util.Validator;
027    import com.liferay.portal.kernel.util.WebKeys;
028    import com.liferay.portal.model.Layout;
029    import com.liferay.portal.theme.ThemeDisplay;
030    import com.liferay.portal.util.PortalUtil;
031    import com.liferay.portlet.asset.model.AssetCategory;
032    import com.liferay.portlet.asset.service.AssetCategoryLocalServiceUtil;
033    import com.liferay.portlet.asset.service.AssetTagLocalServiceUtil;
034    
035    import java.util.Date;
036    
037    import javax.portlet.PortletRequest;
038    
039    /**
040     * @author Brian Wing Shun Chan
041     * @author Jorge Ferrer
042     * @author Juan Fernández
043     */
044    public class AssetEntryQuery {
045    
046            public AssetEntryQuery(AssetEntryQuery assetEntryQuery) {
047                    setAllCategoryIds(assetEntryQuery.getAllCategoryIds());
048                    setAllTagIds(assetEntryQuery.getAllTagIds());
049                    setAnyCategoryIds(assetEntryQuery.getAnyCategoryIds());
050                    setAnyTagIds(assetEntryQuery.getAnyTagIds());
051                    setClassNameIds(assetEntryQuery.getClassNameIds());
052                    setClassTypeIds(assetEntryQuery.getClassTypeIds());
053                    setEnd(assetEntryQuery.getEnd());
054                    setExcludeZeroViewCount(assetEntryQuery.isExcludeZeroViewCount());
055                    setExpirationDate(assetEntryQuery.getExpirationDate());
056                    setGroupIds(assetEntryQuery.getGroupIds());
057                    setLayout(assetEntryQuery.getLayout());
058                    setNotAllCategoryIds(assetEntryQuery.getNotAllCategoryIds());
059                    setNotAllTagIds(assetEntryQuery.getNotAllTagIds());
060                    setNotAnyCategoryIds(assetEntryQuery.getNotAnyCategoryIds());
061                    setNotAnyTagIds(assetEntryQuery.getNotAnyTagIds());
062                    setOrderByCol1(assetEntryQuery.getOrderByCol1());
063                    setOrderByCol2(assetEntryQuery.getOrderByCol2());
064                    setOrderByType1(assetEntryQuery.getOrderByType1());
065                    setOrderByType2(assetEntryQuery.getOrderByType2());
066                    setPublishDate(assetEntryQuery.getPublishDate());
067                    setStart(assetEntryQuery.getStart());
068                    setVisible(assetEntryQuery.isVisible());
069            }
070    
071            public static String[] ORDER_BY_COLUMNS = new String[] {
072                    "title", "createDate", "modifiedDate", "publishDate", "expirationDate",
073                    "priority", "viewCount", "ratings"
074            };
075    
076            public static String checkOrderByCol(String orderByCol) {
077                    if (orderByCol == null) {
078                            return ORDER_BY_COLUMNS[2];
079                    }
080    
081                    for (String curOrderByCol : ORDER_BY_COLUMNS) {
082                            if (orderByCol.equals(curOrderByCol)) {
083                                    return orderByCol;
084                            }
085                    }
086    
087                    return ORDER_BY_COLUMNS[2];
088            }
089    
090            public static String checkOrderByType(String orderByType) {
091                    if ((orderByType == null) || orderByType.equalsIgnoreCase("DESC")) {
092                            return "DESC";
093                    }
094                    else {
095                            return "ASC";
096                    }
097            }
098    
099            public AssetEntryQuery() {
100                    Date now = new Date();
101    
102                    _expirationDate = now;
103                    _publishDate = now;
104            }
105    
106            public AssetEntryQuery(
107                            long[] classNameIds, SearchContainer<?> searchContainer)
108                    throws PortalException, SystemException {
109    
110                    this();
111    
112                    setClassNameIds(classNameIds);
113                    _start = searchContainer.getStart();
114                    _end = searchContainer.getEnd();
115    
116                    if (Validator.isNotNull(searchContainer.getOrderByCol())) {
117                            setOrderByCol1(searchContainer.getOrderByCol());
118                            setOrderByType1(searchContainer.getOrderByType());
119                    }
120    
121                    PortletRequest portletRequest = searchContainer.getPortletRequest();
122    
123                    ThemeDisplay themeDisplay = (ThemeDisplay)portletRequest.getAttribute(
124                            WebKeys.THEME_DISPLAY);
125    
126                    _groupIds = new long[] {themeDisplay.getScopeGroupId()};
127    
128                    long categoryId = ParamUtil.getLong(portletRequest, "categoryId");
129    
130                    if (categoryId > 0) {
131                            _allCategoryIds = new long[] {categoryId};
132                    }
133    
134                    String tagName = ParamUtil.getString(portletRequest, "tag");
135    
136                    if (Validator.isNotNull(tagName)) {
137                            _allTagIds = AssetTagLocalServiceUtil.getTagIds(
138                                    themeDisplay.getParentGroupId(), new String[] {tagName});
139                    }
140            }
141    
142            public AssetEntryQuery(String className, SearchContainer<?> searchContainer)
143                    throws PortalException, SystemException {
144    
145                    this(
146                            new long[] {PortalUtil.getClassNameId(className)}, searchContainer);
147            }
148    
149            public long[] getAllCategoryIds() {
150                    return _allCategoryIds;
151            }
152    
153            public long[] getAllLeftAndRightCategoryIds() {
154                    return _getLeftAndRightCategoryIds(_allCategoryIds);
155            }
156    
157            public long[] getAllTagIds() {
158                    return _allTagIds;
159            }
160    
161            public long[] getAnyCategoryIds() {
162                    return _anyCategoryIds;
163            }
164    
165            public long[] getAnyLeftAndRightCategoryIds() {
166                    return _getLeftAndRightCategoryIds(_anyCategoryIds);
167            }
168    
169            public long[] getAnyTagIds() {
170                    return _anyTagIds;
171            }
172    
173            public long[] getClassNameIds() {
174                    return _classNameIds;
175            }
176    
177            public long[] getClassTypeIds() {
178                    return _classTypeIds;
179            }
180    
181            public Layout getLayout() {
182                    return _layout;
183            }
184    
185            public long getLinkedAssetEntryId() {
186                    return _linkedAssetEntryId;
187            }
188    
189            public int getEnd() {
190                    return _end;
191            }
192    
193            public Date getExpirationDate() {
194                    return _expirationDate;
195            }
196    
197            public long[] getGroupIds() {
198                    return _groupIds;
199            }
200    
201            public long[] getNotAllCategoryIds() {
202                    return _notAllCategoryIds;
203            }
204    
205            public long[] getNotAllLeftAndRightCategoryIds() {
206                    return _getLeftAndRightCategoryIds(_notAllCategoryIds);
207            }
208    
209            public long[] getNotAllTagIds() {
210                    return _notAllTagIds;
211            }
212    
213            public long[] getNotAnyCategoryIds() {
214                    return _notAnyCategoryIds;
215            }
216    
217            public long[] getNotAnyLeftAndRightCategoryIds() {
218                    return _getLeftAndRightCategoryIds(_notAnyCategoryIds);
219            }
220    
221            public long[] getNotAnyTagIds() {
222                    return _notAnyTagIds;
223            }
224    
225            public String getOrderByCol1() {
226                    return checkOrderByCol(_orderByCol1);
227            }
228    
229            public String getOrderByCol2() {
230                    return checkOrderByCol(_orderByCol2);
231            }
232    
233            public String getOrderByType1() {
234                    return checkOrderByType(_orderByType1);
235            }
236    
237            public String getOrderByType2() {
238                    return checkOrderByType(_orderByType2);
239            }
240    
241            public Date getPublishDate() {
242                    return _publishDate;
243            }
244    
245            public int getStart() {
246                    return _start;
247            }
248    
249            public boolean isEnablePermissions() {
250                    return _enablePermissions;
251            }
252    
253            public boolean isExcludeZeroViewCount() {
254                    return _excludeZeroViewCount;
255            }
256    
257            public Boolean isVisible() {
258                    return _visible;
259            }
260    
261            public void setAllCategoryIds(long[] allCategoryIds) {
262                    _allCategoryIds = allCategoryIds;
263    
264                    _toString = null;
265            }
266    
267            public void setAllTagIds(long[] allTagIds) {
268                    _allTagIds = allTagIds;
269    
270                    _toString = null;
271            }
272    
273            public void setAnyCategoryIds(long[] anyCategoryIds) {
274                    _anyCategoryIds = anyCategoryIds;
275    
276                    _toString = null;
277            }
278    
279            public void setAnyTagIds(long[] anyTagIds) {
280                    _anyTagIds = anyTagIds;
281    
282                    _toString = null;
283            }
284    
285            public void setClassName(String className) {
286                    long classNameId = PortalUtil.getClassNameId(className);
287    
288                    _classNameIds = new long[] {classNameId};
289    
290                    _toString = null;
291            }
292    
293            public void setClassNameIds(long[] classNameIds) {
294                    _classNameIds = classNameIds;
295    
296                    _toString = null;
297            }
298    
299            public void setClassTypeIds(long[] classTypeIds) {
300                    _classTypeIds = classTypeIds;
301    
302                    _toString = null;
303            }
304    
305            public void setEnablePermissions(boolean enablePermissions) {
306                    _enablePermissions = enablePermissions;
307            }
308    
309            public void setLayout(Layout layout) {
310                    _layout = layout;
311    
312                    _toString = null;
313            }
314    
315            public void setLinkedAssetEntryId(long linkedAssetEntryId) {
316                    _linkedAssetEntryId = linkedAssetEntryId;
317    
318                    _toString = null;
319            }
320    
321            public void setEnd(int end) {
322                    _end = end;
323    
324                    _toString = null;
325            }
326    
327            public void setExcludeZeroViewCount(boolean excludeZeroViewCount) {
328                    _excludeZeroViewCount = excludeZeroViewCount;
329    
330                    _toString = null;
331            }
332    
333            public void setExpirationDate(Date expirationDate) {
334                    _expirationDate = expirationDate;
335    
336                    _toString = null;
337            }
338    
339            public void setGroupIds(long[] groupIds) {
340                    _groupIds = groupIds;
341    
342                    _toString = null;
343            }
344    
345            public void setNotAllCategoryIds(long[] notAllCategoryIds) {
346                    _notAllCategoryIds = notAllCategoryIds;
347    
348                    _toString = null;
349            }
350    
351            public void setNotAllTagIds(long[] notAllTagIds) {
352                    _notAllTagIds = notAllTagIds;
353    
354                    _toString = null;
355            }
356    
357            public void setNotAnyCategoryIds(long[] notAnyCategoryIds) {
358                    _notAnyCategoryIds = notAnyCategoryIds;
359    
360                    _toString = null;
361            }
362    
363            public void setNotAnyTagIds(long[] notAnyTagIds) {
364                    _notAnyTagIds = notAnyTagIds;
365    
366                    _toString = null;
367            }
368    
369            public void setOrderByCol1(String orderByCol1) {
370                    _orderByCol1 = orderByCol1;
371    
372                    _toString = null;
373            }
374    
375            public void setOrderByCol2(String orderByCol2) {
376                    _orderByCol2 = orderByCol2;
377    
378                    _toString = null;
379            }
380    
381            public void setOrderByType1(String orderByType1) {
382                    _orderByType1 = orderByType1;
383    
384                    _toString = null;
385            }
386    
387            public void setOrderByType2(String orderByType2) {
388                    _orderByType2 = orderByType2;
389    
390                    _toString = null;
391            }
392    
393            public void setPublishDate(Date publishDate) {
394                    _publishDate = publishDate;
395    
396                    _toString = null;
397            }
398    
399            public void setStart(int start) {
400                    _start = start;
401    
402                    _toString = null;
403            }
404    
405            public void setVisible(Boolean visible) {
406                    _visible = visible;
407    
408                    _toString = null;
409            }
410    
411            @Override
412            public String toString() {
413                    if (_toString != null) {
414                            return _toString;
415                    }
416    
417                    StringBundler sb = new StringBundler(47);
418    
419                    sb.append("{allCategoryIds=");
420                    sb.append(StringUtil.merge(_allCategoryIds));
421                    sb.append(", allTagIds");
422                    sb.append(StringUtil.merge(_allTagIds));
423                    sb.append(", anyCategoryIds");
424                    sb.append(StringUtil.merge(_anyCategoryIds));
425                    sb.append(", anyTagIds");
426                    sb.append(StringUtil.merge(_anyTagIds));
427                    sb.append(", classNameIds");
428                    sb.append(StringUtil.merge(_classNameIds));
429                    sb.append(", classTypeIds");
430                    sb.append(StringUtil.merge(_classTypeIds));
431    
432                    if (_layout != null) {
433                            sb.append(", layout");
434                            sb.append(_layout.getPlid());
435                    }
436    
437                    sb.append(", end");
438                    sb.append(_end);
439                    sb.append(", excludeZeroViewCount");
440                    sb.append(_excludeZeroViewCount);
441                    sb.append(", expirationDate");
442                    sb.append(_expirationDate);
443                    sb.append(", groupIds");
444                    sb.append(StringUtil.merge(_groupIds));
445                    sb.append(", linkedAssetEntryId");
446                    sb.append(_linkedAssetEntryId);
447                    sb.append(", notAllCategoryIds");
448                    sb.append(StringUtil.merge(_notAllCategoryIds));
449                    sb.append(", notAllTagIds");
450                    sb.append(StringUtil.merge(_notAllTagIds));
451                    sb.append(", notAnyCategoryIds");
452                    sb.append(StringUtil.merge(_notAnyCategoryIds));
453                    sb.append(", notAnyTagIds");
454                    sb.append(StringUtil.merge(_notAnyTagIds));
455                    sb.append(", orderByCol1");
456                    sb.append(_orderByCol1);
457                    sb.append(", orderByCol2");
458                    sb.append(_orderByCol2);
459                    sb.append(", orderByType1");
460                    sb.append(_orderByType1);
461                    sb.append(", orderByType2");
462                    sb.append(_orderByType2);
463                    sb.append(", publishDate");
464                    sb.append(_publishDate);
465                    sb.append(", start");
466                    sb.append(_start);
467                    sb.append(", visible");
468                    sb.append(_visible);
469                    sb.append("}");
470    
471                    _toString = sb.toString();
472    
473                    return _toString;
474            }
475    
476            private long[] _getLeftAndRightCategoryIds(long[] categoryIds) {
477                    long[] leftRightIds = new long[categoryIds.length * 3];
478    
479                    for (int i = 0; i < categoryIds.length; i++) {
480                            long categoryId = categoryIds[i];
481    
482                            try {
483                                    AssetCategory category =
484                                            AssetCategoryLocalServiceUtil.getCategory(categoryId);
485    
486                                    leftRightIds[3 * i] = category.getGroupId();
487                                    leftRightIds[3 * i + 1] = category.getLeftCategoryId();
488                                    leftRightIds[3 * i + 2] = category.getRightCategoryId();
489                            }
490                            catch (Exception e) {
491                                    _log.warn("Error retrieving category " + categoryId);
492                            }
493                    }
494    
495                    return leftRightIds;
496            }
497    
498            private static Log _log = LogFactoryUtil.getLog(AssetEntryQuery.class);
499    
500            private long[] _allCategoryIds = new long[0];
501            private long[] _allTagIds = new long[0];
502            private long[] _anyCategoryIds = new long[0];
503            private long[] _anyTagIds = new long[0];
504            private long[] _classNameIds = new long[0];
505            private long[] _classTypeIds = new long[0];
506            private boolean _enablePermissions;
507            private Layout _layout;
508            private int _end = QueryUtil.ALL_POS;
509            private boolean _excludeZeroViewCount;
510            private Date _expirationDate;
511            private long[] _groupIds = new long[0];
512            private long _linkedAssetEntryId = 0;
513            private long[] _notAllCategoryIds = new long[0];
514            private long[] _notAllTagIds = new long[0];
515            private long[] _notAnyCategoryIds = new long[0];
516            private long[] _notAnyTagIds = new long[0];
517            private String _orderByCol1;
518            private String _orderByCol2;
519            private String _orderByType1;
520            private String _orderByType2;
521            private Date _publishDate;
522            private int _start = QueryUtil.ALL_POS;
523            private String _toString;
524            private Boolean _visible = Boolean.TRUE;
525    
526    }