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.model.Dummy;
024    import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
025    import com.liferay.util.dao.orm.CustomSQLUtil;
026    
027    import java.util.Iterator;
028    import java.util.List;
029    
030    /**
031     * @author Brian Wing Shun Chan
032     */
033    public class AssetTagPropertyKeyFinderImpl
034            extends BasePersistenceImpl<Dummy> implements AssetTagPropertyKeyFinder {
035    
036            public static final String COUNT_BY_GROUP_ID =
037                    AssetTagPropertyKeyFinder.class.getName() + ".countByGroupId";
038    
039            public static final String FIND_BY_GROUP_ID =
040                    AssetTagPropertyKeyFinder.class.getName() + ".findByGroupId";
041    
042            @Override
043            public int countByGroupId(long groupId) throws SystemException {
044                    Session session = null;
045    
046                    try {
047                            session = openSession();
048    
049                            String sql = CustomSQLUtil.get(COUNT_BY_GROUP_ID);
050    
051                            SQLQuery q = session.createSQLQuery(sql);
052    
053                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
054    
055                            QueryPos qPos = QueryPos.getInstance(q);
056    
057                            qPos.add(groupId);
058    
059                            Iterator<Long> itr = q.iterate();
060    
061                            if (itr.hasNext()) {
062                                    Long count = itr.next();
063    
064                                    if (count != null) {
065                                            return count.intValue();
066                                    }
067                            }
068    
069                            return 0;
070                    }
071                    catch (Exception e) {
072                            throw new SystemException(e);
073                    }
074                    finally {
075                            closeSession(session);
076                    }
077            }
078    
079            @Override
080            public String[] findByGroupId(long groupId) throws SystemException {
081                    return findByGroupId(groupId, QueryUtil.ALL_POS, QueryUtil.ALL_POS);
082            }
083    
084            @Override
085            public String[] findByGroupId(long groupId, int start, int end)
086                    throws SystemException {
087    
088                    Session session = null;
089    
090                    try {
091                            session = openSession();
092    
093                            String sql = CustomSQLUtil.get(FIND_BY_GROUP_ID);
094    
095                            SQLQuery q = session.createSQLQuery(sql);
096    
097                            q.addScalar("propertyKey", Type.STRING);
098    
099                            QueryPos qPos = QueryPos.getInstance(q);
100    
101                            qPos.add(groupId);
102    
103                            List<String> list = (List<String>)QueryUtil.list(
104                                    q, getDialect(), start, end);
105    
106                            return list.toArray(new String[list.size()]);
107                    }
108                    catch (Exception e) {
109                            throw new SystemException(e);
110                    }
111                    finally {
112                            closeSession(session);
113                    }
114            }
115    
116    }