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