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