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