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