1   /**
2    * Copyright (c) 2000-2008 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.util.OrderByComparator;
27  import com.liferay.portal.kernel.util.StringPool;
28  import com.liferay.portal.kernel.util.StringUtil;
29  import com.liferay.portal.kernel.util.Validator;
30  import com.liferay.portal.spring.hibernate.CustomSQLUtil;
31  import com.liferay.portal.spring.hibernate.HibernateUtil;
32  import com.liferay.portlet.journal.model.impl.JournalFeedImpl;
33  import com.liferay.util.dao.hibernate.QueryPos;
34  import com.liferay.util.dao.hibernate.QueryUtil;
35  
36  import java.util.Iterator;
37  import java.util.List;
38  
39  import org.hibernate.Hibernate;
40  import org.hibernate.SQLQuery;
41  import org.hibernate.Session;
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 implements JournalFeedFinder {
50  
51      public static String COUNT_BY_C_G_F_N_D =
52          JournalFeedFinder.class.getName() + ".countByC_G_F_N_D";
53  
54      public static String FIND_BY_C_G_F_N_D =
55          JournalFeedFinder.class.getName() + ".findByC_G_F_N_D";
56  
57      public int countByKeywords(long companyId, long groupId, String keywords)
58          throws SystemException {
59  
60          String[] feedIds = null;
61          String[] names = null;
62          String[] descriptions = null;
63          boolean andOperator = false;
64  
65          if (Validator.isNotNull(keywords)) {
66              feedIds = CustomSQLUtil.keywords(keywords, false);
67              names = CustomSQLUtil.keywords(keywords);
68              descriptions = CustomSQLUtil.keywords(keywords);
69          }
70          else {
71              andOperator = true;
72          }
73  
74          return countByC_G_F_N_D(
75              companyId, groupId, feedIds, names, descriptions, andOperator);
76      }
77  
78      public int countByC_G_F_N_D(
79              long companyId, long groupId, String feedId, String name,
80              String description, boolean andOperator)
81          throws SystemException {
82  
83          return countByC_G_F_N_D(
84              companyId, groupId, new String[] {feedId}, new String[] {name},
85              new String[] {description}, andOperator);
86      }
87  
88      public int countByC_G_F_N_D(
89              long companyId, long groupId, String[] feedIds, String[] names,
90              String[] descriptions, boolean andOperator)
91          throws SystemException {
92  
93          feedIds = CustomSQLUtil.keywords(feedIds, false);
94          names = CustomSQLUtil.keywords(names);
95          descriptions = CustomSQLUtil.keywords(descriptions);
96  
97          Session session = null;
98  
99          try {
100             session = HibernateUtil.openSession();
101 
102             String sql = CustomSQLUtil.get(COUNT_BY_C_G_F_N_D);
103 
104             if (groupId <= 0) {
105                 sql = StringUtil.replace(sql, "(groupId = ?) AND", "");
106             }
107 
108             sql = CustomSQLUtil.replaceKeywords(
109                 sql, "feedId", StringPool.LIKE, false, feedIds);
110             sql = CustomSQLUtil.replaceKeywords(
111                 sql, "lower(name)", StringPool.LIKE, false, names);
112             sql = CustomSQLUtil.replaceKeywords(
113                 sql, "lower(description)", StringPool.LIKE, true, descriptions);
114 
115             sql = CustomSQLUtil.replaceAndOperator(sql, andOperator);
116 
117             SQLQuery q = session.createSQLQuery(sql);
118 
119             q.addScalar(HibernateUtil.getCountColumnName(), Hibernate.LONG);
120 
121             QueryPos qPos = QueryPos.getInstance(q);
122 
123             qPos.add(companyId);
124 
125             if (groupId > 0) {
126                 qPos.add(groupId);
127             }
128 
129             qPos.add(feedIds, 2);
130             qPos.add(names, 2);
131             qPos.add(descriptions, 2);
132 
133             Iterator itr = q.list().iterator();
134 
135             if (itr.hasNext()) {
136                 Long count = (Long)itr.next();
137 
138                 if (count != null) {
139                     return count.intValue();
140                 }
141             }
142 
143             return 0;
144         }
145         catch (Exception e) {
146             throw new SystemException(e);
147         }
148         finally {
149             HibernateUtil.closeSession(session);
150         }
151     }
152 
153     public List findByKeywords(
154             long companyId, long groupId, String keywords, int begin, int end,
155             OrderByComparator obc)
156         throws SystemException {
157 
158         String[] feedIds = null;
159         String[] names = null;
160         String[] descriptions = null;
161         boolean andOperator = false;
162 
163         if (Validator.isNotNull(keywords)) {
164             feedIds = CustomSQLUtil.keywords(keywords, false);
165             names = CustomSQLUtil.keywords(keywords);
166             descriptions = CustomSQLUtil.keywords(keywords);
167         }
168         else {
169             andOperator = true;
170         }
171 
172         return findByC_G_F_N_D(
173             companyId, groupId, feedIds, names, descriptions, andOperator,
174             begin, end, obc);
175     }
176 
177     public List findByC_G_F_N_D(
178             long companyId, long groupId, String feedId, String name,
179             String description, boolean andOperator, int begin, int end,
180             OrderByComparator obc)
181         throws SystemException {
182 
183         return findByC_G_F_N_D(
184             companyId, groupId, new String[] {feedId}, new String[] {name},
185             new String[] {description}, andOperator, begin, end, obc);
186     }
187 
188     public List findByC_G_F_N_D(
189             long companyId, long groupId, String[] feedIds, String[] names,
190             String[] descriptions, boolean andOperator, int begin, int end,
191             OrderByComparator obc)
192         throws SystemException {
193 
194         feedIds = CustomSQLUtil.keywords(feedIds, false);
195         names = CustomSQLUtil.keywords(names);
196         descriptions = CustomSQLUtil.keywords(descriptions);
197 
198         Session session = null;
199 
200         try {
201             session = HibernateUtil.openSession();
202 
203             String sql = CustomSQLUtil.get(FIND_BY_C_G_F_N_D);
204 
205             if (groupId <= 0) {
206                 sql = StringUtil.replace(sql, "(groupId = ?) AND", "");
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 QueryUtil.list(q, HibernateUtil.getDialect(), begin, end);
236         }
237         catch (Exception e) {
238             throw new SystemException(e);
239         }
240         finally {
241             HibernateUtil.closeSession(session);
242         }
243     }
244 
245 }