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