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.CalendarUtil;
24  import com.liferay.portal.kernel.util.OrderByComparator;
25  import com.liferay.portal.kernel.util.StringPool;
26  import com.liferay.portal.kernel.util.StringUtil;
27  import com.liferay.portal.kernel.util.Validator;
28  import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
29  import com.liferay.portlet.journal.model.JournalArticle;
30  import com.liferay.portlet.journal.model.impl.JournalArticleImpl;
31  import com.liferay.util.dao.orm.CustomSQLUtil;
32  
33  import java.sql.Timestamp;
34  
35  import java.util.Date;
36  import java.util.Iterator;
37  import java.util.List;
38  
39  /**
40   * <a href="JournalArticleFinderImpl.java.html"><b><i>View Source</i></b></a>
41   *
42   * @author Brian Wing Shun Chan
43   */
44  public class JournalArticleFinderImpl
45      extends BasePersistenceImpl<JournalArticle>
46      implements JournalArticleFinder {
47  
48      public static String COUNT_BY_C_G_A_V_T_D_C_T_S_T_D_A_E_R =
49          JournalArticleFinder.class.getName() +
50              ".countByC_G_A_V_T_D_C_T_S_T_D_A_E_R";
51  
52      public static String FIND_BY_EXPIRATION_DATE =
53          JournalArticleFinder.class.getName() + ".findByExpirationDate";
54  
55      public static String FIND_BY_REVIEW_DATE =
56          JournalArticleFinder.class.getName() + ".findByReviewDate";
57  
58      public static String FIND_BY_C_G_A_V_T_D_C_T_S_T_D_A_E_R =
59          JournalArticleFinder.class.getName() +
60              ".findByC_G_A_V_T_D_C_T_S_T_D_A_E_R";
61  
62      public int countByKeywords(
63              long companyId, long groupId, String keywords, Double version,
64              String type, String structureId, String templateId,
65              Date displayDateGT, Date displayDateLT, Boolean approved,
66              Boolean expired, Date reviewDate)
67          throws SystemException {
68  
69          String[] articleIds = null;
70          String[] titles = null;
71          String[] descriptions = null;
72          String[] contents = null;
73          boolean andOperator = false;
74  
75          if (Validator.isNotNull(keywords)) {
76              articleIds = CustomSQLUtil.keywords(keywords, false);
77              titles = CustomSQLUtil.keywords(keywords);
78              descriptions = CustomSQLUtil.keywords(keywords, false);
79              contents = CustomSQLUtil.keywords(keywords, false);
80          }
81          else {
82              andOperator = true;
83          }
84  
85          return countByC_G_A_V_T_D_C_T_S_T_D_A_E_R(
86              companyId, groupId, articleIds, version, titles, descriptions,
87              contents, type, new String[] {structureId},
88              new String[] {templateId}, displayDateGT, displayDateLT, approved,
89              expired, reviewDate, andOperator);
90      }
91  
92      public int countByC_G_A_V_T_D_C_T_S_T_D_A_E_R(
93              long companyId, long groupId, String articleId, Double version,
94              String title, String description, String content, String type,
95              String structureId, String templateId, Date displayDateGT,
96              Date displayDateLT, Boolean approved, Boolean expired,
97              Date reviewDate, boolean andOperator)
98          throws SystemException {
99  
100         return countByC_G_A_V_T_D_C_T_S_T_D_A_E_R(
101             companyId, groupId, articleId, version, title, description,
102             content, type, new String[] {structureId},
103             new String[] {templateId}, displayDateGT, displayDateLT, approved,
104             expired, reviewDate, andOperator);
105     }
106 
107     public int countByC_G_A_V_T_D_C_T_S_T_D_A_E_R(
108             long companyId, long groupId, String articleId, Double version,
109             String title, String description, String content, String type,
110             String[] structureIds, String[] templateIds, Date displayDateGT,
111             Date displayDateLT, Boolean approved, Boolean expired,
112             Date reviewDate, boolean andOperator)
113         throws SystemException {
114 
115         return countByC_G_A_V_T_D_C_T_S_T_D_A_E_R(
116             companyId, groupId, new String[] {articleId}, version,
117             new String[] {title}, new String[] {description},
118             new String[] {content}, type, structureIds, templateIds,
119             displayDateGT, displayDateLT, approved, expired, reviewDate,
120             andOperator);
121     }
122 
123     public int countByC_G_A_V_T_D_C_T_S_T_D_A_E_R(
124             long companyId, long groupId, String[] articleIds, Double version,
125             String[] titles, String[] descriptions, String[] contents,
126             String type, String[] structureIds, String[] templateIds,
127             Date displayDateGT, Date displayDateLT, Boolean approved,
128             Boolean expired, Date reviewDate, boolean andOperator)
129         throws SystemException {
130 
131         articleIds = CustomSQLUtil.keywords(articleIds, false);
132         titles = CustomSQLUtil.keywords(titles);
133         descriptions = CustomSQLUtil.keywords(descriptions, false);
134         contents = CustomSQLUtil.keywords(contents, false);
135         structureIds = CustomSQLUtil.keywords(structureIds, false);
136         templateIds = CustomSQLUtil.keywords(templateIds, false);
137         Timestamp displayDateGT_TS = CalendarUtil.getTimestamp(displayDateGT);
138         Timestamp displayDateLT_TS = CalendarUtil.getTimestamp(displayDateLT);
139         Timestamp reviewDate_TS = CalendarUtil.getTimestamp(reviewDate);
140 
141         Session session = null;
142 
143         try {
144             session = openSession();
145 
146             String sql = CustomSQLUtil.get(
147                 COUNT_BY_C_G_A_V_T_D_C_T_S_T_D_A_E_R);
148 
149             if (groupId <= 0) {
150                 sql = StringUtil.replace(sql, "(groupId = ?) AND", "");
151             }
152 
153             sql = CustomSQLUtil.replaceKeywords(
154                 sql, "articleId", StringPool.LIKE, false, articleIds);
155 
156             if (version == null) {
157                 sql = StringUtil.replace(
158                     sql, "(version = ?) [$AND_OR_CONNECTOR$]", "");
159             }
160 
161             sql = CustomSQLUtil.replaceKeywords(
162                 sql, "lower(title)", StringPool.LIKE, false, titles);
163             sql = CustomSQLUtil.replaceKeywords(
164                 sql, "description", StringPool.LIKE, false, descriptions);
165             sql = CustomSQLUtil.replaceKeywords(
166                 sql, "content", StringPool.LIKE, false, contents);
167             sql = CustomSQLUtil.replaceKeywords(
168                 sql, "structureId", StringPool.EQUAL, false, structureIds);
169             sql = CustomSQLUtil.replaceKeywords(
170                 sql, "templateId", StringPool.EQUAL, false, templateIds);
171 
172             if (approved == null) {
173                 sql = StringUtil.replace(sql, "(approved = ?) AND", "");
174             }
175 
176             if (expired == null) {
177                 sql = StringUtil.replace(sql, "(expired = ?) AND", "");
178             }
179 
180             sql = CustomSQLUtil.replaceAndOperator(sql, andOperator);
181 
182             SQLQuery q = session.createSQLQuery(sql);
183 
184             q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
185 
186             QueryPos qPos = QueryPos.getInstance(q);
187 
188             qPos.add(companyId);
189 
190             if (groupId > 0) {
191                 qPos.add(groupId);
192             }
193 
194             qPos.add(articleIds, 2);
195 
196             if (version != null) {
197                 qPos.add(version);
198             }
199 
200             qPos.add(titles, 2);
201             qPos.add(descriptions, 2);
202             qPos.add(contents, 2);
203             qPos.add(type);
204             qPos.add(type);
205             qPos.add(structureIds, 2);
206             qPos.add(templateIds, 2);
207             qPos.add(displayDateGT_TS);
208             qPos.add(displayDateGT_TS);
209             qPos.add(displayDateLT_TS);
210             qPos.add(displayDateLT_TS);
211 
212             if (approved != null) {
213                 qPos.add(approved);
214             }
215 
216             if (expired != null) {
217                 qPos.add(expired);
218             }
219 
220             qPos.add(reviewDate_TS);
221             qPos.add(reviewDate_TS);
222 
223             Iterator<Long> itr = q.list().iterator();
224 
225             if (itr.hasNext()) {
226                 Long count = itr.next();
227 
228                 if (count != null) {
229                     return count.intValue();
230                 }
231             }
232 
233             return 0;
234         }
235         catch (Exception e) {
236             throw new SystemException(e);
237         }
238         finally {
239             closeSession(session);
240         }
241     }
242 
243     public List<JournalArticle> findByExpirationDate(
244             Boolean expired, Date expirationDateLT, Date expirationDateGT)
245         throws SystemException {
246 
247         Timestamp expirationDateLT_TS = CalendarUtil.getTimestamp(
248             expirationDateLT);
249         Timestamp expirationDateGT_TS = CalendarUtil.getTimestamp(
250             expirationDateGT);
251 
252         Session session = null;
253         try {
254             session = openSession();
255 
256             String sql = CustomSQLUtil.get(FIND_BY_EXPIRATION_DATE);
257 
258             if (expired == null) {
259                 sql = StringUtil.replace(sql, "(expired = ?) AND", "");
260             }
261 
262             SQLQuery q = session.createSQLQuery(sql);
263 
264             q.addEntity("JournalArticle", JournalArticleImpl.class);
265 
266             QueryPos qPos = QueryPos.getInstance(q);
267 
268             if (expired != null) {
269                 qPos.add(expired);
270             }
271 
272             qPos.add(expirationDateGT_TS);
273             qPos.add(expirationDateLT_TS);
274 
275             return q.list();
276         }
277         catch (Exception e) {
278             throw new SystemException(e);
279         }
280         finally {
281             closeSession(session);
282         }
283     }
284 
285     public List<JournalArticle> findByKeywords(
286             long companyId, long groupId, String keywords, Double version,
287             String type, String structureId, String templateId,
288             Date displayDateGT, Date displayDateLT, Boolean approved,
289             Boolean expired, Date reviewDate, int start, int end,
290             OrderByComparator obc)
291         throws SystemException {
292 
293         String[] articleIds = null;
294         String[] titles = null;
295         String[] descriptions = null;
296         String[] contents = null;
297         boolean andOperator = false;
298 
299         if (Validator.isNotNull(keywords)) {
300             articleIds = CustomSQLUtil.keywords(keywords, false);
301             titles = CustomSQLUtil.keywords(keywords);
302             descriptions = CustomSQLUtil.keywords(keywords, false);
303             contents = CustomSQLUtil.keywords(keywords, false);
304         }
305         else {
306             andOperator = true;
307         }
308 
309         return findByC_G_A_V_T_D_C_T_S_T_D_A_E_R(
310             companyId, groupId, articleIds, version, titles, descriptions,
311             contents, type, new String[] {structureId},
312             new String[] {templateId}, displayDateGT, displayDateLT, approved,
313             expired, reviewDate, andOperator, start, end, obc);
314     }
315 
316     public List<JournalArticle> findByReviewDate(
317             Date reviewDateLT, Date reviewDateGT)
318         throws SystemException {
319 
320         Timestamp reviewDateLT_TS = CalendarUtil.getTimestamp(reviewDateLT);
321         Timestamp reviewDateGT_TS = CalendarUtil.getTimestamp(reviewDateGT);
322 
323         Session session = null;
324         try {
325             session = openSession();
326 
327             String sql = CustomSQLUtil.get(FIND_BY_REVIEW_DATE);
328 
329             SQLQuery q = session.createSQLQuery(sql);
330 
331             q.addEntity("JournalArticle", JournalArticleImpl.class);
332 
333             QueryPos qPos = QueryPos.getInstance(q);
334 
335             qPos.add(reviewDateGT_TS);
336             qPos.add(reviewDateLT_TS);
337 
338             return q.list();
339         }
340         catch (Exception e) {
341             throw new SystemException(e);
342         }
343         finally {
344             closeSession(session);
345         }
346     }
347 
348     public List<JournalArticle> findByC_G_A_V_T_D_C_T_S_T_D_A_E_R(
349             long companyId, long groupId, String articleId, Double version,
350             String title, String description, String content, String type,
351             String structureId, String templateId, Date displayDateGT,
352             Date displayDateLT, Boolean approved, Boolean expired,
353             Date reviewDate, boolean andOperator, int start, int end,
354             OrderByComparator obc)
355         throws SystemException {
356 
357         return findByC_G_A_V_T_D_C_T_S_T_D_A_E_R(
358             companyId, groupId, articleId, version, title, description,
359             content, type, new String[] {structureId},
360             new String[] {templateId}, displayDateGT, displayDateLT, approved,
361             expired, reviewDate, andOperator, start, end, obc);
362     }
363 
364     public List<JournalArticle> findByC_G_A_V_T_D_C_T_S_T_D_A_E_R(
365             long companyId, long groupId, String articleId, Double version,
366             String title, String description, String content, String type,
367             String[] structureIds, String[] templateIds, Date displayDateGT,
368             Date displayDateLT, Boolean approved, Boolean expired,
369             Date reviewDate, boolean andOperator, int start, int end,
370             OrderByComparator obc)
371         throws SystemException {
372 
373         return findByC_G_A_V_T_D_C_T_S_T_D_A_E_R(
374             companyId, groupId, new String[] {articleId}, version,
375             new String[] {title}, new String[] {description},
376             new String[] {content}, type, structureIds, templateIds,
377             displayDateGT, displayDateLT, approved, expired, reviewDate,
378             andOperator, start, end, obc);
379     }
380 
381     public List<JournalArticle> findByC_G_A_V_T_D_C_T_S_T_D_A_E_R(
382             long companyId, long groupId, String[] articleIds, Double version,
383             String[] titles, String[] descriptions, String[] contents,
384             String type, String[] structureIds, String[] templateIds,
385             Date displayDateGT, Date displayDateLT, Boolean approved,
386             Boolean expired, Date reviewDate, boolean andOperator, int start,
387             int end, OrderByComparator obc)
388         throws SystemException {
389 
390         articleIds = CustomSQLUtil.keywords(articleIds, false);
391         titles = CustomSQLUtil.keywords(titles);
392         descriptions = CustomSQLUtil.keywords(descriptions, false);
393         contents = CustomSQLUtil.keywords(contents, false);
394         structureIds = CustomSQLUtil.keywords(structureIds, false);
395         templateIds = CustomSQLUtil.keywords(templateIds, false);
396         Timestamp displayDateGT_TS = CalendarUtil.getTimestamp(displayDateGT);
397         Timestamp displayDateLT_TS = CalendarUtil.getTimestamp(displayDateLT);
398         Timestamp reviewDate_TS = CalendarUtil.getTimestamp(reviewDate);
399 
400         Session session = null;
401 
402         try {
403             session = openSession();
404 
405             String sql = CustomSQLUtil.get(FIND_BY_C_G_A_V_T_D_C_T_S_T_D_A_E_R);
406 
407             if (groupId <= 0) {
408                 sql = StringUtil.replace(sql, "(groupId = ?) AND", "");
409             }
410 
411             sql = CustomSQLUtil.replaceKeywords(
412                 sql, "articleId", StringPool.LIKE, false, articleIds);
413 
414             if (version == null) {
415                 sql = StringUtil.replace(
416                     sql, "(version = ?) [$AND_OR_CONNECTOR$]", "");
417             }
418 
419             sql = CustomSQLUtil.replaceKeywords(
420                 sql, "lower(title)", StringPool.LIKE, false, titles);
421             sql = CustomSQLUtil.replaceKeywords(
422                 sql, "description", StringPool.LIKE, false, descriptions);
423             sql = CustomSQLUtil.replaceKeywords(
424                 sql, "content", StringPool.LIKE, false, contents);
425             sql = CustomSQLUtil.replaceKeywords(
426                 sql, "structureId", StringPool.EQUAL, false, structureIds);
427             sql = CustomSQLUtil.replaceKeywords(
428                 sql, "templateId", StringPool.EQUAL, false, templateIds);
429 
430             if (approved == null) {
431                 sql = StringUtil.replace(sql, "(approved = ?) AND", "");
432             }
433 
434             if (expired == null) {
435                 sql = StringUtil.replace(sql, "(expired = ?) AND", "");
436             }
437 
438             sql = CustomSQLUtil.replaceAndOperator(sql, andOperator);
439             sql = CustomSQLUtil.replaceOrderBy(sql, obc);
440 
441             SQLQuery q = session.createSQLQuery(sql);
442 
443             q.addEntity("JournalArticle", JournalArticleImpl.class);
444 
445             QueryPos qPos = QueryPos.getInstance(q);
446 
447             qPos.add(companyId);
448 
449             if (groupId > 0) {
450                 qPos.add(groupId);
451             }
452 
453             qPos.add(articleIds, 2);
454 
455             if (version != null) {
456                 qPos.add(version);
457             }
458 
459             qPos.add(titles, 2);
460             qPos.add(descriptions, 2);
461             qPos.add(contents, 2);
462             qPos.add(type);
463             qPos.add(type);
464             qPos.add(structureIds, 2);
465             qPos.add(templateIds, 2);
466             qPos.add(displayDateGT_TS);
467             qPos.add(displayDateGT_TS);
468             qPos.add(displayDateLT_TS);
469             qPos.add(displayDateLT_TS);
470 
471             if (approved != null) {
472                 qPos.add(approved);
473             }
474 
475             if (expired != null) {
476                 qPos.add(expired);
477             }
478 
479             qPos.add(reviewDate_TS);
480             qPos.add(reviewDate_TS);
481 
482             return (List<JournalArticle>)QueryUtil.list(
483                 q, getDialect(), start, end);
484         }
485         catch (Exception e) {
486             throw new SystemException(e);
487         }
488         finally {
489             closeSession(session);
490         }
491     }
492 
493 }