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    
048                    return doCountByG_N(groupId, name, false);
049            }
050    
051            public int filterCountByG_N(long groupId, String name)
052                    throws SystemException {
053    
054                    return doCountByG_N(groupId, name, true);
055            }
056    
057            public List<AssetVocabulary> filterFindByG_N(long groupId, String name,
058                            int start, int end, OrderByComparator obc)
059                    throws SystemException {
060    
061                    return doFindByG_N(groupId, name, start, end, obc, true);
062            }
063    
064            public List<AssetVocabulary> findByG_N(long groupId, String name, int start,
065                            int end, OrderByComparator obc)
066                    throws SystemException {
067    
068                    return doFindByG_N(groupId, name, start, end, obc, false);
069            }
070    
071            protected int doCountByG_N(
072                            long groupId, String name, boolean inlineSQLHelper)
073                    throws SystemException {
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.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(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 obc, boolean inlineSQLHelper)
121                    throws SystemException {
122    
123                    name = name.trim().toLowerCase();
124    
125                    Session session = null;
126    
127                    try {
128                            session = openSession();
129    
130                            String sql = CustomSQLUtil.get(FIND_BY_G_N);
131    
132                            sql = CustomSQLUtil.replaceOrderBy(sql, obc);
133    
134                            if (inlineSQLHelper) {
135                                    sql = InlineSQLHelperUtil.replacePermissionCheck(
136                                            sql, AssetVocabulary.class.getName(),
137                                            "AssetVocabulary.vocabularyId", groupId);
138                            }
139    
140                            SQLQuery q = session.createSQLQuery(sql);
141    
142                            q.addEntity("AssetVocabulary", AssetVocabularyImpl.class);
143    
144                            QueryPos qPos = QueryPos.getInstance(q);
145    
146                            qPos.add(groupId);
147                            qPos.add(name);
148                            qPos.add(name);
149    
150                            return (List<AssetVocabulary>)QueryUtil.list(
151                                    q, getDialect(), start, end);
152                    }
153                    catch (Exception e) {
154                            throw new SystemException(e);
155                    }
156                    finally {
157                            closeSession(session);
158                    }
159            }
160    
161    }