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