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