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