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