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.kernel.util.GetterUtil;
024    import com.liferay.portal.kernel.util.OrderByComparator;
025    import com.liferay.portal.kernel.util.StringBundler;
026    import com.liferay.portal.kernel.util.StringPool;
027    import com.liferay.portal.kernel.util.StringUtil;
028    import com.liferay.portlet.asset.model.AssetTag;
029    import com.liferay.portlet.asset.model.impl.AssetTagImpl;
030    import com.liferay.portlet.asset.service.persistence.AssetTagFinder;
031    import com.liferay.util.dao.orm.CustomSQLUtil;
032    
033    import java.util.ArrayList;
034    import java.util.Iterator;
035    import java.util.List;
036    
037    /**
038     * @author Brian Wing Shun Chan
039     * @author Bruno Farache
040     */
041    public class AssetTagFinderImpl
042            extends AssetTagFinderBaseImpl implements AssetTagFinder {
043    
044            public static final String COUNT_BY_G_N =
045                    AssetTagFinder.class.getName() + ".countByG_N";
046    
047            public static final String COUNT_BY_G_C_N =
048                    AssetTagFinder.class.getName() + ".countByG_C_N";
049    
050            public static final String FIND_BY_G_C_N =
051                    AssetTagFinder.class.getName() + ".findByG_C_N";
052    
053            public static final String FIND_BY_G_N_S_E =
054                    AssetTagFinder.class.getName() + ".findByG_N_S_E";
055    
056            @Override
057            public int countByG_N(long groupId, String name) {
058                    Session session = null;
059    
060                    try {
061                            session = openSession();
062    
063                            String sql = CustomSQLUtil.get(COUNT_BY_G_N);
064    
065                            SQLQuery q = session.createSynchronizedSQLQuery(sql);
066    
067                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
068    
069                            QueryPos qPos = QueryPos.getInstance(q);
070    
071                            qPos.add(groupId);
072    
073                            String lowerCaseName = StringUtil.toLowerCase(name);
074    
075                            qPos.add(lowerCaseName);
076    
077                            Iterator<Long> itr = q.iterate();
078    
079                            if (itr.hasNext()) {
080                                    Long count = itr.next();
081    
082                                    if (count != null) {
083                                            return count.intValue();
084                                    }
085                            }
086    
087                            return 0;
088                    }
089                    catch (Exception e) {
090                            throw new SystemException(e);
091                    }
092                    finally {
093                            closeSession(session);
094                    }
095            }
096    
097            @Override
098            public int countByG_C_N(long groupId, long classNameId, String name) {
099                    Session session = null;
100    
101                    try {
102                            session = openSession();
103    
104                            String sql = CustomSQLUtil.get(COUNT_BY_G_C_N);
105    
106                            SQLQuery q = session.createSynchronizedSQLQuery(sql);
107    
108                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
109    
110                            QueryPos qPos = QueryPos.getInstance(q);
111    
112                            qPos.add(groupId);
113                            qPos.add(classNameId);
114    
115                            String lowerCaseName = StringUtil.toLowerCase(name);
116    
117                            qPos.add(lowerCaseName);
118                            qPos.add(lowerCaseName);
119    
120                            Iterator<Long> itr = q.iterate();
121    
122                            if (itr.hasNext()) {
123                                    Long count = itr.next();
124    
125                                    if (count != null) {
126                                            return count.intValue();
127                                    }
128                            }
129    
130                            return 0;
131                    }
132                    catch (Exception e) {
133                            throw new SystemException(e);
134                    }
135                    finally {
136                            closeSession(session);
137                    }
138            }
139    
140            @Override
141            public List<AssetTag> findByG_C_N(
142                    long groupId, long classNameId, String name, int start, int end,
143                    OrderByComparator<AssetTag> obc) {
144    
145                    Session session = null;
146    
147                    try {
148                            session = openSession();
149    
150                            String sql = CustomSQLUtil.get(FIND_BY_G_C_N);
151    
152                            sql = CustomSQLUtil.replaceOrderBy(sql, obc);
153    
154                            SQLQuery q = session.createSynchronizedSQLQuery(sql);
155    
156                            q.addEntity("AssetTag", AssetTagImpl.class);
157    
158                            QueryPos qPos = QueryPos.getInstance(q);
159    
160                            qPos.add(groupId);
161                            qPos.add(classNameId);
162    
163                            String lowerCaseName = StringUtil.toLowerCase(name);
164    
165                            qPos.add(lowerCaseName);
166                            qPos.add(lowerCaseName);
167    
168                            return (List<AssetTag>)QueryUtil.list(q, getDialect(), start, end);
169                    }
170                    catch (Exception e) {
171                            throw new SystemException(e);
172                    }
173                    finally {
174                            closeSession(session);
175                    }
176            }
177    
178            @Override
179            public List<AssetTag> findByG_N_S_E(
180                    long groupId, String name, int startPeriod, int endPeriod,
181                    int periodLength) {
182    
183                    Session session = null;
184    
185                    try {
186                            session = openSession();
187    
188                            String sql = CustomSQLUtil.get(FIND_BY_G_N_S_E);
189                            SQLQuery q = session.createSynchronizedSQLQuery(sql);
190    
191                            QueryPos qPos = QueryPos.getInstance(q);
192    
193                            qPos.add(groupId);
194                            qPos.add(name);
195                            qPos.add(startPeriod);
196                            qPos.add(endPeriod);
197                            qPos.add(periodLength);
198                            qPos.add(endPeriod);
199    
200                            List<AssetTag> assetTags = new ArrayList<>();
201    
202                            Iterator<Object[]> itr = q.iterate();
203    
204                            while (itr.hasNext()) {
205                                    Object[] array = itr.next();
206    
207                                    AssetTag assetTag = new AssetTagImpl();
208    
209                                    assetTag.setTagId(GetterUtil.getLong(array[0]));
210                                    assetTag.setName(GetterUtil.getString(array[1]));
211                                    assetTag.setAssetCount(GetterUtil.getInteger(array[2]));
212    
213                                    assetTags.add(assetTag);
214                            }
215    
216                            return assetTags;
217                    }
218                    catch (Exception e) {
219                            throw new SystemException(e);
220                    }
221                    finally {
222                            closeSession(session);
223                    }
224            }
225    
226            protected String getGroupIds(long[] groupIds) {
227                    if (groupIds.length == 0) {
228                            return StringPool.BLANK;
229                    }
230    
231                    StringBundler sb = new StringBundler(groupIds.length * 2);
232    
233                    sb.append(StringPool.OPEN_PARENTHESIS);
234    
235                    for (int i = 0; i < groupIds.length; i++) {
236                            sb.append("groupId = ?");
237    
238                            if ((i + 1) < groupIds.length) {
239                                    sb.append(" OR ");
240                            }
241                    }
242    
243                    sb.append(") AND");
244    
245                    return sb.toString();
246            }
247    
248    }