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