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