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.OrderByComparator;
028    import com.liferay.portal.kernel.util.StringBundler;
029    import com.liferay.portal.kernel.util.StringPool;
030    import com.liferay.portal.kernel.util.StringUtil;
031    import com.liferay.portal.security.permission.InlineSQLHelperUtil;
032    import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
033    import com.liferay.portlet.asset.NoSuchCategoryException;
034    import com.liferay.portlet.asset.model.AssetCategory;
035    import com.liferay.portlet.asset.model.AssetEntry;
036    import com.liferay.portlet.asset.model.AssetVocabulary;
037    import com.liferay.portlet.asset.model.impl.AssetCategoryImpl;
038    import com.liferay.portlet.asset.model.impl.AssetCategoryModelImpl;
039    import com.liferay.util.dao.orm.CustomSQLUtil;
040    
041    import java.util.Collections;
042    import java.util.Iterator;
043    import java.util.List;
044    
045    /**
046     * @author Brian Wing Shun Chan
047     * @author Bruno Farache
048     * @author Jorge Ferrer
049     * @author Shuyang Zhou
050     */
051    public class AssetCategoryFinderImpl
052            extends BasePersistenceImpl<AssetCategory> implements AssetCategoryFinder {
053    
054            public static final String COUNT_BY_G_C_N =
055                    AssetCategoryFinder.class.getName() + ".countByG_C_N";
056    
057            public static final String COUNT_BY_G_N_V =
058                    AssetCategoryFinder.class.getName() + ".countByG_N_V";
059    
060            public static final String COUNT_BY_G_N_P =
061                    AssetCategoryFinder.class.getName() + ".countByG_N_P";
062    
063            public static final String FIND_BY_ENTRY_ID =
064                    AssetCategoryFinder.class.getName() + ".findByEntryId";
065    
066            public static final String FIND_BY_G_L =
067                    AssetCategoryFinder.class.getName() + ".findByG_L";
068    
069            public static final String FIND_BY_G_N =
070                    AssetCategoryFinder.class.getName() + ".findByG_N";
071    
072            public static final String FIND_BY_C_C =
073                    AssetCategoryFinder.class.getName() + ".findByC_C";
074    
075            public static final String FIND_BY_G_N_V =
076                    AssetCategoryFinder.class.getName() + ".findByG_N_V";
077    
078            public static final String FIND_BY_G_N_P =
079                    AssetCategoryFinder.class.getName() + ".findByG_N_P";
080    
081            public static final FinderPath FINDER_PATH_FIND_BY_G_L = new FinderPath(
082                    AssetCategoryModelImpl.ENTITY_CACHE_ENABLED,
083                    AssetCategoryModelImpl.FINDER_CACHE_ENABLED, List.class,
084                    AssetCategoryPersistenceImpl.FINDER_CLASS_NAME_LIST_WITH_PAGINATION,
085                    "findByG_L", new String[] {Long.class.getName()});
086    
087            public int countByG_C_N(long groupId, long classNameId, String name)
088                    throws SystemException {
089    
090                    Session session = null;
091    
092                    try {
093                            session = openSession();
094    
095                            String sql = CustomSQLUtil.get(COUNT_BY_G_C_N);
096    
097                            SQLQuery q = session.createSQLQuery(sql);
098    
099                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
100    
101                            QueryPos qPos = QueryPos.getInstance(q);
102    
103                            qPos.add(groupId);
104                            qPos.add(classNameId);
105                            qPos.add(name);
106                            qPos.add(name);
107    
108                            Iterator<Long> itr = q.iterate();
109    
110                            if (itr.hasNext()) {
111                                    Long count = itr.next();
112    
113                                    if (count != null) {
114                                            return count.intValue();
115                                    }
116                            }
117    
118                            return 0;
119                    }
120                    catch (Exception e) {
121                            throw new SystemException(e);
122                    }
123                    finally {
124                            closeSession(session);
125                    }
126            }
127    
128            public int countByG_N_V(long groupId, String name, long vocabularyId)
129                    throws SystemException {
130    
131                    return doCountByG_N_V(groupId, name, vocabularyId, false);
132            }
133    
134            public int countByG_N_P(
135                            long groupId, String name, String[] categoryProperties)
136                    throws SystemException {
137    
138                    Session session = null;
139    
140                    try {
141                            session = openSession();
142    
143                            String sql = CustomSQLUtil.get(COUNT_BY_G_N_P);
144    
145                            SQLQuery q = session.createSQLQuery(sql);
146    
147                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
148    
149                            QueryPos qPos = QueryPos.getInstance(q);
150    
151                            setJoin(qPos, categoryProperties);
152    
153                            qPos.add(groupId);
154                            qPos.add(name);
155                            qPos.add(name);
156    
157                            Iterator<Long> itr = q.iterate();
158    
159                            if (itr.hasNext()) {
160                                    Long count = itr.next();
161    
162                                    if (count != null) {
163                                            return count.intValue();
164                                    }
165                            }
166    
167                            return 0;
168                    }
169                    catch (Exception e) {
170                            throw new SystemException(e);
171                    }
172                    finally {
173                            closeSession(session);
174                    }
175            }
176    
177            public int filterCountByG_N_V(long groupId, String name, long vocabularyId)
178                    throws SystemException {
179    
180                    return doCountByG_N_V(groupId, name, vocabularyId, true);
181            }
182    
183            public List<AssetCategory> filterFindByG_N_V(
184                            long groupId, String name, long vocabularyId, int start, int end,
185                            OrderByComparator obc)
186                    throws SystemException {
187    
188                    return doFindByG_N_V(
189                            groupId, name, vocabularyId, start, end, obc, true);
190            }
191    
192            public List<AssetCategory> findByEntryId(long entryId)
193                    throws SystemException {
194    
195                    Session session = null;
196    
197                    try {
198                            session = openSession();
199    
200                            String sql = CustomSQLUtil.get(FIND_BY_ENTRY_ID);
201    
202                            SQLQuery q = session.createSQLQuery(sql);
203    
204                            q.addEntity("AssetCategory", AssetCategoryImpl.class);
205    
206                            QueryPos qPos = QueryPos.getInstance(q);
207    
208                            qPos.add(entryId);
209    
210                            return (List<AssetCategory>)QueryUtil.list(
211                                    q, getDialect(), QueryUtil.ALL_POS, QueryUtil.ALL_POS);
212                    }
213                    catch (Exception e) {
214                            throw new SystemException(e);
215                    }
216                    finally {
217                            closeSession(session);
218                    }
219            }
220    
221            public List<Long> findByG_L(Long parentCategoryId) throws SystemException {
222                    Object[] finderArgs = new Object[] {parentCategoryId};
223    
224                    List<Long> list = (List<Long>)FinderCacheUtil.getResult(
225                            FINDER_PATH_FIND_BY_G_L, finderArgs, this);
226    
227                    if (list != null) {
228                            return list;
229                    }
230    
231                    AssetCategory parentAssetCategory = AssetCategoryUtil.fetchByPrimaryKey(
232                            parentCategoryId);
233    
234                    if (parentAssetCategory == null) {
235                            return Collections.emptyList();
236                    }
237    
238                    Session session = null;
239    
240                    try {
241                            session = openSession();
242    
243                            String sql = CustomSQLUtil.get(FIND_BY_G_L);
244    
245                            SQLQuery q = session.createSQLQuery(sql);
246    
247                            q.addScalar("categoryId", Type.LONG);
248    
249                            QueryPos qPos = QueryPos.getInstance(q);
250    
251                            qPos.add(parentAssetCategory.getGroupId());
252                            qPos.add(parentAssetCategory.getLeftCategoryId());
253                            qPos.add(parentAssetCategory.getRightCategoryId());
254    
255                            list = q.list();
256                    }
257                    catch (Exception e) {
258                            throw processException(e);
259                    }
260                    finally {
261                            if (list == null) {
262                                    FinderCacheUtil.removeResult(
263                                            FINDER_PATH_FIND_BY_G_L, finderArgs);
264                            }
265                            else {
266                                    FinderCacheUtil.putResult(
267                                            FINDER_PATH_FIND_BY_G_L, finderArgs, list);
268                            }
269    
270                            closeSession(session);
271                    }
272    
273                    return list;
274            }
275    
276            public AssetCategory findByG_N(long groupId, String name)
277                    throws NoSuchCategoryException, SystemException {
278    
279                    name = name.trim().toLowerCase();
280    
281                    Session session = null;
282    
283                    try {
284                            session = openSession();
285    
286                            String sql = CustomSQLUtil.get(FIND_BY_G_N);
287    
288                            SQLQuery q = session.createSQLQuery(sql);
289    
290                            q.addEntity("AssetCategory", AssetCategoryImpl.class);
291    
292                            QueryPos qPos = QueryPos.getInstance(q);
293    
294                            qPos.add(groupId);
295                            qPos.add(name);
296    
297                            List<AssetCategory> categories = q.list();
298    
299                            if (!categories.isEmpty()) {
300                                    return categories.get(0);
301                            }
302                    }
303                    catch (Exception e) {
304                            throw new SystemException(e);
305                    }
306                    finally {
307                            closeSession(session);
308                    }
309    
310                    StringBundler sb = new StringBundler(6);
311    
312                    sb.append("No AssetCategory exists with the key ");
313                    sb.append("{groupId=");
314                    sb.append(groupId);
315                    sb.append(", name=");
316                    sb.append(name);
317                    sb.append("}");
318    
319                    throw new NoSuchCategoryException(sb.toString());
320            }
321    
322            public List<AssetCategory> findByC_C(long classNameId, long classPK)
323                    throws SystemException {
324    
325                    Session session = null;
326    
327                    try {
328                            AssetEntry entry = AssetEntryUtil.fetchByC_C(classNameId, classPK);
329    
330                            if (entry == null) {
331                                    return Collections.emptyList();
332                            }
333    
334                            session = openSession();
335    
336                            String sql = CustomSQLUtil.get(FIND_BY_C_C);
337    
338                            SQLQuery q = session.createSQLQuery(sql);
339    
340                            q.addEntity("AssetCategory", AssetCategoryImpl.class);
341    
342                            QueryPos qPos = QueryPos.getInstance(q);
343    
344                            qPos.add(entry.getEntryId());
345    
346                            return (List<AssetCategory>)QueryUtil.list(
347                                    q, getDialect(), QueryUtil.ALL_POS, QueryUtil.ALL_POS);
348                    }
349                    catch (Exception e) {
350                            throw new SystemException(e);
351                    }
352                    finally {
353                            closeSession(session);
354                    }
355            }
356    
357            public List<AssetCategory> findByG_N_V(
358                            long groupId, String name, long vocabularyId, int start, int end,
359                            OrderByComparator obc)
360                    throws SystemException {
361    
362                    return doFindByG_N_V(
363                            groupId, name, vocabularyId, start, end, obc, false);
364            }
365    
366            public List<AssetCategory> findByG_N_P(
367                            long groupId, String name, String[] categoryProperties)
368                    throws SystemException {
369    
370                    return findByG_N_P(
371                            groupId, name, categoryProperties, QueryUtil.ALL_POS,
372                            QueryUtil.ALL_POS);
373            }
374    
375            public List<AssetCategory> findByG_N_P(
376                            long groupId, String name, String[] categoryProperties, int start,
377                            int end)
378                    throws SystemException {
379    
380                    Session session = null;
381    
382                    try {
383                            session = openSession();
384    
385                            String sql = CustomSQLUtil.get(FIND_BY_G_N_P);
386    
387                            sql = StringUtil.replace(
388                                    sql, "[$JOIN$]", getJoin(categoryProperties));
389    
390                            SQLQuery q = session.createSQLQuery(sql);
391    
392                            q.addEntity("AssetCategory", AssetCategoryImpl.class);
393    
394                            QueryPos qPos = QueryPos.getInstance(q);
395    
396                            setJoin(qPos, categoryProperties);
397    
398                            qPos.add(groupId);
399                            qPos.add(name);
400                            qPos.add(name);
401    
402                            return (List<AssetCategory>)QueryUtil.list(
403                                    q, getDialect(), start, end);
404                    }
405                    catch (Exception e) {
406                            throw new SystemException(e);
407                    }
408                    finally {
409                            closeSession(session);
410                    }
411            }
412    
413            protected int doCountByG_N_V(
414                            long groupId, String name, long vocabularyId,
415                            boolean inlineSQLHelper)
416                    throws SystemException {
417    
418                    Session session = null;
419    
420                    try {
421                            session = openSession();
422    
423                            String sql = CustomSQLUtil.get(COUNT_BY_G_N_V);
424    
425                            if (inlineSQLHelper) {
426                                    sql = InlineSQLHelperUtil.replacePermissionCheck(
427                                            sql, AssetCategory.class.getName(),
428                                            "AssetCategory.categoryId", groupId);
429                            }
430    
431                            SQLQuery q = session.createSQLQuery(sql);
432    
433                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
434    
435                            QueryPos qPos = QueryPos.getInstance(q);
436    
437                            qPos.add(groupId);
438                            qPos.add(name);
439                            qPos.add(name);
440                            qPos.add(vocabularyId);
441    
442                            Iterator<Long> itr = q.iterate();
443    
444                            if (itr.hasNext()) {
445                                    Long count = itr.next();
446    
447                                    if (count != null) {
448                                            return count.intValue();
449                                    }
450                            }
451    
452                            return 0;
453                    }
454                    catch (Exception e) {
455                            throw new SystemException(e);
456                    }
457                    finally {
458                            closeSession(session);
459                    }
460            }
461    
462            protected List<AssetCategory> doFindByG_N_V(
463                            long groupId, String name, long vocabularyId, int start, int end,
464                            OrderByComparator obc, boolean inlineSQLHelper)
465                    throws SystemException {
466    
467                    name = name.trim().toLowerCase();
468    
469                    Session session = null;
470    
471                    try {
472                            session = openSession();
473    
474                            String sql = CustomSQLUtil.get(FIND_BY_G_N_V);
475    
476                            sql = CustomSQLUtil.replaceOrderBy(sql, obc);
477    
478                            if (inlineSQLHelper) {
479                                    sql = InlineSQLHelperUtil.replacePermissionCheck(
480                                            sql, AssetVocabulary.class.getName(),
481                                            "AssetCategory.categoryId", groupId);
482                            }
483    
484                            SQLQuery q = session.createSQLQuery(sql);
485    
486                            q.addEntity("AssetCategory", AssetCategoryImpl.class);
487    
488                            QueryPos qPos = QueryPos.getInstance(q);
489    
490                            qPos.add(groupId);
491                            qPos.add(name);
492                            qPos.add(name);
493                            qPos.add(vocabularyId);
494    
495                            return (List<AssetCategory>)QueryUtil.list(
496                                    q, getDialect(), start, end);
497                    }
498                    catch (Exception e) {
499                            throw new SystemException(e);
500                    }
501                    finally {
502                            closeSession(session);
503                    }
504            }
505    
506            protected String getJoin(String[] categoryProperties) {
507                    if (categoryProperties.length == 0) {
508                            return StringPool.BLANK;
509                    }
510                    else {
511                            StringBundler sb = new StringBundler(
512                                    categoryProperties.length * 3 + 2);
513    
514                            sb.append(" INNER JOIN AssetCategoryProperty ON ");
515                            sb.append(" (AssetCategoryProperty.categoryId = ");
516                            sb.append(" AssetCategory.categoryId) AND ");
517    
518                            for (int i = 0; i < categoryProperties.length; i++) {
519                                    sb.append("(AssetCategoryProperty.key_ = ? AND ");
520                                    sb.append("AssetCategoryProperty.value = ?) ");
521    
522                                    if ((i + 1) < categoryProperties.length) {
523                                            sb.append(" AND ");
524                                    }
525                            }
526    
527                            return sb.toString();
528                    }
529            }
530    
531            protected void setJoin(QueryPos qPos, String[] categoryProperties) {
532                    for (int i = 0; i < categoryProperties.length; i++) {
533                            String[] categoryProperty = StringUtil.split(
534                                    categoryProperties[i], CharPool.COLON);
535    
536                            String key = StringPool.BLANK;
537    
538                            if (categoryProperty.length > 0) {
539                                    key = GetterUtil.getString(categoryProperty[0]);
540                            }
541    
542                            String value = StringPool.BLANK;
543    
544                            if (categoryProperty.length > 1) {
545                                    value = GetterUtil.getString(categoryProperty[1]);
546                            }
547    
548                            qPos.add(key);
549                            qPos.add(value);
550                    }
551            }
552    
553    }