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