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.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.security.permission.InlineSQLHelperUtil;
025    import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
026    import com.liferay.portlet.asset.model.AssetVocabulary;
027    import com.liferay.portlet.asset.model.impl.AssetVocabularyImpl;
028    import com.liferay.util.dao.orm.CustomSQLUtil;
029    
030    import java.util.Iterator;
031    import java.util.List;
032    
033    /**
034     * @author Juan Fernández
035     */
036    public class AssetVocabularyFinderImpl
037            extends BasePersistenceImpl<AssetVocabulary>
038            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            public int countByG_N(long groupId, String name) throws SystemException {
047                    return doCountByG_N(groupId, name, false);
048            }
049    
050            public int filterCountByG_N(long groupId, String name)
051                    throws SystemException {
052    
053                    return doCountByG_N(groupId, name, true);
054            }
055    
056            public List<AssetVocabulary> filterFindByG_N(
057                            long groupId, String name, int start, int end,
058                            OrderByComparator obc)
059                    throws SystemException {
060    
061                    return doFindByG_N(groupId, name, start, end, obc, true);
062            }
063    
064            public List<AssetVocabulary> findByG_N(
065                            long groupId, String name, int start, int end,
066                            OrderByComparator obc)
067                    throws SystemException {
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                    throws SystemException {
075    
076                    Session session = null;
077    
078                    try {
079                            session = openSession();
080    
081                            String sql = CustomSQLUtil.get(COUNT_BY_G_N);
082    
083                            if (inlineSQLHelper) {
084                                    sql = InlineSQLHelperUtil.replacePermissionCheck(
085                                            sql, AssetVocabulary.class.getName(),
086                                            "AssetVocabulary.vocabularyId", groupId);
087                            }
088    
089                            SQLQuery q = session.createSQLQuery(sql);
090    
091                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
092    
093                            QueryPos qPos = QueryPos.getInstance(q);
094    
095                            qPos.add(groupId);
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            protected List<AssetVocabulary> doFindByG_N(
120                            long groupId, String name, int start, int end,
121                            OrderByComparator obc, boolean inlineSQLHelper)
122                    throws SystemException {
123    
124                    name = name.trim().toLowerCase();
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.createSQLQuery(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    }