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