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