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