1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet.journal.service.persistence;
16  
17  import com.liferay.portal.kernel.dao.orm.QueryPos;
18  import com.liferay.portal.kernel.dao.orm.QueryUtil;
19  import com.liferay.portal.kernel.dao.orm.SQLQuery;
20  import com.liferay.portal.kernel.dao.orm.Session;
21  import com.liferay.portal.kernel.dao.orm.Type;
22  import com.liferay.portal.kernel.exception.SystemException;
23  import com.liferay.portal.kernel.util.CalendarUtil;
24  import com.liferay.portal.kernel.util.OrderByComparator;
25  import com.liferay.portal.kernel.util.StringBundler;
26  import com.liferay.portal.kernel.util.StringPool;
27  import com.liferay.portal.kernel.util.StringUtil;
28  import com.liferay.portal.kernel.util.Validator;
29  import com.liferay.portal.kernel.workflow.StatusConstants;
30  import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
31  import com.liferay.portlet.journal.NoSuchArticleException;
32  import com.liferay.portlet.journal.model.JournalArticle;
33  import com.liferay.portlet.journal.model.impl.JournalArticleImpl;
34  import com.liferay.util.dao.orm.CustomSQLUtil;
35  
36  import java.sql.Timestamp;
37  
38  import java.util.Date;
39  import java.util.Iterator;
40  import java.util.List;
41  
42  /**
43   * <a href="JournalArticleFinderImpl.java.html"><b><i>View Source</i></b></a>
44   *
45   * @author Brian Wing Shun Chan
46   * @author Raymond Augé
47   */
48  public class JournalArticleFinderImpl
49      extends BasePersistenceImpl<JournalArticle>
50      implements JournalArticleFinder {
51  
52      public static String COUNT_BY_C_G_A_V_T_D_C_T_S_T_D_S_R =
53          JournalArticleFinder.class.getName() +
54              ".countByC_G_A_V_T_D_C_T_S_T_D_S_R";
55  
56      public static String FIND_BY_EXPIRATION_DATE =
57          JournalArticleFinder.class.getName() + ".findByExpirationDate";
58  
59      public static String FIND_BY_REVIEW_DATE =
60          JournalArticleFinder.class.getName() + ".findByReviewDate";
61  
62      public static String FIND_BY_R_D =
63          JournalArticleFinder.class.getName() + ".findByR_D";
64  
65      public static String FIND_BY_C_G_A_V_T_D_C_T_S_T_D_S_R =
66          JournalArticleFinder.class.getName() +
67              ".findByC_G_A_V_T_D_C_T_S_T_D_S_R";
68  
69      public int countByKeywords(
70              long companyId, long groupId, String keywords, Double version,
71              String type, String structureId, String templateId,
72              Date displayDateGT, Date displayDateLT, int status, Date reviewDate)
73          throws SystemException {
74  
75          String[] articleIds = null;
76          String[] titles = null;
77          String[] descriptions = null;
78          String[] contents = null;
79          boolean andOperator = false;
80  
81          if (Validator.isNotNull(keywords)) {
82              articleIds = CustomSQLUtil.keywords(keywords, false);
83              titles = CustomSQLUtil.keywords(keywords);
84              descriptions = CustomSQLUtil.keywords(keywords, false);
85              contents = CustomSQLUtil.keywords(keywords, false);
86          }
87          else {
88              andOperator = true;
89          }
90  
91          return countByC_G_A_V_T_D_C_T_S_T_D_S_R(
92              companyId, groupId, articleIds, version, titles, descriptions,
93              contents, type, new String[] {structureId},
94              new String[] {templateId}, displayDateGT, displayDateLT, status,
95              reviewDate, andOperator);
96      }
97  
98      public int countByC_G_A_V_T_D_C_T_S_T_D_S_R(
99              long companyId, long groupId, String articleId, Double version,
100             String title, String description, String content, String type,
101             String structureId, String templateId, Date displayDateGT,
102             Date displayDateLT, int status, Date reviewDate,
103             boolean andOperator)
104         throws SystemException {
105 
106         return countByC_G_A_V_T_D_C_T_S_T_D_S_R(
107             companyId, groupId, articleId, version, title, description,
108             content, type, new String[] {structureId},
109             new String[] {templateId}, displayDateGT, displayDateLT, status,
110             reviewDate, andOperator);
111     }
112 
113     public int countByC_G_A_V_T_D_C_T_S_T_D_S_R(
114             long companyId, long groupId, String articleId, Double version,
115             String title, String description, String content, String type,
116             String[] structureIds, String[] templateIds, Date displayDateGT,
117             Date displayDateLT, int status, Date reviewDate,
118             boolean andOperator)
119         throws SystemException {
120 
121         return countByC_G_A_V_T_D_C_T_S_T_D_S_R(
122             companyId, groupId, new String[] {articleId}, version,
123             new String[] {title}, new String[] {description},
124             new String[] {content}, type, structureIds, templateIds,
125             displayDateGT, displayDateLT, status, reviewDate, andOperator);
126     }
127 
128     public int countByC_G_A_V_T_D_C_T_S_T_D_S_R(
129             long companyId, long groupId, String[] articleIds, Double version,
130             String[] titles, String[] descriptions, String[] contents,
131             String type, String[] structureIds, String[] templateIds,
132             Date displayDateGT, Date displayDateLT, int status, Date reviewDate,
133             boolean andOperator)
134         throws SystemException {
135 
136         articleIds = CustomSQLUtil.keywords(articleIds, false);
137         titles = CustomSQLUtil.keywords(titles);
138         descriptions = CustomSQLUtil.keywords(descriptions, false);
139         contents = CustomSQLUtil.keywords(contents, false);
140         structureIds = CustomSQLUtil.keywords(structureIds, false);
141         templateIds = CustomSQLUtil.keywords(templateIds, false);
142         Timestamp displayDateGT_TS = CalendarUtil.getTimestamp(displayDateGT);
143         Timestamp displayDateLT_TS = CalendarUtil.getTimestamp(displayDateLT);
144         Timestamp reviewDate_TS = CalendarUtil.getTimestamp(reviewDate);
145 
146         Session session = null;
147 
148         try {
149             session = openSession();
150 
151             String sql = CustomSQLUtil.get(COUNT_BY_C_G_A_V_T_D_C_T_S_T_D_S_R);
152 
153             if (groupId <= 0) {
154                 sql = StringUtil.replace(sql, "(groupId = ?) AND", "");
155             }
156 
157             sql = CustomSQLUtil.replaceKeywords(
158                 sql, "articleId", StringPool.LIKE, false, articleIds);
159 
160             if (version == null) {
161                 sql = StringUtil.replace(
162                     sql, "(version = ?) [$AND_OR_CONNECTOR$]", "");
163             }
164 
165             sql = CustomSQLUtil.replaceKeywords(
166                 sql, "lower(title)", StringPool.LIKE, false, titles);
167             sql = CustomSQLUtil.replaceKeywords(
168                 sql, "description", StringPool.LIKE, false, descriptions);
169             sql = CustomSQLUtil.replaceKeywords(
170                 sql, "content", StringPool.LIKE, false, contents);
171             sql = CustomSQLUtil.replaceKeywords(
172                 sql, "structureId", StringPool.EQUAL, false, structureIds);
173             sql = CustomSQLUtil.replaceKeywords(
174                 sql, "templateId", StringPool.EQUAL, false, templateIds);
175 
176             if (status == StatusConstants.ANY) {
177                 sql = StringUtil.replace(sql, "(status = ?) 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 (status != StatusConstants.ANY) {
213                 qPos.add(status);
214             }
215 
216             qPos.add(reviewDate_TS);
217             qPos.add(reviewDate_TS);
218 
219             Iterator<Long> itr = q.list().iterator();
220 
221             if (itr.hasNext()) {
222                 Long count = itr.next();
223 
224                 if (count != null) {
225                     return count.intValue();
226                 }
227             }
228 
229             return 0;
230         }
231         catch (Exception e) {
232             throw new SystemException(e);
233         }
234         finally {
235             closeSession(session);
236         }
237     }
238 
239     public List<JournalArticle> findByExpirationDate(
240             int status, Date expirationDateLT, Date expirationDateGT)
241         throws SystemException {
242 
243         Timestamp expirationDateLT_TS = CalendarUtil.getTimestamp(
244             expirationDateLT);
245         Timestamp expirationDateGT_TS = CalendarUtil.getTimestamp(
246             expirationDateGT);
247 
248         Session session = null;
249 
250         try {
251             session = openSession();
252 
253             String sql = CustomSQLUtil.get(FIND_BY_EXPIRATION_DATE);
254 
255             if (status == StatusConstants.ANY) {
256                 sql = StringUtil.replace(sql, "(status = ?) AND", "");
257             }
258 
259             SQLQuery q = session.createSQLQuery(sql);
260 
261             q.addEntity("JournalArticle", JournalArticleImpl.class);
262 
263             QueryPos qPos = QueryPos.getInstance(q);
264 
265             if (status != StatusConstants.ANY) {
266                 qPos.add(status);
267             }
268 
269             qPos.add(expirationDateGT_TS);
270             qPos.add(expirationDateLT_TS);
271 
272             return q.list();
273         }
274         catch (Exception e) {
275             throw new SystemException(e);
276         }
277         finally {
278             closeSession(session);
279         }
280     }
281 
282     public List<JournalArticle> findByKeywords(
283             long companyId, long groupId, String keywords, Double version,
284             String type, String structureId, String templateId,
285             Date displayDateGT, Date displayDateLT, int status, Date reviewDate,
286             int start, int end, OrderByComparator obc)
287         throws SystemException {
288 
289         String[] articleIds = null;
290         String[] titles = null;
291         String[] descriptions = null;
292         String[] contents = null;
293         boolean andOperator = false;
294 
295         if (Validator.isNotNull(keywords)) {
296             articleIds = CustomSQLUtil.keywords(keywords, false);
297             titles = CustomSQLUtil.keywords(keywords);
298             descriptions = CustomSQLUtil.keywords(keywords, false);
299             contents = CustomSQLUtil.keywords(keywords, false);
300         }
301         else {
302             andOperator = true;
303         }
304 
305         return findByC_G_A_V_T_D_C_T_S_T_D_S_R(
306             companyId, groupId, articleIds, version, titles, descriptions,
307             contents, type, new String[] {structureId},
308             new String[] {templateId}, displayDateGT, displayDateLT, status,
309             reviewDate, andOperator, start, end, obc);
310     }
311 
312     public List<JournalArticle> findByReviewDate(
313             Date reviewDateLT, Date reviewDateGT)
314         throws SystemException {
315 
316         Timestamp reviewDateLT_TS = CalendarUtil.getTimestamp(reviewDateLT);
317         Timestamp reviewDateGT_TS = CalendarUtil.getTimestamp(reviewDateGT);
318 
319         Session session = null;
320         try {
321             session = openSession();
322 
323             String sql = CustomSQLUtil.get(FIND_BY_REVIEW_DATE);
324 
325             SQLQuery q = session.createSQLQuery(sql);
326 
327             q.addEntity("JournalArticle", JournalArticleImpl.class);
328 
329             QueryPos qPos = QueryPos.getInstance(q);
330 
331             qPos.add(reviewDateGT_TS);
332             qPos.add(reviewDateLT_TS);
333 
334             return q.list();
335         }
336         catch (Exception e) {
337             throw new SystemException(e);
338         }
339         finally {
340             closeSession(session);
341         }
342     }
343 
344     public JournalArticle findByR_D(long resourcePrimKey, Date displayDate)
345         throws NoSuchArticleException, SystemException {
346 
347         Timestamp displayDate_TS = CalendarUtil.getTimestamp(displayDate);
348 
349         Session session = null;
350 
351         try {
352             session = openSession();
353 
354             String sql = CustomSQLUtil.get(FIND_BY_R_D);
355 
356             SQLQuery q = session.createSQLQuery(sql);
357 
358             q.addEntity("JournalArticle", JournalArticleImpl.class);
359 
360             QueryPos qPos = QueryPos.getInstance(q);
361 
362             qPos.add(resourcePrimKey);
363             qPos.add(displayDate_TS);
364 
365             List<JournalArticle> list = q.list();
366 
367             if (list.size() == 0) {
368                 StringBundler sb = new StringBundler(6);
369 
370                 sb.append("No JournalArticle exists with the key ");
371                 sb.append("{resourcePrimKey=");
372                 sb.append(resourcePrimKey);
373                 sb.append(", displayDate=");
374                 sb.append(displayDate);
375                 sb.append("}");
376 
377                 throw new NoSuchArticleException(sb.toString());
378             }
379             else {
380                 return list.get(0);
381             }
382         }
383         catch (NoSuchArticleException nsae) {
384             throw nsae;
385         }
386         catch (Exception e) {
387             throw new SystemException(e);
388         }
389         finally {
390             closeSession(session);
391         }
392     }
393 
394     public List<JournalArticle> findByC_G_A_V_T_D_C_T_S_T_D_S_R(
395             long companyId, long groupId, String articleId, Double version,
396             String title, String description, String content, String type,
397             String structureId, String templateId, Date displayDateGT,
398             Date displayDateLT, int status, Date reviewDate,
399             boolean andOperator, int start, int end, OrderByComparator obc)
400         throws SystemException {
401 
402         return findByC_G_A_V_T_D_C_T_S_T_D_S_R(
403             companyId, groupId, articleId, version, title, description,
404             content, type, new String[] {structureId},
405             new String[] {templateId}, displayDateGT, displayDateLT, status,
406             reviewDate, andOperator, start, end, obc);
407     }
408 
409     public List<JournalArticle> findByC_G_A_V_T_D_C_T_S_T_D_S_R(
410             long companyId, long groupId, String articleId, Double version,
411             String title, String description, String content, String type,
412             String[] structureIds, String[] templateIds, Date displayDateGT,
413             Date displayDateLT, int status, Date reviewDate,
414             boolean andOperator, int start, int end, OrderByComparator obc)
415         throws SystemException {
416 
417         return findByC_G_A_V_T_D_C_T_S_T_D_S_R(
418             companyId, groupId, new String[] {articleId}, version,
419             new String[] {title}, new String[] {description},
420             new String[] {content}, type, structureIds, templateIds,
421             displayDateGT, displayDateLT, status, reviewDate, andOperator,
422             start, end, obc);
423     }
424 
425     public List<JournalArticle> findByC_G_A_V_T_D_C_T_S_T_D_S_R(
426             long companyId, long groupId, String[] articleIds, Double version,
427             String[] titles, String[] descriptions, String[] contents,
428             String type, String[] structureIds, String[] templateIds,
429             Date displayDateGT, Date displayDateLT, int status,
430             Date reviewDate, boolean andOperator, int start, int end,
431             OrderByComparator obc)
432         throws SystemException {
433 
434         articleIds = CustomSQLUtil.keywords(articleIds, false);
435         titles = CustomSQLUtil.keywords(titles);
436         descriptions = CustomSQLUtil.keywords(descriptions, false);
437         contents = CustomSQLUtil.keywords(contents, false);
438         structureIds = CustomSQLUtil.keywords(structureIds, false);
439         templateIds = CustomSQLUtil.keywords(templateIds, false);
440         Timestamp displayDateGT_TS = CalendarUtil.getTimestamp(displayDateGT);
441         Timestamp displayDateLT_TS = CalendarUtil.getTimestamp(displayDateLT);
442         Timestamp reviewDate_TS = CalendarUtil.getTimestamp(reviewDate);
443 
444         Session session = null;
445 
446         try {
447             session = openSession();
448 
449             String sql = CustomSQLUtil.get(FIND_BY_C_G_A_V_T_D_C_T_S_T_D_S_R);
450 
451             if (groupId <= 0) {
452                 sql = StringUtil.replace(sql, "(groupId = ?) AND", "");
453             }
454 
455             sql = CustomSQLUtil.replaceKeywords(
456                 sql, "articleId", StringPool.LIKE, false, articleIds);
457 
458             if (version == null) {
459                 sql = StringUtil.replace(
460                     sql, "(version = ?) [$AND_OR_CONNECTOR$]", "");
461             }
462 
463             sql = CustomSQLUtil.replaceKeywords(
464                 sql, "lower(title)", StringPool.LIKE, false, titles);
465             sql = CustomSQLUtil.replaceKeywords(
466                 sql, "description", StringPool.LIKE, false, descriptions);
467             sql = CustomSQLUtil.replaceKeywords(
468                 sql, "content", StringPool.LIKE, false, contents);
469             sql = CustomSQLUtil.replaceKeywords(
470                 sql, "structureId", StringPool.EQUAL, false, structureIds);
471             sql = CustomSQLUtil.replaceKeywords(
472                 sql, "templateId", StringPool.EQUAL, false, templateIds);
473 
474             if (status == StatusConstants.ANY) {
475                 sql = StringUtil.replace(sql, "(status = ?) AND", "");
476             }
477 
478             sql = CustomSQLUtil.replaceAndOperator(sql, andOperator);
479             sql = CustomSQLUtil.replaceOrderBy(sql, obc);
480 
481             SQLQuery q = session.createSQLQuery(sql);
482 
483             q.addEntity("JournalArticle", JournalArticleImpl.class);
484 
485             QueryPos qPos = QueryPos.getInstance(q);
486 
487             qPos.add(companyId);
488 
489             if (groupId > 0) {
490                 qPos.add(groupId);
491             }
492 
493             qPos.add(articleIds, 2);
494 
495             if (version != null) {
496                 qPos.add(version);
497             }
498 
499             qPos.add(titles, 2);
500             qPos.add(descriptions, 2);
501             qPos.add(contents, 2);
502             qPos.add(type);
503             qPos.add(type);
504             qPos.add(structureIds, 2);
505             qPos.add(templateIds, 2);
506             qPos.add(displayDateGT_TS);
507             qPos.add(displayDateGT_TS);
508             qPos.add(displayDateLT_TS);
509             qPos.add(displayDateLT_TS);
510 
511             if (status != StatusConstants.ANY) {
512                 qPos.add(status);
513             }
514 
515             qPos.add(reviewDate_TS);
516             qPos.add(reviewDate_TS);
517 
518             return (List<JournalArticle>)QueryUtil.list(
519                 q, getDialect(), start, end);
520         }
521         catch (Exception e) {
522             throw new SystemException(e);
523         }
524         finally {
525             closeSession(session);
526         }
527     }
528 
529 }