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.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 isExcludeZeroViewCount() {
250                    return _excludeZeroViewCount;
251            }
252    
253            public Boolean isVisible() {
254                    return _visible;
255            }
256    
257            public void setAllCategoryIds(long[] allCategoryIds) {
258                    _allCategoryIds = allCategoryIds;
259    
260                    _toString = null;
261            }
262    
263            public void setAllTagIds(long[] allTagIds) {
264                    _allTagIds = allTagIds;
265    
266                    _toString = null;
267            }
268    
269            public void setAnyCategoryIds(long[] anyCategoryIds) {
270                    _anyCategoryIds = anyCategoryIds;
271    
272                    _toString = null;
273            }
274    
275            public void setAnyTagIds(long[] anyTagIds) {
276                    _anyTagIds = anyTagIds;
277    
278                    _toString = null;
279            }
280    
281            public void setClassName(String className) {
282                    long classNameId = PortalUtil.getClassNameId(className);
283    
284                    _classNameIds = new long[] {classNameId};
285    
286                    _toString = null;
287            }
288    
289            public void setClassNameIds(long[] classNameIds) {
290                    _classNameIds = classNameIds;
291    
292                    _toString = null;
293            }
294    
295            public void setClassTypeIds(long[] classTypeIds) {
296                    _classTypeIds = classTypeIds;
297    
298                    _toString = null;
299            }
300    
301            public void setLayout(Layout layout) {
302                    _layout = layout;
303    
304                    _toString = null;
305            }
306    
307            public void setLinkedAssetEntryId(long linkedAssetEntryId) {
308                    _linkedAssetEntryId = linkedAssetEntryId;
309    
310                    _toString = null;
311            }
312    
313            public void setEnd(int end) {
314                    _end = end;
315    
316                    _toString = null;
317            }
318    
319            public void setExcludeZeroViewCount(boolean excludeZeroViewCount) {
320                    _excludeZeroViewCount = excludeZeroViewCount;
321    
322                    _toString = null;
323            }
324    
325            public void setExpirationDate(Date expirationDate) {
326                    _expirationDate = expirationDate;
327    
328                    _toString = null;
329            }
330    
331            public void setGroupIds(long[] groupIds) {
332                    _groupIds = groupIds;
333    
334                    _toString = null;
335            }
336    
337            public void setNotAllCategoryIds(long[] notAllCategoryIds) {
338                    _notAllCategoryIds = notAllCategoryIds;
339    
340                    _toString = null;
341            }
342    
343            public void setNotAllTagIds(long[] notAllTagIds) {
344                    _notAllTagIds = notAllTagIds;
345    
346                    _toString = null;
347            }
348    
349            public void setNotAnyCategoryIds(long[] notAnyCategoryIds) {
350                    _notAnyCategoryIds = notAnyCategoryIds;
351    
352                    _toString = null;
353            }
354    
355            public void setNotAnyTagIds(long[] notAnyTagIds) {
356                    _notAnyTagIds = notAnyTagIds;
357    
358                    _toString = null;
359            }
360    
361            public void setOrderByCol1(String orderByCol1) {
362                    _orderByCol1 = orderByCol1;
363    
364                    _toString = null;
365            }
366    
367            public void setOrderByCol2(String orderByCol2) {
368                    _orderByCol2 = orderByCol2;
369    
370                    _toString = null;
371            }
372    
373            public void setOrderByType1(String orderByType1) {
374                    _orderByType1 = orderByType1;
375    
376                    _toString = null;
377            }
378    
379            public void setOrderByType2(String orderByType2) {
380                    _orderByType2 = orderByType2;
381    
382                    _toString = null;
383            }
384    
385            public void setPublishDate(Date publishDate) {
386                    _publishDate = publishDate;
387    
388                    _toString = null;
389            }
390    
391            public void setStart(int start) {
392                    _start = start;
393    
394                    _toString = null;
395            }
396    
397            public void setVisible(Boolean visible) {
398                    _visible = visible;
399    
400                    _toString = null;
401            }
402    
403            @Override
404            public String toString() {
405                    if (_toString != null) {
406                            return _toString;
407                    }
408    
409                    StringBundler sb = new StringBundler(47);
410    
411                    sb.append("{allCategoryIds=");
412                    sb.append(StringUtil.merge(_allCategoryIds));
413                    sb.append(", allTagIds");
414                    sb.append(StringUtil.merge(_allTagIds));
415                    sb.append(", anyCategoryIds");
416                    sb.append(StringUtil.merge(_anyCategoryIds));
417                    sb.append(", anyTagIds");
418                    sb.append(StringUtil.merge(_anyTagIds));
419                    sb.append(", classNameIds");
420                    sb.append(StringUtil.merge(_classNameIds));
421                    sb.append(", classTypeIds");
422                    sb.append(StringUtil.merge(_classTypeIds));
423    
424                    if (_layout != null) {
425                            sb.append(", layout");
426                            sb.append(_layout.getPlid());
427                    }
428    
429                    sb.append(", end");
430                    sb.append(_end);
431                    sb.append(", excludeZeroViewCount");
432                    sb.append(_excludeZeroViewCount);
433                    sb.append(", expirationDate");
434                    sb.append(_expirationDate);
435                    sb.append(", groupIds");
436                    sb.append(StringUtil.merge(_groupIds));
437                    sb.append(", linkedAssetEntryId");
438                    sb.append(_linkedAssetEntryId);
439                    sb.append(", notAllCategoryIds");
440                    sb.append(StringUtil.merge(_notAllCategoryIds));
441                    sb.append(", notAllTagIds");
442                    sb.append(StringUtil.merge(_notAllTagIds));
443                    sb.append(", notAnyCategoryIds");
444                    sb.append(StringUtil.merge(_notAnyCategoryIds));
445                    sb.append(", notAnyTagIds");
446                    sb.append(StringUtil.merge(_notAnyTagIds));
447                    sb.append(", orderByCol1");
448                    sb.append(_orderByCol1);
449                    sb.append(", orderByCol2");
450                    sb.append(_orderByCol2);
451                    sb.append(", orderByType1");
452                    sb.append(_orderByType1);
453                    sb.append(", orderByType2");
454                    sb.append(_orderByType2);
455                    sb.append(", publishDate");
456                    sb.append(_publishDate);
457                    sb.append(", start");
458                    sb.append(_start);
459                    sb.append(", visible");
460                    sb.append(_visible);
461                    sb.append("}");
462    
463                    _toString = sb.toString();
464    
465                    return _toString;
466            }
467    
468            private long[] _getLeftAndRightCategoryIds(long[] categoryIds) {
469                    long[] leftRightIds = new long[categoryIds.length * 3];
470    
471                    for (int i = 0; i < categoryIds.length; i++) {
472                            long categoryId = categoryIds[i];
473    
474                            try {
475                                    AssetCategory category =
476                                            AssetCategoryLocalServiceUtil.getCategory(categoryId);
477    
478                                    leftRightIds[3 * i] = category.getGroupId();
479                                    leftRightIds[3 * i + 1] = category.getLeftCategoryId();
480                                    leftRightIds[3 * i + 2] = category.getRightCategoryId();
481                            }
482                            catch (Exception e) {
483                                    _log.warn("Error retrieving category " + categoryId);
484                            }
485                    }
486    
487                    return leftRightIds;
488            }
489    
490            private static Log _log = LogFactoryUtil.getLog(AssetEntryQuery.class);
491    
492            private long[] _allCategoryIds = new long[0];
493            private long[] _allTagIds = new long[0];
494            private long[] _anyCategoryIds = new long[0];
495            private long[] _anyTagIds = new long[0];
496            private long[] _classNameIds = new long[0];
497            private long[] _classTypeIds = new long[0];
498            private Layout _layout;
499            private int _end = QueryUtil.ALL_POS;
500            private boolean _excludeZeroViewCount;
501            private Date _expirationDate;
502            private long[] _groupIds = new long[0];
503            private long _linkedAssetEntryId = 0;
504            private long[] _notAllCategoryIds = new long[0];
505            private long[] _notAllTagIds = new long[0];
506            private long[] _notAnyCategoryIds = new long[0];
507            private long[] _notAnyTagIds = new long[0];
508            private String _orderByCol1;
509            private String _orderByCol2;
510            private String _orderByType1;
511            private String _orderByType2;
512            private Date _publishDate;
513            private int _start = QueryUtil.ALL_POS;
514            private String _toString;
515            private Boolean _visible = Boolean.TRUE;
516    
517    }