1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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  public class JournalFeedFinderImpl
49      extends BasePersistenceImpl 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 = 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(COUNT_COLUMN_NAME, Type.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<Long> itr = q.list().iterator();
134 
135             if (itr.hasNext()) {
136                 Long count = 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             closeSession(session);
150         }
151     }
152 
153     public List<JournalFeed> findByKeywords(
154             long companyId, long groupId, String keywords, int start, 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             start, end, obc);
175     }
176 
177     public List<JournalFeed> findByC_G_F_N_D(
178             long companyId, long groupId, String feedId, String name,
179             String description, boolean andOperator, int start, 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, start, end, obc);
186     }
187 
188     public List<JournalFeed> findByC_G_F_N_D(
189             long companyId, long groupId, String[] feedIds, String[] names,
190             String[] descriptions, boolean andOperator, int start, 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 = 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 (List<JournalFeed>)QueryUtil.list(
236                 q, getDialect(), start, end);
237         }
238         catch (Exception e) {
239             throw new SystemException(e);
240         }
241         finally {
242             closeSession(session);
243         }
244     }
245 
246 }