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