001    /**
002     * Copyright (c) 2000-2012 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(sql, "(groupId = ?) AND", "");
100                            }
101    
102                            sql = CustomSQLUtil.replaceKeywords(
103                                    sql, "feedId", StringPool.LIKE, false, feedIds);
104                            sql = CustomSQLUtil.replaceKeywords(
105                                    sql, "lower(name)", StringPool.LIKE, false, names);
106                            sql = CustomSQLUtil.replaceKeywords(
107                                    sql, "lower(description)", StringPool.LIKE, true, descriptions);
108    
109                            sql = CustomSQLUtil.replaceAndOperator(sql, andOperator);
110    
111                            SQLQuery q = session.createSQLQuery(sql);
112    
113                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
114    
115                            QueryPos qPos = QueryPos.getInstance(q);
116    
117                            qPos.add(companyId);
118    
119                            if (groupId > 0) {
120                                    qPos.add(groupId);
121                            }
122    
123                            qPos.add(feedIds, 2);
124                            qPos.add(names, 2);
125                            qPos.add(descriptions, 2);
126    
127                            Iterator<Long> itr = q.iterate();
128    
129                            if (itr.hasNext()) {
130                                    Long count = itr.next();
131    
132                                    if (count != null) {
133                                            return count.intValue();
134                                    }
135                            }
136    
137                            return 0;
138                    }
139                    catch (Exception e) {
140                            throw new SystemException(e);
141                    }
142                    finally {
143                            closeSession(session);
144                    }
145            }
146    
147            public List<JournalFeed> findByKeywords(
148                            long companyId, long groupId, String keywords, int start, int end,
149                            OrderByComparator obc)
150                    throws SystemException {
151    
152                    String[] feedIds = null;
153                    String[] names = null;
154                    String[] descriptions = null;
155                    boolean andOperator = false;
156    
157                    if (Validator.isNotNull(keywords)) {
158                            feedIds = CustomSQLUtil.keywords(keywords, false);
159                            names = CustomSQLUtil.keywords(keywords);
160                            descriptions = CustomSQLUtil.keywords(keywords);
161                    }
162                    else {
163                            andOperator = true;
164                    }
165    
166                    return findByC_G_F_N_D(
167                            companyId, groupId, feedIds, names, descriptions, andOperator,
168                            start, end, obc);
169            }
170    
171            public List<JournalFeed> findByC_G_F_N_D(
172                            long companyId, long groupId, String feedId, String name,
173                            String description, boolean andOperator, int start, int end,
174                            OrderByComparator obc)
175                    throws SystemException {
176    
177                    String[] feedIds = CustomSQLUtil.keywords(feedId, false);
178                    String[] names = CustomSQLUtil.keywords(name);
179                    String[] descriptions = CustomSQLUtil.keywords(description);
180    
181                    return findByC_G_F_N_D(
182                            companyId, groupId, feedIds, names, descriptions, andOperator,
183                            start, end, obc);
184            }
185    
186            public List<JournalFeed> findByC_G_F_N_D(
187                            long companyId, long groupId, String[] feedIds, String[] names,
188                            String[] descriptions, boolean andOperator, int start, int end,
189                            OrderByComparator obc)
190                    throws SystemException {
191    
192                    feedIds = CustomSQLUtil.keywords(feedIds, false);
193                    names = CustomSQLUtil.keywords(names);
194                    descriptions = CustomSQLUtil.keywords(descriptions);
195    
196                    Session session = null;
197    
198                    try {
199                            session = openSession();
200    
201                            String sql = CustomSQLUtil.get(FIND_BY_C_G_F_N_D);
202    
203                            if (groupId <= 0) {
204                                    sql = StringUtil.replace(sql, "(groupId = ?) AND", "");
205                            }
206    
207                            sql = CustomSQLUtil.replaceKeywords(
208                                    sql, "feedId", StringPool.LIKE, false, feedIds);
209                            sql = CustomSQLUtil.replaceKeywords(
210                                    sql, "lower(name)", StringPool.LIKE, false, names);
211                            sql = CustomSQLUtil.replaceKeywords(
212                                    sql, "lower(description)", StringPool.LIKE, true, descriptions);
213    
214                            sql = CustomSQLUtil.replaceAndOperator(sql, andOperator);
215                            sql = CustomSQLUtil.replaceOrderBy(sql, obc);
216    
217                            SQLQuery q = session.createSQLQuery(sql);
218    
219                            q.addEntity("JournalFeed", JournalFeedImpl.class);
220    
221                            QueryPos qPos = QueryPos.getInstance(q);
222    
223                            qPos.add(companyId);
224    
225                            if (groupId > 0) {
226                                    qPos.add(groupId);
227                            }
228    
229                            qPos.add(feedIds, 2);
230                            qPos.add(names, 2);
231                            qPos.add(descriptions, 2);
232    
233                            return (List<JournalFeed>)QueryUtil.list(
234                                    q, getDialect(), start, end);
235                    }
236                    catch (Exception e) {
237                            throw new SystemException(e);
238                    }
239                    finally {
240                            closeSession(session);
241                    }
242            }
243    
244    }