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