001    /**
002     * Copyright (c) 2000-present 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.test;
016    
017    import com.liferay.portal.kernel.util.ArrayUtil;
018    import com.liferay.portal.kernel.util.Validator;
019    import com.liferay.portal.util.PortalUtil;
020    import com.liferay.portlet.asset.service.AssetTagLocalServiceUtil;
021    import com.liferay.portlet.asset.service.persistence.AssetEntryQuery;
022    
023    /**
024     * @author Eudaldo Alonso
025     */
026    public class AssetEntryQueryTestUtil {
027    
028            public static AssetEntryQuery createAssetEntryQuery(
029                            long groupId, long[] classNameIds)
030                    throws Exception {
031    
032                    return createAssetEntryQuery(
033                            groupId, classNameIds, null, null, null, null, null, null, null,
034                            null, null);
035            }
036    
037            public static AssetEntryQuery createAssetEntryQuery(
038                            long groupId, long[] classNameIds, long[] classTypeIds,
039                            long[] notAllCategories, long[] notAnyCategories,
040                            long[] allCategories, long[] anyCategories, String[] notAllTags,
041                            String[] notAnyTags, String[] allTags, String[] anyTags)
042                    throws Exception {
043    
044                    AssetEntryQuery assetEntryQuery = new AssetEntryQuery();
045    
046                    // Class name IDs
047    
048                    assetEntryQuery.setClassNameIds(classNameIds);
049    
050                    // Class type IDs
051    
052                    if (Validator.isNotNull(classTypeIds)) {
053                            assetEntryQuery.setClassTypeIds(classTypeIds);
054                    }
055    
056                    // Categories
057    
058                    if (Validator.isNotNull(notAllCategories)) {
059                            assetEntryQuery.setNotAllCategoryIds(notAllCategories);
060                    }
061    
062                    if (Validator.isNotNull(notAnyCategories)) {
063                            assetEntryQuery.setNotAnyCategoryIds(notAnyCategories);
064                    }
065    
066                    if (Validator.isNotNull(anyCategories)) {
067                            assetEntryQuery.setAnyCategoryIds(anyCategories);
068                    }
069    
070                    if (Validator.isNotNull(allCategories)) {
071                            assetEntryQuery.setAllCategoryIds(allCategories);
072                    }
073    
074                    // Tags
075    
076                    if (ArrayUtil.isNotEmpty(notAllTags)) {
077                            assetEntryQuery.setNotAllTagIds(
078                                    getAssetTagsIds(groupId, notAllTags));
079                    }
080    
081                    if (ArrayUtil.isNotEmpty(notAnyTags)) {
082                            assetEntryQuery.setNotAnyTagIds(
083                                    getAssetTagsIds(groupId, notAnyTags));
084                    }
085    
086                    if (ArrayUtil.isNotEmpty(anyTags)) {
087                            assetEntryQuery.setAnyTagIds(getAssetTagsIds(groupId, anyTags));
088                    }
089    
090                    if (ArrayUtil.isNotEmpty(allTags)) {
091                            assetEntryQuery.setAllTagIds(getAssetTagsIds(groupId, allTags));
092                    }
093    
094                    // Group IDs
095    
096                    assetEntryQuery.setGroupIds(new long[] {groupId});
097    
098                    return assetEntryQuery;
099            }
100    
101            public static AssetEntryQuery createAssetEntryQuery(
102                            long groupId, String className, long[] notAllCategories,
103                            long[] notAnyCategories, long[] allCategories, long[] anyCategories)
104                    throws Exception {
105    
106                    return createAssetEntryQuery(
107                            groupId, new String[] {className}, null, notAllCategories,
108                            notAnyCategories, allCategories, anyCategories, null, null, null,
109                            null);
110            }
111    
112            public static AssetEntryQuery createAssetEntryQuery(
113                            long groupId, String className, String[] notAllTags,
114                            String[] notAnyTags, String[] allTags, String[] anyTags)
115                    throws Exception {
116    
117                    return createAssetEntryQuery(
118                            groupId, new String[] {className}, null, null, null, null, null,
119                            notAllTags, notAnyTags, allTags, anyTags);
120            }
121    
122            public static AssetEntryQuery createAssetEntryQuery(
123                            long groupId, String[] classNames)
124                    throws Exception {
125    
126                    return createAssetEntryQuery(
127                            groupId, classNames, null, null, null, null, null, null, null, null,
128                            null);
129            }
130    
131            public static AssetEntryQuery createAssetEntryQuery(
132                            long groupId, String[] classNames, long[] classTypeIds)
133                    throws Exception {
134    
135                    return createAssetEntryQuery(
136                            groupId, classNames, classTypeIds, null, null, null, null, null,
137                            null, null, null);
138            }
139    
140            public static AssetEntryQuery createAssetEntryQuery(
141                            long groupId, String[] classNames, long[] classTypeIds,
142                            long[] notAllCategories, long[] notAnyCategories,
143                            long[] allCategories, long[] anyCategories, String[] notAllTags,
144                            String[] notAnyTags, String[] allTags, String[] anyTags)
145                    throws Exception {
146    
147                    long[] classNameIds = new long[classNames.length];
148    
149                    for (int i = 0; i < classNames.length; i++) {
150                            classNameIds[i] = PortalUtil.getClassNameId(classNames[i]);
151                    }
152    
153                    return createAssetEntryQuery(
154                            groupId, classNameIds, classTypeIds, notAllCategories,
155                            notAnyCategories, allCategories, anyCategories, notAllTags,
156                            notAnyTags, allTags, anyTags);
157            }
158    
159            protected static long[] getAssetTagsIds(
160                            long groupId, String[] assetTagNames)
161                    throws Exception {
162    
163                    if (ArrayUtil.isEmpty(assetTagNames)) {
164                            return new long[0];
165                    }
166    
167                    return AssetTagLocalServiceUtil.getTagIds(groupId, assetTagNames);
168            }
169    
170    }