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.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 final String COUNT_BY_G_K =
040                    AssetTagPropertyFinder.class.getName() + ".countByG_K";
041    
042            public static final String FIND_BY_G_K =
043                    AssetTagPropertyFinder.class.getName() + ".findByG_K";
044    
045            @Override
046            public int countByG_K(long groupId, String key) throws SystemException {
047                    Session session = null;
048    
049                    try {
050                            session = openSession();
051    
052                            String sql = CustomSQLUtil.get(COUNT_BY_G_K);
053    
054                            SQLQuery q = session.createSQLQuery(sql);
055    
056                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
057    
058                            QueryPos qPos = QueryPos.getInstance(q);
059    
060                            qPos.add(groupId);
061                            qPos.add(key);
062    
063                            Iterator<Long> itr = q.iterate();
064    
065                            if (itr.hasNext()) {
066                                    Long count = itr.next();
067    
068                                    if (count != null) {
069                                            return count.intValue();
070                                    }
071                            }
072    
073                            return 0;
074                    }
075                    catch (Exception e) {
076                            throw new SystemException(e);
077                    }
078                    finally {
079                            closeSession(session);
080                    }
081            }
082    
083            @Override
084            public List<AssetTagProperty> findByG_K(long groupId, String key)
085                    throws SystemException {
086    
087                    return findByG_K(groupId, key, QueryUtil.ALL_POS, QueryUtil.ALL_POS);
088            }
089    
090            @Override
091            public List<AssetTagProperty> findByG_K(
092                            long groupId, String key, int start, int end)
093                    throws SystemException {
094    
095                    Session session = null;
096    
097                    try {
098                            session = openSession();
099    
100                            String sql = CustomSQLUtil.get(FIND_BY_G_K);
101    
102                            SQLQuery q = session.createSQLQuery(sql);
103    
104                            q.addScalar("tagPropertyValue", Type.STRING);
105    
106                            QueryPos qPos = QueryPos.getInstance(q);
107    
108                            qPos.add(groupId);
109                            qPos.add(key);
110    
111                            List<AssetTagProperty> tagProperties =
112                                    new ArrayList<AssetTagProperty>();
113    
114                            Iterator<String> itr = (Iterator<String>)QueryUtil.iterate(
115                                    q, getDialect(), start, end);
116    
117                            while (itr.hasNext()) {
118                                    String value = itr.next();
119    
120                                    AssetTagProperty tagProperty = new AssetTagPropertyImpl();
121    
122                                    tagProperty.setKey(key);
123                                    tagProperty.setValue(value);
124    
125                                    tagProperties.add(tagProperty);
126                            }
127    
128                            return tagProperties;
129                    }
130                    catch (Exception e) {
131                            throw new SystemException(e);
132                    }
133                    finally {
134                            closeSession(session);
135                    }
136            }
137    
138    }