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.impl;
016    
017    import com.liferay.portal.kernel.dao.orm.QueryPos;
018    import com.liferay.portal.kernel.dao.orm.QueryUtil;
019    import com.liferay.portal.kernel.dao.orm.SQLQuery;
020    import com.liferay.portal.kernel.dao.orm.Session;
021    import com.liferay.portal.kernel.dao.orm.Type;
022    import com.liferay.portal.kernel.exception.SystemException;
023    import com.liferay.portal.kernel.util.OrderByComparator;
024    import com.liferay.portal.kernel.util.StringUtil;
025    import com.liferay.portal.security.permission.InlineSQLHelperUtil;
026    import com.liferay.portlet.asset.model.AssetVocabulary;
027    import com.liferay.portlet.asset.model.impl.AssetVocabularyImpl;
028    import com.liferay.portlet.asset.service.persistence.AssetVocabularyFinder;
029    import com.liferay.util.dao.orm.CustomSQLUtil;
030    
031    import java.util.Iterator;
032    import java.util.List;
033    
034    /**
035     * @author Juan Fern??ndez
036     */
037    public class AssetVocabularyFinderImpl
038            extends AssetVocabularyFinderBaseImpl implements AssetVocabularyFinder {
039    
040            public static final String COUNT_BY_G_N =
041                    AssetVocabularyFinder.class.getName() + ".countByG_N";
042    
043            public static final String FIND_BY_G_N =
044                    AssetVocabularyFinder.class.getName() + ".findByG_N";
045    
046            @Override
047            public int countByG_N(long groupId, String name) {
048                    return doCountByG_N(groupId, name, false);
049            }
050    
051            @Override
052            public int filterCountByG_N(long groupId, String name) {
053                    return doCountByG_N(groupId, name, true);
054            }
055    
056            @Override
057            public List<AssetVocabulary> filterFindByG_N(
058                    long groupId, String name, int start, int end,
059                    OrderByComparator<AssetVocabulary> obc) {
060    
061                    return doFindByG_N(groupId, name, start, end, obc, true);
062            }
063    
064            @Override
065            public List<AssetVocabulary> findByG_N(
066                    long groupId, String name, int start, int end,
067                    OrderByComparator<AssetVocabulary> obc) {
068    
069                    return doFindByG_N(groupId, name, start, end, obc, false);
070            }
071    
072            protected int doCountByG_N(
073                    long groupId, String name, boolean inlineSQLHelper) {
074    
075                    Session session = null;
076    
077                    try {
078                            session = openSession();
079    
080                            String sql = CustomSQLUtil.get(COUNT_BY_G_N);
081    
082                            if (inlineSQLHelper) {
083                                    sql = InlineSQLHelperUtil.replacePermissionCheck(
084                                            sql, AssetVocabulary.class.getName(),
085                                            "AssetVocabulary.vocabularyId", groupId);
086                            }
087    
088                            SQLQuery q = session.createSynchronizedSQLQuery(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(name);
096                            qPos.add(name);
097    
098                            Iterator<Long> itr = q.iterate();
099    
100                            if (itr.hasNext()) {
101                                    Long count = itr.next();
102    
103                                    if (count != null) {
104                                            return count.intValue();
105                                    }
106                            }
107    
108                            return 0;
109                    }
110                    catch (Exception e) {
111                            throw new SystemException(e);
112                    }
113                    finally {
114                            closeSession(session);
115                    }
116            }
117    
118            protected List<AssetVocabulary> doFindByG_N(
119                    long groupId, String name, int start, int end,
120                    OrderByComparator<AssetVocabulary> obc, boolean inlineSQLHelper) {
121    
122                    name = StringUtil.toLowerCase(name.trim());
123    
124                    Session session = null;
125    
126                    try {
127                            session = openSession();
128    
129                            String sql = CustomSQLUtil.get(FIND_BY_G_N);
130    
131                            sql = CustomSQLUtil.replaceOrderBy(sql, obc);
132    
133                            if (inlineSQLHelper) {
134                                    sql = InlineSQLHelperUtil.replacePermissionCheck(
135                                            sql, AssetVocabulary.class.getName(),
136                                            "AssetVocabulary.vocabularyId", groupId);
137                            }
138    
139                            SQLQuery q = session.createSynchronizedSQLQuery(sql);
140    
141                            q.addEntity("AssetVocabulary", AssetVocabularyImpl.class);
142    
143                            QueryPos qPos = QueryPos.getInstance(q);
144    
145                            qPos.add(groupId);
146                            qPos.add(name);
147                            qPos.add(name);
148    
149                            return (List<AssetVocabulary>)QueryUtil.list(
150                                    q, getDialect(), start, end);
151                    }
152                    catch (Exception e) {
153                            throw new SystemException(e);
154                    }
155                    finally {
156                            closeSession(session);
157                    }
158            }
159    
160    }