001    /**
002     * Copyright (c) 2000-2013 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.journal.service.persistence;
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.OrderByComparator;
024    import com.liferay.portal.kernel.util.StringPool;
025    import com.liferay.portal.kernel.util.StringUtil;
026    import com.liferay.portal.kernel.util.Validator;
027    import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
028    import com.liferay.portlet.journal.model.JournalFeed;
029    import com.liferay.portlet.journal.model.impl.JournalFeedImpl;
030    import com.liferay.util.dao.orm.CustomSQLUtil;
031    
032    import java.util.Iterator;
033    import java.util.List;
034    
035    /**
036     * @author Raymond Aug??
037     * @author Connor McKay
038     */
039    public class JournalFeedFinderImpl
040            extends BasePersistenceImpl<JournalFeed> implements JournalFeedFinder {
041    
042            public static final String COUNT_BY_C_G_F_N_D =
043                    JournalFeedFinder.class.getName() + ".countByC_G_F_N_D";
044    
045            public static final String FIND_BY_C_G_F_N_D =
046                    JournalFeedFinder.class.getName() + ".findByC_G_F_N_D";
047    
048            @Override
049            public int countByKeywords(long companyId, long groupId, String keywords)
050                    throws SystemException {
051    
052                    String[] feedIds = null;
053                    String[] names = null;
054                    String[] descriptions = null;
055                    boolean andOperator = false;
056    
057                    if (Validator.isNotNull(keywords)) {
058                            feedIds = CustomSQLUtil.keywords(keywords, false);
059                            names = CustomSQLUtil.keywords(keywords);
060                            descriptions = CustomSQLUtil.keywords(keywords);
061                    }
062                    else {
063                            andOperator = true;
064                    }
065    
066                    return countByC_G_F_N_D(
067                            companyId, groupId, feedIds, names, descriptions, andOperator);
068            }
069    
070            @Override
071            public int countByC_G_F_N_D(
072                            long companyId, long groupId, String feedId, String name,
073                            String description, boolean andOperator)
074                    throws SystemException {
075    
076                    String[] feedIds = CustomSQLUtil.keywords(feedId, false);
077                    String[] names = CustomSQLUtil.keywords(name);
078                    String[] descriptions = CustomSQLUtil.keywords(description);
079    
080                    return countByC_G_F_N_D(
081                            companyId, groupId, feedIds, names, descriptions, andOperator);
082            }
083    
084            @Override
085            public int countByC_G_F_N_D(
086                            long companyId, long groupId, String[] feedIds, String[] names,
087                            String[] descriptions, boolean andOperator)
088                    throws SystemException {
089    
090                    feedIds = CustomSQLUtil.keywords(feedIds, false);
091                    names = CustomSQLUtil.keywords(names);
092                    descriptions = CustomSQLUtil.keywords(descriptions);
093    
094                    Session session = null;
095    
096                    try {
097                            session = openSession();
098    
099                            String sql = CustomSQLUtil.get(COUNT_BY_C_G_F_N_D);
100    
101                            if (groupId <= 0) {
102                                    sql = StringUtil.replace(
103                                            sql, "(groupId = ?) AND", StringPool.BLANK);
104                            }
105    
106                            sql = CustomSQLUtil.replaceKeywords(
107                                    sql, "feedId", StringPool.LIKE, false, feedIds);
108                            sql = CustomSQLUtil.replaceKeywords(
109                                    sql, "lower(name)", StringPool.LIKE, false, names);
110                            sql = CustomSQLUtil.replaceKeywords(
111                                    sql, "lower(description)", StringPool.LIKE, true, descriptions);
112    
113                            sql = CustomSQLUtil.replaceAndOperator(sql, andOperator);
114    
115                            SQLQuery q = session.createSQLQuery(sql);
116    
117                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
118    
119                            QueryPos qPos = QueryPos.getInstance(q);
120    
121                            qPos.add(companyId);
122    
123                            if (groupId > 0) {
124                                    qPos.add(groupId);
125                            }
126    
127                            qPos.add(feedIds, 2);
128                            qPos.add(names, 2);
129                            qPos.add(descriptions, 2);
130    
131                            Iterator<Long> itr = q.iterate();
132    
133                            if (itr.hasNext()) {
134                                    Long count = itr.next();
135    
136                                    if (count != null) {
137                                            return count.intValue();
138                                    }
139                            }
140    
141                            return 0;
142                    }
143                    catch (Exception e) {
144                            throw new SystemException(e);
145                    }
146                    finally {
147                            closeSession(session);
148                    }
149            }
150    
151            @Override
152            public List<JournalFeed> findByKeywords(
153                            long companyId, long groupId, String keywords, int start, int end,
154                            OrderByComparator obc)
155                    throws SystemException {
156    
157                    String[] feedIds = null;
158                    String[] names = null;
159                    String[] descriptions = null;
160                    boolean andOperator = false;
161    
162                    if (Validator.isNotNull(keywords)) {
163                            feedIds = CustomSQLUtil.keywords(keywords, false);
164                            names = CustomSQLUtil.keywords(keywords);
165                            descriptions = CustomSQLUtil.keywords(keywords);
166                    }
167                    else {
168                            andOperator = true;
169                    }
170    
171                    return findByC_G_F_N_D(
172                            companyId, groupId, feedIds, names, descriptions, andOperator,
173                            start, end, obc);
174            }
175    
176            @Override
177            public List<JournalFeed> findByC_G_F_N_D(
178                            long companyId, long groupId, String feedId, String name,
179                            String description, boolean andOperator, int start, int end,
180                            OrderByComparator obc)
181                    throws SystemException {
182    
183                    String[] feedIds = CustomSQLUtil.keywords(feedId, false);
184                    String[] names = CustomSQLUtil.keywords(name);
185                    String[] descriptions = CustomSQLUtil.keywords(description);
186    
187                    return findByC_G_F_N_D(
188                            companyId, groupId, feedIds, names, descriptions, andOperator,
189                            start, end, obc);
190            }
191    
192            @Override
193            public List<JournalFeed> findByC_G_F_N_D(
194                            long companyId, long groupId, String[] feedIds, String[] names,
195                            String[] descriptions, boolean andOperator, int start, int end,
196                            OrderByComparator obc)
197                    throws SystemException {
198    
199                    feedIds = CustomSQLUtil.keywords(feedIds, false);
200                    names = CustomSQLUtil.keywords(names);
201                    descriptions = CustomSQLUtil.keywords(descriptions);
202    
203                    Session session = null;
204    
205                    try {
206                            session = openSession();
207    
208                            String sql = CustomSQLUtil.get(FIND_BY_C_G_F_N_D);
209    
210                            if (groupId <= 0) {
211                                    sql = StringUtil.replace(
212                                            sql, "(groupId = ?) AND", StringPool.BLANK);
213                            }
214    
215                            sql = CustomSQLUtil.replaceKeywords(
216                                    sql, "feedId", StringPool.LIKE, false, feedIds);
217                            sql = CustomSQLUtil.replaceKeywords(
218                                    sql, "lower(name)", StringPool.LIKE, false, names);
219                            sql = CustomSQLUtil.replaceKeywords(
220                                    sql, "lower(description)", StringPool.LIKE, true, descriptions);
221    
222                            sql = CustomSQLUtil.replaceAndOperator(sql, andOperator);
223                            sql = CustomSQLUtil.replaceOrderBy(sql, obc);
224    
225                            SQLQuery q = session.createSQLQuery(sql);
226    
227                            q.addEntity("JournalFeed", JournalFeedImpl.class);
228    
229                            QueryPos qPos = QueryPos.getInstance(q);
230    
231                            qPos.add(companyId);
232    
233                            if (groupId > 0) {
234                                    qPos.add(groupId);
235                            }
236    
237                            qPos.add(feedIds, 2);
238                            qPos.add(names, 2);
239                            qPos.add(descriptions, 2);
240    
241                            return (List<JournalFeed>)QueryUtil.list(
242                                    q, getDialect(), start, end);
243                    }
244                    catch (Exception e) {
245                            throw new SystemException(e);
246                    }
247                    finally {
248                            closeSession(session);
249                    }
250            }
251    
252    }