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