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.AssetTagProperty;
025    import com.liferay.portlet.asset.model.impl.AssetTagPropertyImpl;
026    import com.liferay.portlet.asset.service.persistence.AssetTagPropertyFinder;
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     */
036    public class AssetTagPropertyFinderImpl
037            extends BasePersistenceImpl<AssetTagProperty>
038            implements AssetTagPropertyFinder {
039    
040            public static final String COUNT_BY_G_K =
041                    AssetTagPropertyFinder.class.getName() + ".countByG_K";
042    
043            public static final String FIND_BY_G_K =
044                    AssetTagPropertyFinder.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<AssetTagProperty> 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<AssetTagProperty> 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("tagPropertyValue", Type.STRING);
103    
104                            QueryPos qPos = QueryPos.getInstance(q);
105    
106                            qPos.add(groupId);
107                            qPos.add(key);
108    
109                            List<AssetTagProperty> tagProperties =
110                                    new ArrayList<AssetTagProperty>();
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                                    AssetTagProperty tagProperty = new AssetTagPropertyImpl();
119    
120                                    tagProperty.setKey(key);
121                                    tagProperty.setValue(value);
122    
123                                    tagProperties.add(tagProperty);
124                            }
125    
126                            return tagProperties;
127                    }
128                    catch (Exception e) {
129                            throw new SystemException(e);
130                    }
131                    finally {
132                            closeSession(session);
133                    }
134            }
135    
136    }