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.QueryDefinition;
018    import com.liferay.portal.kernel.dao.orm.QueryPos;
019    import com.liferay.portal.kernel.dao.orm.QueryUtil;
020    import com.liferay.portal.kernel.dao.orm.SQLQuery;
021    import com.liferay.portal.kernel.dao.orm.Session;
022    import com.liferay.portal.kernel.dao.orm.Type;
023    import com.liferay.portal.kernel.exception.SystemException;
024    import com.liferay.portal.kernel.util.CalendarUtil;
025    import com.liferay.portal.kernel.util.OrderByComparator;
026    import com.liferay.portal.kernel.util.StringBundler;
027    import com.liferay.portal.kernel.util.StringPool;
028    import com.liferay.portal.kernel.util.StringUtil;
029    import com.liferay.portal.kernel.util.Validator;
030    import com.liferay.portal.kernel.workflow.WorkflowConstants;
031    import com.liferay.portal.security.permission.InlineSQLHelperUtil;
032    import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
033    import com.liferay.portlet.journal.NoSuchArticleException;
034    import com.liferay.portlet.journal.model.JournalArticle;
035    import com.liferay.portlet.journal.model.JournalArticleConstants;
036    import com.liferay.portlet.journal.model.impl.JournalArticleImpl;
037    import com.liferay.util.dao.orm.CustomSQLUtil;
038    
039    import java.sql.Timestamp;
040    
041    import java.util.Date;
042    import java.util.Iterator;
043    import java.util.List;
044    
045    /**
046     * @author Brian Wing Shun Chan
047     * @author Raymond Aug??
048     * @author Connor McKay
049     */
050    public class JournalArticleFinderImpl
051            extends BasePersistenceImpl<JournalArticle>
052            implements JournalArticleFinder {
053    
054            public static final String COUNT_BY_G_F =
055                    JournalArticleFinder.class.getName() + ".countByG_F";
056    
057            public static final String COUNT_BY_G_C_S =
058                    JournalArticleFinder.class.getName() + ".countByG_C_S";
059    
060            public static final String COUNT_BY_G_U_F_C =
061                    JournalArticleFinder.class.getName() + ".countByG_U_F_C";
062    
063            public static final String COUNT_BY_C_G_F_C_A_V_T_D_C_T_S_T_D_R =
064                    JournalArticleFinder.class.getName() +
065                            ".countByC_G_F_C_A_V_T_D_C_T_S_T_D_R";
066    
067            public static final String FIND_BY_EXPIRATION_DATE =
068                    JournalArticleFinder.class.getName() + ".findByExpirationDate";
069    
070            public static final String FIND_BY_REVIEW_DATE =
071                    JournalArticleFinder.class.getName() + ".findByReviewDate";
072    
073            public static final String FIND_BY_R_D =
074                    JournalArticleFinder.class.getName() + ".findByR_D";
075    
076            public static final String FIND_BY_G_F =
077                    JournalArticleFinder.class.getName() + ".findByG_F";
078    
079            public static final String FIND_BY_G_C_S =
080                    JournalArticleFinder.class.getName() + ".findByG_C_S";
081    
082            public static final String FIND_BY_G_U_F_C =
083                    JournalArticleFinder.class.getName() + ".findByG_U_F_C";
084    
085            public static final String FIND_BY_C_G_F_C_A_V_T_D_C_T_S_T_D_R =
086                    JournalArticleFinder.class.getName() +
087                            ".findByC_G_F_C_A_V_T_D_C_T_S_T_D_R";
088    
089            @Override
090            public int countByKeywords(
091                            long companyId, long groupId, List<Long> folderIds,
092                            long classNameId, String keywords, Double version, String type,
093                            String ddmStructureKey, String ddmTemplateKey, Date displayDateGT,
094                            Date displayDateLT, int status, Date reviewDate)
095                    throws SystemException {
096    
097                    String[] articleIds = null;
098                    String[] titles = null;
099                    String[] descriptions = null;
100                    String[] contents = null;
101                    String[] ddmStructureKeys = CustomSQLUtil.keywords(
102                            ddmStructureKey, false);
103                    String[] ddmTemplateKeys = CustomSQLUtil.keywords(
104                            ddmTemplateKey, false);
105                    boolean andOperator = false;
106    
107                    if (Validator.isNotNull(keywords)) {
108                            articleIds = CustomSQLUtil.keywords(keywords, false);
109                            titles = CustomSQLUtil.keywords(keywords);
110                            descriptions = CustomSQLUtil.keywords(keywords, false);
111                            contents = CustomSQLUtil.keywords(keywords, false);
112                    }
113                    else {
114                            andOperator = true;
115                    }
116    
117                    QueryDefinition queryDefinition = new QueryDefinition(status);
118    
119                    return doCountByC_G_F_C_A_V_T_D_C_T_S_T_D_R(
120                            companyId, groupId, folderIds, classNameId, articleIds, version,
121                            titles, descriptions, contents, type, ddmStructureKeys,
122                            ddmTemplateKeys, displayDateGT, displayDateLT, reviewDate,
123                            andOperator, queryDefinition, false);
124            }
125    
126            @Override
127            public int countByG_F(
128                            long groupId, List<Long> folderIds, QueryDefinition queryDefinition)
129                    throws SystemException {
130    
131                    return doCountByG_F(groupId, folderIds, queryDefinition, false);
132            }
133    
134            @Override
135            public int countByG_C_S(
136                            long groupId, long classNameId, String ddmStructureKey,
137                            QueryDefinition queryDefinition)
138                    throws SystemException {
139    
140                    return doCountByG_C_S(
141                            groupId, classNameId, ddmStructureKey, queryDefinition, false);
142            }
143    
144            @Override
145            public int countByG_U_F_C(
146                            long groupId, long userId, List<Long> folderIds, long classNameId,
147                            QueryDefinition queryDefinition)
148                    throws SystemException {
149    
150                    return doCountByG_U_F_C(
151                            groupId, userId, folderIds, classNameId, queryDefinition, false);
152            }
153    
154            @Override
155            public int countByC_G_F_C_A_V_T_D_C_T_S_T_D_R(
156                            long companyId, long groupId, List<Long> folderIds,
157                            long classNameId, String articleId, Double version, String title,
158                            String description, String content, String type,
159                            String ddmStructureKey, String ddmTemplateKey, Date displayDateGT,
160                            Date displayDateLT, Date reviewDate, boolean andOperator,
161                            QueryDefinition queryDefinition)
162                    throws SystemException {
163    
164                    String[] ddmStructureKeys = CustomSQLUtil.keywords(
165                            ddmStructureKey, false);
166                    String[] ddmTemplateKeys = CustomSQLUtil.keywords(
167                            ddmTemplateKey, false);
168    
169                    return countByC_G_F_C_A_V_T_D_C_T_S_T_D_R(
170                            companyId, groupId, folderIds, classNameId, articleId, version,
171                            title, description, content, type, ddmStructureKeys,
172                            ddmTemplateKeys, displayDateGT, displayDateLT, reviewDate,
173                            andOperator, queryDefinition);
174            }
175    
176            @Override
177            public int countByC_G_F_C_A_V_T_D_C_T_S_T_D_R(
178                            long companyId, long groupId, List<Long> folderIds,
179                            long classNameId, String articleId, Double version, String title,
180                            String description, String content, String type,
181                            String[] ddmStructureKeys, String[] ddmTemplateKeys,
182                            Date displayDateGT, Date displayDateLT, Date reviewDate,
183                            boolean andOperator, QueryDefinition queryDefinition)
184                    throws SystemException {
185    
186                    String[] articleIds = CustomSQLUtil.keywords(articleId, false);
187                    String[] titles = CustomSQLUtil.keywords(title);
188                    String[] descriptions = CustomSQLUtil.keywords(description, false);
189                    String[] contents = CustomSQLUtil.keywords(content, false);
190    
191                    return countByC_G_F_C_A_V_T_D_C_T_S_T_D_R(
192                            companyId, groupId, folderIds, classNameId, articleIds, version,
193                            titles, descriptions, contents, type, ddmStructureKeys,
194                            ddmTemplateKeys, displayDateGT, displayDateLT, reviewDate,
195                            andOperator, queryDefinition);
196            }
197    
198            @Override
199            public int countByC_G_F_C_A_V_T_D_C_T_S_T_D_R(
200                            long companyId, long groupId, List<Long> folderIds,
201                            long classNameId, String[] articleIds, Double version,
202                            String[] titles, String[] descriptions, String[] contents,
203                            String type, String[] ddmStructureKeys, String[] ddmTemplateKeys,
204                            Date displayDateGT, Date displayDateLT, Date reviewDate,
205                            boolean andOperator, QueryDefinition queryDefinition)
206                    throws SystemException {
207    
208                    return doCountByC_G_F_C_A_V_T_D_C_T_S_T_D_R(
209                            companyId, groupId, folderIds, classNameId, articleIds, version,
210                            titles, descriptions, contents, type, ddmStructureKeys,
211                            ddmTemplateKeys, displayDateGT, displayDateLT, reviewDate,
212                            andOperator, queryDefinition, false);
213            }
214    
215            @Override
216            public int filterCountByKeywords(
217                            long companyId, long groupId, List<Long> folderIds,
218                            long classNameId, String keywords, Double version, String type,
219                            String ddmStructureKey, String ddmTemplateKey, Date displayDateGT,
220                            Date displayDateLT, int status, Date reviewDate)
221                    throws SystemException {
222    
223                    String[] articleIds = null;
224                    String[] titles = null;
225                    String[] descriptions = null;
226                    String[] contents = null;
227                    String[] ddmStructureKeys = CustomSQLUtil.keywords(
228                            ddmStructureKey, false);
229                    String[] ddmTemplateKeys = CustomSQLUtil.keywords(
230                            ddmTemplateKey, false);
231                    boolean andOperator = false;
232    
233                    if (Validator.isNotNull(keywords)) {
234                            articleIds = CustomSQLUtil.keywords(keywords, false);
235                            titles = CustomSQLUtil.keywords(keywords);
236                            descriptions = CustomSQLUtil.keywords(keywords, false);
237                            contents = CustomSQLUtil.keywords(keywords, false);
238                    }
239                    else {
240                            andOperator = true;
241                    }
242    
243                    QueryDefinition queryDefinition = new QueryDefinition(status);
244    
245                    return doCountByC_G_F_C_A_V_T_D_C_T_S_T_D_R(
246                            companyId, groupId, folderIds, classNameId, articleIds, version,
247                            titles, descriptions, contents, type, ddmStructureKeys,
248                            ddmTemplateKeys, displayDateGT, displayDateLT, reviewDate,
249                            andOperator, queryDefinition, true);
250            }
251    
252            @Override
253            public int filterCountByG_F(
254                            long groupId, List<Long> folderIds, QueryDefinition queryDefinition)
255                    throws SystemException {
256    
257                    return doCountByG_F(groupId, folderIds, queryDefinition, true);
258            }
259    
260            @Override
261            public int filterCountByG_C_S(
262                            long groupId, long classNameId, String ddmStructureKey,
263                            QueryDefinition queryDefinition)
264                    throws SystemException {
265    
266                    return doCountByG_C_S(
267                            groupId, classNameId, ddmStructureKey, queryDefinition, true);
268            }
269    
270            @Override
271            public int filterCountByG_U_F_C(
272                            long groupId, long userId, List<Long> folderIds, long classNameId,
273                            QueryDefinition queryDefinition)
274                    throws SystemException {
275    
276                    return doCountByG_U_F_C(
277                            groupId, userId, folderIds, classNameId, queryDefinition, true);
278            }
279    
280            @Override
281            public int filterCountByC_G_F_C_A_V_T_D_C_T_S_T_D_R(
282                            long companyId, long groupId, List<Long> folderIds,
283                            long classNameId, String articleId, Double version, String title,
284                            String description, String content, String type,
285                            String ddmStructureKey, String ddmTemplateKey, Date displayDateGT,
286                            Date displayDateLT, Date reviewDate, boolean andOperator,
287                            QueryDefinition queryDefinition)
288                    throws SystemException {
289    
290                    String[] ddmStructureKeys = CustomSQLUtil.keywords(
291                            ddmStructureKey, false);
292                    String[] ddmTemplateKeys = CustomSQLUtil.keywords(
293                            ddmTemplateKey, false);
294    
295                    return filterCountByC_G_F_C_A_V_T_D_C_T_S_T_D_R(
296                            companyId, groupId, folderIds, classNameId, articleId, version,
297                            title, description, content, type, ddmStructureKeys,
298                            ddmTemplateKeys, displayDateGT, displayDateLT, reviewDate,
299                            andOperator, queryDefinition);
300            }
301    
302            @Override
303            public int filterCountByC_G_F_C_A_V_T_D_C_T_S_T_D_R(
304                            long companyId, long groupId, List<Long> folderIds,
305                            long classNameId, String articleId, Double version, String title,
306                            String description, String content, String type,
307                            String[] ddmStructureKeys, String[] ddmTemplateKeys,
308                            Date displayDateGT, Date displayDateLT, Date reviewDate,
309                            boolean andOperator, QueryDefinition queryDefinition)
310                    throws SystemException {
311    
312                    String[] articleIds = CustomSQLUtil.keywords(articleId, false);
313                    String[] titles = CustomSQLUtil.keywords(title);
314                    String[] descriptions = CustomSQLUtil.keywords(description, false);
315                    String[] contents = CustomSQLUtil.keywords(content, false);
316    
317                    return filterCountByC_G_F_C_A_V_T_D_C_T_S_T_D_R(
318                            companyId, groupId, folderIds, classNameId, articleIds, version,
319                            titles, descriptions, contents, type, ddmStructureKeys,
320                            ddmTemplateKeys, displayDateGT, displayDateLT, reviewDate,
321                            andOperator, queryDefinition);
322            }
323    
324            @Override
325            public int filterCountByC_G_F_C_A_V_T_D_C_T_S_T_D_R(
326                            long companyId, long groupId, List<Long> folderIds,
327                            long classNameId, String[] articleIds, Double version,
328                            String[] titles, String[] descriptions, String[] contents,
329                            String type, String[] ddmStructureKeys, String[] ddmTemplateKeys,
330                            Date displayDateGT, Date displayDateLT, Date reviewDate,
331                            boolean andOperator, QueryDefinition queryDefinition)
332                    throws SystemException {
333    
334                    return doCountByC_G_F_C_A_V_T_D_C_T_S_T_D_R(
335                            companyId, groupId, folderIds, classNameId, articleIds, version,
336                            titles, descriptions, contents, type, ddmStructureKeys,
337                            ddmTemplateKeys, displayDateGT, displayDateLT, reviewDate,
338                            andOperator, queryDefinition, true);
339            }
340    
341            @Override
342            public List<JournalArticle> filterFindByKeywords(
343                            long companyId, long groupId, List<Long> folderIds,
344                            long classNameId, String keywords, Double version, String type,
345                            String ddmStructureKey, String ddmTemplateKey, Date displayDateGT,
346                            Date displayDateLT, int status, Date reviewDate, int start, int end,
347                            OrderByComparator orderByComparator)
348                    throws SystemException {
349    
350                    String[] articleIds = null;
351                    String[] titles = null;
352                    String[] descriptions = null;
353                    String[] contents = null;
354                    String[] ddmStructureKeys = CustomSQLUtil.keywords(
355                            ddmStructureKey, false);
356                    String[] ddmTemplateKeys = CustomSQLUtil.keywords(
357                            ddmTemplateKey, false);
358                    boolean andOperator = false;
359    
360                    if (Validator.isNotNull(keywords)) {
361                            articleIds = CustomSQLUtil.keywords(keywords, false);
362                            titles = CustomSQLUtil.keywords(keywords);
363                            descriptions = CustomSQLUtil.keywords(keywords, false);
364                            contents = CustomSQLUtil.keywords(keywords, false);
365                    }
366                    else {
367                            andOperator = true;
368                    }
369    
370                    QueryDefinition queryDefinition = new QueryDefinition(
371                            status, start, end, orderByComparator);
372    
373                    return doFindByC_G_F_C_A_V_T_D_C_T_S_T_D_R(
374                            companyId, groupId, folderIds, classNameId, articleIds, version,
375                            titles, descriptions, contents, type, ddmStructureKeys,
376                            ddmTemplateKeys, displayDateGT, displayDateLT, reviewDate,
377                            andOperator, queryDefinition, true);
378            }
379    
380            @Override
381            public List<JournalArticle> filterFindByG_F(
382                            long groupId, List<Long> folderIds, QueryDefinition queryDefinition)
383                    throws SystemException {
384    
385                    return doFindByG_F(groupId, folderIds, queryDefinition, true);
386            }
387    
388            @Override
389            public List<JournalArticle> filterFindByG_C_S(
390                            long groupId, long classNameId, String ddmStructureKey,
391                            QueryDefinition queryDefinition)
392                    throws SystemException {
393    
394                    return doFindByG_C_S(
395                            groupId, classNameId, ddmStructureKey, queryDefinition, true);
396            }
397    
398            @Override
399            public List<JournalArticle> filterFindByG_U_F_C(
400                            long groupId, long userId, List<Long> folderIds, long classNameId,
401                            QueryDefinition queryDefinition)
402                    throws SystemException {
403    
404                    return doFindByG_U_F_C(
405                            groupId, userId, folderIds, classNameId, queryDefinition, true);
406            }
407    
408            @Override
409            public List<JournalArticle> filterFindByC_G_F_C_A_V_T_D_C_T_S_T_D_R(
410                            long companyId, long groupId, List<Long> folderIds,
411                            long classNameId, String articleId, Double version, String title,
412                            String description, String content, String type,
413                            String ddmStructureKey, String ddmTemplateKey, Date displayDateGT,
414                            Date displayDateLT, Date reviewDate, boolean andOperator,
415                            QueryDefinition queryDefinition)
416                    throws SystemException {
417    
418                    String[] ddmStructureKeys = CustomSQLUtil.keywords(
419                            ddmStructureKey, false);
420                    String[] ddmTemplateKeys = CustomSQLUtil.keywords(
421                            ddmTemplateKey, false);
422    
423                    return filterFindByC_G_F_C_A_V_T_D_C_T_S_T_D_R(
424                            companyId, groupId, folderIds, classNameId, articleId, version,
425                            title, description, content, type, ddmStructureKeys,
426                            ddmTemplateKeys, displayDateGT, displayDateLT, reviewDate,
427                            andOperator, queryDefinition);
428            }
429    
430            @Override
431            public List<JournalArticle> filterFindByC_G_F_C_A_V_T_D_C_T_S_T_D_R(
432                            long companyId, long groupId, List<Long> folderIds,
433                            long classNameId, String articleId, Double version, String title,
434                            String description, String content, String type,
435                            String[] ddmStructureKeys, String[] ddmTemplateKeys,
436                            Date displayDateGT, Date displayDateLT, Date reviewDate,
437                            boolean andOperator, QueryDefinition queryDefinition)
438                    throws SystemException {
439    
440                    String[] articleIds = CustomSQLUtil.keywords(articleId, false);
441                    String[] titles = CustomSQLUtil.keywords(title);
442                    String[] descriptions = CustomSQLUtil.keywords(description, false);
443                    String[] contents = CustomSQLUtil.keywords(content, false);
444    
445                    return filterFindByC_G_F_C_A_V_T_D_C_T_S_T_D_R(
446                            companyId, groupId, folderIds, classNameId, articleIds, version,
447                            titles, descriptions, contents, type, ddmStructureKeys,
448                            ddmTemplateKeys, displayDateGT, displayDateLT, reviewDate,
449                            andOperator, queryDefinition);
450            }
451    
452            @Override
453            public List<JournalArticle> filterFindByC_G_F_C_A_V_T_D_C_T_S_T_D_R(
454                            long companyId, long groupId, List<Long> folderIds,
455                            long classNameId, String[] articleIds, Double version,
456                            String[] titles, String[] descriptions, String[] contents,
457                            String type, String[] ddmStructureKeys, String[] ddmTemplateKeys,
458                            Date displayDateGT, Date displayDateLT, Date reviewDate,
459                            boolean andOperator, QueryDefinition queryDefinition)
460                    throws SystemException {
461    
462                    return doFindByC_G_F_C_A_V_T_D_C_T_S_T_D_R(
463                            companyId, groupId, folderIds, classNameId, articleIds, version,
464                            titles, descriptions, contents, type, ddmStructureKeys,
465                            ddmTemplateKeys, displayDateGT, displayDateLT, reviewDate,
466                            andOperator, queryDefinition, true);
467            }
468    
469            @Override
470            public List<JournalArticle> findByExpirationDate(
471                            long classNameId, Date expirationDateLT,
472                            QueryDefinition queryDefinition)
473                    throws SystemException {
474    
475                    Timestamp expirationDateLT_TS = CalendarUtil.getTimestamp(
476                            expirationDateLT);
477    
478                    Session session = null;
479    
480                    try {
481                            session = openSession();
482    
483                            String sql = CustomSQLUtil.get(
484                                    FIND_BY_EXPIRATION_DATE, queryDefinition);
485    
486                            SQLQuery q = session.createSQLQuery(sql);
487    
488                            q.addEntity(
489                                    JournalArticleImpl.TABLE_NAME, JournalArticleImpl.class);
490    
491                            QueryPos qPos = QueryPos.getInstance(q);
492    
493                            qPos.add(classNameId);
494                            qPos.add(queryDefinition.getStatus());
495                            qPos.add(expirationDateLT_TS);
496    
497                            return q.list(true);
498                    }
499                    catch (Exception e) {
500                            throw new SystemException(e);
501                    }
502                    finally {
503                            closeSession(session);
504                    }
505            }
506    
507            @Override
508            public List<JournalArticle> findByKeywords(
509                            long companyId, long groupId, List<Long> folderIds,
510                            long classNameId, String keywords, Double version, String type,
511                            String ddmStructureKey, String ddmTemplateKey, Date displayDateGT,
512                            Date displayDateLT, int status, Date reviewDate, int start, int end,
513                            OrderByComparator orderByComparator)
514                    throws SystemException {
515    
516                    String[] articleIds = null;
517                    String[] titles = null;
518                    String[] descriptions = null;
519                    String[] contents = null;
520                    String[] ddmStructureKeys = CustomSQLUtil.keywords(
521                            ddmStructureKey, false);
522                    String[] ddmTemplateKeys = CustomSQLUtil.keywords(
523                            ddmTemplateKey, false);
524                    boolean andOperator = false;
525    
526                    if (Validator.isNotNull(keywords)) {
527                            articleIds = CustomSQLUtil.keywords(keywords, false);
528                            titles = CustomSQLUtil.keywords(keywords);
529                            descriptions = CustomSQLUtil.keywords(keywords, false);
530                            contents = CustomSQLUtil.keywords(keywords, false);
531                    }
532                    else {
533                            andOperator = true;
534                    }
535    
536                    QueryDefinition queryDefinition = new QueryDefinition(
537                            status, start, end, orderByComparator);
538    
539                    return findByC_G_F_C_A_V_T_D_C_T_S_T_D_R(
540                            companyId, groupId, folderIds, classNameId, articleIds, version,
541                            titles, descriptions, contents, type, ddmStructureKeys,
542                            ddmTemplateKeys, displayDateGT, displayDateLT, reviewDate,
543                            andOperator, queryDefinition);
544            }
545    
546            @Override
547            public List<JournalArticle> findByReviewDate(
548                            long classNameId, Date reviewDateLT, Date reviewDateGT)
549                    throws SystemException {
550    
551                    Timestamp reviewDateLT_TS = CalendarUtil.getTimestamp(reviewDateLT);
552                    Timestamp reviewDateGT_TS = CalendarUtil.getTimestamp(reviewDateGT);
553    
554                    Session session = null;
555                    try {
556                            session = openSession();
557    
558                            String sql = CustomSQLUtil.get(FIND_BY_REVIEW_DATE);
559    
560                            SQLQuery q = session.createSQLQuery(sql);
561    
562                            q.addEntity(
563                                    JournalArticleImpl.TABLE_NAME, JournalArticleImpl.class);
564    
565                            QueryPos qPos = QueryPos.getInstance(q);
566    
567                            qPos.add(classNameId);
568                            qPos.add(reviewDateGT_TS);
569                            qPos.add(reviewDateLT_TS);
570    
571                            return q.list(true);
572                    }
573                    catch (Exception e) {
574                            throw new SystemException(e);
575                    }
576                    finally {
577                            closeSession(session);
578                    }
579            }
580    
581            @Override
582            public JournalArticle findByR_D(long resourcePrimKey, Date displayDate)
583                    throws NoSuchArticleException, SystemException {
584    
585                    Timestamp displayDate_TS = CalendarUtil.getTimestamp(displayDate);
586    
587                    Session session = null;
588    
589                    try {
590                            session = openSession();
591    
592                            String sql = CustomSQLUtil.get(FIND_BY_R_D);
593    
594                            SQLQuery q = session.createSQLQuery(sql);
595    
596                            q.addEntity(
597                                    JournalArticleImpl.TABLE_NAME, JournalArticleImpl.class);
598    
599                            QueryPos qPos = QueryPos.getInstance(q);
600    
601                            qPos.add(resourcePrimKey);
602                            qPos.add(displayDate_TS);
603    
604                            List<JournalArticle> articles = q.list();
605    
606                            if (!articles.isEmpty()) {
607                                    return articles.get(0);
608                            }
609                    }
610                    catch (Exception e) {
611                            throw new SystemException(e);
612                    }
613                    finally {
614                            closeSession(session);
615                    }
616    
617                    StringBundler sb = new StringBundler(6);
618    
619                    sb.append("No JournalArticle exists with the key ");
620                    sb.append("{resourcePrimKey=");
621                    sb.append(resourcePrimKey);
622                    sb.append(", displayDate=");
623                    sb.append(displayDate);
624                    sb.append("}");
625    
626                    throw new NoSuchArticleException(sb.toString());
627            }
628    
629            @Override
630            public List<JournalArticle> findByG_F(
631                            long groupId, List<Long> folderIds, QueryDefinition queryDefinition)
632                    throws SystemException {
633    
634                    return doFindByG_F(groupId, folderIds, queryDefinition, false);
635            }
636    
637            @Override
638            public List<JournalArticle> findByG_C_S(
639                            long groupId, long classNameId, String ddmStructureKey,
640                            QueryDefinition queryDefinition)
641                    throws SystemException {
642    
643                    return doFindByG_C_S(
644                            groupId, classNameId, ddmStructureKey, queryDefinition, false);
645            }
646    
647            @Override
648            public List<JournalArticle> findByG_U_F_C(
649                            long groupId, long userId, List<Long> folderIds, long classNameId,
650                            QueryDefinition queryDefinition)
651                    throws SystemException {
652    
653                    return doFindByG_U_F_C(
654                            groupId, userId, folderIds, classNameId, queryDefinition, false);
655            }
656    
657            @Override
658            public List<JournalArticle> findByC_G_F_C_A_V_T_D_C_T_S_T_D_R(
659                            long companyId, long groupId, List<Long> folderIds,
660                            long classNameId, String articleId, Double version, String title,
661                            String description, String content, String type,
662                            String ddmStructureKey, String ddmTemplateKey, Date displayDateGT,
663                            Date displayDateLT, Date reviewDate, boolean andOperator,
664                            QueryDefinition queryDefinition)
665                    throws SystemException {
666    
667                    String[] articleIds = CustomSQLUtil.keywords(articleId, false);
668                    String[] titles = CustomSQLUtil.keywords(title);
669                    String[] descriptions = CustomSQLUtil.keywords(description, false);
670                    String[] contents = CustomSQLUtil.keywords(content, false);
671                    String[] ddmStructureKeys = CustomSQLUtil.keywords(
672                            ddmStructureKey, false);
673                    String[] ddmTemplateKeys = CustomSQLUtil.keywords(
674                            ddmTemplateKey, false);
675    
676                    return findByC_G_F_C_A_V_T_D_C_T_S_T_D_R(
677                            companyId, groupId, folderIds, classNameId, articleIds, version,
678                            titles, descriptions, contents, type, ddmStructureKeys,
679                            ddmTemplateKeys, displayDateGT, displayDateLT, reviewDate,
680                            andOperator, queryDefinition);
681            }
682    
683            @Override
684            public List<JournalArticle> findByC_G_F_C_A_V_T_D_C_T_S_T_D_R(
685                            long companyId, long groupId, List<Long> folderIds,
686                            long classNameId, String articleId, Double version, String title,
687                            String description, String content, String type,
688                            String[] ddmStructureKeys, String[] ddmTemplateKeys,
689                            Date displayDateGT, Date displayDateLT, Date reviewDate,
690                            boolean andOperator, QueryDefinition queryDefinition)
691                    throws SystemException {
692    
693                    String[] articleIds = CustomSQLUtil.keywords(articleId, false);
694                    String[] titles = CustomSQLUtil.keywords(title);
695                    String[] descriptions = CustomSQLUtil.keywords(description, false);
696                    String[] contents = CustomSQLUtil.keywords(content, false);
697    
698                    return findByC_G_F_C_A_V_T_D_C_T_S_T_D_R(
699                            companyId, groupId, folderIds, classNameId, articleIds, version,
700                            titles, descriptions, contents, type, ddmStructureKeys,
701                            ddmTemplateKeys, displayDateGT, displayDateLT, reviewDate,
702                            andOperator, queryDefinition);
703            }
704    
705            @Override
706            public List<JournalArticle> findByC_G_F_C_A_V_T_D_C_T_S_T_D_R(
707                            long companyId, long groupId, List<Long> folderIds,
708                            long classNameId, String[] articleIds, Double version,
709                            String[] titles, String[] descriptions, String[] contents,
710                            String type, String[] ddmStructureKeys, String[] ddmTemplateKeys,
711                            Date displayDateGT, Date displayDateLT, Date reviewDate,
712                            boolean andOperator, QueryDefinition queryDefinition)
713                    throws SystemException {
714    
715                    return doFindByC_G_F_C_A_V_T_D_C_T_S_T_D_R(
716                            companyId, groupId, folderIds, classNameId, articleIds, version,
717                            titles, descriptions, contents, type, ddmStructureKeys,
718                            ddmTemplateKeys, displayDateGT, displayDateLT, reviewDate,
719                            andOperator, queryDefinition, false);
720            }
721    
722            protected int doCountByG_F(
723                            long groupId, List<Long> folderIds, QueryDefinition queryDefinition,
724                            boolean inlineSQLHelper)
725                    throws SystemException {
726    
727                    Session session = null;
728    
729                    try {
730                            session = openSession();
731    
732                            String sql = CustomSQLUtil.get(COUNT_BY_G_F, queryDefinition);
733    
734                            if (inlineSQLHelper) {
735                                    sql = InlineSQLHelperUtil.replacePermissionCheck(
736                                            sql, JournalArticle.class.getName(),
737                                            "JournalArticle.resourcePrimKey", groupId);
738                            }
739    
740                            sql = StringUtil.replace(
741                                    sql, "[$FOLDER_ID$]",
742                                    getFolderIds(folderIds, JournalArticleImpl.TABLE_NAME));
743    
744                            SQLQuery q = session.createSQLQuery(sql);
745    
746                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
747    
748                            QueryPos qPos = QueryPos.getInstance(q);
749    
750                            qPos.add(groupId);
751                            qPos.add(queryDefinition.getStatus());
752    
753                            for (int i = 0; i < folderIds.size(); i++) {
754                                    Long folderId = folderIds.get(i);
755    
756                                    qPos.add(folderId);
757                            }
758    
759                            Iterator<Long> itr = q.iterate();
760    
761                            if (itr.hasNext()) {
762                                    Long count = itr.next();
763    
764                                    if (count != null) {
765                                            return count.intValue();
766                                    }
767                            }
768    
769                            return 0;
770                    }
771                    catch (Exception e) {
772                            throw new SystemException(e);
773                    }
774                    finally {
775                            closeSession(session);
776                    }
777            }
778    
779            protected int doCountByG_C_S(
780                            long groupId, long classNameId, String ddmStructureKey,
781                            QueryDefinition queryDefinition, boolean inlineSQLHelper)
782                    throws SystemException {
783    
784                    Session session = null;
785    
786                    try {
787                            session = openSession();
788    
789                            String sql = CustomSQLUtil.get(
790                                    COUNT_BY_G_C_S, queryDefinition, "JournalArticle");
791    
792                            if (ddmStructureKey.equals(
793                                            String.valueOf(
794                                                    JournalArticleConstants.CLASSNAME_ID_DEFAULT))) {
795    
796                                    sql = StringUtil.replace(
797                                            sql, "(structureId = ?)",
798                                            "((structureId = ?) OR (structureId = '') OR " +
799                                                    "(structureId IS NULL))");
800                            }
801    
802                            if (inlineSQLHelper) {
803                                    sql = InlineSQLHelperUtil.replacePermissionCheck(
804                                            sql, JournalArticle.class.getName(),
805                                            "JournalArticle.resourcePrimKey", groupId);
806                            }
807    
808                            SQLQuery q = session.createSQLQuery(sql);
809    
810                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
811    
812                            QueryPos qPos = QueryPos.getInstance(q);
813    
814                            qPos.add(groupId);
815                            qPos.add(classNameId);
816                            qPos.add(ddmStructureKey);
817                            qPos.add(queryDefinition.getStatus());
818    
819                            Iterator<Long> itr = q.iterate();
820    
821                            if (itr.hasNext()) {
822                                    Long count = itr.next();
823    
824                                    if (count != null) {
825                                            return count.intValue();
826                                    }
827                            }
828    
829                            return 0;
830                    }
831                    catch (Exception e) {
832                            throw new SystemException(e);
833                    }
834                    finally {
835                            closeSession(session);
836                    }
837            }
838    
839            protected int doCountByG_U_F_C(
840                            long groupId, long userId, List<Long> folderIds, long classNameId,
841                            QueryDefinition queryDefinition, boolean inlineSQLHelper)
842                    throws SystemException {
843    
844                    Session session = null;
845    
846                    try {
847                            session = openSession();
848    
849                            String sql = CustomSQLUtil.get(
850                                    COUNT_BY_G_U_F_C, queryDefinition, "JournalArticle");
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, JournalArticleImpl.TABLE_NAME));
860                            }
861    
862                            if (inlineSQLHelper) {
863                                    sql = InlineSQLHelperUtil.replacePermissionCheck(
864                                            sql, JournalArticle.class.getName(),
865                                            "JournalArticle.resourcePrimKey", groupId);
866                            }
867    
868                            SQLQuery q = session.createSQLQuery(sql);
869    
870                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
871    
872                            QueryPos qPos = QueryPos.getInstance(q);
873    
874                            qPos.add(groupId);
875                            qPos.add(userId);
876    
877                            for (long folderId : folderIds) {
878                                    qPos.add(folderId);
879                            }
880    
881                            qPos.add(classNameId);
882                            qPos.add(queryDefinition.getStatus());
883    
884                            Iterator<Long> itr = q.iterate();
885    
886                            if (itr.hasNext()) {
887                                    Long count = itr.next();
888    
889                                    if (count != null) {
890                                            return count.intValue();
891                                    }
892                            }
893    
894                            return 0;
895                    }
896                    catch (Exception e) {
897                            throw new SystemException(e);
898                    }
899                    finally {
900                            closeSession(session);
901                    }
902            }
903    
904            protected int doCountByC_G_F_C_A_V_T_D_C_T_S_T_D_R(
905                            long companyId, long groupId, List<Long> folderIds,
906                            long classNameId, String[] articleIds, Double version,
907                            String[] titles, String[] descriptions, String[] contents,
908                            String type, String[] ddmStructureKeys, String[] ddmTemplateKeys,
909                            Date displayDateGT, Date displayDateLT, Date reviewDate,
910                            boolean andOperator, QueryDefinition queryDefinition,
911                            boolean inlineSQLHelper)
912                    throws SystemException {
913    
914                    articleIds = CustomSQLUtil.keywords(articleIds, false);
915                    titles = CustomSQLUtil.keywords(titles);
916                    descriptions = CustomSQLUtil.keywords(descriptions, false);
917                    contents = CustomSQLUtil.keywords(contents, false);
918                    ddmStructureKeys = CustomSQLUtil.keywords(ddmStructureKeys, false);
919                    ddmTemplateKeys = CustomSQLUtil.keywords(ddmTemplateKeys, false);
920                    Timestamp displayDateGT_TS = CalendarUtil.getTimestamp(displayDateGT);
921                    Timestamp displayDateLT_TS = CalendarUtil.getTimestamp(displayDateLT);
922                    Timestamp reviewDate_TS = CalendarUtil.getTimestamp(reviewDate);
923    
924                    Session session = null;
925    
926                    try {
927                            session = openSession();
928    
929                            String sql = CustomSQLUtil.get(
930                                    COUNT_BY_C_G_F_C_A_V_T_D_C_T_S_T_D_R, queryDefinition);
931    
932                            if (groupId <= 0) {
933                                    sql = StringUtil.replace(
934                                            sql, "(groupId = ?) AND", StringPool.BLANK);
935                            }
936    
937                            if (folderIds.isEmpty()) {
938                                    sql = StringUtil.replace(
939                                            sql, "([$FOLDER_ID$]) AND", StringPool.BLANK);
940                            }
941                            else {
942                                    sql = StringUtil.replace(
943                                            sql, "[$FOLDER_ID$]",
944                                            getFolderIds(folderIds, JournalArticleImpl.TABLE_NAME));
945                            }
946    
947                            sql = CustomSQLUtil.replaceKeywords(
948                                    sql, "articleId", StringPool.LIKE, false, articleIds);
949    
950                            if ((version == null) || (version <= 0)) {
951                                    sql = StringUtil.replace(
952                                            sql, "(version = ?) [$AND_OR_CONNECTOR$]",
953                                            StringPool.BLANK);
954                            }
955    
956                            sql = CustomSQLUtil.replaceKeywords(
957                                    sql, "lower(title)", StringPool.LIKE, false, titles);
958                            sql = CustomSQLUtil.replaceKeywords(
959                                    sql, "description", StringPool.LIKE, false, descriptions);
960                            sql = CustomSQLUtil.replaceKeywords(
961                                    sql, "content", StringPool.LIKE, false, contents);
962    
963                            if (Validator.isNull(type)) {
964                                    sql = StringUtil.replace(
965                                            sql, _TYPE_UNNAMESPACED_SQL, StringPool.BLANK);
966                            }
967    
968                            if (isNullArray(ddmStructureKeys)) {
969                                    sql = StringUtil.replace(
970                                            sql, _STRUCTURE_ID_UNNAMESPACED_SQL, StringPool.BLANK);
971                            }
972                            else {
973                                    sql = CustomSQLUtil.replaceKeywords(
974                                            sql, "structureId", StringPool.LIKE, false,
975                                            ddmStructureKeys);
976                            }
977    
978                            if (isNullArray(ddmTemplateKeys)) {
979                                    sql = StringUtil.replace(
980                                            sql, _TEMPLATE_ID_UNNAMESPACED_SQL, StringPool.BLANK);
981                            }
982                            else {
983                                    sql = CustomSQLUtil.replaceKeywords(
984                                            sql, "templateId", StringPool.LIKE, false, ddmTemplateKeys);
985                            }
986    
987                            sql = CustomSQLUtil.replaceAndOperator(sql, andOperator);
988    
989                            if (inlineSQLHelper) {
990                                    sql = InlineSQLHelperUtil.replacePermissionCheck(
991                                            sql, JournalArticle.class.getName(),
992                                            "JournalArticle.resourcePrimKey", groupId);
993    
994                                    sql = StringUtil.replace(
995                                            sql, "(companyId", "(JournalArticle.companyId");
996                            }
997    
998                            SQLQuery q = session.createSQLQuery(sql);
999    
1000                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
1001    
1002                            QueryPos qPos = QueryPos.getInstance(q);
1003    
1004                            if (groupId > 0) {
1005                                    qPos.add(groupId);
1006                            }
1007    
1008                            for (long folderId : folderIds) {
1009                                    qPos.add(folderId);
1010                            }
1011    
1012                            qPos.add(classNameId);
1013                            qPos.add(queryDefinition.getStatus());
1014                            qPos.add(articleIds, 2);
1015    
1016                            if ((version != null) && (version > 0)) {
1017                                    qPos.add(version);
1018                            }
1019    
1020                            qPos.add(titles, 2);
1021                            qPos.add(descriptions, 2);
1022                            qPos.add(contents, 2);
1023                            qPos.add(displayDateGT_TS);
1024                            qPos.add(displayDateGT_TS);
1025                            qPos.add(displayDateLT_TS);
1026                            qPos.add(displayDateLT_TS);
1027                            qPos.add(reviewDate_TS);
1028                            qPos.add(reviewDate_TS);
1029    
1030                            if (Validator.isNotNull(type)) {
1031                                    qPos.add(type);
1032                                    qPos.add(type);
1033                            }
1034    
1035                            if (!isNullArray(ddmStructureKeys)) {
1036                                    qPos.add(ddmStructureKeys, 2);
1037                            }
1038    
1039                            if (!isNullArray(ddmTemplateKeys)) {
1040                                    qPos.add(ddmTemplateKeys, 2);
1041                            }
1042    
1043                            qPos.add(companyId);
1044    
1045                            Iterator<Long> itr = q.iterate();
1046    
1047                            if (itr.hasNext()) {
1048                                    Long count = itr.next();
1049    
1050                                    if (count != null) {
1051                                            return count.intValue();
1052                                    }
1053                            }
1054    
1055                            return 0;
1056                    }
1057                    catch (Exception e) {
1058                            throw new SystemException(e);
1059                    }
1060                    finally {
1061                            closeSession(session);
1062                    }
1063            }
1064    
1065            protected List<JournalArticle> doFindByG_F(
1066                            long groupId, List<Long> folderIds, QueryDefinition queryDefinition,
1067                            boolean inlineSQLHelper)
1068                    throws SystemException {
1069    
1070                    Session session = null;
1071    
1072                    try {
1073                            session = openSession();
1074    
1075                            String sql = CustomSQLUtil.get(
1076                                    FIND_BY_G_F, queryDefinition, "JournalArticle");
1077    
1078                            sql = CustomSQLUtil.replaceOrderBy(
1079                                    sql, queryDefinition.getOrderByComparator());
1080    
1081                            if (inlineSQLHelper) {
1082                                    sql = InlineSQLHelperUtil.replacePermissionCheck(
1083                                            sql, JournalArticle.class.getName(),
1084                                            "JournalArticle.resourcePrimKey", groupId);
1085                            }
1086    
1087                            sql = StringUtil.replace(
1088                                    sql, "[$FOLDER_ID$]",
1089                                    getFolderIds(folderIds, JournalArticleImpl.TABLE_NAME));
1090    
1091                            SQLQuery q = session.createSQLQuery(sql);
1092    
1093                            q.addEntity(
1094                                    JournalArticleImpl.TABLE_NAME, JournalArticleImpl.class);
1095    
1096                            QueryPos qPos = QueryPos.getInstance(q);
1097    
1098                            qPos.add(groupId);
1099                            qPos.add(queryDefinition.getStatus());
1100    
1101                            for (int i = 0; i < folderIds.size(); i++) {
1102                                    Long folderId = folderIds.get(i);
1103    
1104                                    qPos.add(folderId);
1105                            }
1106    
1107                            return (List<JournalArticle>)QueryUtil.list(
1108                                    q, getDialect(), queryDefinition.getStart(),
1109                                    queryDefinition.getEnd());
1110                    }
1111                    catch (Exception e) {
1112                            throw new SystemException(e);
1113                    }
1114                    finally {
1115                            closeSession(session);
1116                    }
1117            }
1118    
1119            protected List<JournalArticle> doFindByG_C_S(
1120                            long groupId, long classNameId, String ddmStructureKey,
1121                            QueryDefinition queryDefinition, boolean inlineSQLHelper)
1122                    throws SystemException {
1123    
1124                    Session session = null;
1125    
1126                    try {
1127                            session = openSession();
1128    
1129                            String sql = CustomSQLUtil.get(
1130                                    FIND_BY_G_C_S, queryDefinition, "JournalArticle");
1131    
1132                            sql = CustomSQLUtil.replaceOrderBy(
1133                                    sql, queryDefinition.getOrderByComparator());
1134    
1135                            if (ddmStructureKey.equals(
1136                                            String.valueOf(
1137                                                    JournalArticleConstants.CLASSNAME_ID_DEFAULT))) {
1138    
1139                                    sql = StringUtil.replace(
1140                                            sql, "(JournalArticle.structureId = ?)",
1141                                            "((JournalArticle.structureId = ?) OR " +
1142                                                    "(JournalArticle.structureId = '') OR" +
1143                                                            "(JournalArticle.structureId IS NULL))");
1144                            }
1145    
1146                            if (inlineSQLHelper) {
1147                                    sql = InlineSQLHelperUtil.replacePermissionCheck(
1148                                            sql, JournalArticle.class.getName(),
1149                                            "JournalArticle.resourcePrimKey", groupId);
1150                            }
1151    
1152                            SQLQuery q = session.createSQLQuery(sql);
1153    
1154                            q.addEntity(
1155                                    JournalArticleImpl.TABLE_NAME, JournalArticleImpl.class);
1156    
1157                            QueryPos qPos = QueryPos.getInstance(q);
1158    
1159                            qPos.add(groupId);
1160                            qPos.add(classNameId);
1161                            qPos.add(ddmStructureKey);
1162                            qPos.add(queryDefinition.getStatus());
1163    
1164                            return (List<JournalArticle>)QueryUtil.list(
1165                                    q, getDialect(), queryDefinition.getStart(),
1166                                    queryDefinition.getEnd());
1167                    }
1168                    catch (Exception e) {
1169                            throw new SystemException(e);
1170                    }
1171                    finally {
1172                            closeSession(session);
1173                    }
1174            }
1175    
1176            protected List<JournalArticle> doFindByG_U_F_C(
1177                            long groupId, long userId, List<Long> folderIds, long classNameId,
1178                            QueryDefinition queryDefinition, boolean inlineSQLHelper)
1179                    throws SystemException {
1180    
1181                    Session session = null;
1182    
1183                    try {
1184                            session = openSession();
1185    
1186                            String sql = CustomSQLUtil.get(
1187                                    FIND_BY_G_U_F_C, queryDefinition, "JournalArticle");
1188    
1189                            sql = CustomSQLUtil.replaceOrderBy(
1190                                    sql, queryDefinition.getOrderByComparator());
1191    
1192                            if (folderIds.isEmpty()) {
1193                                    sql = StringUtil.replace(
1194                                            sql, "([$FOLDER_ID$]) AND", StringPool.BLANK);
1195                            }
1196                            else {
1197                                    sql = StringUtil.replace(
1198                                            sql, "[$FOLDER_ID$]",
1199                                            getFolderIds(folderIds, JournalArticleImpl.TABLE_NAME));
1200                            }
1201    
1202                            if (inlineSQLHelper) {
1203                                    sql = InlineSQLHelperUtil.replacePermissionCheck(
1204                                            sql, JournalArticle.class.getName(),
1205                                            "JournalArticle.resourcePrimKey", groupId);
1206                            }
1207    
1208                            SQLQuery q = session.createSQLQuery(sql);
1209    
1210                            q.addEntity(
1211                                    JournalArticleImpl.TABLE_NAME, JournalArticleImpl.class);
1212    
1213                            QueryPos qPos = QueryPos.getInstance(q);
1214    
1215                            qPos.add(groupId);
1216                            qPos.add(classNameId);
1217                            qPos.add(userId);
1218    
1219                            for (long folderId : folderIds) {
1220                                    qPos.add(folderId);
1221                            }
1222    
1223                            qPos.add(queryDefinition.getStatus());
1224    
1225                            return (List<JournalArticle>)QueryUtil.list(
1226                                    q, getDialect(), queryDefinition.getStart(),
1227                                    queryDefinition.getEnd());
1228                    }
1229                    catch (Exception e) {
1230                            throw new SystemException(e);
1231                    }
1232                    finally {
1233                            closeSession(session);
1234                    }
1235            }
1236    
1237            protected List<JournalArticle> doFindByC_G_F_C_A_V_T_D_C_T_S_T_D_R(
1238                            long companyId, long groupId, List<Long> folderIds,
1239                            long classNameId, String[] articleIds, Double version,
1240                            String[] titles, String[] descriptions, String[] contents,
1241                            String type, String[] ddmStructureKeys, String[] ddmTemplateKeys,
1242                            Date displayDateGT, Date displayDateLT, Date reviewDate,
1243                            boolean andOperator, QueryDefinition queryDefinition,
1244                            boolean inlineSQLHelper)
1245                    throws SystemException {
1246    
1247                    articleIds = CustomSQLUtil.keywords(articleIds, false);
1248                    titles = CustomSQLUtil.keywords(titles);
1249                    descriptions = CustomSQLUtil.keywords(descriptions, false);
1250                    contents = CustomSQLUtil.keywords(contents, false);
1251                    ddmStructureKeys = CustomSQLUtil.keywords(ddmStructureKeys, false);
1252                    ddmTemplateKeys = CustomSQLUtil.keywords(ddmTemplateKeys, false);
1253                    Timestamp displayDateGT_TS = CalendarUtil.getTimestamp(displayDateGT);
1254                    Timestamp displayDateLT_TS = CalendarUtil.getTimestamp(displayDateLT);
1255                    Timestamp reviewDate_TS = CalendarUtil.getTimestamp(reviewDate);
1256    
1257                    Session session = null;
1258    
1259                    try {
1260                            session = openSession();
1261    
1262                            String sql = CustomSQLUtil.get(
1263                                    FIND_BY_C_G_F_C_A_V_T_D_C_T_S_T_D_R, queryDefinition,
1264                                    "JournalArticle");
1265    
1266                            if (groupId <= 0) {
1267                                    sql = StringUtil.replace(
1268                                            sql, "(JournalArticle.groupId = ?) AND", StringPool.BLANK);
1269                            }
1270    
1271                            if (folderIds.isEmpty()) {
1272                                    sql = StringUtil.replace(
1273                                            sql, "([$FOLDER_ID$]) AND", StringPool.BLANK);
1274                            }
1275                            else {
1276                                    sql = StringUtil.replace(
1277                                            sql, "[$FOLDER_ID$]",
1278                                            getFolderIds(folderIds, JournalArticleImpl.TABLE_NAME));
1279                            }
1280    
1281                            sql = CustomSQLUtil.replaceKeywords(
1282                                    sql, "JournalArticle.articleId", StringPool.LIKE, false,
1283                                    articleIds);
1284    
1285                            if ((version == null) || (version <= 0)) {
1286                                    sql = StringUtil.replace(
1287                                            sql, "(JournalArticle.version = ?) [$AND_OR_CONNECTOR$]",
1288                                            StringPool.BLANK);
1289                            }
1290    
1291                            sql = CustomSQLUtil.replaceKeywords(
1292                                    sql, "lower(JournalArticle.title)", StringPool.LIKE, false,
1293                                    titles);
1294                            sql = CustomSQLUtil.replaceKeywords(
1295                                    sql, "JournalArticle.description", StringPool.LIKE, false,
1296                                    descriptions);
1297                            sql = CustomSQLUtil.replaceKeywords(
1298                                    sql, "JournalArticle.content", StringPool.LIKE, false,
1299                                    contents);
1300    
1301                            if (Validator.isNull(type)) {
1302                                    sql = StringUtil.replace(
1303                                            sql, _TYPE_NAMESPACED_SQL, StringPool.BLANK);
1304                            }
1305    
1306                            if (isNullArray(ddmStructureKeys)) {
1307                                    sql = StringUtil.replace(
1308                                            sql, _STRUCTURE_ID_NAMESPACED_SQL, StringPool.BLANK);
1309                            }
1310                            else {
1311                                    sql = CustomSQLUtil.replaceKeywords(
1312                                            sql, "JournalArticle.structureId", StringPool.LIKE, false,
1313                                            ddmStructureKeys);
1314                            }
1315    
1316                            if (isNullArray(ddmTemplateKeys)) {
1317                                    sql = StringUtil.replace(
1318                                            sql, _TEMPLATE_ID_NAMESPACED_SQL, StringPool.BLANK);
1319                            }
1320                            else {
1321                                    sql = CustomSQLUtil.replaceKeywords(
1322                                            sql, "JournalArticle.templateId", StringPool.LIKE, false,
1323                                            ddmTemplateKeys);
1324                            }
1325    
1326                            sql = CustomSQLUtil.replaceAndOperator(sql, andOperator);
1327                            sql = CustomSQLUtil.replaceOrderBy(
1328                                    sql, queryDefinition.getOrderByComparator());
1329    
1330                            if (inlineSQLHelper) {
1331                                    sql = InlineSQLHelperUtil.replacePermissionCheck(
1332                                            sql, JournalArticle.class.getName(),
1333                                            "JournalArticle.resourcePrimKey", groupId);
1334    
1335                                    sql = StringUtil.replace(
1336                                            sql, "(companyId", "(JournalArticle.companyId");
1337                            }
1338    
1339                            SQLQuery q = session.createSQLQuery(sql);
1340    
1341                            q.addEntity(
1342                                    JournalArticleImpl.TABLE_NAME, JournalArticleImpl.class);
1343    
1344                            QueryPos qPos = QueryPos.getInstance(q);
1345    
1346                            qPos.add(companyId);
1347    
1348                            if (groupId > 0) {
1349                                    qPos.add(groupId);
1350                            }
1351    
1352                            for (long folderId : folderIds) {
1353                                    qPos.add(folderId);
1354                            }
1355    
1356                            qPos.add(classNameId);
1357                            qPos.add(queryDefinition.getStatus());
1358                            qPos.add(articleIds, 2);
1359    
1360                            if ((version != null) && (version > 0)) {
1361                                    qPos.add(version);
1362                            }
1363    
1364                            qPos.add(titles, 2);
1365                            qPos.add(descriptions, 2);
1366                            qPos.add(contents, 2);
1367                            qPos.add(displayDateGT_TS);
1368                            qPos.add(displayDateGT_TS);
1369                            qPos.add(displayDateLT_TS);
1370                            qPos.add(displayDateLT_TS);
1371                            qPos.add(reviewDate_TS);
1372                            qPos.add(reviewDate_TS);
1373    
1374                            if (Validator.isNotNull(type)) {
1375                                    qPos.add(type);
1376                                    qPos.add(type);
1377                            }
1378    
1379                            if (!isNullArray(ddmStructureKeys)) {
1380                                    qPos.add(ddmStructureKeys, 2);
1381                            }
1382    
1383                            if (!isNullArray(ddmTemplateKeys)) {
1384                                    qPos.add(ddmTemplateKeys, 2);
1385                            }
1386    
1387                            return (List<JournalArticle>)QueryUtil.list(
1388                                    q, getDialect(), queryDefinition.getStart(),
1389                                    queryDefinition.getEnd());
1390                    }
1391                    catch (Exception e) {
1392                            throw new SystemException(e);
1393                    }
1394                    finally {
1395                            closeSession(session);
1396                    }
1397            }
1398    
1399            protected String getFolderIds(List<Long> folderIds, String tableName) {
1400                    if (folderIds.isEmpty()) {
1401                            return StringPool.BLANK;
1402                    }
1403    
1404                    StringBundler sb = new StringBundler(folderIds.size() * 3 + 1);
1405    
1406                    sb.append(StringPool.OPEN_PARENTHESIS);
1407    
1408                    for (int i = 0; i < folderIds.size(); i++) {
1409                            sb.append(tableName);
1410                            sb.append(".folderId = ? ");
1411    
1412                            if ((i + 1) != folderIds.size()) {
1413                                    sb.append(WHERE_OR);
1414                            }
1415                    }
1416    
1417                    sb.append(StringPool.CLOSE_PARENTHESIS);
1418    
1419                    return sb.toString();
1420            }
1421    
1422            protected JournalArticle getLatestArticle(
1423                            long groupId, String articleId, int status)
1424                    throws SystemException {
1425    
1426                    List<JournalArticle> articles = null;
1427    
1428                    if (status == WorkflowConstants.STATUS_ANY) {
1429                            articles = JournalArticleUtil.findByG_A(groupId, articleId, 0, 1);
1430                    }
1431                    else {
1432                            articles = JournalArticleUtil.findByG_A_ST(
1433                                    groupId, articleId, status, 0, 1);
1434                    }
1435    
1436                    if (articles.isEmpty()) {
1437                            return null;
1438                    }
1439    
1440                    return articles.get(0);
1441            }
1442    
1443            protected boolean isNullArray(Object[] array) {
1444                    if ((array == null) || (array.length == 0)) {
1445                            return true;
1446                    }
1447    
1448                    for (Object obj : array) {
1449                            if (Validator.isNotNull(obj)) {
1450                                    return false;
1451                            }
1452                    }
1453    
1454                    return true;
1455            }
1456    
1457            private static final String _STRUCTURE_ID_NAMESPACED_SQL =
1458                    "(JournalArticle.structureId LIKE ? [$AND_OR_NULL_CHECK$]) " +
1459                            "[$AND_OR_CONNECTOR$]";
1460    
1461            private static final String _STRUCTURE_ID_UNNAMESPACED_SQL =
1462                    "(structureId LIKE ? [$AND_OR_NULL_CHECK$]) [$AND_OR_CONNECTOR$]";
1463    
1464            private static final String _TEMPLATE_ID_NAMESPACED_SQL =
1465                    "(JournalArticle.templateId LIKE ? [$AND_OR_NULL_CHECK$]) " +
1466                            "[$AND_OR_CONNECTOR$]";
1467    
1468            private static final String _TEMPLATE_ID_UNNAMESPACED_SQL =
1469                    "(templateId LIKE ? [$AND_OR_NULL_CHECK$]) [$AND_OR_CONNECTOR$]";
1470    
1471            private static final String _TYPE_NAMESPACED_SQL =
1472                    "(JournalArticle.type_ = ? [$AND_OR_NULL_CHECK$]) [$AND_OR_CONNECTOR$]";
1473    
1474            private static final String _TYPE_UNNAMESPACED_SQL =
1475                    "(type_ = ? [$AND_OR_NULL_CHECK$]) [$AND_OR_CONNECTOR$]";
1476    
1477    }