001    /**
002     * Copyright (c) 2000-2012 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;
016    
017    import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
018    import com.liferay.portal.kernel.dao.orm.FinderPath;
019    import com.liferay.portal.kernel.dao.orm.QueryPos;
020    import com.liferay.portal.kernel.dao.orm.QueryUtil;
021    import com.liferay.portal.kernel.dao.orm.SQLQuery;
022    import com.liferay.portal.kernel.dao.orm.Session;
023    import com.liferay.portal.kernel.dao.orm.Type;
024    import com.liferay.portal.kernel.exception.SystemException;
025    import com.liferay.portal.kernel.util.CharPool;
026    import com.liferay.portal.kernel.util.GetterUtil;
027    import com.liferay.portal.kernel.util.StringBundler;
028    import com.liferay.portal.kernel.util.StringPool;
029    import com.liferay.portal.kernel.util.StringUtil;
030    import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
031    import com.liferay.portlet.asset.NoSuchCategoryException;
032    import com.liferay.portlet.asset.model.AssetCategory;
033    import com.liferay.portlet.asset.model.AssetEntry;
034    import com.liferay.portlet.asset.model.impl.AssetCategoryImpl;
035    import com.liferay.portlet.asset.model.impl.AssetCategoryModelImpl;
036    import com.liferay.util.dao.orm.CustomSQLUtil;
037    
038    import java.util.Collections;
039    import java.util.Iterator;
040    import java.util.List;
041    
042    /**
043     * @author Brian Wing Shun Chan
044     * @author Bruno Farache
045     * @author Jorge Ferrer
046     * @author Shuyang Zhou
047     */
048    public class AssetCategoryFinderImpl
049            extends BasePersistenceImpl<AssetCategory> implements AssetCategoryFinder {
050    
051            public static final String COUNT_BY_G_C_N =
052                    AssetCategoryFinder.class.getName() + ".countByG_C_N";
053    
054            public static final String COUNT_BY_G_N_P =
055                    AssetCategoryFinder.class.getName() + ".countByG_N_P";
056    
057            public static final String FIND_BY_ENTRY_ID =
058                    AssetCategoryFinder.class.getName() + ".findByEntryId";
059    
060            public static final String FIND_BY_G_L =
061                    AssetCategoryFinder.class.getName() + ".findByG_L";
062    
063            public static final String FIND_BY_G_N =
064                    AssetCategoryFinder.class.getName() + ".findByG_N";
065    
066            public static final String FIND_BY_C_C =
067                    AssetCategoryFinder.class.getName() + ".findByC_C";
068    
069            public static final String FIND_BY_G_N_P =
070                    AssetCategoryFinder.class.getName() + ".findByG_N_P";
071    
072            public static final FinderPath FINDER_PATH_FIND_BY_G_L = new FinderPath(
073                    AssetCategoryModelImpl.ENTITY_CACHE_ENABLED,
074                    AssetCategoryModelImpl.FINDER_CACHE_ENABLED, List.class,
075                    AssetCategoryPersistenceImpl.FINDER_CLASS_NAME_LIST_WITH_PAGINATION,
076                    "findByG_L", new String[] {Long.class.getName()});
077    
078            public int countByG_C_N(long groupId, long classNameId, String name)
079                    throws SystemException {
080    
081                    Session session = null;
082    
083                    try {
084                            session = openSession();
085    
086                            String sql = CustomSQLUtil.get(COUNT_BY_G_C_N);
087    
088                            SQLQuery q = session.createSQLQuery(sql);
089    
090                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
091    
092                            QueryPos qPos = QueryPos.getInstance(q);
093    
094                            qPos.add(groupId);
095                            qPos.add(classNameId);
096                            qPos.add(name);
097                            qPos.add(name);
098    
099                            Iterator<Long> itr = q.iterate();
100    
101                            if (itr.hasNext()) {
102                                    Long count = itr.next();
103    
104                                    if (count != null) {
105                                            return count.intValue();
106                                    }
107                            }
108    
109                            return 0;
110                    }
111                    catch (Exception e) {
112                            throw new SystemException(e);
113                    }
114                    finally {
115                            closeSession(session);
116                    }
117            }
118    
119            public int countByG_N_P(
120                            long groupId, String name, String[] categoryProperties)
121                    throws SystemException {
122    
123                    Session session = null;
124    
125                    try {
126                            session = openSession();
127    
128                            String sql = CustomSQLUtil.get(COUNT_BY_G_N_P);
129    
130                            SQLQuery q = session.createSQLQuery(sql);
131    
132                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
133    
134                            QueryPos qPos = QueryPos.getInstance(q);
135    
136                            setJoin(qPos, categoryProperties);
137    
138                            qPos.add(groupId);
139                            qPos.add(name);
140                            qPos.add(name);
141    
142                            Iterator<Long> itr = q.iterate();
143    
144                            if (itr.hasNext()) {
145                                    Long count = itr.next();
146    
147                                    if (count != null) {
148                                            return count.intValue();
149                                    }
150                            }
151    
152                            return 0;
153                    }
154                    catch (Exception e) {
155                            throw new SystemException(e);
156                    }
157                    finally {
158                            closeSession(session);
159                    }
160            }
161    
162            public List<AssetCategory> findByEntryId(long entryId)
163                    throws SystemException {
164    
165                    Session session = null;
166    
167                    try {
168                            session = openSession();
169    
170                            String sql = CustomSQLUtil.get(FIND_BY_ENTRY_ID);
171    
172                            SQLQuery q = session.createSQLQuery(sql);
173    
174                            q.addEntity("AssetCategory", AssetCategoryImpl.class);
175    
176                            QueryPos qPos = QueryPos.getInstance(q);
177    
178                            qPos.add(entryId);
179    
180                            return (List<AssetCategory>)QueryUtil.list(
181                                    q, getDialect(), QueryUtil.ALL_POS, QueryUtil.ALL_POS);
182                    }
183                    catch (Exception e) {
184                            throw new SystemException(e);
185                    }
186                    finally {
187                            closeSession(session);
188                    }
189            }
190    
191            public List<Long> findByG_L(Long parentCategoryId) throws SystemException {
192                    Object[] finderArgs = new Object[] {parentCategoryId};
193    
194                    List<Long> list = (List<Long>)FinderCacheUtil.getResult(
195                            FINDER_PATH_FIND_BY_G_L, finderArgs, this);
196    
197                    if (list != null) {
198                            return list;
199                    }
200    
201                    AssetCategory parentAssetCategory = AssetCategoryUtil.fetchByPrimaryKey(
202                            parentCategoryId);
203    
204                    if (parentAssetCategory == null) {
205                            return Collections.emptyList();
206                    }
207    
208                    Session session = null;
209    
210                    try {
211                            session = openSession();
212    
213                            String sql = CustomSQLUtil.get(FIND_BY_G_L);
214    
215                            SQLQuery q = session.createSQLQuery(sql);
216    
217                            q.addScalar("categoryId", Type.LONG);
218    
219                            QueryPos qPos = QueryPos.getInstance(q);
220    
221                            qPos.add(parentAssetCategory.getGroupId());
222                            qPos.add(parentAssetCategory.getLeftCategoryId());
223                            qPos.add(parentAssetCategory.getRightCategoryId());
224    
225                            list = q.list();
226                    }
227                    catch (Exception e) {
228                            throw processException(e);
229                    }
230                    finally {
231                            if (list == null) {
232                                    FinderCacheUtil.removeResult(
233                                            FINDER_PATH_FIND_BY_G_L, finderArgs);
234                            }
235                            else {
236                                    FinderCacheUtil.putResult(
237                                            FINDER_PATH_FIND_BY_G_L, finderArgs, list);
238                            }
239    
240                            closeSession(session);
241                    }
242    
243                    return list;
244            }
245    
246            public AssetCategory findByG_N(long groupId, String name)
247                    throws NoSuchCategoryException, SystemException {
248    
249                    name = name.trim().toLowerCase();
250    
251                    Session session = null;
252    
253                    try {
254                            session = openSession();
255    
256                            String sql = CustomSQLUtil.get(FIND_BY_G_N);
257    
258                            SQLQuery q = session.createSQLQuery(sql);
259    
260                            q.addEntity("AssetCategory", AssetCategoryImpl.class);
261    
262                            QueryPos qPos = QueryPos.getInstance(q);
263    
264                            qPos.add(groupId);
265                            qPos.add(name);
266    
267                            List<AssetCategory> categories = q.list();
268    
269                            if (!categories.isEmpty()) {
270                                    return categories.get(0);
271                            }
272                    }
273                    catch (Exception e) {
274                            throw new SystemException(e);
275                    }
276                    finally {
277                            closeSession(session);
278                    }
279    
280                    StringBundler sb = new StringBundler(6);
281    
282                    sb.append("No AssetCategory exists with the key ");
283                    sb.append("{groupId=");
284                    sb.append(groupId);
285                    sb.append(", name=");
286                    sb.append(name);
287                    sb.append("}");
288    
289                    throw new NoSuchCategoryException(sb.toString());
290            }
291    
292            public List<AssetCategory> findByC_C(long classNameId, long classPK)
293                    throws SystemException {
294    
295                    AssetEntry entry = AssetEntryUtil.fetchByC_C(classNameId, classPK);
296    
297                    if (entry == null) {
298                            return Collections.emptyList();
299                    }
300    
301                    Session session = null;
302    
303                    try {
304                            session = openSession();
305    
306                            String sql = CustomSQLUtil.get(FIND_BY_C_C);
307    
308                            SQLQuery q = session.createSQLQuery(sql);
309    
310                            q.addEntity("AssetCategory", AssetCategoryImpl.class);
311    
312                            QueryPos qPos = QueryPos.getInstance(q);
313    
314                            qPos.add(entry.getEntryId());
315    
316                            return (List<AssetCategory>)QueryUtil.list(
317                                    q, getDialect(), QueryUtil.ALL_POS, QueryUtil.ALL_POS);
318                    }
319                    catch (Exception e) {
320                            throw new SystemException(e);
321                    }
322                    finally {
323                            closeSession(session);
324                    }
325            }
326    
327            public List<AssetCategory> findByG_N_P(
328                            long groupId, String name, String[] categoryProperties)
329                    throws SystemException {
330    
331                    return findByG_N_P(
332                            groupId, name, categoryProperties, QueryUtil.ALL_POS,
333                            QueryUtil.ALL_POS);
334            }
335    
336            public List<AssetCategory> findByG_N_P(
337                            long groupId, String name, String[] categoryProperties, int start,
338                            int end)
339                    throws SystemException {
340    
341                    Session session = null;
342    
343                    try {
344                            session = openSession();
345    
346                            String sql = CustomSQLUtil.get(FIND_BY_G_N_P);
347    
348                            sql = StringUtil.replace(
349                                    sql, "[$JOIN$]", getJoin(categoryProperties));
350    
351                            SQLQuery q = session.createSQLQuery(sql);
352    
353                            q.addEntity("AssetCategory", AssetCategoryImpl.class);
354    
355                            QueryPos qPos = QueryPos.getInstance(q);
356    
357                            setJoin(qPos, categoryProperties);
358    
359                            qPos.add(groupId);
360                            qPos.add(name);
361                            qPos.add(name);
362    
363                            return (List<AssetCategory>)QueryUtil.list(
364                                    q, getDialect(), start, end);
365                    }
366                    catch (Exception e) {
367                            throw new SystemException(e);
368                    }
369                    finally {
370                            closeSession(session);
371                    }
372            }
373    
374            protected String getJoin(String[] categoryProperties) {
375                    if (categoryProperties.length == 0) {
376                            return StringPool.BLANK;
377                    }
378                    else {
379                            StringBundler sb = new StringBundler(
380                                    categoryProperties.length * 3 + 2);
381    
382                            sb.append(" INNER JOIN AssetCategoryProperty ON ");
383                            sb.append(" (AssetCategoryProperty.categoryId = ");
384                            sb.append(" AssetCategory.categoryId) AND ");
385    
386                            for (int i = 0; i < categoryProperties.length; i++) {
387                                    sb.append("(AssetCategoryProperty.key_ = ? AND ");
388                                    sb.append("AssetCategoryProperty.value = ?) ");
389    
390                                    if ((i + 1) < categoryProperties.length) {
391                                            sb.append(" AND ");
392                                    }
393                            }
394    
395                            return sb.toString();
396                    }
397            }
398    
399            protected void setJoin(QueryPos qPos, String[] categoryProperties) {
400                    for (int i = 0; i < categoryProperties.length; i++) {
401                            String[] categoryProperty = StringUtil.split(
402                                    categoryProperties[i], CharPool.COLON);
403    
404                            String key = StringPool.BLANK;
405    
406                            if (categoryProperty.length > 0) {
407                                    key = GetterUtil.getString(categoryProperty[0]);
408                            }
409    
410                            String value = StringPool.BLANK;
411    
412                            if (categoryProperty.length > 1) {
413                                    value = GetterUtil.getString(categoryProperty[1]);
414                            }
415    
416                            qPos.add(key);
417                            qPos.add(value);
418                    }
419            }
420    
421    }