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.portlet.asset.model.AssetCategoryProperty;
024    import com.liferay.portlet.asset.model.impl.AssetCategoryPropertyImpl;
025    import com.liferay.portlet.asset.service.persistence.AssetCategoryPropertyFinder;
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 AssetCategoryPropertyFinderBaseImpl
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) {
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.createSynchronizedSQLQuery(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                    return findByG_K(groupId, key, QueryUtil.ALL_POS, QueryUtil.ALL_POS);
087            }
088    
089            @Override
090            public List<AssetCategoryProperty> findByG_K(
091                    long groupId, String key, int start, int end) {
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.createSynchronizedSQLQuery(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 = new ArrayList<>();
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                                    AssetCategoryProperty categoryProperty =
118                                            new AssetCategoryPropertyImpl();
119    
120                                    categoryProperty.setKey(key);
121                                    categoryProperty.setValue(value);
122    
123                                    categoryProperties.add(categoryProperty);
124                            }
125    
126                            return categoryProperties;
127                    }
128                    catch (Exception e) {
129                            throw new SystemException(e);
130                    }
131                    finally {
132                            closeSession(session);
133                    }
134            }
135    
136    }