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