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