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.AssetCategoryProperty;
025    import com.liferay.portlet.asset.model.impl.AssetCategoryPropertyImpl;
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     * @author Jorge Ferrer
035     */
036    public class AssetCategoryPropertyFinderImpl
037            extends BasePersistenceImpl<AssetCategoryProperty>
038            implements AssetCategoryPropertyFinder {
039    
040            public static String COUNT_BY_G_K =
041                    AssetCategoryPropertyFinder.class.getName() + ".countByG_K";
042    
043            public static String FIND_BY_G_K =
044                    AssetCategoryPropertyFinder.class.getName() + ".findByG_K";
045    
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.list().iterator();
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            public List<AssetCategoryProperty> findByG_K(long groupId, String key)
084                    throws SystemException {
085    
086                    return findByG_K(groupId, key, QueryUtil.ALL_POS, QueryUtil.ALL_POS);
087            }
088    
089            public List<AssetCategoryProperty> findByG_K(
090                            long groupId, String key, int start, int end)
091                    throws SystemException {
092    
093                    Session session = null;
094    
095                    try {
096                            session = openSession();
097    
098                            String sql = CustomSQLUtil.get(FIND_BY_G_K);
099    
100                            SQLQuery q = session.createSQLQuery(sql);
101    
102                            q.addScalar("categoryPropertyValue", Type.STRING);
103    
104                            QueryPos qPos = QueryPos.getInstance(q);
105    
106                            qPos.add(groupId);
107                            qPos.add(key);
108    
109                            List<AssetCategoryProperty> categoryProperties =
110                                    new ArrayList<AssetCategoryProperty>();
111    
112                            Iterator<String> itr = (Iterator<String>)QueryUtil.iterate(
113                                    q, getDialect(), start, end);
114    
115                            while (itr.hasNext()) {
116                                    String value = itr.next();
117    
118                                    AssetCategoryProperty categoryProperty =
119                                            new AssetCategoryPropertyImpl();
120    
121                                    categoryProperty.setKey(key);
122                                    categoryProperty.setValue(value);
123    
124                                    categoryProperties.add(categoryProperty);
125                            }
126    
127                            return categoryProperties;
128                    }
129                    catch (Exception e) {
130                            throw new SystemException(e);
131                    }
132                    finally {
133                            closeSession(session);
134                    }
135            }
136    
137    }