001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.journal.service.persistence;
016    
017    import com.liferay.portal.kernel.dao.orm.QueryPos;
018    import com.liferay.portal.kernel.dao.orm.QueryUtil;
019    import com.liferay.portal.kernel.dao.orm.SQLQuery;
020    import com.liferay.portal.kernel.dao.orm.Session;
021    import com.liferay.portal.kernel.dao.orm.Type;
022    import com.liferay.portal.kernel.exception.SystemException;
023    import com.liferay.portal.kernel.util.CalendarUtil;
024    import com.liferay.portal.kernel.util.OrderByComparator;
025    import com.liferay.portal.kernel.util.StringBundler;
026    import com.liferay.portal.kernel.util.StringPool;
027    import com.liferay.portal.kernel.util.StringUtil;
028    import com.liferay.portal.kernel.util.Validator;
029    import com.liferay.portal.kernel.workflow.WorkflowConstants;
030    import com.liferay.portal.security.permission.InlineSQLHelperUtil;
031    import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
032    import com.liferay.portlet.journal.NoSuchArticleException;
033    import com.liferay.portlet.journal.model.JournalArticle;
034    import com.liferay.portlet.journal.model.impl.JournalArticleImpl;
035    import com.liferay.util.dao.orm.CustomSQLUtil;
036    
037    import java.sql.Timestamp;
038    
039    import java.util.Date;
040    import java.util.Iterator;
041    import java.util.List;
042    
043    /**
044     * @author Brian Wing Shun Chan
045     * @author Raymond Aug??
046     * @author Connor McKay
047     */
048    public class JournalArticleFinderImpl
049            extends BasePersistenceImpl<JournalArticle>
050            implements JournalArticleFinder {
051    
052            public static final String COUNT_BY_C_G_C_A_V_T_D_C_T_S_T_D_S_R =
053                    JournalArticleFinder.class.getName() +
054                            ".countByC_G_C_A_V_T_D_C_T_S_T_D_S_R";
055    
056            public static final String FIND_BY_EXPIRATION_DATE =
057                    JournalArticleFinder.class.getName() + ".findByExpirationDate";
058    
059            public static final String FIND_BY_REVIEW_DATE =
060                    JournalArticleFinder.class.getName() + ".findByReviewDate";
061    
062            public static final String FIND_BY_R_D =
063                    JournalArticleFinder.class.getName() + ".findByR_D";
064    
065            public static final String FIND_BY_C_G_C_A_V_T_D_C_T_S_T_D_S_R =
066                    JournalArticleFinder.class.getName() +
067                            ".findByC_G_C_A_V_T_D_C_T_S_T_D_S_R";
068    
069            @Override
070            public int countByKeywords(
071                            long companyId, long groupId, long classNameId, String keywords,
072                            Double version, String type, String structureId, String templateId,
073                            Date displayDateGT, Date displayDateLT, int status, Date reviewDate)
074                    throws SystemException {
075    
076                    String[] articleIds = null;
077                    String[] titles = null;
078                    String[] descriptions = null;
079                    String[] contents = null;
080                    String[] structureIds = CustomSQLUtil.keywords(structureId, false);
081                    String[] templateIds = CustomSQLUtil.keywords(templateId, false);
082                    boolean andOperator = false;
083    
084                    if (Validator.isNotNull(keywords)) {
085                            articleIds = CustomSQLUtil.keywords(keywords, false);
086                            titles = CustomSQLUtil.keywords(keywords);
087                            descriptions = CustomSQLUtil.keywords(keywords, false);
088                            contents = CustomSQLUtil.keywords(keywords, false);
089                    }
090                    else {
091                            andOperator = true;
092                    }
093    
094                    return doCountByC_G_C_A_V_T_D_C_T_S_T_D_S_R(
095                            companyId, groupId, classNameId, articleIds, version, titles,
096                            descriptions, contents, type, structureIds, templateIds,
097                            displayDateGT, displayDateLT, status, reviewDate, andOperator,
098                            false);
099            }
100    
101            @Override
102            public int countByC_G_C_A_V_T_D_C_T_S_T_D_S_R(
103                            long companyId, long groupId, long classNameId, String articleId,
104                            Double version, String title, String description, String content,
105                            String type, String structureId, String templateId,
106                            Date displayDateGT, Date displayDateLT, int status, Date reviewDate,
107                            boolean andOperator)
108                    throws SystemException {
109    
110                    String[] structureIds = CustomSQLUtil.keywords(structureId, false);
111                    String[] templateIds = CustomSQLUtil.keywords(templateId, false);
112    
113                    return countByC_G_C_A_V_T_D_C_T_S_T_D_S_R(
114                            companyId, groupId, classNameId, articleId, version, title,
115                            description, content, type, structureIds, templateIds,
116                            displayDateGT, displayDateLT, status, reviewDate, andOperator);
117            }
118    
119            @Override
120            public int countByC_G_C_A_V_T_D_C_T_S_T_D_S_R(
121                            long companyId, long groupId, long classNameId, String articleId,
122                            Double version, String title, String description, String content,
123                            String type, String[] structureIds, String[] templateIds,
124                            Date displayDateGT, Date displayDateLT, int status, Date reviewDate,
125                            boolean andOperator)
126                    throws SystemException {
127    
128                    String[] articleIds = CustomSQLUtil.keywords(articleId, false);
129                    String[] titles = CustomSQLUtil.keywords(title);
130                    String[] descriptions = CustomSQLUtil.keywords(description, false);
131                    String[] contents = CustomSQLUtil.keywords(content, false);
132    
133                    return countByC_G_C_A_V_T_D_C_T_S_T_D_S_R(
134                            companyId, groupId, classNameId, articleIds, version, titles,
135                            descriptions, contents, type, structureIds, templateIds,
136                            displayDateGT, displayDateLT, status, reviewDate, andOperator);
137            }
138    
139            @Override
140            public int countByC_G_C_A_V_T_D_C_T_S_T_D_S_R(
141                            long companyId, long groupId, long classNameId, String[] articleIds,
142                            Double version, String[] titles, String[] descriptions,
143                            String[] contents, String type, String[] structureIds,
144                            String[] templateIds, Date displayDateGT, Date displayDateLT,
145                            int status, Date reviewDate, boolean andOperator)
146                    throws SystemException {
147    
148                    return doCountByC_G_C_A_V_T_D_C_T_S_T_D_S_R(
149                            companyId, groupId, classNameId, articleIds, version, titles,
150                            descriptions, contents, type, structureIds, templateIds,
151                            displayDateGT, displayDateLT, status, reviewDate, andOperator,
152                            false);
153            }
154    
155            @Override
156            public int filterCountByKeywords(
157                            long companyId, long groupId, long classNameId, String keywords,
158                            Double version, String type, String structureId, String templateId,
159                            Date displayDateGT, Date displayDateLT, int status, Date reviewDate)
160                    throws SystemException {
161    
162                    String[] articleIds = null;
163                    String[] titles = null;
164                    String[] descriptions = null;
165                    String[] contents = null;
166                    String[] structureIds = CustomSQLUtil.keywords(structureId, false);
167                    String[] templateIds = CustomSQLUtil.keywords(templateId, false);
168                    boolean andOperator = false;
169    
170                    if (Validator.isNotNull(keywords)) {
171                            articleIds = CustomSQLUtil.keywords(keywords, false);
172                            titles = CustomSQLUtil.keywords(keywords);
173                            descriptions = CustomSQLUtil.keywords(keywords, false);
174                            contents = CustomSQLUtil.keywords(keywords, false);
175                    }
176                    else {
177                            andOperator = true;
178                    }
179    
180                    return doCountByC_G_C_A_V_T_D_C_T_S_T_D_S_R(
181                            companyId, groupId, classNameId, articleIds, version, titles,
182                            descriptions, contents, type, structureIds, templateIds,
183                            displayDateGT, displayDateLT, status, reviewDate, andOperator,
184                            true);
185            }
186    
187            @Override
188            public int filterCountByC_G_C_A_V_T_D_C_T_S_T_D_S_R(
189                            long companyId, long groupId, long classNameId, String articleId,
190                            Double version, String title, String description, String content,
191                            String type, String structureId, String templateId,
192                            Date displayDateGT, Date displayDateLT, int status, Date reviewDate,
193                            boolean andOperator)
194                    throws SystemException {
195    
196                    String[] structureIds = CustomSQLUtil.keywords(structureId, false);
197                    String[] templateIds = CustomSQLUtil.keywords(templateId, false);
198    
199                    return filterCountByC_G_C_A_V_T_D_C_T_S_T_D_S_R(
200                            companyId, groupId, classNameId, articleId, version, title,
201                            description, content, type, structureIds, templateIds,
202                            displayDateGT, displayDateLT, status, reviewDate, andOperator);
203            }
204    
205            @Override
206            public int filterCountByC_G_C_A_V_T_D_C_T_S_T_D_S_R(
207                            long companyId, long groupId, long classNameId, String articleId,
208                            Double version, String title, String description, String content,
209                            String type, String[] structureIds, String[] templateIds,
210                            Date displayDateGT, Date displayDateLT, int status, Date reviewDate,
211                            boolean andOperator)
212                    throws SystemException {
213    
214                    String[] articleIds = CustomSQLUtil.keywords(articleId, false);
215                    String[] titles = CustomSQLUtil.keywords(title);
216                    String[] descriptions = CustomSQLUtil.keywords(description, false);
217                    String[] contents = CustomSQLUtil.keywords(content, false);
218    
219                    return filterCountByC_G_C_A_V_T_D_C_T_S_T_D_S_R(
220                            companyId, groupId, classNameId, articleIds, version, titles,
221                            descriptions, contents, type, structureIds, templateIds,
222                            displayDateGT, displayDateLT, status, reviewDate, andOperator);
223            }
224    
225            @Override
226            public int filterCountByC_G_C_A_V_T_D_C_T_S_T_D_S_R(
227                            long companyId, long groupId, long classNameId, String[] articleIds,
228                            Double version, String[] titles, String[] descriptions,
229                            String[] contents, String type, String[] structureIds,
230                            String[] templateIds, Date displayDateGT, Date displayDateLT,
231                            int status, Date reviewDate, boolean andOperator)
232                    throws SystemException {
233    
234                    return doCountByC_G_C_A_V_T_D_C_T_S_T_D_S_R(
235                            companyId, groupId, classNameId, articleIds, version, titles,
236                            descriptions, contents, type, structureIds, templateIds,
237                            displayDateGT, displayDateLT, status, reviewDate, andOperator,
238                            true);
239            }
240    
241            @Override
242            public List<JournalArticle> filterFindByKeywords(
243                            long companyId, long groupId, long classNameId, String keywords,
244                            Double version, String type, String structureId, String templateId,
245                            Date displayDateGT, Date displayDateLT, int status, Date reviewDate,
246                            int start, int end, OrderByComparator orderByComparator)
247                    throws SystemException {
248    
249                    String[] articleIds = null;
250                    String[] titles = null;
251                    String[] descriptions = null;
252                    String[] contents = null;
253                    String[] structureIds = CustomSQLUtil.keywords(structureId, false);
254                    String[] templateIds = CustomSQLUtil.keywords(templateId, false);
255                    boolean andOperator = false;
256    
257                    if (Validator.isNotNull(keywords)) {
258                            articleIds = CustomSQLUtil.keywords(keywords, false);
259                            titles = CustomSQLUtil.keywords(keywords);
260                            descriptions = CustomSQLUtil.keywords(keywords, false);
261                            contents = CustomSQLUtil.keywords(keywords, false);
262                    }
263                    else {
264                            andOperator = true;
265                    }
266    
267                    return doFindByC_G_C_A_V_T_D_C_T_S_T_D_S_R(
268                            companyId, groupId, classNameId, articleIds, version, titles,
269                            descriptions, contents, type, structureIds, templateIds,
270                            displayDateGT, displayDateLT, status, reviewDate, andOperator,
271                            start, end, orderByComparator, true);
272            }
273    
274            @Override
275            public List<JournalArticle> filterFindByC_G_C_A_V_T_D_C_T_S_T_D_S_R(
276                            long companyId, long groupId, long classNameId, String articleId,
277                            Double version, String title, String description, String content,
278                            String type, String structureId, String templateId,
279                            Date displayDateGT, Date displayDateLT, int status, Date reviewDate,
280                            boolean andOperator, int start, int end,
281                            OrderByComparator orderByComparator)
282                    throws SystemException {
283    
284                    String[] structureIds = CustomSQLUtil.keywords(structureId, false);
285                    String[] templateIds = CustomSQLUtil.keywords(templateId, false);
286    
287                    return filterFindByC_G_C_A_V_T_D_C_T_S_T_D_S_R(
288                            companyId, groupId, classNameId, articleId, version, title,
289                            description, content, type, structureIds, templateIds,
290                            displayDateGT, displayDateLT, status, reviewDate, andOperator,
291                            start, end, orderByComparator);
292            }
293    
294            @Override
295            public List<JournalArticle> filterFindByC_G_C_A_V_T_D_C_T_S_T_D_S_R(
296                            long companyId, long groupId, long classNameId, String articleId,
297                            Double version, String title, String description, String content,
298                            String type, String[] structureIds, String[] templateIds,
299                            Date displayDateGT, Date displayDateLT, int status, Date reviewDate,
300                            boolean andOperator, int start, int end,
301                            OrderByComparator orderByComparator)
302                    throws SystemException {
303    
304                    String[] articleIds = CustomSQLUtil.keywords(articleId, false);
305                    String[] titles = CustomSQLUtil.keywords(title);
306                    String[] descriptions = CustomSQLUtil.keywords(description, false);
307                    String[] contents = CustomSQLUtil.keywords(content, false);
308    
309                    return filterFindByC_G_C_A_V_T_D_C_T_S_T_D_S_R(
310                            companyId, groupId, classNameId, articleIds, version, titles,
311                            descriptions, contents, type, structureIds, templateIds,
312                            displayDateGT, displayDateLT, status, reviewDate, andOperator,
313                            start, end, orderByComparator);
314            }
315    
316            @Override
317            public List<JournalArticle> filterFindByC_G_C_A_V_T_D_C_T_S_T_D_S_R(
318                            long companyId, long groupId, long classNameId, String[] articleIds,
319                            Double version, String[] titles, String[] descriptions,
320                            String[] contents, String type, String[] structureIds,
321                            String[] templateIds, Date displayDateGT, Date displayDateLT,
322                            int status, Date reviewDate, boolean andOperator, int start,
323                            int end, OrderByComparator orderByComparator)
324                    throws SystemException {
325    
326                    return doFindByC_G_C_A_V_T_D_C_T_S_T_D_S_R(
327                            companyId, groupId, classNameId, articleIds, version, titles,
328                            descriptions, contents, type, structureIds, templateIds,
329                            displayDateGT, displayDateLT, status, reviewDate, andOperator,
330                            start, end, orderByComparator, true);
331            }
332    
333            @Override
334            public List<JournalArticle> findByExpirationDate(
335                            long classNameId, int status, Date expirationDateLT)
336                    throws SystemException {
337    
338                    Timestamp expirationDateLT_TS = CalendarUtil.getTimestamp(
339                            expirationDateLT);
340    
341                    Session session = null;
342    
343                    try {
344                            session = openSession();
345    
346                            String sql = CustomSQLUtil.get(FIND_BY_EXPIRATION_DATE);
347    
348                            if (status == WorkflowConstants.STATUS_ANY) {
349                                    sql = StringUtil.replace(sql, "(status = ?) AND", "");
350                            }
351    
352                            SQLQuery q = session.createSQLQuery(sql);
353    
354                            q.addEntity("JournalArticle", JournalArticleImpl.class);
355    
356                            QueryPos qPos = QueryPos.getInstance(q);
357    
358                            qPos.add(classNameId);
359    
360                            if (status != WorkflowConstants.STATUS_ANY) {
361                                    qPos.add(status);
362                            }
363    
364                            qPos.add(expirationDateLT_TS);
365    
366                            return q.list(true);
367                    }
368                    catch (Exception e) {
369                            throw new SystemException(e);
370                    }
371                    finally {
372                            closeSession(session);
373                    }
374            }
375    
376            @Override
377            public List<JournalArticle> findByKeywords(
378                            long companyId, long groupId, long classNameId, String keywords,
379                            Double version, String type, String structureId, String templateId,
380                            Date displayDateGT, Date displayDateLT, int status, Date reviewDate,
381                            int start, int end, OrderByComparator orderByComparator)
382                    throws SystemException {
383    
384                    String[] articleIds = null;
385                    String[] titles = null;
386                    String[] descriptions = null;
387                    String[] contents = null;
388                    String[] structureIds = CustomSQLUtil.keywords(structureId, false);
389                    String[] templateIds = CustomSQLUtil.keywords(templateId, false);
390                    boolean andOperator = false;
391    
392                    if (Validator.isNotNull(keywords)) {
393                            articleIds = CustomSQLUtil.keywords(keywords, false);
394                            titles = CustomSQLUtil.keywords(keywords);
395                            descriptions = CustomSQLUtil.keywords(keywords, false);
396                            contents = CustomSQLUtil.keywords(keywords, false);
397                    }
398                    else {
399                            andOperator = true;
400                    }
401    
402                    return findByC_G_C_A_V_T_D_C_T_S_T_D_S_R(
403                            companyId, groupId, classNameId, articleIds, version, titles,
404                            descriptions, contents, type, structureIds, templateIds,
405                            displayDateGT, displayDateLT, status, reviewDate, andOperator,
406                            start, end, orderByComparator);
407            }
408    
409            @Override
410            public List<JournalArticle> findByReviewDate(
411                            long classNameId, Date reviewDateLT, Date reviewDateGT)
412                    throws SystemException {
413    
414                    Timestamp reviewDateLT_TS = CalendarUtil.getTimestamp(reviewDateLT);
415                    Timestamp reviewDateGT_TS = CalendarUtil.getTimestamp(reviewDateGT);
416    
417                    Session session = null;
418                    try {
419                            session = openSession();
420    
421                            String sql = CustomSQLUtil.get(FIND_BY_REVIEW_DATE);
422    
423                            SQLQuery q = session.createSQLQuery(sql);
424    
425                            q.addEntity("JournalArticle", JournalArticleImpl.class);
426    
427                            QueryPos qPos = QueryPos.getInstance(q);
428    
429                            qPos.add(classNameId);
430                            qPos.add(reviewDateGT_TS);
431                            qPos.add(reviewDateLT_TS);
432    
433                            return q.list(true);
434                    }
435                    catch (Exception e) {
436                            throw new SystemException(e);
437                    }
438                    finally {
439                            closeSession(session);
440                    }
441            }
442    
443            @Override
444            public JournalArticle findByR_D(long resourcePrimKey, Date displayDate)
445                    throws NoSuchArticleException, SystemException {
446    
447                    Timestamp displayDate_TS = CalendarUtil.getTimestamp(displayDate);
448    
449                    Session session = null;
450    
451                    try {
452                            session = openSession();
453    
454                            String sql = CustomSQLUtil.get(FIND_BY_R_D);
455    
456                            SQLQuery q = session.createSQLQuery(sql);
457    
458                            q.addEntity("JournalArticle", JournalArticleImpl.class);
459    
460                            QueryPos qPos = QueryPos.getInstance(q);
461    
462                            qPos.add(resourcePrimKey);
463                            qPos.add(displayDate_TS);
464    
465                            List<JournalArticle> articles = q.list();
466    
467                            if (!articles.isEmpty()) {
468                                    return articles.get(0);
469                            }
470                    }
471                    catch (Exception e) {
472                            throw new SystemException(e);
473                    }
474                    finally {
475                            closeSession(session);
476                    }
477    
478                    StringBundler sb = new StringBundler(6);
479    
480                    sb.append("No JournalArticle exists with the key ");
481                    sb.append("{resourcePrimKey=");
482                    sb.append(resourcePrimKey);
483                    sb.append(", displayDate=");
484                    sb.append(displayDate);
485                    sb.append("}");
486    
487                    throw new NoSuchArticleException(sb.toString());
488            }
489    
490            @Override
491            public List<JournalArticle> findByC_G_C_A_V_T_D_C_T_S_T_D_S_R(
492                            long companyId, long groupId, long classNameId, String articleId,
493                            Double version, String title, String description, String content,
494                            String type, String structureId, String templateId,
495                            Date displayDateGT, Date displayDateLT, int status, Date reviewDate,
496                            boolean andOperator, int start, int end,
497                            OrderByComparator orderByComparator)
498                    throws SystemException {
499    
500                    String[] articleIds = CustomSQLUtil.keywords(articleId, false);
501                    String[] titles = CustomSQLUtil.keywords(title);
502                    String[] descriptions = CustomSQLUtil.keywords(description, false);
503                    String[] contents = CustomSQLUtil.keywords(content, false);
504                    String[] structureIds = CustomSQLUtil.keywords(structureId, false);
505                    String[] templateIds = CustomSQLUtil.keywords(templateId, false);
506    
507                    return findByC_G_C_A_V_T_D_C_T_S_T_D_S_R(
508                            companyId, groupId, classNameId, articleIds, version, titles,
509                            descriptions, contents, type, structureIds, templateIds,
510                            displayDateGT, displayDateLT, status, reviewDate, andOperator,
511                            start, end, orderByComparator);
512            }
513    
514            @Override
515            public List<JournalArticle> findByC_G_C_A_V_T_D_C_T_S_T_D_S_R(
516                            long companyId, long groupId, long classNameId, String articleId,
517                            Double version, String title, String description, String content,
518                            String type, String[] structureIds, String[] templateIds,
519                            Date displayDateGT, Date displayDateLT, int status, Date reviewDate,
520                            boolean andOperator, int start, int end,
521                            OrderByComparator orderByComparator)
522                    throws SystemException {
523    
524                    String[] articleIds = CustomSQLUtil.keywords(articleId, false);
525                    String[] titles = CustomSQLUtil.keywords(title);
526                    String[] descriptions = CustomSQLUtil.keywords(description, false);
527                    String[] contents = CustomSQLUtil.keywords(content, false);
528    
529                    return findByC_G_C_A_V_T_D_C_T_S_T_D_S_R(
530                            companyId, groupId, classNameId, articleIds, version, titles,
531                            descriptions, contents, type, structureIds, templateIds,
532                            displayDateGT, displayDateLT, status, reviewDate, andOperator,
533                            start, end, orderByComparator);
534            }
535    
536            @Override
537            public List<JournalArticle> findByC_G_C_A_V_T_D_C_T_S_T_D_S_R(
538                            long companyId, long groupId, long classNameId, String[] articleIds,
539                            Double version, String[] titles, String[] descriptions,
540                            String[] contents, String type, String[] structureIds,
541                            String[] templateIds, Date displayDateGT, Date displayDateLT,
542                            int status, Date reviewDate, boolean andOperator, int start,
543                            int end, OrderByComparator orderByComparator)
544                    throws SystemException {
545    
546                    return doFindByC_G_C_A_V_T_D_C_T_S_T_D_S_R(
547                            companyId, groupId, classNameId, articleIds, version, titles,
548                            descriptions, contents, type, structureIds, templateIds,
549                            displayDateGT, displayDateLT, status, reviewDate, andOperator,
550                            start, end, orderByComparator, false);
551            }
552    
553            protected int doCountByC_G_C_A_V_T_D_C_T_S_T_D_S_R(
554                            long companyId, long groupId, long classNameId, String[] articleIds,
555                            Double version, String[] titles, String[] descriptions,
556                            String[] contents, String type, String[] structureIds,
557                            String[] templateIds, Date displayDateGT, Date displayDateLT,
558                            int status, Date reviewDate, boolean andOperator,
559                            boolean inlineSQLHelper)
560                    throws SystemException {
561    
562                    articleIds = CustomSQLUtil.keywords(articleIds, false);
563                    titles = CustomSQLUtil.keywords(titles);
564                    descriptions = CustomSQLUtil.keywords(descriptions, false);
565                    contents = CustomSQLUtil.keywords(contents, false);
566                    structureIds = CustomSQLUtil.keywords(structureIds, false);
567                    templateIds = CustomSQLUtil.keywords(templateIds, false);
568                    Timestamp displayDateGT_TS = CalendarUtil.getTimestamp(displayDateGT);
569                    Timestamp displayDateLT_TS = CalendarUtil.getTimestamp(displayDateLT);
570                    Timestamp reviewDate_TS = CalendarUtil.getTimestamp(reviewDate);
571    
572                    Session session = null;
573    
574                    try {
575                            session = openSession();
576    
577                            String sql = CustomSQLUtil.get(
578                                    COUNT_BY_C_G_C_A_V_T_D_C_T_S_T_D_S_R);
579    
580                            if (groupId <= 0) {
581                                    sql = StringUtil.replace(sql, "(groupId = ?) AND", "");
582                            }
583    
584                            sql = CustomSQLUtil.replaceKeywords(
585                                    sql, "articleId", StringPool.LIKE, false, articleIds);
586    
587                            if ((version == null) || (version <= 0)) {
588                                    sql = StringUtil.replace(
589                                            sql, "(version = ?) [$AND_OR_CONNECTOR$]", "");
590                            }
591    
592                            sql = CustomSQLUtil.replaceKeywords(
593                                    sql, "lower(title)", StringPool.LIKE, false, titles);
594                            sql = CustomSQLUtil.replaceKeywords(
595                                    sql, "description", StringPool.LIKE, false, descriptions);
596                            sql = CustomSQLUtil.replaceKeywords(
597                                    sql, "content", StringPool.LIKE, false, contents);
598    
599                            if (status == WorkflowConstants.STATUS_ANY) {
600                                    sql = StringUtil.replace(sql, "(status = ?) AND", "");
601                            }
602    
603                            if (Validator.isNull(type)) {
604                                    sql = StringUtil.replace(sql, _TYPE_SQL, StringPool.BLANK);
605                            }
606    
607                            if (isNullArray(structureIds)) {
608                                    sql = StringUtil.replace(
609                                            sql, _STRUCTURE_ID_SQL, StringPool.BLANK);
610                            }
611                            else {
612                                    sql = CustomSQLUtil.replaceKeywords(
613                                            sql, "structureId", StringPool.LIKE, false, structureIds);
614                            }
615    
616                            if (isNullArray(templateIds)) {
617                                    sql = StringUtil.replace(
618                                            sql, _TEMPLATE_ID_SQL, StringPool.BLANK);
619                            }
620                            else {
621                                    sql = CustomSQLUtil.replaceKeywords(
622                                            sql, "templateId", StringPool.LIKE, false, templateIds);
623                            }
624    
625                            sql = CustomSQLUtil.replaceAndOperator(sql, andOperator);
626    
627                            if (inlineSQLHelper) {
628                                    sql = InlineSQLHelperUtil.replacePermissionCheck(
629                                            sql, JournalArticle.class.getName(),
630                                            "JournalArticle.resourcePrimKey", groupId);
631    
632                                    sql = StringUtil.replace(
633                                            sql, "(companyId", "(JournalArticle.companyId");
634                            }
635    
636                            SQLQuery q = session.createSQLQuery(sql);
637    
638                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
639    
640                            QueryPos qPos = QueryPos.getInstance(q);
641    
642                            qPos.add(companyId);
643    
644                            if (groupId > 0) {
645                                    qPos.add(groupId);
646                            }
647    
648                            qPos.add(classNameId);
649                            qPos.add(articleIds, 2);
650    
651                            if ((version != null) && (version > 0)) {
652                                    qPos.add(version);
653                            }
654    
655                            qPos.add(titles, 2);
656                            qPos.add(descriptions, 2);
657                            qPos.add(contents, 2);
658                            qPos.add(displayDateGT_TS);
659                            qPos.add(displayDateGT_TS);
660                            qPos.add(displayDateLT_TS);
661                            qPos.add(displayDateLT_TS);
662    
663                            if (status != WorkflowConstants.STATUS_ANY) {
664                                    qPos.add(status);
665                            }
666    
667                            qPos.add(reviewDate_TS);
668                            qPos.add(reviewDate_TS);
669    
670                            if (Validator.isNotNull(type)) {
671                                    qPos.add(type);
672                                    qPos.add(type);
673                            }
674    
675                            if (!isNullArray(structureIds)) {
676                                    qPos.add(structureIds, 2);
677                            }
678    
679                            if (!isNullArray(templateIds)) {
680                                    qPos.add(templateIds, 2);
681                            }
682    
683                            qPos.add(companyId);
684    
685                            Iterator<Long> itr = q.iterate();
686    
687                            if (itr.hasNext()) {
688                                    Long count = itr.next();
689    
690                                    if (count != null) {
691                                            return count.intValue();
692                                    }
693                            }
694    
695                            return 0;
696                    }
697                    catch (Exception e) {
698                            throw new SystemException(e);
699                    }
700                    finally {
701                            closeSession(session);
702                    }
703            }
704    
705            protected List<JournalArticle> doFindByC_G_C_A_V_T_D_C_T_S_T_D_S_R(
706                            long companyId, long groupId, long classNameId, String[] articleIds,
707                            Double version, String[] titles, String[] descriptions,
708                            String[] contents, String type, String[] structureIds,
709                            String[] templateIds, Date displayDateGT, Date displayDateLT,
710                            int status, Date reviewDate, boolean andOperator, int start,
711                            int end, OrderByComparator orderByComparator,
712                            boolean inlineSQLHelper)
713                    throws SystemException {
714    
715                    articleIds = CustomSQLUtil.keywords(articleIds, false);
716                    titles = CustomSQLUtil.keywords(titles);
717                    descriptions = CustomSQLUtil.keywords(descriptions, false);
718                    contents = CustomSQLUtil.keywords(contents, false);
719                    structureIds = CustomSQLUtil.keywords(structureIds, false);
720                    templateIds = CustomSQLUtil.keywords(templateIds, false);
721                    Timestamp displayDateGT_TS = CalendarUtil.getTimestamp(displayDateGT);
722                    Timestamp displayDateLT_TS = CalendarUtil.getTimestamp(displayDateLT);
723                    Timestamp reviewDate_TS = CalendarUtil.getTimestamp(reviewDate);
724    
725                    Session session = null;
726    
727                    try {
728                            session = openSession();
729    
730                            String sql = CustomSQLUtil.get(FIND_BY_C_G_C_A_V_T_D_C_T_S_T_D_S_R);
731    
732                            if (groupId <= 0) {
733                                    sql = StringUtil.replace(sql, "(groupId = ?) AND", "");
734                            }
735    
736                            sql = CustomSQLUtil.replaceKeywords(
737                                    sql, "articleId", StringPool.LIKE, false, articleIds);
738    
739                            if ((version == null) || (version <= 0)) {
740                                    sql = StringUtil.replace(
741                                            sql, "(version = ?) [$AND_OR_CONNECTOR$]", "");
742                            }
743    
744                            sql = CustomSQLUtil.replaceKeywords(
745                                    sql, "lower(title)", StringPool.LIKE, false, titles);
746                            sql = CustomSQLUtil.replaceKeywords(
747                                    sql, "description", StringPool.LIKE, false, descriptions);
748                            sql = CustomSQLUtil.replaceKeywords(
749                                    sql, "content", StringPool.LIKE, false, contents);
750    
751                            if (status == WorkflowConstants.STATUS_ANY) {
752                                    sql = StringUtil.replace(sql, "(status = ?) AND", "");
753                            }
754    
755                            if (Validator.isNull(type)) {
756                                    sql = StringUtil.replace(sql, _TYPE_SQL, StringPool.BLANK);
757                            }
758    
759                            if (isNullArray(structureIds)) {
760                                    sql = StringUtil.replace(
761                                            sql, _STRUCTURE_ID_SQL, StringPool.BLANK);
762                            }
763                            else {
764                                    sql = CustomSQLUtil.replaceKeywords(
765                                            sql, "structureId", StringPool.LIKE, false, structureIds);
766                            }
767    
768                            if (isNullArray(templateIds)) {
769                                    sql = StringUtil.replace(
770                                            sql, _TEMPLATE_ID_SQL, StringPool.BLANK);
771                            }
772                            else {
773                                    sql = CustomSQLUtil.replaceKeywords(
774                                            sql, "templateId", StringPool.LIKE, false, templateIds);
775                            }
776    
777                            sql = CustomSQLUtil.replaceAndOperator(sql, andOperator);
778                            sql = CustomSQLUtil.replaceOrderBy(sql, orderByComparator);
779    
780                            if (inlineSQLHelper) {
781                                    sql = InlineSQLHelperUtil.replacePermissionCheck(
782                                            sql, JournalArticle.class.getName(),
783                                            "JournalArticle.resourcePrimKey", groupId);
784    
785                                    sql = StringUtil.replace(
786                                            sql, "(companyId", "(JournalArticle.companyId");
787                            }
788    
789                            SQLQuery q = session.createSQLQuery(sql);
790    
791                            q.addEntity("JournalArticle", JournalArticleImpl.class);
792    
793                            QueryPos qPos = QueryPos.getInstance(q);
794    
795                            qPos.add(companyId);
796    
797                            if (groupId > 0) {
798                                    qPos.add(groupId);
799                            }
800    
801                            qPos.add(classNameId);
802                            qPos.add(articleIds, 2);
803    
804                            if ((version != null) && (version > 0)) {
805                                    qPos.add(version);
806                            }
807    
808                            qPos.add(titles, 2);
809                            qPos.add(descriptions, 2);
810                            qPos.add(contents, 2);
811                            qPos.add(displayDateGT_TS);
812                            qPos.add(displayDateGT_TS);
813                            qPos.add(displayDateLT_TS);
814                            qPos.add(displayDateLT_TS);
815    
816                            if (status != WorkflowConstants.STATUS_ANY) {
817                                    qPos.add(status);
818                            }
819    
820                            qPos.add(reviewDate_TS);
821                            qPos.add(reviewDate_TS);
822    
823                            if (Validator.isNotNull(type)) {
824                                    qPos.add(type);
825                                    qPos.add(type);
826                            }
827    
828                            if (!isNullArray(structureIds)) {
829                                    qPos.add(structureIds, 2);
830                            }
831    
832                            if (!isNullArray(templateIds)) {
833                                    qPos.add(templateIds, 2);
834                            }
835    
836                            qPos.add(companyId);
837    
838                            return (List<JournalArticle>)QueryUtil.list(
839                                    q, getDialect(), start, end);
840                    }
841                    catch (Exception e) {
842                            throw new SystemException(e);
843                    }
844                    finally {
845                            closeSession(session);
846                    }
847            }
848    
849            protected JournalArticle getLatestArticle(
850                            long groupId, String articleId, int status)
851                    throws SystemException {
852    
853                    List<JournalArticle> articles = null;
854    
855                    if (status == WorkflowConstants.STATUS_ANY) {
856                            articles = JournalArticleUtil.findByG_A(groupId, articleId, 0, 1);
857                    }
858                    else {
859                            articles = JournalArticleUtil.findByG_A_ST(
860                                    groupId, articleId, status, 0, 1);
861                    }
862    
863                    if (articles.isEmpty()) {
864                            return null;
865                    }
866    
867                    return articles.get(0);
868            }
869    
870            protected boolean isNullArray(Object[] array) {
871                    if ((array == null) || (array.length == 0)) {
872                            return true;
873                    }
874    
875                    for (Object obj : array) {
876                            if (Validator.isNotNull(obj)) {
877                                    return false;
878                            }
879                    }
880    
881                    return true;
882            }
883    
884            private static final String _STRUCTURE_ID_SQL =
885                    "(structureId LIKE ? [$AND_OR_NULL_CHECK$]) [$AND_OR_CONNECTOR$]";
886    
887            private static final String _TEMPLATE_ID_SQL =
888                    "(templateId LIKE ? [$AND_OR_NULL_CHECK$]) [$AND_OR_CONNECTOR$]";
889    
890            private static final String _TYPE_SQL =
891                    "(type_ = ? [$AND_OR_NULL_CHECK$]) [$AND_OR_CONNECTOR$]";
892    
893    }