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.service.persistence.impl.BasePersistenceImpl;
024    import com.liferay.portlet.asset.model.AssetTagProperty;
025    import com.liferay.portlet.asset.model.impl.AssetTagPropertyImpl;
026    import com.liferay.util.dao.orm.CustomSQLUtil;
027    
028    import java.util.ArrayList;
029    import java.util.Iterator;
030    import java.util.List;
031    
032    /**
033     * @author Brian Wing Shun Chan
034     */
035    public class AssetTagPropertyFinderImpl
036            extends BasePersistenceImpl<AssetTagProperty>
037            implements AssetTagPropertyFinder {
038    
039            public static String COUNT_BY_G_K =
040                    AssetTagPropertyFinder.class.getName() + ".countByG_K";
041    
042            public static String FIND_BY_G_K =
043                    AssetTagPropertyFinder.class.getName() + ".findByG_K";
044    
045            public int countByG_K(long groupId, String key) throws SystemException {
046                    Session session = null;
047    
048                    try {
049                            session = openSession();
050    
051                            String sql = CustomSQLUtil.get(COUNT_BY_G_K);
052    
053                            SQLQuery q = session.createSQLQuery(sql);
054    
055                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
056    
057                            QueryPos qPos = QueryPos.getInstance(q);
058    
059                            qPos.add(groupId);
060                            qPos.add(key);
061    
062                            Iterator<Long> itr = q.list().iterator();
063    
064                            if (itr.hasNext()) {
065                                    Long count = itr.next();
066    
067                                    if (count != null) {
068                                            return count.intValue();
069                                    }
070                            }
071    
072                            return 0;
073                    }
074                    catch (Exception e) {
075                            throw new SystemException(e);
076                    }
077                    finally {
078                            closeSession(session);
079                    }
080            }
081    
082            public List<AssetTagProperty> findByG_K(long groupId, String key)
083                    throws SystemException {
084    
085                    return findByG_K(groupId, key, QueryUtil.ALL_POS, QueryUtil.ALL_POS);
086            }
087    
088            public List<AssetTagProperty> findByG_K(
089                            long groupId, String key, int start, int end)
090                    throws SystemException {
091    
092                    Session session = null;
093    
094                    try {
095                            session = openSession();
096    
097                            String sql = CustomSQLUtil.get(FIND_BY_G_K);
098    
099                            SQLQuery q = session.createSQLQuery(sql);
100    
101                            q.addScalar("tagPropertyValue", Type.STRING);
102    
103                            QueryPos qPos = QueryPos.getInstance(q);
104    
105                            qPos.add(groupId);
106                            qPos.add(key);
107    
108                            List<AssetTagProperty> tagProperties =
109                                    new ArrayList<AssetTagProperty>();
110    
111                            Iterator<String> itr = (Iterator<String>)QueryUtil.iterate(
112                                    q, getDialect(), start, end);
113    
114                            while (itr.hasNext()) {
115                                    String value = itr.next();
116    
117                                    AssetTagProperty tagProperty = new AssetTagPropertyImpl();
118    
119                                    tagProperty.setKey(key);
120                                    tagProperty.setValue(value);
121    
122                                    tagProperties.add(tagProperty);
123                            }
124    
125                            return tagProperties;
126                    }
127                    catch (Exception e) {
128                            throw new SystemException(e);
129                    }
130                    finally {
131                            closeSession(session);
132                    }
133            }
134    
135    }