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