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