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