1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portlet.journal.service.persistence;
21  
22  import com.liferay.portal.SystemException;
23  import com.liferay.portal.kernel.dao.orm.QueryPos;
24  import com.liferay.portal.kernel.dao.orm.QueryUtil;
25  import com.liferay.portal.kernel.dao.orm.SQLQuery;
26  import com.liferay.portal.kernel.dao.orm.Session;
27  import com.liferay.portal.kernel.dao.orm.Type;
28  import com.liferay.portal.kernel.util.CalendarUtil;
29  import com.liferay.portal.kernel.util.OrderByComparator;
30  import com.liferay.portal.kernel.util.StringPool;
31  import com.liferay.portal.kernel.util.StringUtil;
32  import com.liferay.portal.kernel.util.Validator;
33  import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
34  import com.liferay.portlet.journal.model.JournalArticle;
35  import com.liferay.portlet.journal.model.impl.JournalArticleImpl;
36  import com.liferay.util.dao.orm.CustomSQLUtil;
37  
38  import java.sql.Timestamp;
39  
40  import java.util.Date;
41  import java.util.Iterator;
42  import java.util.List;
43  
44  /**
45   * <a href="JournalArticleFinderImpl.java.html"><b><i>View Source</i></b></a>
46   *
47   * @author Brian Wing Shun Chan
48   *
49   */
50  public class JournalArticleFinderImpl
51      extends BasePersistenceImpl implements JournalArticleFinder {
52  
53      public static String COUNT_BY_C_G_A_V_T_D_C_T_S_T_D_A_E_R =
54          JournalArticleFinder.class.getName() +
55              ".countByC_G_A_V_T_D_C_T_S_T_D_A_E_R";
56  
57      public static String FIND_BY_EXPIRATION_DATE =
58          JournalArticleFinder.class.getName() + ".findByExpirationDate";
59  
60      public static String FIND_BY_REVIEW_DATE =
61          JournalArticleFinder.class.getName() + ".findByReviewDate";
62  
63      public static String FIND_BY_C_G_A_V_T_D_C_T_S_T_D_A_E_R =
64          JournalArticleFinder.class.getName() +
65              ".findByC_G_A_V_T_D_C_T_S_T_D_A_E_R";
66  
67      public int countByKeywords(
68              long companyId, long groupId, String keywords, Double version,
69              String type, String structureId, String templateId,
70              Date displayDateGT, Date displayDateLT, Boolean approved,
71              Boolean expired, Date reviewDate)
72          throws SystemException {
73  
74          String[] articleIds = null;
75          String[] titles = null;
76          String[] descriptions = null;
77          String[] contents = null;
78          boolean andOperator = false;
79  
80          if (Validator.isNotNull(keywords)) {
81              articleIds = CustomSQLUtil.keywords(keywords, false);
82              titles = CustomSQLUtil.keywords(keywords);
83              descriptions = CustomSQLUtil.keywords(keywords, false);
84              contents = CustomSQLUtil.keywords(keywords, false);
85          }
86          else {
87              andOperator = true;
88          }
89  
90          return countByC_G_A_V_T_D_C_T_S_T_D_A_E_R(
91              companyId, groupId, articleIds, version, titles, descriptions,
92              contents, type, new String[] {structureId},
93              new String[] {templateId}, displayDateGT, displayDateLT, approved,
94              expired, reviewDate, andOperator);
95      }
96  
97      public int countByC_G_A_V_T_D_C_T_S_T_D_A_E_R(
98              long companyId, long groupId, String articleId, Double version,
99              String title, String description, String content, String type,
100             String structureId, String templateId, Date displayDateGT,
101             Date displayDateLT, Boolean approved, Boolean expired,
102             Date reviewDate, boolean andOperator)
103         throws SystemException {
104 
105         return countByC_G_A_V_T_D_C_T_S_T_D_A_E_R(
106             companyId, groupId, articleId, version, title, description,
107             content, type, new String[] {structureId},
108             new String[] {templateId}, displayDateGT, displayDateLT, approved,
109             expired, reviewDate, andOperator);
110     }
111 
112     public int countByC_G_A_V_T_D_C_T_S_T_D_A_E_R(
113             long companyId, long groupId, String articleId, Double version,
114             String title, String description, String content, String type,
115             String[] structureIds, String[] templateIds, Date displayDateGT,
116             Date displayDateLT, Boolean approved, Boolean expired,
117             Date reviewDate, boolean andOperator)
118         throws SystemException {
119 
120         return countByC_G_A_V_T_D_C_T_S_T_D_A_E_R(
121             companyId, groupId, new String[] {articleId}, version,
122             new String[] {title}, new String[] {description},
123             new String[] {content}, type, structureIds, templateIds,
124             displayDateGT, displayDateLT, approved, expired, reviewDate,
125             andOperator);
126     }
127 
128     public int countByC_G_A_V_T_D_C_T_S_T_D_A_E_R(
129             long companyId, long groupId, String[] articleIds, Double version,
130             String[] titles, String[] descriptions, String[] contents,
131             String type, String[] structureIds, String[] templateIds,
132             Date displayDateGT, Date displayDateLT, Boolean approved,
133             Boolean expired, Date reviewDate, boolean andOperator)
134         throws SystemException {
135 
136         articleIds = CustomSQLUtil.keywords(articleIds, false);
137         titles = CustomSQLUtil.keywords(titles);
138         descriptions = CustomSQLUtil.keywords(descriptions, false);
139         contents = CustomSQLUtil.keywords(contents, false);
140         structureIds = CustomSQLUtil.keywords(structureIds, false);
141         templateIds = CustomSQLUtil.keywords(templateIds, false);
142         Timestamp displayDateGT_TS = CalendarUtil.getTimestamp(displayDateGT);
143         Timestamp displayDateLT_TS = CalendarUtil.getTimestamp(displayDateLT);
144         Timestamp reviewDate_TS = CalendarUtil.getTimestamp(reviewDate);
145 
146         Session session = null;
147 
148         try {
149             session = openSession();
150 
151             String sql = CustomSQLUtil.get(
152                 COUNT_BY_C_G_A_V_T_D_C_T_S_T_D_A_E_R);
153 
154             if (groupId <= 0) {
155                 sql = StringUtil.replace(sql, "(groupId = ?) AND", "");
156             }
157 
158             sql = CustomSQLUtil.replaceKeywords(
159                 sql, "articleId", StringPool.LIKE, false, articleIds);
160 
161             if (version == null) {
162                 sql = StringUtil.replace(
163                     sql, "(version = ?) [$AND_OR_CONNECTOR$]", "");
164             }
165 
166             sql = CustomSQLUtil.replaceKeywords(
167                 sql, "lower(title)", StringPool.LIKE, false, titles);
168             sql = CustomSQLUtil.replaceKeywords(
169                 sql, "description", StringPool.LIKE, false, descriptions);
170             sql = CustomSQLUtil.replaceKeywords(
171                 sql, "content", StringPool.LIKE, false, contents);
172             sql = CustomSQLUtil.replaceKeywords(
173                 sql, "structureId", StringPool.EQUAL, false, structureIds);
174             sql = CustomSQLUtil.replaceKeywords(
175                 sql, "templateId", StringPool.EQUAL, false, templateIds);
176 
177             if (approved == null) {
178                 sql = StringUtil.replace(sql, "(approved = ?) AND", "");
179             }
180 
181             if (expired == null) {
182                 sql = StringUtil.replace(sql, "(expired = ?) AND", "");
183             }
184 
185             sql = CustomSQLUtil.replaceAndOperator(sql, andOperator);
186 
187             SQLQuery q = session.createSQLQuery(sql);
188 
189             q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
190 
191             QueryPos qPos = QueryPos.getInstance(q);
192 
193             qPos.add(companyId);
194 
195             if (groupId > 0) {
196                 qPos.add(groupId);
197             }
198 
199             qPos.add(articleIds, 2);
200 
201             if (version != null) {
202                 qPos.add(version);
203             }
204 
205             qPos.add(titles, 2);
206             qPos.add(descriptions, 2);
207             qPos.add(contents, 2);
208             qPos.add(type);
209             qPos.add(type);
210             qPos.add(structureIds, 2);
211             qPos.add(templateIds, 2);
212             qPos.add(displayDateGT_TS);
213             qPos.add(displayDateGT_TS);
214             qPos.add(displayDateLT_TS);
215             qPos.add(displayDateLT_TS);
216 
217             if (approved != null) {
218                 qPos.add(approved);
219             }
220 
221             if (expired != null) {
222                 qPos.add(expired);
223             }
224 
225             qPos.add(reviewDate_TS);
226             qPos.add(reviewDate_TS);
227 
228             Iterator<Long> itr = q.list().iterator();
229 
230             if (itr.hasNext()) {
231                 Long count = itr.next();
232 
233                 if (count != null) {
234                     return count.intValue();
235                 }
236             }
237 
238             return 0;
239         }
240         catch (Exception e) {
241             throw new SystemException(e);
242         }
243         finally {
244             closeSession(session);
245         }
246     }
247 
248     public List<JournalArticle> findByExpirationDate(
249             Boolean expired, Date expirationDateLT, Date expirationDateGT)
250         throws SystemException {
251 
252         Timestamp expirationDateLT_TS = CalendarUtil.getTimestamp(
253             expirationDateLT);
254         Timestamp expirationDateGT_TS = CalendarUtil.getTimestamp(
255             expirationDateGT);
256 
257         Session session = null;
258         try {
259             session = openSession();
260 
261             String sql = CustomSQLUtil.get(FIND_BY_EXPIRATION_DATE);
262 
263             if (expired == null) {
264                 sql = StringUtil.replace(sql, "(expired = ?) AND", "");
265             }
266 
267             SQLQuery q = session.createSQLQuery(sql);
268 
269             q.addEntity("JournalArticle", JournalArticleImpl.class);
270 
271             QueryPos qPos = QueryPos.getInstance(q);
272 
273             if (expired != null) {
274                 qPos.add(expired);
275             }
276 
277             qPos.add(expirationDateGT_TS);
278             qPos.add(expirationDateLT_TS);
279 
280             return q.list();
281         }
282         catch (Exception e) {
283             throw new SystemException(e);
284         }
285         finally {
286             closeSession(session);
287         }
288     }
289 
290     public List<JournalArticle> findByKeywords(
291             long companyId, long groupId, String keywords, Double version,
292             String type, String structureId, String templateId,
293             Date displayDateGT, Date displayDateLT, Boolean approved,
294             Boolean expired, Date reviewDate, int start, int end,
295             OrderByComparator obc)
296         throws SystemException {
297 
298         String[] articleIds = null;
299         String[] titles = null;
300         String[] descriptions = null;
301         String[] contents = null;
302         boolean andOperator = false;
303 
304         if (Validator.isNotNull(keywords)) {
305             articleIds = CustomSQLUtil.keywords(keywords, false);
306             titles = CustomSQLUtil.keywords(keywords);
307             descriptions = CustomSQLUtil.keywords(keywords, false);
308             contents = CustomSQLUtil.keywords(keywords, false);
309         }
310         else {
311             andOperator = true;
312         }
313 
314         return findByC_G_A_V_T_D_C_T_S_T_D_A_E_R(
315             companyId, groupId, articleIds, version, titles, descriptions,
316             contents, type, new String[] {structureId},
317             new String[] {templateId}, displayDateGT, displayDateLT, approved,
318             expired, reviewDate, andOperator, start, end, obc);
319     }
320 
321     public List<JournalArticle> findByReviewDate(
322             Date reviewDateLT, Date reviewDateGT)
323         throws SystemException {
324 
325         Timestamp reviewDateLT_TS = CalendarUtil.getTimestamp(reviewDateLT);
326         Timestamp reviewDateGT_TS = CalendarUtil.getTimestamp(reviewDateGT);
327 
328         Session session = null;
329         try {
330             session = openSession();
331 
332             String sql = CustomSQLUtil.get(FIND_BY_REVIEW_DATE);
333 
334             SQLQuery q = session.createSQLQuery(sql);
335 
336             q.addEntity("JournalArticle", JournalArticleImpl.class);
337 
338             QueryPos qPos = QueryPos.getInstance(q);
339 
340             qPos.add(reviewDateGT_TS);
341             qPos.add(reviewDateLT_TS);
342 
343             return q.list();
344         }
345         catch (Exception e) {
346             throw new SystemException(e);
347         }
348         finally {
349             closeSession(session);
350         }
351     }
352 
353     public List<JournalArticle> findByC_G_A_V_T_D_C_T_S_T_D_A_E_R(
354             long companyId, long groupId, String articleId, Double version,
355             String title, String description, String content, String type,
356             String structureId, String templateId, Date displayDateGT,
357             Date displayDateLT, Boolean approved, Boolean expired,
358             Date reviewDate, boolean andOperator, int start, int end,
359             OrderByComparator obc)
360         throws SystemException {
361 
362         return findByC_G_A_V_T_D_C_T_S_T_D_A_E_R(
363             companyId, groupId, articleId, version, title, description,
364             content, type, new String[] {structureId},
365             new String[] {templateId}, displayDateGT, displayDateLT, approved,
366             expired, reviewDate, andOperator, start, end, obc);
367     }
368 
369     public List<JournalArticle> findByC_G_A_V_T_D_C_T_S_T_D_A_E_R(
370             long companyId, long groupId, String articleId, Double version,
371             String title, String description, String content, String type,
372             String[] structureIds, String[] templateIds, Date displayDateGT,
373             Date displayDateLT, Boolean approved, Boolean expired,
374             Date reviewDate, boolean andOperator, int start, int end,
375             OrderByComparator obc)
376         throws SystemException {
377 
378         return findByC_G_A_V_T_D_C_T_S_T_D_A_E_R(
379             companyId, groupId, new String[] {articleId}, version,
380             new String[] {title}, new String[] {description},
381             new String[] {content}, type, structureIds, templateIds,
382             displayDateGT, displayDateLT, approved, expired, reviewDate,
383             andOperator, start, end, obc);
384     }
385 
386     public List<JournalArticle> findByC_G_A_V_T_D_C_T_S_T_D_A_E_R(
387             long companyId, long groupId, String[] articleIds, Double version,
388             String[] titles, String[] descriptions, String[] contents,
389             String type, String[] structureIds, String[] templateIds,
390             Date displayDateGT, Date displayDateLT, Boolean approved,
391             Boolean expired, Date reviewDate, boolean andOperator, int start,
392             int end, OrderByComparator obc)
393         throws SystemException {
394 
395         articleIds = CustomSQLUtil.keywords(articleIds, false);
396         titles = CustomSQLUtil.keywords(titles);
397         descriptions = CustomSQLUtil.keywords(descriptions, false);
398         contents = CustomSQLUtil.keywords(contents, false);
399         structureIds = CustomSQLUtil.keywords(structureIds, false);
400         templateIds = CustomSQLUtil.keywords(templateIds, false);
401         Timestamp displayDateGT_TS = CalendarUtil.getTimestamp(displayDateGT);
402         Timestamp displayDateLT_TS = CalendarUtil.getTimestamp(displayDateLT);
403         Timestamp reviewDate_TS = CalendarUtil.getTimestamp(reviewDate);
404 
405         Session session = null;
406 
407         try {
408             session = openSession();
409 
410             String sql = CustomSQLUtil.get(FIND_BY_C_G_A_V_T_D_C_T_S_T_D_A_E_R);
411 
412             if (groupId <= 0) {
413                 sql = StringUtil.replace(sql, "(groupId = ?) AND", "");
414             }
415 
416             sql = CustomSQLUtil.replaceKeywords(
417                 sql, "articleId", StringPool.LIKE, false, articleIds);
418 
419             if (version == null) {
420                 sql = StringUtil.replace(
421                     sql, "(version = ?) [$AND_OR_CONNECTOR$]", "");
422             }
423 
424             sql = CustomSQLUtil.replaceKeywords(
425                 sql, "lower(title)", StringPool.LIKE, false, titles);
426             sql = CustomSQLUtil.replaceKeywords(
427                 sql, "description", StringPool.LIKE, false, descriptions);
428             sql = CustomSQLUtil.replaceKeywords(
429                 sql, "content", StringPool.LIKE, false, contents);
430             sql = CustomSQLUtil.replaceKeywords(
431                 sql, "structureId", StringPool.EQUAL, false, structureIds);
432             sql = CustomSQLUtil.replaceKeywords(
433                 sql, "templateId", StringPool.EQUAL, false, templateIds);
434 
435             if (approved == null) {
436                 sql = StringUtil.replace(sql, "(approved = ?) AND", "");
437             }
438 
439             if (expired == null) {
440                 sql = StringUtil.replace(sql, "(expired = ?) AND", "");
441             }
442 
443             sql = CustomSQLUtil.replaceAndOperator(sql, andOperator);
444             sql = CustomSQLUtil.replaceOrderBy(sql, obc);
445 
446             SQLQuery q = session.createSQLQuery(sql);
447 
448             q.addEntity("JournalArticle", JournalArticleImpl.class);
449 
450             QueryPos qPos = QueryPos.getInstance(q);
451 
452             qPos.add(companyId);
453 
454             if (groupId > 0) {
455                 qPos.add(groupId);
456             }
457 
458             qPos.add(articleIds, 2);
459 
460             if (version != null) {
461                 qPos.add(version);
462             }
463 
464             qPos.add(titles, 2);
465             qPos.add(descriptions, 2);
466             qPos.add(contents, 2);
467             qPos.add(type);
468             qPos.add(type);
469             qPos.add(structureIds, 2);
470             qPos.add(templateIds, 2);
471             qPos.add(displayDateGT_TS);
472             qPos.add(displayDateGT_TS);
473             qPos.add(displayDateLT_TS);
474             qPos.add(displayDateLT_TS);
475 
476             if (approved != null) {
477                 qPos.add(approved);
478             }
479 
480             if (expired != null) {
481                 qPos.add(expired);
482             }
483 
484             qPos.add(reviewDate_TS);
485             qPos.add(reviewDate_TS);
486 
487             return (List<JournalArticle>)QueryUtil.list(
488                 q, getDialect(), start, end);
489         }
490         catch (Exception e) {
491             throw new SystemException(e);
492         }
493         finally {
494             closeSession(session);
495         }
496     }
497 
498 }