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