001    /**
002     * Copyright (c) 2000-2010 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 String COUNT_BY_GROUP_ID =
037                    AssetTagPropertyKeyFinder.class.getName() + ".countByGroupId";
038    
039            public static String FIND_BY_GROUP_ID =
040                    AssetTagPropertyKeyFinder.class.getName() + ".findByGroupId";
041    
042            public int countByGroupId(long groupId) throws SystemException {
043                    Session session = null;
044    
045                    try {
046                            session = openSession();
047    
048                            String sql = CustomSQLUtil.get(COUNT_BY_GROUP_ID);
049    
050                            SQLQuery q = session.createSQLQuery(sql);
051    
052                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
053    
054                            QueryPos qPos = QueryPos.getInstance(q);
055    
056                            qPos.add(groupId);
057    
058                            Iterator<Long> itr = q.list().iterator();
059    
060                            if (itr.hasNext()) {
061                                    Long count = itr.next();
062    
063                                    if (count != null) {
064                                            return count.intValue();
065                                    }
066                            }
067    
068                            return 0;
069                    }
070                    catch (Exception e) {
071                            throw new SystemException(e);
072                    }
073                    finally {
074                            closeSession(session);
075                    }
076            }
077    
078            public String[] findByGroupId(long groupId) throws SystemException {
079                    return findByGroupId(groupId, QueryUtil.ALL_POS, QueryUtil.ALL_POS);
080            }
081    
082            public String[] findByGroupId(long groupId, int start, int end)
083                    throws SystemException {
084    
085                    Session session = null;
086    
087                    try {
088                            session = openSession();
089    
090                            String sql = CustomSQLUtil.get(FIND_BY_GROUP_ID);
091    
092                            SQLQuery q = session.createSQLQuery(sql);
093    
094                            q.addScalar("propertyKey", Type.STRING);
095    
096                            QueryPos qPos = QueryPos.getInstance(q);
097    
098                            qPos.add(groupId);
099    
100                            List<String> list = (List<String>)QueryUtil.list(
101                                    q, getDialect(), start, end);
102    
103                            return list.toArray(new String[list.size()]);
104                    }
105                    catch (Exception e) {
106                            throw new SystemException(e);
107                    }
108                    finally {
109                            closeSession(session);
110                    }
111            }
112    
113    }