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.exception.PortalException;
020    import com.liferay.portal.kernel.log.Log;
021    import com.liferay.portal.kernel.log.LogFactoryUtil;
022    import com.liferay.portal.kernel.util.ArrayUtil;
023    import com.liferay.portal.kernel.util.ParamUtil;
024    import com.liferay.portal.kernel.util.StringBundler;
025    import com.liferay.portal.kernel.util.StringPool;
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    import com.liferay.portlet.dynamicdatamapping.util.DDMIndexer;
036    
037    import java.io.Serializable;
038    
039    import java.util.ArrayList;
040    import java.util.Date;
041    import java.util.HashMap;
042    import java.util.List;
043    import java.util.Map;
044    
045    import javax.portlet.PortletRequest;
046    
047    /**
048     * @author Brian Wing Shun Chan
049     * @author Jorge Ferrer
050     * @author Juan Fern??ndez
051     */
052    public class AssetEntryQuery {
053    
054            public static final String[] ORDER_BY_COLUMNS = new String[] {
055                    "title", "createDate", "modifiedDate", "publishDate", "expirationDate",
056                    "priority", "viewCount", "ratings"
057            };
058    
059            public static String checkOrderByCol(String orderByCol) {
060                    if (ArrayUtil.contains(ORDER_BY_COLUMNS, orderByCol) ||
061                            ((orderByCol != null) &&
062                             orderByCol.startsWith(
063                                    DDMIndexer.DDM_FIELD_NAMESPACE + StringPool.FORWARD_SLASH))) {
064    
065                            return orderByCol;
066                    }
067    
068                    return ORDER_BY_COLUMNS[2];
069            }
070    
071            public static String checkOrderByType(String orderByType) {
072                    if ((orderByType == null) ||
073                            StringUtil.equalsIgnoreCase(orderByType, "DESC")) {
074    
075                            return "DESC";
076                    }
077                    else {
078                            return "ASC";
079                    }
080            }
081    
082            public AssetEntryQuery() {
083                    Date now = new Date();
084    
085                    _expirationDate = now;
086                    _publishDate = now;
087            }
088    
089            public AssetEntryQuery(AssetEntryQuery assetEntryQuery) {
090                    setAllCategoryIds(assetEntryQuery.getAllCategoryIds());
091                    setAllTagIdsArray(assetEntryQuery.getAllTagIdsArray());
092                    setAnyCategoryIds(assetEntryQuery.getAnyCategoryIds());
093                    setAnyTagIds(assetEntryQuery.getAnyTagIds());
094                    setAttributes(assetEntryQuery.getAttributes());
095                    setClassNameIds(assetEntryQuery.getClassNameIds());
096                    setClassTypeIds(assetEntryQuery.getClassTypeIds());
097                    setDescription(assetEntryQuery.getDescription());
098                    setEnablePermissions(assetEntryQuery.isEnablePermissions());
099                    setEnd(assetEntryQuery.getEnd());
100                    setExcludeZeroViewCount(assetEntryQuery.isExcludeZeroViewCount());
101                    setExpirationDate(assetEntryQuery.getExpirationDate());
102                    setGroupIds(assetEntryQuery.getGroupIds());
103                    setKeywords(assetEntryQuery.getKeywords());
104                    setLayout(assetEntryQuery.getLayout());
105                    setLinkedAssetEntryId(assetEntryQuery.getLinkedAssetEntryId());
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                    setVisible(assetEntryQuery.isVisible());
119            }
120    
121            public AssetEntryQuery(
122                            long[] classNameIds, SearchContainer<?> searchContainer)
123                    throws PortalException {
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(String className, SearchContainer<?> searchContainer)
160                    throws PortalException {
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 boolean isEnablePermissions() {
320                    return _enablePermissions;
321            }
322    
323            public boolean isExcludeZeroViewCount() {
324                    return _excludeZeroViewCount;
325            }
326    
327            public Boolean isVisible() {
328                    return _visible;
329            }
330    
331            public void setAllCategoryIds(long[] allCategoryIds) {
332                    _allCategoryIds = allCategoryIds;
333    
334                    _toString = null;
335            }
336    
337            public void setAllTagIds(long[] allTagIds) {
338                    _allTagIds = allTagIds;
339    
340                    _allTagIdsArray = _expandTagIds(allTagIds);
341    
342                    _toString = null;
343            }
344    
345            public void setAllTagIdsArray(long[][] allTagIdsArray) {
346                    _allTagIdsArray = allTagIdsArray;
347    
348                    _allTagIds = _flattenTagIds(allTagIdsArray);
349    
350                    _toString = null;
351            }
352    
353            public void setAnyCategoryIds(long[] anyCategoryIds) {
354                    _anyCategoryIds = anyCategoryIds;
355    
356                    _toString = null;
357            }
358    
359            public void setAnyTagIds(long[] anyTagIds) {
360                    _anyTagIds = anyTagIds;
361    
362                    _toString = null;
363            }
364    
365            public void setAttribute(String name, Serializable value) {
366                    _attributes.put(name, value);
367            }
368    
369            public void setAttributes(Map<String, Serializable> attributes) {
370                    if (_attributes == null) {
371                            _attributes = new HashMap<String, Serializable>();
372                    }
373                    else {
374                            _attributes = attributes;
375                    }
376            }
377    
378            public void setClassName(String className) {
379                    long classNameId = PortalUtil.getClassNameId(className);
380    
381                    _classNameIds = new long[] {classNameId};
382    
383                    _toString = null;
384            }
385    
386            public void setClassNameIds(long[] classNameIds) {
387                    _classNameIds = classNameIds;
388    
389                    _toString = null;
390            }
391    
392            public void setClassTypeIds(long[] classTypeIds) {
393                    _classTypeIds = classTypeIds;
394    
395                    _toString = null;
396            }
397    
398            public void setDescription(String description) {
399                    _description = description;
400            }
401    
402            public void setEnablePermissions(boolean enablePermissions) {
403                    _enablePermissions = enablePermissions;
404            }
405    
406            public void setEnd(int end) {
407                    _end = end;
408    
409                    _toString = null;
410            }
411    
412            public void setExcludeZeroViewCount(boolean excludeZeroViewCount) {
413                    _excludeZeroViewCount = excludeZeroViewCount;
414    
415                    _toString = null;
416            }
417    
418            public void setExpirationDate(Date expirationDate) {
419                    _expirationDate = expirationDate;
420    
421                    _toString = null;
422            }
423    
424            public void setGroupIds(long[] groupIds) {
425                    _groupIds = groupIds;
426    
427                    _toString = null;
428            }
429    
430            public void setKeywords(String keywords) {
431                    _keywords = keywords;
432            }
433    
434            public void setLayout(Layout layout) {
435                    _layout = layout;
436    
437                    _toString = null;
438            }
439    
440            public void setLinkedAssetEntryId(long linkedAssetEntryId) {
441                    _linkedAssetEntryId = linkedAssetEntryId;
442    
443                    _toString = null;
444            }
445    
446            public void setNotAllCategoryIds(long[] notAllCategoryIds) {
447                    _notAllCategoryIds = notAllCategoryIds;
448    
449                    _toString = null;
450            }
451    
452            public void setNotAllTagIds(long[] notAllTagIds) {
453                    _notAllTagIds = notAllTagIds;
454    
455                    _notAllTagIdsArray = _expandTagIds(notAllTagIds);
456    
457                    _toString = null;
458            }
459    
460            public void setNotAllTagIdsArray(long[][] notAllTagIdsArray) {
461                    _notAllTagIdsArray = notAllTagIdsArray;
462    
463                    _notAllTagIds = _flattenTagIds(notAllTagIdsArray);
464    
465                    _toString = null;
466            }
467    
468            public void setNotAnyCategoryIds(long[] notAnyCategoryIds) {
469                    _notAnyCategoryIds = notAnyCategoryIds;
470    
471                    _toString = null;
472            }
473    
474            public void setNotAnyTagIds(long[] notAnyTagIds) {
475                    _notAnyTagIds = notAnyTagIds;
476    
477                    _toString = null;
478            }
479    
480            public void setOrderByCol1(String orderByCol1) {
481                    _orderByCol1 = orderByCol1;
482    
483                    _toString = null;
484            }
485    
486            public void setOrderByCol2(String orderByCol2) {
487                    _orderByCol2 = orderByCol2;
488    
489                    _toString = null;
490            }
491    
492            public void setOrderByType1(String orderByType1) {
493                    _orderByType1 = orderByType1;
494    
495                    _toString = null;
496            }
497    
498            public void setOrderByType2(String orderByType2) {
499                    _orderByType2 = orderByType2;
500    
501                    _toString = null;
502            }
503    
504            public void setPaginationType(String paginationType) {
505                    _paginationType = paginationType;
506    
507                    _toString = null;
508            }
509    
510            public void setPublishDate(Date publishDate) {
511                    _publishDate = publishDate;
512    
513                    _toString = null;
514            }
515    
516            public void setStart(int start) {
517                    _start = start;
518    
519                    _toString = null;
520            }
521    
522            public void setTitle(String title) {
523                    _title = title;
524            }
525    
526            public void setVisible(Boolean visible) {
527                    _visible = visible;
528    
529                    _toString = null;
530            }
531    
532            @Override
533            public String toString() {
534                    if (_toString != null) {
535                            return _toString;
536                    }
537    
538                    StringBundler sb = new StringBundler(55);
539    
540                    sb.append("{allCategoryIds=");
541                    sb.append(StringUtil.merge(_allCategoryIds));
542                    sb.append(", allTagIds=");
543                    sb.append(StringUtil.merge(_allTagIds));
544                    sb.append(", anyCategoryIds=");
545                    sb.append(StringUtil.merge(_anyCategoryIds));
546                    sb.append(", anyTagIds=");
547                    sb.append(StringUtil.merge(_anyTagIds));
548                    sb.append(", classNameIds=");
549                    sb.append(StringUtil.merge(_classNameIds));
550                    sb.append(", classTypeIds=");
551                    sb.append(StringUtil.merge(_classTypeIds));
552                    sb.append(", description=");
553                    sb.append(_description);
554    
555                    if (_layout != null) {
556                            sb.append(", layout=");
557                            sb.append(_layout.getPlid());
558                    }
559    
560                    sb.append(", end=");
561                    sb.append(_end);
562                    sb.append(", excludeZeroViewCount=");
563                    sb.append(_excludeZeroViewCount);
564                    sb.append(", expirationDate=");
565                    sb.append(_expirationDate);
566                    sb.append(", groupIds=");
567                    sb.append(StringUtil.merge(_groupIds));
568                    sb.append(", keywords=");
569                    sb.append(_keywords);
570                    sb.append(", linkedAssetEntryId=");
571                    sb.append(_linkedAssetEntryId);
572                    sb.append(", notAllCategoryIds=");
573                    sb.append(StringUtil.merge(_notAllCategoryIds));
574                    sb.append(", notAllTagIds=");
575                    sb.append(StringUtil.merge(_notAllTagIds));
576                    sb.append(", notAnyCategoryIds=");
577                    sb.append(StringUtil.merge(_notAnyCategoryIds));
578                    sb.append(", notAnyTagIds=");
579                    sb.append(StringUtil.merge(_notAnyTagIds));
580                    sb.append(", orderByCol1=");
581                    sb.append(_orderByCol1);
582                    sb.append(", orderByCol2=");
583                    sb.append(_orderByCol2);
584                    sb.append(", orderByType1=");
585                    sb.append(_orderByType1);
586                    sb.append(", orderByType2=");
587                    sb.append(_orderByType2);
588                    sb.append(", paginationType=");
589                    sb.append(_paginationType);
590                    sb.append(", publishDate=");
591                    sb.append(_publishDate);
592                    sb.append(", start=");
593                    sb.append(_start);
594                    sb.append(", title=");
595                    sb.append(_title);
596                    sb.append(", visible=");
597                    sb.append(_visible);
598                    sb.append("}");
599    
600                    _toString = sb.toString();
601    
602                    return _toString;
603            }
604    
605            private long[][] _expandTagIds(long[] tagIds) {
606                    long[][] tagIdsArray = new long[tagIds.length][1];
607    
608                    for (int i = 0; i < tagIds.length; i++) {
609                            tagIdsArray[i][0] = tagIds[i];
610                    }
611    
612                    return tagIdsArray;
613            }
614    
615            private long[] _flattenTagIds(long[][] tagIdsArray) {
616                    List<Long> tagIdsList = new ArrayList<Long>();
617    
618                    for (int i = 0; i < tagIdsArray.length; i++) {
619                            long[] tagIds = tagIdsArray[i];
620    
621                            for (int j = 0; j < tagIds.length; j++) {
622                                    long tagId = tagIds[j];
623    
624                                    tagIdsList.add(tagId);
625                            }
626                    }
627    
628                    return ArrayUtil.toArray(
629                            tagIdsList.toArray(new Long[tagIdsList.size()]));
630            }
631    
632            private long[] _getLeftAndRightCategoryIds(long[] categoryIds) {
633                    long[] leftRightIds = new long[categoryIds.length * 3];
634    
635                    for (int i = 0; i < categoryIds.length; i++) {
636                            long categoryId = categoryIds[i];
637    
638                            try {
639                                    AssetCategory category =
640                                            AssetCategoryLocalServiceUtil.getCategory(categoryId);
641    
642                                    leftRightIds[3 * i] = category.getGroupId();
643                                    leftRightIds[3 * i + 1] = category.getLeftCategoryId();
644                                    leftRightIds[3 * i + 2] = category.getRightCategoryId();
645                            }
646                            catch (Exception e) {
647                                    if (_log.isWarnEnabled()) {
648                                            _log.warn("Error retrieving category " + categoryId);
649                                    }
650                            }
651                    }
652    
653                    return leftRightIds;
654            }
655    
656            private static Log _log = LogFactoryUtil.getLog(AssetEntryQuery.class);
657    
658            private long[] _allCategoryIds = new long[0];
659            private long[] _allTagIds = new long[0];
660            private long[][] _allTagIdsArray = new long[0][];
661            private long[] _anyCategoryIds = new long[0];
662            private long[] _anyTagIds = new long[0];
663            private Map<String, Serializable> _attributes =
664                    new HashMap<String, Serializable>();
665            private long[] _classNameIds = new long[0];
666            private long[] _classTypeIds = new long[0];
667            private String _description;
668            private boolean _enablePermissions;
669            private int _end = QueryUtil.ALL_POS;
670            private boolean _excludeZeroViewCount;
671            private Date _expirationDate;
672            private long[] _groupIds = new long[0];
673            private String _keywords;
674            private Layout _layout;
675            private long _linkedAssetEntryId = 0;
676            private long[] _notAllCategoryIds = new long[0];
677            private long[] _notAllTagIds = new long[0];
678            private long[][] _notAllTagIdsArray = new long[0][];
679            private long[] _notAnyCategoryIds = new long[0];
680            private long[] _notAnyTagIds = new long[0];
681            private String _orderByCol1;
682            private String _orderByCol2;
683            private String _orderByType1;
684            private String _orderByType2;
685            private String _paginationType;
686            private Date _publishDate;
687            private int _start = QueryUtil.ALL_POS;
688            private String _title;
689            private String _toString;
690            private Boolean _visible = Boolean.TRUE;
691    
692    }