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