1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.journal.service.persistence;
24  
25  import com.liferay.portal.SystemException;
26  import com.liferay.portal.kernel.dao.orm.QueryPos;
27  import com.liferay.portal.kernel.dao.orm.QueryUtil;
28  import com.liferay.portal.kernel.dao.orm.SQLQuery;
29  import com.liferay.portal.kernel.dao.orm.Session;
30  import com.liferay.portal.kernel.dao.orm.Type;
31  import com.liferay.portal.kernel.util.OrderByComparator;
32  import com.liferay.portal.kernel.util.StringPool;
33  import com.liferay.portal.kernel.util.StringUtil;
34  import com.liferay.portal.kernel.util.Validator;
35  import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
36  import com.liferay.portlet.journal.model.JournalFeed;
37  import com.liferay.portlet.journal.model.impl.JournalFeedImpl;
38  import com.liferay.util.dao.orm.CustomSQLUtil;
39  
40  import java.util.Iterator;
41  import java.util.List;
42  
43  /**
44   * <a href="JournalFeedFinderImpl.java.html"><b><i>View Source</i></b></a>
45   *
46   * @author Raymond Augé
47   *
48   */
49  public class JournalFeedFinderImpl
50      extends BasePersistenceImpl implements JournalFeedFinder {
51  
52      public static String COUNT_BY_C_G_F_N_D =
53          JournalFeedFinder.class.getName() + ".countByC_G_F_N_D";
54  
55      public static String FIND_BY_C_G_F_N_D =
56          JournalFeedFinder.class.getName() + ".findByC_G_F_N_D";
57  
58      public int countByKeywords(long companyId, long groupId, String keywords)
59          throws SystemException {
60  
61          String[] feedIds = null;
62          String[] names = null;
63          String[] descriptions = null;
64          boolean andOperator = false;
65  
66          if (Validator.isNotNull(keywords)) {
67              feedIds = CustomSQLUtil.keywords(keywords, false);
68              names = CustomSQLUtil.keywords(keywords);
69              descriptions = CustomSQLUtil.keywords(keywords);
70          }
71          else {
72              andOperator = true;
73          }
74  
75          return countByC_G_F_N_D(
76              companyId, groupId, feedIds, names, descriptions, andOperator);
77      }
78  
79      public int countByC_G_F_N_D(
80              long companyId, long groupId, String feedId, String name,
81              String description, boolean andOperator)
82          throws SystemException {
83  
84          return countByC_G_F_N_D(
85              companyId, groupId, new String[] {feedId}, new String[] {name},
86              new String[] {description}, andOperator);
87      }
88  
89      public int countByC_G_F_N_D(
90              long companyId, long groupId, String[] feedIds, String[] names,
91              String[] descriptions, boolean andOperator)
92          throws SystemException {
93  
94          feedIds = CustomSQLUtil.keywords(feedIds, false);
95          names = CustomSQLUtil.keywords(names);
96          descriptions = CustomSQLUtil.keywords(descriptions);
97  
98          Session session = null;
99  
100         try {
101             session = openSession();
102 
103             String sql = CustomSQLUtil.get(COUNT_BY_C_G_F_N_D);
104 
105             if (groupId <= 0) {
106                 sql = StringUtil.replace(sql, "(groupId = ?) AND", "");
107             }
108 
109             sql = CustomSQLUtil.replaceKeywords(
110                 sql, "feedId", StringPool.LIKE, false, feedIds);
111             sql = CustomSQLUtil.replaceKeywords(
112                 sql, "lower(name)", StringPool.LIKE, false, names);
113             sql = CustomSQLUtil.replaceKeywords(
114                 sql, "lower(description)", StringPool.LIKE, true, descriptions);
115 
116             sql = CustomSQLUtil.replaceAndOperator(sql, andOperator);
117 
118             SQLQuery q = session.createSQLQuery(sql);
119 
120             q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
121 
122             QueryPos qPos = QueryPos.getInstance(q);
123 
124             qPos.add(companyId);
125 
126             if (groupId > 0) {
127                 qPos.add(groupId);
128             }
129 
130             qPos.add(feedIds, 2);
131             qPos.add(names, 2);
132             qPos.add(descriptions, 2);
133 
134             Iterator<Long> itr = q.list().iterator();
135 
136             if (itr.hasNext()) {
137                 Long count = itr.next();
138 
139                 if (count != null) {
140                     return count.intValue();
141                 }
142             }
143 
144             return 0;
145         }
146         catch (Exception e) {
147             throw new SystemException(e);
148         }
149         finally {
150             closeSession(session);
151         }
152     }
153 
154     public List<JournalFeed> findByKeywords(
155             long companyId, long groupId, String keywords, int start, int end,
156             OrderByComparator obc)
157         throws SystemException {
158 
159         String[] feedIds = null;
160         String[] names = null;
161         String[] descriptions = null;
162         boolean andOperator = false;
163 
164         if (Validator.isNotNull(keywords)) {
165             feedIds = CustomSQLUtil.keywords(keywords, false);
166             names = CustomSQLUtil.keywords(keywords);
167             descriptions = CustomSQLUtil.keywords(keywords);
168         }
169         else {
170             andOperator = true;
171         }
172 
173         return findByC_G_F_N_D(
174             companyId, groupId, feedIds, names, descriptions, andOperator,
175             start, end, obc);
176     }
177 
178     public List<JournalFeed> findByC_G_F_N_D(
179             long companyId, long groupId, String feedId, String name,
180             String description, boolean andOperator, int start, int end,
181             OrderByComparator obc)
182         throws SystemException {
183 
184         return findByC_G_F_N_D(
185             companyId, groupId, new String[] {feedId}, new String[] {name},
186             new String[] {description}, andOperator, start, end, obc);
187     }
188 
189     public List<JournalFeed> findByC_G_F_N_D(
190             long companyId, long groupId, String[] feedIds, String[] names,
191             String[] descriptions, boolean andOperator, int start, int end,
192             OrderByComparator obc)
193         throws SystemException {
194 
195         feedIds = CustomSQLUtil.keywords(feedIds, false);
196         names = CustomSQLUtil.keywords(names);
197         descriptions = CustomSQLUtil.keywords(descriptions);
198 
199         Session session = null;
200 
201         try {
202             session = openSession();
203 
204             String sql = CustomSQLUtil.get(FIND_BY_C_G_F_N_D);
205 
206             if (groupId <= 0) {
207                 sql = StringUtil.replace(sql, "(groupId = ?) AND", "");
208             }
209 
210             sql = CustomSQLUtil.replaceKeywords(
211                 sql, "feedId", StringPool.LIKE, false, feedIds);
212             sql = CustomSQLUtil.replaceKeywords(
213                 sql, "lower(name)", StringPool.LIKE, false, names);
214             sql = CustomSQLUtil.replaceKeywords(
215                 sql, "lower(description)", StringPool.LIKE, true, descriptions);
216 
217             sql = CustomSQLUtil.replaceAndOperator(sql, andOperator);
218             sql = CustomSQLUtil.replaceOrderBy(sql, obc);
219 
220             SQLQuery q = session.createSQLQuery(sql);
221 
222             q.addEntity("JournalFeed", JournalFeedImpl.class);
223 
224             QueryPos qPos = QueryPos.getInstance(q);
225 
226             qPos.add(companyId);
227 
228             if (groupId > 0) {
229                 qPos.add(groupId);
230             }
231 
232             qPos.add(feedIds, 2);
233             qPos.add(names, 2);
234             qPos.add(descriptions, 2);
235 
236             return (List<JournalFeed>)QueryUtil.list(
237                 q, getDialect(), start, end);
238         }
239         catch (Exception e) {
240             throw new SystemException(e);
241         }
242         finally {
243             closeSession(session);
244         }
245     }
246 
247 }