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