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