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                            new long[] {groupId}, classNameIds, null, null, null, null, null,
034                            null, null, null, null);
035            }
036    
037            public static AssetEntryQuery createAssetEntryQuery(
038                            long groupId, String className, long[] notAllCategoryIds,
039                            long[] notAnyCategoryIds, long[] allCategoryIds,
040                            long[] anyCategoryIds)
041                    throws Exception {
042    
043                    return createAssetEntryQuery(
044                            new long[] {groupId}, className, notAllCategoryIds,
045                            notAnyCategoryIds, allCategoryIds, anyCategoryIds);
046            }
047    
048            public static AssetEntryQuery createAssetEntryQuery(
049                            long groupId, String className, String[] notAllTagNames,
050                            String[] notAnyTagNames, String[] allTagNames, String[] anyTagNames)
051                    throws Exception {
052    
053                    return createAssetEntryQuery(
054                            new long[] {groupId}, className, notAllTagNames, notAnyTagNames,
055                            allTagNames, anyTagNames);
056            }
057    
058            public static AssetEntryQuery createAssetEntryQuery(
059                            long groupId, String[] classNames)
060                    throws Exception {
061    
062                    return createAssetEntryQuery(new long[] {groupId}, classNames);
063            }
064    
065            public static AssetEntryQuery createAssetEntryQuery(
066                            long groupId, String[] classNames, long[] classTypeIds)
067                    throws Exception {
068    
069                    return createAssetEntryQuery(
070                            new long[] {groupId}, classNames, classTypeIds);
071            }
072    
073            public static AssetEntryQuery createAssetEntryQuery(
074                            long groupId, String[] classNames, long[] classTypeIds,
075                            long[] notAllCategoryIds, long[] notAnyCategoryIds,
076                            long[] allCategoryIds, long[] anyCategoryIds,
077                            String[] notAllTagNames, String[] notAnyTagNames,
078                            String[] allTagNames, String[] anyTagNames)
079                    throws Exception {
080    
081                    return createAssetEntryQuery(
082                            new long[] {groupId}, classNames, classTypeIds, notAllCategoryIds,
083                            notAnyCategoryIds, allCategoryIds, anyCategoryIds, notAllTagNames,
084                            notAnyTagNames, allTagNames, anyTagNames);
085            }
086    
087            public static AssetEntryQuery createAssetEntryQuery(
088                            long[] groupIds, long[] classNameIds, long[] classTypeIds,
089                            long[] notAllCategoryIds, long[] notAnyCategoryIds,
090                            long[] allCategoryIds, long[] anyCategoryIds,
091                            String[] notAllTagNames, String[] notAnyTagNames,
092                            String[] allTagNames, String[] anyTagNames)
093                    throws Exception {
094    
095                    AssetEntryQuery assetEntryQuery = new AssetEntryQuery();
096    
097                    // Class name IDs
098    
099                    assetEntryQuery.setClassNameIds(classNameIds);
100    
101                    // Class type IDs
102    
103                    if (Validator.isNotNull(classTypeIds)) {
104                            assetEntryQuery.setClassTypeIds(classTypeIds);
105                    }
106    
107                    // Categories
108    
109                    if (Validator.isNotNull(notAllCategoryIds)) {
110                            assetEntryQuery.setNotAllCategoryIds(notAllCategoryIds);
111                    }
112    
113                    if (Validator.isNotNull(notAnyCategoryIds)) {
114                            assetEntryQuery.setNotAnyCategoryIds(notAnyCategoryIds);
115                    }
116    
117                    if (Validator.isNotNull(anyCategoryIds)) {
118                            assetEntryQuery.setAnyCategoryIds(anyCategoryIds);
119                    }
120    
121                    if (Validator.isNotNull(allCategoryIds)) {
122                            assetEntryQuery.setAllCategoryIds(allCategoryIds);
123                    }
124    
125                    // Tags
126    
127                    if (ArrayUtil.isNotEmpty(notAllTagNames)) {
128                            for (String assetTagName : notAllTagNames) {
129                                    long[] notAllAssetTagIds = AssetTagLocalServiceUtil.getTagIds(
130                                            groupIds, assetTagName);
131    
132                                    assetEntryQuery.addNotAllTagIdsArray(notAllAssetTagIds);
133                            }
134                    }
135    
136                    if (ArrayUtil.isNotEmpty(notAnyTagNames)) {
137                            assetEntryQuery.setNotAnyTagIds(
138                                    getAssetTagsIds(groupIds, notAnyTagNames));
139                    }
140    
141                    if (ArrayUtil.isNotEmpty(anyTagNames)) {
142                            assetEntryQuery.setAnyTagIds(
143                                    getAssetTagsIds(groupIds, anyTagNames));
144                    }
145    
146                    if (ArrayUtil.isNotEmpty(allTagNames)) {
147                            for (String assetTagName : allTagNames) {
148                                    long[] allAssetTagIds = AssetTagLocalServiceUtil.getTagIds(
149                                            groupIds, assetTagName);
150    
151                                    assetEntryQuery.addAllTagIdsArray(allAssetTagIds);
152                            }
153                    }
154    
155                    // Group IDs
156    
157                    assetEntryQuery.setGroupIds(groupIds);
158    
159                    return assetEntryQuery;
160            }
161    
162            public static AssetEntryQuery createAssetEntryQuery(
163                            long[] groupIds, String className, long[] notAllCategoryIds,
164                            long[] notAnyCategoryIds, long[] allCategoryIds,
165                            long[] anyCategoryIds)
166                    throws Exception {
167    
168                    return createAssetEntryQuery(
169                            groupIds, new String[] {className}, null, notAllCategoryIds,
170                            notAnyCategoryIds, allCategoryIds, anyCategoryIds, null, null, null,
171                            null);
172            }
173    
174            public static AssetEntryQuery createAssetEntryQuery(
175                            long[] groupIds, String className, String[] notAllTagNames,
176                            String[] notAnyTagNames, String[] allTagNames, String[] anyTagNames)
177                    throws Exception {
178    
179                    return createAssetEntryQuery(
180                            groupIds, new String[] {className}, null, null, null, null, null,
181                            notAllTagNames, notAnyTagNames, allTagNames, anyTagNames);
182            }
183    
184            public static AssetEntryQuery createAssetEntryQuery(
185                            long[] groupIds, String[] classNames)
186                    throws Exception {
187    
188                    return createAssetEntryQuery(
189                            groupIds, classNames, null, null, null, null, null, null, null,
190                            null, null);
191            }
192    
193            public static AssetEntryQuery createAssetEntryQuery(
194                            long[] groupIds, String[] classNames, long[] classTypeIds)
195                    throws Exception {
196    
197                    return createAssetEntryQuery(
198                            groupIds, classNames, classTypeIds, null, null, null, null, null,
199                            null, null, null);
200            }
201    
202            public static AssetEntryQuery createAssetEntryQuery(
203                            long[] groupIds, String[] classNames, long[] classTypeIds,
204                            long[] notAllCategoryIds, long[] notAnyCategoryIds,
205                            long[] allCategoryIds, long[] anyCategoryIds,
206                            String[] notAllTagNames, String[] notAnyTagNames,
207                            String[] allTagNames, String[] anyTagNames)
208                    throws Exception {
209    
210                    long[] classNameIds = new long[classNames.length];
211    
212                    for (int i = 0; i < classNames.length; i++) {
213                            classNameIds[i] = PortalUtil.getClassNameId(classNames[i]);
214                    }
215    
216                    return createAssetEntryQuery(
217                            groupIds, classNameIds, classTypeIds, notAllCategoryIds,
218                            notAnyCategoryIds, allCategoryIds, anyCategoryIds, notAllTagNames,
219                            notAnyTagNames, allTagNames, anyTagNames);
220            }
221    
222            protected static long[] getAssetTagsIds(
223                            long[] groupIds, String[] assetTagNames)
224                    throws Exception {
225    
226                    if (ArrayUtil.isEmpty(assetTagNames)) {
227                            return new long[0];
228                    }
229    
230                    return AssetTagLocalServiceUtil.getTagIds(groupIds, assetTagNames);
231            }
232    
233    }