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