1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portlet.journal.service.persistence;
16  
17  import com.liferay.portal.SystemException;
18  import com.liferay.portal.kernel.dao.orm.QueryPos;
19  import com.liferay.portal.kernel.dao.orm.QueryUtil;
20  import com.liferay.portal.kernel.dao.orm.SQLQuery;
21  import com.liferay.portal.kernel.dao.orm.Session;
22  import com.liferay.portal.kernel.dao.orm.Type;
23  import com.liferay.portal.kernel.util.OrderByComparator;
24  import com.liferay.portal.kernel.util.StringPool;
25  import com.liferay.portal.kernel.util.StringUtil;
26  import com.liferay.portal.kernel.util.Validator;
27  import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
28  import com.liferay.portlet.journal.model.JournalFeed;
29  import com.liferay.portlet.journal.model.impl.JournalFeedImpl;
30  import com.liferay.util.dao.orm.CustomSQLUtil;
31  
32  import java.util.Iterator;
33  import java.util.List;
34  
35  /**
36   * <a href="JournalFeedFinderImpl.java.html"><b><i>View Source</i></b></a>
37   *
38   * @author Raymond Augé
39   */
40  public class JournalFeedFinderImpl
41      extends BasePersistenceImpl<JournalFeed> implements JournalFeedFinder {
42  
43      public static String COUNT_BY_C_G_F_N_D =
44          JournalFeedFinder.class.getName() + ".countByC_G_F_N_D";
45  
46      public static String FIND_BY_C_G_F_N_D =
47          JournalFeedFinder.class.getName() + ".findByC_G_F_N_D";
48  
49      public int countByKeywords(long companyId, long groupId, String keywords)
50          throws SystemException {
51  
52          String[] feedIds = null;
53          String[] names = null;
54          String[] descriptions = null;
55          boolean andOperator = false;
56  
57          if (Validator.isNotNull(keywords)) {
58              feedIds = CustomSQLUtil.keywords(keywords, false);
59              names = CustomSQLUtil.keywords(keywords);
60              descriptions = CustomSQLUtil.keywords(keywords);
61          }
62          else {
63              andOperator = true;
64          }
65  
66          return countByC_G_F_N_D(
67              companyId, groupId, feedIds, names, descriptions, andOperator);
68      }
69  
70      public int countByC_G_F_N_D(
71              long companyId, long groupId, String feedId, String name,
72              String description, boolean andOperator)
73          throws SystemException {
74  
75          return countByC_G_F_N_D(
76              companyId, groupId, new String[] {feedId}, new String[] {name},
77              new String[] {description}, andOperator);
78      }
79  
80      public int countByC_G_F_N_D(
81              long companyId, long groupId, String[] feedIds, String[] names,
82              String[] descriptions, boolean andOperator)
83          throws SystemException {
84  
85          feedIds = CustomSQLUtil.keywords(feedIds, false);
86          names = CustomSQLUtil.keywords(names);
87          descriptions = CustomSQLUtil.keywords(descriptions);
88  
89          Session session = null;
90  
91          try {
92              session = openSession();
93  
94              String sql = CustomSQLUtil.get(COUNT_BY_C_G_F_N_D);
95  
96              if (groupId <= 0) {
97                  sql = StringUtil.replace(sql, "(groupId = ?) AND", "");
98              }
99  
100             sql = CustomSQLUtil.replaceKeywords(
101                 sql, "feedId", StringPool.LIKE, false, feedIds);
102             sql = CustomSQLUtil.replaceKeywords(
103                 sql, "lower(name)", StringPool.LIKE, false, names);
104             sql = CustomSQLUtil.replaceKeywords(
105                 sql, "lower(description)", StringPool.LIKE, true, descriptions);
106 
107             sql = CustomSQLUtil.replaceAndOperator(sql, andOperator);
108 
109             SQLQuery q = session.createSQLQuery(sql);
110 
111             q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
112 
113             QueryPos qPos = QueryPos.getInstance(q);
114 
115             qPos.add(companyId);
116 
117             if (groupId > 0) {
118                 qPos.add(groupId);
119             }
120 
121             qPos.add(feedIds, 2);
122             qPos.add(names, 2);
123             qPos.add(descriptions, 2);
124 
125             Iterator<Long> itr = q.list().iterator();
126 
127             if (itr.hasNext()) {
128                 Long count = itr.next();
129 
130                 if (count != null) {
131                     return count.intValue();
132                 }
133             }
134 
135             return 0;
136         }
137         catch (Exception e) {
138             throw new SystemException(e);
139         }
140         finally {
141             closeSession(session);
142         }
143     }
144 
145     public List<JournalFeed> findByKeywords(
146             long companyId, long groupId, String keywords, int start, int end,
147             OrderByComparator obc)
148         throws SystemException {
149 
150         String[] feedIds = null;
151         String[] names = null;
152         String[] descriptions = null;
153         boolean andOperator = false;
154 
155         if (Validator.isNotNull(keywords)) {
156             feedIds = CustomSQLUtil.keywords(keywords, false);
157             names = CustomSQLUtil.keywords(keywords);
158             descriptions = CustomSQLUtil.keywords(keywords);
159         }
160         else {
161             andOperator = true;
162         }
163 
164         return findByC_G_F_N_D(
165             companyId, groupId, feedIds, names, descriptions, andOperator,
166             start, end, obc);
167     }
168 
169     public List<JournalFeed> findByC_G_F_N_D(
170             long companyId, long groupId, String feedId, String name,
171             String description, boolean andOperator, int start, int end,
172             OrderByComparator obc)
173         throws SystemException {
174 
175         return findByC_G_F_N_D(
176             companyId, groupId, new String[] {feedId}, new String[] {name},
177             new String[] {description}, andOperator, start, end, obc);
178     }
179 
180     public List<JournalFeed> findByC_G_F_N_D(
181             long companyId, long groupId, String[] feedIds, String[] names,
182             String[] descriptions, boolean andOperator, int start, int end,
183             OrderByComparator obc)
184         throws SystemException {
185 
186         feedIds = CustomSQLUtil.keywords(feedIds, false);
187         names = CustomSQLUtil.keywords(names);
188         descriptions = CustomSQLUtil.keywords(descriptions);
189 
190         Session session = null;
191 
192         try {
193             session = openSession();
194 
195             String sql = CustomSQLUtil.get(FIND_BY_C_G_F_N_D);
196 
197             if (groupId <= 0) {
198                 sql = StringUtil.replace(sql, "(groupId = ?) AND", "");
199             }
200 
201             sql = CustomSQLUtil.replaceKeywords(
202                 sql, "feedId", StringPool.LIKE, false, feedIds);
203             sql = CustomSQLUtil.replaceKeywords(
204                 sql, "lower(name)", StringPool.LIKE, false, names);
205             sql = CustomSQLUtil.replaceKeywords(
206                 sql, "lower(description)", StringPool.LIKE, true, descriptions);
207 
208             sql = CustomSQLUtil.replaceAndOperator(sql, andOperator);
209             sql = CustomSQLUtil.replaceOrderBy(sql, obc);
210 
211             SQLQuery q = session.createSQLQuery(sql);
212 
213             q.addEntity("JournalFeed", JournalFeedImpl.class);
214 
215             QueryPos qPos = QueryPos.getInstance(q);
216 
217             qPos.add(companyId);
218 
219             if (groupId > 0) {
220                 qPos.add(groupId);
221             }
222 
223             qPos.add(feedIds, 2);
224             qPos.add(names, 2);
225             qPos.add(descriptions, 2);
226 
227             return (List<JournalFeed>)QueryUtil.list(
228                 q, getDialect(), start, end);
229         }
230         catch (Exception e) {
231             throw new SystemException(e);
232         }
233         finally {
234             closeSession(session);
235         }
236     }
237 
238 }