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