001    /**
002     * Copyright (c) 2000-2012 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_G_F =
053                    JournalArticleFinder.class.getName() + ".countByG_F";
054    
055            public static final String COUNT_BY_G_F_S =
056                    JournalArticleFinder.class.getName() + ".countByG_F_S";
057    
058            public static final String COUNT_BY_C_G_F_C_A_V_T_D_C_T_S_T_D_S_R =
059                    JournalArticleFinder.class.getName() +
060                            ".countByC_G_F_C_A_V_T_D_C_T_S_T_D_S_R";
061    
062            public static final String FIND_BY_EXPIRATION_DATE =
063                    JournalArticleFinder.class.getName() + ".findByExpirationDate";
064    
065            public static final String FIND_BY_REVIEW_DATE =
066                    JournalArticleFinder.class.getName() + ".findByReviewDate";
067    
068            public static final String FIND_BY_R_D =
069                    JournalArticleFinder.class.getName() + ".findByR_D";
070    
071            public static final String FIND_BY_C_G_F_C_A_V_T_D_C_T_S_T_D_S_R =
072                    JournalArticleFinder.class.getName() +
073                            ".findByC_G_F_C_A_V_T_D_C_T_S_T_D_S_R";
074    
075            public int countByKeywords(
076                            long companyId, long groupId, List<Long> folderIds,
077                            long classNameId, String keywords, Double version, String type,
078                            String structureId, String templateId, Date displayDateGT,
079                            Date displayDateLT, int status, Date reviewDate)
080                    throws SystemException {
081    
082                    String[] articleIds = null;
083                    String[] titles = null;
084                    String[] descriptions = null;
085                    String[] contents = null;
086                    String[] structureIds = CustomSQLUtil.keywords(structureId, false);
087                    String[] templateIds = CustomSQLUtil.keywords(templateId, false);
088                    boolean andOperator = false;
089    
090                    if (Validator.isNotNull(keywords)) {
091                            articleIds = CustomSQLUtil.keywords(keywords, false);
092                            titles = CustomSQLUtil.keywords(keywords);
093                            descriptions = CustomSQLUtil.keywords(keywords, false);
094                            contents = CustomSQLUtil.keywords(keywords, false);
095                    }
096                    else {
097                            andOperator = true;
098                    }
099    
100                    return doCountByC_G_F_C_A_V_T_D_C_T_S_T_D_S_R(
101                            companyId, groupId, folderIds, classNameId, articleIds, version,
102                            titles, descriptions, contents, type, structureIds, templateIds,
103                            displayDateGT, displayDateLT, status, reviewDate, andOperator,
104                            false);
105            }
106    
107            public int countByG_F_S(long groupId, List<Long> folderIds, int status)
108                    throws SystemException {
109    
110                    return doCountByG_F_S(groupId, folderIds, status, true);
111            }
112    
113            public int countByC_G_F_C_A_V_T_D_C_T_S_T_D_S_R(
114                            long companyId, long groupId, List<Long> folderIds,
115                            long classNameId, String articleId, Double version, String title,
116                            String description, String content, String type, String structureId,
117                            String templateId, Date displayDateGT, Date displayDateLT,
118                            int status, Date reviewDate, boolean andOperator)
119                    throws SystemException {
120    
121                    String[] structureIds = CustomSQLUtil.keywords(structureId, false);
122                    String[] templateIds = CustomSQLUtil.keywords(templateId, false);
123    
124                    return countByC_G_F_C_A_V_T_D_C_T_S_T_D_S_R(
125                            companyId, groupId, folderIds, classNameId, articleId, version,
126                            title, description, content, type, structureIds, templateIds,
127                            displayDateGT, displayDateLT, status, reviewDate, andOperator);
128            }
129    
130            public int countByC_G_F_C_A_V_T_D_C_T_S_T_D_S_R(
131                            long companyId, long groupId, List<Long> folderIds,
132                            long classNameId, String articleId, Double version, String title,
133                            String description, String content, String type,
134                            String[] structureIds, String[] templateIds, Date displayDateGT,
135                            Date displayDateLT, int status, Date reviewDate,
136                            boolean andOperator)
137                    throws SystemException {
138    
139                    String[] articleIds = CustomSQLUtil.keywords(articleId, false);
140                    String[] titles = CustomSQLUtil.keywords(title);
141                    String[] descriptions = CustomSQLUtil.keywords(description, false);
142                    String[] contents = CustomSQLUtil.keywords(content, false);
143    
144                    return countByC_G_F_C_A_V_T_D_C_T_S_T_D_S_R(
145                            companyId, groupId, folderIds, classNameId, articleIds, version,
146                            titles, descriptions, contents, type, structureIds, templateIds,
147                            displayDateGT, displayDateLT, status, reviewDate, andOperator);
148            }
149    
150            public int countByC_G_F_C_A_V_T_D_C_T_S_T_D_S_R(
151                            long companyId, long groupId, List<Long> folderIds,
152                            long classNameId, String[] articleIds, Double version,
153                            String[] titles, String[] descriptions, String[] contents,
154                            String type, String[] structureIds, String[] templateIds,
155                            Date displayDateGT, Date displayDateLT, int status, Date reviewDate,
156                            boolean andOperator)
157                    throws SystemException {
158    
159                    return doCountByC_G_F_C_A_V_T_D_C_T_S_T_D_S_R(
160                            companyId, groupId, folderIds, classNameId, articleIds, version,
161                            titles, descriptions, contents, type, structureIds, templateIds,
162                            displayDateGT, displayDateLT, status, reviewDate, andOperator,
163                            false);
164            }
165    
166            public int filterCountByKeywords(
167                            long companyId, long groupId, List<Long> folderIds,
168                            long classNameId, String keywords, Double version, String type,
169                            String structureId, String templateId, Date displayDateGT,
170                            Date displayDateLT, int status, Date reviewDate)
171                    throws SystemException {
172    
173                    String[] articleIds = null;
174                    String[] titles = null;
175                    String[] descriptions = null;
176                    String[] contents = null;
177                    String[] structureIds = CustomSQLUtil.keywords(structureId, false);
178                    String[] templateIds = CustomSQLUtil.keywords(templateId, false);
179                    boolean andOperator = false;
180    
181                    if (Validator.isNotNull(keywords)) {
182                            articleIds = CustomSQLUtil.keywords(keywords, false);
183                            titles = CustomSQLUtil.keywords(keywords);
184                            descriptions = CustomSQLUtil.keywords(keywords, false);
185                            contents = CustomSQLUtil.keywords(keywords, false);
186                    }
187                    else {
188                            andOperator = true;
189                    }
190    
191                    return doCountByC_G_F_C_A_V_T_D_C_T_S_T_D_S_R(
192                            companyId, groupId, folderIds, classNameId, articleIds, version,
193                            titles, descriptions, contents, type, structureIds, templateIds,
194                            displayDateGT, displayDateLT, status, reviewDate, andOperator,
195                            true);
196            }
197    
198            public int filterCountByG_F_S(
199                            long groupId, List<Long> folderIds, int status)
200                    throws SystemException {
201    
202                    return doCountByG_F_S(groupId, folderIds, status, false);
203            }
204    
205            public int filterCountByC_G_F_C_A_V_T_D_C_T_S_T_D_S_R(
206                            long companyId, long groupId, List<Long> folderIds,
207                            long classNameId, String articleId, Double version, String title,
208                            String description, String content, String type, String structureId,
209                            String templateId, Date displayDateGT, Date displayDateLT,
210                            int status, Date reviewDate, boolean andOperator)
211                    throws SystemException {
212    
213                    String[] structureIds = CustomSQLUtil.keywords(structureId, false);
214                    String[] templateIds = CustomSQLUtil.keywords(templateId, false);
215    
216                    return filterCountByC_G_F_C_A_V_T_D_C_T_S_T_D_S_R(
217                            companyId, groupId, folderIds, classNameId, articleId, version,
218                            title, description, content, type, structureIds, templateIds,
219                            displayDateGT, displayDateLT, status, reviewDate, andOperator);
220            }
221    
222            public int filterCountByC_G_F_C_A_V_T_D_C_T_S_T_D_S_R(
223                            long companyId, long groupId, List<Long> folderIds,
224                            long classNameId, String articleId, Double version, String title,
225                            String description, String content, String type,
226                            String[] structureIds, String[] templateIds, Date displayDateGT,
227                            Date displayDateLT, int status, Date reviewDate,
228                            boolean andOperator)
229                    throws SystemException {
230    
231                    String[] articleIds = CustomSQLUtil.keywords(articleId, false);
232                    String[] titles = CustomSQLUtil.keywords(title);
233                    String[] descriptions = CustomSQLUtil.keywords(description, false);
234                    String[] contents = CustomSQLUtil.keywords(content, false);
235    
236                    return filterCountByC_G_F_C_A_V_T_D_C_T_S_T_D_S_R(
237                            companyId, groupId, folderIds, classNameId, articleIds, version,
238                            titles, descriptions, contents, type, structureIds, templateIds,
239                            displayDateGT, displayDateLT, status, reviewDate, andOperator);
240            }
241    
242            public int filterCountByC_G_F_C_A_V_T_D_C_T_S_T_D_S_R(
243                            long companyId, long groupId, List<Long> folderIds,
244                            long classNameId, String[] articleIds, Double version,
245                            String[] titles, String[] descriptions, String[] contents,
246                            String type, String[] structureIds, String[] templateIds,
247                            Date displayDateGT, Date displayDateLT, int status, Date reviewDate,
248                            boolean andOperator)
249                    throws SystemException {
250    
251                    return doCountByC_G_F_C_A_V_T_D_C_T_S_T_D_S_R(
252                            companyId, groupId, folderIds, classNameId, articleIds, version,
253                            titles, descriptions, contents, type, structureIds, templateIds,
254                            displayDateGT, displayDateLT, status, reviewDate, andOperator,
255                            true);
256            }
257    
258            public List<JournalArticle> filterFindByKeywords(
259                            long companyId, long groupId, List<Long> folderIds,
260                            long classNameId, String keywords, Double version, String type,
261                            String structureId, String templateId, Date displayDateGT,
262                            Date displayDateLT, int status, Date reviewDate, int start, int end,
263                            OrderByComparator orderByComparator)
264                    throws SystemException {
265    
266                    String[] articleIds = null;
267                    String[] titles = null;
268                    String[] descriptions = null;
269                    String[] contents = null;
270                    String[] structureIds = CustomSQLUtil.keywords(structureId, false);
271                    String[] templateIds = CustomSQLUtil.keywords(templateId, false);
272                    boolean andOperator = false;
273    
274                    if (Validator.isNotNull(keywords)) {
275                            articleIds = CustomSQLUtil.keywords(keywords, false);
276                            titles = CustomSQLUtil.keywords(keywords);
277                            descriptions = CustomSQLUtil.keywords(keywords, false);
278                            contents = CustomSQLUtil.keywords(keywords, false);
279                    }
280                    else {
281                            andOperator = true;
282                    }
283    
284                    return doFindByC_G_F_C_A_V_T_D_C_T_S_T_D_S_R(
285                            companyId, groupId, folderIds, classNameId, articleIds, version,
286                            titles, descriptions, contents, type, structureIds, templateIds,
287                            displayDateGT, displayDateLT, status, reviewDate, andOperator,
288                            start, end, orderByComparator, true);
289            }
290    
291            public List<JournalArticle> filterFindByC_G_F_C_A_V_T_D_C_T_S_T_D_S_R(
292                            long companyId, long groupId, List<Long> folderIds,
293                            long classNameId, String articleId, Double version, String title,
294                            String description, String content, String type, String structureId,
295                            String templateId, Date displayDateGT, Date displayDateLT,
296                            int status, Date reviewDate, boolean andOperator, int start,
297                            int end, OrderByComparator orderByComparator)
298                    throws SystemException {
299    
300                    String[] structureIds = CustomSQLUtil.keywords(structureId, false);
301                    String[] templateIds = CustomSQLUtil.keywords(templateId, false);
302    
303                    return filterFindByC_G_F_C_A_V_T_D_C_T_S_T_D_S_R(
304                            companyId, groupId, folderIds, classNameId, articleId, version,
305                            title, description, content, type, structureIds, templateIds,
306                            displayDateGT, displayDateLT, status, reviewDate, andOperator,
307                            start, end, orderByComparator);
308            }
309    
310            public List<JournalArticle> filterFindByC_G_F_C_A_V_T_D_C_T_S_T_D_S_R(
311                            long companyId, long groupId, List<Long> folderIds,
312                            long classNameId, String articleId, Double version, String title,
313                            String description, String content, String type,
314                            String[] structureIds, String[] templateIds, Date displayDateGT,
315                            Date displayDateLT, int status, Date reviewDate,
316                            boolean andOperator, int start, int end,
317                            OrderByComparator orderByComparator)
318                    throws SystemException {
319    
320                    String[] articleIds = CustomSQLUtil.keywords(articleId, false);
321                    String[] titles = CustomSQLUtil.keywords(title);
322                    String[] descriptions = CustomSQLUtil.keywords(description, false);
323                    String[] contents = CustomSQLUtil.keywords(content, false);
324    
325                    return filterFindByC_G_F_C_A_V_T_D_C_T_S_T_D_S_R(
326                            companyId, groupId, folderIds, classNameId, articleIds, version,
327                            titles, descriptions, contents, type, structureIds, templateIds,
328                            displayDateGT, displayDateLT, status, reviewDate, andOperator,
329                            start, end, orderByComparator);
330            }
331    
332            public List<JournalArticle> filterFindByC_G_F_C_A_V_T_D_C_T_S_T_D_S_R(
333                            long companyId, long groupId, List<Long> folderIds,
334                            long classNameId, String[] articleIds, Double version,
335                            String[] titles, String[] descriptions, String[] contents,
336                            String type, String[] structureIds, String[] templateIds,
337                            Date displayDateGT, Date displayDateLT, int status, Date reviewDate,
338                            boolean andOperator, int start, int end,
339                            OrderByComparator orderByComparator)
340                    throws SystemException {
341    
342                    return doFindByC_G_F_C_A_V_T_D_C_T_S_T_D_S_R(
343                            companyId, groupId, folderIds, classNameId, articleIds, version,
344                            titles, descriptions, contents, type, structureIds, templateIds,
345                            displayDateGT, displayDateLT, status, reviewDate, andOperator,
346                            start, end, orderByComparator, true);
347            }
348    
349            public List<JournalArticle> findByExpirationDate(
350                            long classNameId, int status, Date expirationDateLT)
351                    throws SystemException {
352    
353                    Timestamp expirationDateLT_TS = CalendarUtil.getTimestamp(
354                            expirationDateLT);
355    
356                    Session session = null;
357    
358                    try {
359                            session = openSession();
360    
361                            String sql = CustomSQLUtil.get(FIND_BY_EXPIRATION_DATE);
362    
363                            if (status == WorkflowConstants.STATUS_ANY) {
364                                    sql = StringUtil.replace(
365                                            sql, "(status = ?) AND", StringPool.BLANK);
366                            }
367    
368                            SQLQuery q = session.createSQLQuery(sql);
369    
370                            q.addEntity("JournalArticle", JournalArticleImpl.class);
371    
372                            QueryPos qPos = QueryPos.getInstance(q);
373    
374                            qPos.add(classNameId);
375    
376                            if (status != WorkflowConstants.STATUS_ANY) {
377                                    qPos.add(status);
378                            }
379    
380                            qPos.add(expirationDateLT_TS);
381    
382                            return q.list(true);
383                    }
384                    catch (Exception e) {
385                            throw new SystemException(e);
386                    }
387                    finally {
388                            closeSession(session);
389                    }
390            }
391    
392            public List<JournalArticle> findByKeywords(
393                            long companyId, long groupId, List<Long> folderIds,
394                            long classNameId, String keywords, Double version, String type,
395                            String structureId, String templateId, Date displayDateGT,
396                            Date displayDateLT, int status, Date reviewDate, int start, int end,
397                            OrderByComparator orderByComparator)
398                    throws SystemException {
399    
400                    String[] articleIds = null;
401                    String[] titles = null;
402                    String[] descriptions = null;
403                    String[] contents = null;
404                    String[] structureIds = CustomSQLUtil.keywords(structureId, false);
405                    String[] templateIds = CustomSQLUtil.keywords(templateId, false);
406                    boolean andOperator = false;
407    
408                    if (Validator.isNotNull(keywords)) {
409                            articleIds = CustomSQLUtil.keywords(keywords, false);
410                            titles = CustomSQLUtil.keywords(keywords);
411                            descriptions = CustomSQLUtil.keywords(keywords, false);
412                            contents = CustomSQLUtil.keywords(keywords, false);
413                    }
414                    else {
415                            andOperator = true;
416                    }
417    
418                    return findByC_G_F_C_A_V_T_D_C_T_S_T_D_S_R(
419                            companyId, groupId, folderIds, classNameId, articleIds, version,
420                            titles, descriptions, contents, type, structureIds, templateIds,
421                            displayDateGT, displayDateLT, status, reviewDate, andOperator,
422                            start, end, orderByComparator);
423            }
424    
425            public List<JournalArticle> findByReviewDate(
426                            long classNameId, Date reviewDateLT, Date reviewDateGT)
427                    throws SystemException {
428    
429                    Timestamp reviewDateLT_TS = CalendarUtil.getTimestamp(reviewDateLT);
430                    Timestamp reviewDateGT_TS = CalendarUtil.getTimestamp(reviewDateGT);
431    
432                    Session session = null;
433                    try {
434                            session = openSession();
435    
436                            String sql = CustomSQLUtil.get(FIND_BY_REVIEW_DATE);
437    
438                            SQLQuery q = session.createSQLQuery(sql);
439    
440                            q.addEntity("JournalArticle", JournalArticleImpl.class);
441    
442                            QueryPos qPos = QueryPos.getInstance(q);
443    
444                            qPos.add(classNameId);
445                            qPos.add(reviewDateGT_TS);
446                            qPos.add(reviewDateLT_TS);
447    
448                            return q.list(true);
449                    }
450                    catch (Exception e) {
451                            throw new SystemException(e);
452                    }
453                    finally {
454                            closeSession(session);
455                    }
456            }
457    
458            public JournalArticle findByR_D(long resourcePrimKey, Date displayDate)
459                    throws NoSuchArticleException, SystemException {
460    
461                    Timestamp displayDate_TS = CalendarUtil.getTimestamp(displayDate);
462    
463                    Session session = null;
464    
465                    try {
466                            session = openSession();
467    
468                            String sql = CustomSQLUtil.get(FIND_BY_R_D);
469    
470                            SQLQuery q = session.createSQLQuery(sql);
471    
472                            q.addEntity("JournalArticle", JournalArticleImpl.class);
473    
474                            QueryPos qPos = QueryPos.getInstance(q);
475    
476                            qPos.add(resourcePrimKey);
477                            qPos.add(displayDate_TS);
478    
479                            List<JournalArticle> articles = q.list();
480    
481                            if (!articles.isEmpty()) {
482                                    return articles.get(0);
483                            }
484                    }
485                    catch (Exception e) {
486                            throw new SystemException(e);
487                    }
488                    finally {
489                            closeSession(session);
490                    }
491    
492                    StringBundler sb = new StringBundler(6);
493    
494                    sb.append("No JournalArticle exists with the key ");
495                    sb.append("{resourcePrimKey=");
496                    sb.append(resourcePrimKey);
497                    sb.append(", displayDate=");
498                    sb.append(displayDate);
499                    sb.append("}");
500    
501                    throw new NoSuchArticleException(sb.toString());
502            }
503    
504            public List<JournalArticle> findByC_G_F_C_A_V_T_D_C_T_S_T_D_S_R(
505                            long companyId, long groupId, List<Long> folderIds,
506                            long classNameId, String articleId, Double version, String title,
507                            String description, String content, String type, String structureId,
508                            String templateId, Date displayDateGT, Date displayDateLT,
509                            int status, Date reviewDate, boolean andOperator, int start,
510                            int end, OrderByComparator orderByComparator)
511                    throws SystemException {
512    
513                    String[] articleIds = CustomSQLUtil.keywords(articleId, false);
514                    String[] titles = CustomSQLUtil.keywords(title);
515                    String[] descriptions = CustomSQLUtil.keywords(description, false);
516                    String[] contents = CustomSQLUtil.keywords(content, false);
517                    String[] structureIds = CustomSQLUtil.keywords(structureId, false);
518                    String[] templateIds = CustomSQLUtil.keywords(templateId, false);
519    
520                    return findByC_G_F_C_A_V_T_D_C_T_S_T_D_S_R(
521                            companyId, groupId, folderIds, classNameId, articleIds, version,
522                            titles, descriptions, contents, type, structureIds, templateIds,
523                            displayDateGT, displayDateLT, status, reviewDate, andOperator,
524                            start, end, orderByComparator);
525            }
526    
527            public List<JournalArticle> findByC_G_F_C_A_V_T_D_C_T_S_T_D_S_R(
528                            long companyId, long groupId, List<Long> folderIds,
529                            long classNameId, String articleId, Double version, String title,
530                            String description, String content, String type,
531                            String[] structureIds, String[] templateIds, Date displayDateGT,
532                            Date displayDateLT, int status, Date reviewDate,
533                            boolean andOperator, int start, int end,
534                            OrderByComparator orderByComparator)
535                    throws SystemException {
536    
537                    String[] articleIds = CustomSQLUtil.keywords(articleId, false);
538                    String[] titles = CustomSQLUtil.keywords(title);
539                    String[] descriptions = CustomSQLUtil.keywords(description, false);
540                    String[] contents = CustomSQLUtil.keywords(content, false);
541    
542                    return findByC_G_F_C_A_V_T_D_C_T_S_T_D_S_R(
543                            companyId, groupId, folderIds, classNameId, articleIds, version,
544                            titles, descriptions, contents, type, structureIds, templateIds,
545                            displayDateGT, displayDateLT, status, reviewDate, andOperator,
546                            start, end, orderByComparator);
547            }
548    
549            public List<JournalArticle> findByC_G_F_C_A_V_T_D_C_T_S_T_D_S_R(
550                            long companyId, long groupId, List<Long> folderIds,
551                            long classNameId, String[] articleIds, Double version,
552                            String[] titles, String[] descriptions, String[] contents,
553                            String type, String[] structureIds, String[] templateIds,
554                            Date displayDateGT, Date displayDateLT, int status, Date reviewDate,
555                            boolean andOperator, int start, int end,
556                            OrderByComparator orderByComparator)
557                    throws SystemException {
558    
559                    return doFindByC_G_F_C_A_V_T_D_C_T_S_T_D_S_R(
560                            companyId, groupId, folderIds, classNameId, articleIds, version,
561                            titles, descriptions, contents, type, structureIds, templateIds,
562                            displayDateGT, displayDateLT, status, reviewDate, andOperator,
563                            start, end, orderByComparator, false);
564            }
565    
566            protected int doCountByG_F_S(
567                            long groupId, List<Long> folderIds, int status,
568                            boolean inlineSQLHelper)
569                    throws SystemException {
570    
571                    Session session = null;
572    
573                    try {
574                            session = openSession();
575    
576                            String sql = null;
577    
578                            String table = "JournalArticle";
579    
580                            if (status == WorkflowConstants.STATUS_ANY) {
581                                    sql = CustomSQLUtil.get(COUNT_BY_G_F);
582                            }
583                            else {
584                                    sql = CustomSQLUtil.get(COUNT_BY_G_F_S);
585                            }
586    
587                            if (inlineSQLHelper) {
588                                    sql = InlineSQLHelperUtil.replacePermissionCheck(
589                                            sql, JournalArticle.class.getName(),
590                                            "JournalArticle.resourcePrimKey", groupId);
591                            }
592    
593                            sql = StringUtil.replace(
594                                    sql, "[$FOLDER_ID$]", getFolderIds(folderIds, table));
595    
596                            SQLQuery q = session.createSQLQuery(sql);
597    
598                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
599    
600                            QueryPos qPos = QueryPos.getInstance(q);
601    
602                            qPos.add(groupId);
603    
604                            if (status != WorkflowConstants.STATUS_ANY) {
605                                    qPos.add(status);
606                            }
607    
608                            for (int i = 0; i < folderIds.size(); i++) {
609                                    Long folderId = folderIds.get(i);
610    
611                                    qPos.add(folderId);
612                            }
613    
614                            Iterator<Long> itr = q.iterate();
615    
616                            if (itr.hasNext()) {
617                                    Long count = itr.next();
618    
619                                    if (count != null) {
620                                            return count.intValue();
621                                    }
622                            }
623    
624                            return 0;
625                    }
626                    catch (Exception e) {
627                            throw new SystemException(e);
628                    }
629                    finally {
630                            closeSession(session);
631                    }
632            }
633    
634            protected int doCountByC_G_F_C_A_V_T_D_C_T_S_T_D_S_R(
635                            long companyId, long groupId, List<Long> folderIds,
636                            long classNameId, String[] articleIds, Double version,
637                            String[] titles, String[] descriptions, String[] contents,
638                            String type, String[] structureIds, String[] templateIds,
639                            Date displayDateGT, Date displayDateLT, int status, Date reviewDate,
640                            boolean andOperator, boolean inlineSQLHelper)
641                    throws SystemException {
642    
643                    articleIds = CustomSQLUtil.keywords(articleIds, false);
644                    titles = CustomSQLUtil.keywords(titles);
645                    descriptions = CustomSQLUtil.keywords(descriptions, false);
646                    contents = CustomSQLUtil.keywords(contents, false);
647                    structureIds = CustomSQLUtil.keywords(structureIds, false);
648                    templateIds = CustomSQLUtil.keywords(templateIds, false);
649                    Timestamp displayDateGT_TS = CalendarUtil.getTimestamp(displayDateGT);
650                    Timestamp displayDateLT_TS = CalendarUtil.getTimestamp(displayDateLT);
651                    Timestamp reviewDate_TS = CalendarUtil.getTimestamp(reviewDate);
652    
653                    Session session = null;
654    
655                    try {
656                            session = openSession();
657    
658                            String sql = CustomSQLUtil.get(
659                                    COUNT_BY_C_G_F_C_A_V_T_D_C_T_S_T_D_S_R);
660    
661                            if (groupId <= 0) {
662                                    sql = StringUtil.replace(
663                                            sql, "(groupId = ?) AND", StringPool.BLANK);
664                            }
665    
666                            if (folderIds.isEmpty()) {
667                                    sql = StringUtil.replace(
668                                            sql, "([$FOLDER_ID$]) AND", StringPool.BLANK);
669                            }
670                            else {
671                                    sql = StringUtil.replace(
672                                            sql, "[$FOLDER_ID$]",
673                                            getFolderIds(folderIds, "JournalArticle"));
674                            }
675    
676                            sql = CustomSQLUtil.replaceKeywords(
677                                    sql, "articleId", StringPool.LIKE, false, articleIds);
678    
679                            if ((version == null) || (version <= 0)) {
680                                    sql = StringUtil.replace(
681                                            sql, "(version = ?) [$AND_OR_CONNECTOR$]",
682                                            StringPool.BLANK);
683                            }
684    
685                            sql = CustomSQLUtil.replaceKeywords(
686                                    sql, "lower(title)", StringPool.LIKE, false, titles);
687                            sql = CustomSQLUtil.replaceKeywords(
688                                    sql, "description", StringPool.LIKE, false, descriptions);
689                            sql = CustomSQLUtil.replaceKeywords(
690                                    sql, "content", StringPool.LIKE, false, contents);
691    
692                            if (status == WorkflowConstants.STATUS_ANY) {
693                                    sql = StringUtil.replace(
694                                            sql, "(status = ?) AND", StringPool.BLANK);
695                            }
696    
697                            if (Validator.isNull(type)) {
698                                    sql = StringUtil.replace(sql, _TYPE_SQL, StringPool.BLANK);
699                            }
700    
701                            if (isNullArray(structureIds)) {
702                                    sql = StringUtil.replace(
703                                            sql, _STRUCTURE_ID_SQL, StringPool.BLANK);
704                            }
705                            else {
706                                    sql = CustomSQLUtil.replaceKeywords(
707                                            sql, "structureId", StringPool.LIKE, false, structureIds);
708                            }
709    
710                            if (isNullArray(templateIds)) {
711                                    sql = StringUtil.replace(
712                                            sql, _TEMPLATE_ID_SQL, StringPool.BLANK);
713                            }
714                            else {
715                                    sql = CustomSQLUtil.replaceKeywords(
716                                            sql, "templateId", StringPool.LIKE, false, templateIds);
717                            }
718    
719                            sql = CustomSQLUtil.replaceAndOperator(sql, andOperator);
720    
721                            if ((articleIds != null) &&
722                                    ((articleIds.length > 1) ||
723                                     ((articleIds.length == 1) && (articleIds[0] != null))) &&
724                                    (version == null)) {
725    
726                                    sql = StringUtil.replace(
727                                            sql, "MAX(version) AS tempVersion",
728                                            "version AS tempVersion");
729                                    sql = StringUtil.replace(
730                                            sql, "[$GROUP_BY_CLAUSE$]", StringPool.BLANK);
731                            }
732                            else {
733                                    sql = StringUtil.replace(
734                                            sql, "[$GROUP_BY_CLAUSE$]", "GROUP BY groupId, articleId");
735                            }
736    
737                            if (inlineSQLHelper) {
738                                    sql = InlineSQLHelperUtil.replacePermissionCheck(
739                                            sql, JournalArticle.class.getName(),
740                                            "JournalArticle.resourcePrimKey", groupId);
741    
742                                    sql = StringUtil.replace(
743                                            sql, "(companyId", "(JournalArticle.companyId");
744                            }
745    
746                            SQLQuery q = session.createSQLQuery(sql);
747    
748                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
749    
750                            QueryPos qPos = QueryPos.getInstance(q);
751    
752                            qPos.add(companyId);
753    
754                            if (groupId > 0) {
755                                    qPos.add(groupId);
756                            }
757    
758                            for (long folderId : folderIds) {
759                                    qPos.add(folderId);
760                            }
761    
762                            qPos.add(classNameId);
763                            qPos.add(articleIds, 2);
764    
765                            if ((version != null) && (version > 0)) {
766                                    qPos.add(version);
767                            }
768    
769                            qPos.add(titles, 2);
770                            qPos.add(descriptions, 2);
771                            qPos.add(contents, 2);
772                            qPos.add(displayDateGT_TS);
773                            qPos.add(displayDateGT_TS);
774                            qPos.add(displayDateLT_TS);
775                            qPos.add(displayDateLT_TS);
776    
777                            if (status != WorkflowConstants.STATUS_ANY) {
778                                    qPos.add(status);
779                            }
780    
781                            qPos.add(reviewDate_TS);
782                            qPos.add(reviewDate_TS);
783    
784                            if (Validator.isNotNull(type)) {
785                                    qPos.add(type);
786                                    qPos.add(type);
787                            }
788    
789                            if (!isNullArray(structureIds)) {
790                                    qPos.add(structureIds, 2);
791                            }
792    
793                            if (!isNullArray(templateIds)) {
794                                    qPos.add(templateIds, 2);
795                            }
796    
797                            qPos.add(companyId);
798    
799                            Iterator<Long> itr = q.iterate();
800    
801                            if (itr.hasNext()) {
802                                    Long count = itr.next();
803    
804                                    if (count != null) {
805                                            return count.intValue();
806                                    }
807                            }
808    
809                            return 0;
810                    }
811                    catch (Exception e) {
812                            throw new SystemException(e);
813                    }
814                    finally {
815                            closeSession(session);
816                    }
817            }
818    
819            protected List<JournalArticle> doFindByC_G_F_C_A_V_T_D_C_T_S_T_D_S_R(
820                            long companyId, long groupId, List<Long> folderIds,
821                            long classNameId, String[] articleIds, Double version,
822                            String[] titles, String[] descriptions, String[] contents,
823                            String type, String[] structureIds, String[] templateIds,
824                            Date displayDateGT, Date displayDateLT, int status, Date reviewDate,
825                            boolean andOperator, int start, int end,
826                            OrderByComparator orderByComparator, boolean inlineSQLHelper)
827                    throws SystemException {
828    
829                    articleIds = CustomSQLUtil.keywords(articleIds, false);
830                    titles = CustomSQLUtil.keywords(titles);
831                    descriptions = CustomSQLUtil.keywords(descriptions, false);
832                    contents = CustomSQLUtil.keywords(contents, false);
833                    structureIds = CustomSQLUtil.keywords(structureIds, false);
834                    templateIds = CustomSQLUtil.keywords(templateIds, false);
835                    Timestamp displayDateGT_TS = CalendarUtil.getTimestamp(displayDateGT);
836                    Timestamp displayDateLT_TS = CalendarUtil.getTimestamp(displayDateLT);
837                    Timestamp reviewDate_TS = CalendarUtil.getTimestamp(reviewDate);
838    
839                    Session session = null;
840    
841                    try {
842                            session = openSession();
843    
844                            String sql = CustomSQLUtil.get(
845                                    FIND_BY_C_G_F_C_A_V_T_D_C_T_S_T_D_S_R);
846    
847                            if (groupId <= 0) {
848                                    sql = StringUtil.replace(
849                                            sql, "(groupId = ?) AND", StringPool.BLANK);
850                            }
851    
852                            if (folderIds.isEmpty()) {
853                                    sql = StringUtil.replace(
854                                            sql, "([$FOLDER_ID$]) AND", StringPool.BLANK);
855                            }
856                            else {
857                                    sql = StringUtil.replace(
858                                            sql, "[$FOLDER_ID$]",
859                                            getFolderIds(folderIds, "JournalArticle"));
860                            }
861    
862                            sql = CustomSQLUtil.replaceKeywords(
863                                    sql, "articleId", StringPool.LIKE, false, articleIds);
864    
865                            if ((version == null) || (version <= 0)) {
866                                    sql = StringUtil.replace(
867                                            sql, "(version = ?) [$AND_OR_CONNECTOR$]",
868                                            StringPool.BLANK);
869                            }
870    
871                            sql = CustomSQLUtil.replaceKeywords(
872                                    sql, "lower(title)", StringPool.LIKE, false, titles);
873                            sql = CustomSQLUtil.replaceKeywords(
874                                    sql, "description", StringPool.LIKE, false, descriptions);
875                            sql = CustomSQLUtil.replaceKeywords(
876                                    sql, "content", StringPool.LIKE, false, contents);
877    
878                            if (status == WorkflowConstants.STATUS_ANY) {
879                                    sql = StringUtil.replace(
880                                            sql, "(status = ?) AND", StringPool.BLANK);
881                            }
882    
883                            if (Validator.isNull(type)) {
884                                    sql = StringUtil.replace(sql, _TYPE_SQL, StringPool.BLANK);
885                            }
886    
887                            if (isNullArray(structureIds)) {
888                                    sql = StringUtil.replace(
889                                            sql, _STRUCTURE_ID_SQL, StringPool.BLANK);
890                            }
891                            else {
892                                    sql = CustomSQLUtil.replaceKeywords(
893                                            sql, "structureId", StringPool.LIKE, false, structureIds);
894                            }
895    
896                            if (isNullArray(templateIds)) {
897                                    sql = StringUtil.replace(
898                                            sql, _TEMPLATE_ID_SQL, StringPool.BLANK);
899                            }
900                            else {
901                                    sql = CustomSQLUtil.replaceKeywords(
902                                            sql, "templateId", StringPool.LIKE, false, templateIds);
903                            }
904    
905                            sql = CustomSQLUtil.replaceAndOperator(sql, andOperator);
906    
907                            if ((articleIds != null) &&
908                                    ((articleIds.length > 1) ||
909                                     ((articleIds.length == 1) && (articleIds[0] != null))) &&
910                                    (version == null)) {
911    
912                                    sql = StringUtil.replace(
913                                            sql, "MAX(version) AS tempVersion",
914                                            "version AS tempVersion");
915                                    sql = StringUtil.replace(
916                                            sql, "[$GROUP_BY_CLAUSE$]", StringPool.BLANK);
917                            }
918                            else {
919                                    sql = StringUtil.replace(
920                                            sql, "[$GROUP_BY_CLAUSE$]", "GROUP BY groupId, articleId");
921                            }
922    
923                            sql = CustomSQLUtil.replaceOrderBy(sql, orderByComparator);
924    
925                            if (inlineSQLHelper) {
926                                    sql = InlineSQLHelperUtil.replacePermissionCheck(
927                                            sql, JournalArticle.class.getName(),
928                                            "JournalArticle.resourcePrimKey", groupId);
929    
930                                    sql = StringUtil.replace(
931                                            sql, "(companyId", "(JournalArticle.companyId");
932                            }
933    
934                            SQLQuery q = session.createSQLQuery(sql);
935    
936                            q.addEntity("JournalArticle", JournalArticleImpl.class);
937    
938                            QueryPos qPos = QueryPos.getInstance(q);
939    
940                            qPos.add(companyId);
941    
942                            if (groupId > 0) {
943                                    qPos.add(groupId);
944                            }
945    
946                            for (long folderId : folderIds) {
947                                    qPos.add(folderId);
948                            }
949    
950                            qPos.add(classNameId);
951                            qPos.add(articleIds, 2);
952    
953                            if ((version != null) && (version > 0)) {
954                                    qPos.add(version);
955                            }
956    
957                            qPos.add(titles, 2);
958                            qPos.add(descriptions, 2);
959                            qPos.add(contents, 2);
960                            qPos.add(displayDateGT_TS);
961                            qPos.add(displayDateGT_TS);
962                            qPos.add(displayDateLT_TS);
963                            qPos.add(displayDateLT_TS);
964    
965                            if (status != WorkflowConstants.STATUS_ANY) {
966                                    qPos.add(status);
967                            }
968    
969                            qPos.add(reviewDate_TS);
970                            qPos.add(reviewDate_TS);
971    
972                            if (Validator.isNotNull(type)) {
973                                    qPos.add(type);
974                                    qPos.add(type);
975                            }
976    
977                            if (!isNullArray(structureIds)) {
978                                    qPos.add(structureIds, 2);
979                            }
980    
981                            if (!isNullArray(templateIds)) {
982                                    qPos.add(templateIds, 2);
983                            }
984    
985                            qPos.add(companyId);
986    
987                            return (List<JournalArticle>)QueryUtil.list(
988                                    q, getDialect(), start, end);
989                    }
990                    catch (Exception e) {
991                            throw new SystemException(e);
992                    }
993                    finally {
994                            closeSession(session);
995                    }
996            }
997    
998            protected String getFolderIds(List<Long> folderIds, String table) {
999                    if (folderIds.isEmpty()) {
1000                            return StringPool.BLANK;
1001                    }
1002    
1003                    StringBundler sb = new StringBundler(folderIds.size() * 2 - 1);
1004    
1005                    for (int i = 0; i < folderIds.size(); i++) {
1006                            sb.append(table);
1007                            sb.append(".folderId = ? ");
1008    
1009                            if ((i + 1) != folderIds.size()) {
1010                                    sb.append(WHERE_OR);
1011                            }
1012                    }
1013    
1014                    return sb.toString();
1015            }
1016    
1017            protected JournalArticle getLatestArticle(
1018                            long groupId, String articleId, int status)
1019                    throws SystemException {
1020    
1021                    List<JournalArticle> articles = null;
1022    
1023                    if (status == WorkflowConstants.STATUS_ANY) {
1024                            articles = JournalArticleUtil.findByG_A(groupId, articleId, 0, 1);
1025                    }
1026                    else {
1027                            articles = JournalArticleUtil.findByG_A_ST(
1028                                    groupId, articleId, status, 0, 1);
1029                    }
1030    
1031                    if (articles.isEmpty()) {
1032                            return null;
1033                    }
1034    
1035                    return articles.get(0);
1036            }
1037    
1038            protected boolean isNullArray(Object[] array) {
1039                    if ((array == null) || (array.length == 0)) {
1040                            return true;
1041                    }
1042    
1043                    for (Object obj : array) {
1044                            if (Validator.isNotNull(obj)) {
1045                                    return false;
1046                            }
1047                    }
1048    
1049                    return true;
1050            }
1051    
1052            private static final String _STRUCTURE_ID_SQL =
1053                    "(structureId LIKE ? [$AND_OR_NULL_CHECK$]) [$AND_OR_CONNECTOR$]";
1054    
1055            private static final String _TEMPLATE_ID_SQL =
1056                    "(templateId LIKE ? [$AND_OR_NULL_CHECK$]) [$AND_OR_CONNECTOR$]";
1057    
1058            private static final String _TYPE_SQL =
1059                    "(type_ = ? [$AND_OR_NULL_CHECK$]) [$AND_OR_CONNECTOR$]";
1060    
1061    }