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