1   /**
2    * Copyright (c) 2000-2007 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions 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.util.OrderByComparator;
27  import com.liferay.portal.kernel.util.StringPool;
28  import com.liferay.portal.kernel.util.StringUtil;
29  import com.liferay.portal.kernel.util.Validator;
30  import com.liferay.portal.spring.hibernate.CustomSQLUtil;
31  import com.liferay.portal.spring.hibernate.HibernateUtil;
32  import com.liferay.portlet.journal.model.impl.JournalTemplateImpl;
33  import com.liferay.util.dao.hibernate.QueryPos;
34  import com.liferay.util.dao.hibernate.QueryUtil;
35  
36  import java.util.Iterator;
37  import java.util.List;
38  
39  import org.hibernate.Hibernate;
40  import org.hibernate.SQLQuery;
41  import org.hibernate.Session;
42  
43  /**
44   * <a href="JournalTemplateFinder.java.html"><b><i>View Source</i></b></a>
45   *
46   * @author Brian Wing Shun Chan
47   * @author Bruno Farache
48   *
49   */
50  public class JournalTemplateFinder {
51  
52      public static String COUNT_BY_C_G_T_S_N_D =
53          JournalTemplateFinder.class.getName() + ".countByC_G_T_S_N_D";
54  
55      public static String FIND_BY_C_G_T_S_N_D =
56          JournalTemplateFinder.class.getName() + ".findByC_G_T_S_N_D";
57  
58      public static int countByKeywords(
59              long companyId, long groupId, String keywords, String structureId,
60              String structureIdComparator)
61          throws SystemException {
62  
63          String[] templateIds = null;
64          String[] names = null;
65          String[] descriptions = null;
66          boolean andOperator = false;
67  
68          if (Validator.isNotNull(keywords)) {
69              templateIds = CustomSQLUtil.keywords(keywords, false);
70              names = CustomSQLUtil.keywords(keywords);
71              descriptions = CustomSQLUtil.keywords(keywords);
72          }
73          else {
74              andOperator = true;
75          }
76  
77          return countByC_G_T_S_N_D(
78              companyId, groupId, templateIds, structureId, structureIdComparator,
79              names, descriptions, andOperator);
80      }
81  
82      public static int countByC_G_T_S_N_D(
83              long companyId, long groupId, String templateId, String structureId,
84              String structureIdComparator, String name, String description,
85              boolean andOperator)
86          throws SystemException {
87  
88          return countByC_G_T_S_N_D(
89              companyId, groupId, new String[] {templateId}, structureId,
90              structureIdComparator, new String[] {name},
91              new String[] {description}, andOperator);
92      }
93  
94      public static int countByC_G_T_S_N_D(
95              long companyId, long groupId, String[] templateIds,
96              String structureId, String structureIdComparator, String[] names,
97              String[] descriptions, boolean andOperator)
98          throws SystemException {
99  
100         templateIds = CustomSQLUtil.keywords(templateIds, false);
101         names = CustomSQLUtil.keywords(names);
102         descriptions = CustomSQLUtil.keywords(descriptions);
103 
104         Session session = null;
105 
106         try {
107             session = HibernateUtil.openSession();
108 
109             String sql = CustomSQLUtil.get(COUNT_BY_C_G_T_S_N_D);
110 
111             if (groupId <= 0) {
112                 sql = StringUtil.replace(sql, "(groupId = ?) AND", "");
113             }
114 
115             sql = CustomSQLUtil.replaceKeywords(
116                 sql, "templateId", StringPool.LIKE, false, templateIds);
117 
118             if (structureIdComparator.equals(StringPool.NOT_EQUAL)) {
119                 String replaceWith =
120                     "structureId != ? AND structureId IS NOT NULL";
121 
122                 if (CustomSQLUtil.isVendorOracle()) {
123                     replaceWith = "structureId IS NOT NULL";
124                 }
125 
126                 sql = StringUtil.replace(
127                     sql, "structureId = ? [$AND_OR_NULL_CHECK$]", replaceWith);
128             }
129 
130             sql = CustomSQLUtil.replaceKeywords(
131                 sql, "lower(name)", StringPool.LIKE, false, names);
132             sql = CustomSQLUtil.replaceKeywords(
133                 sql, "lower(description)", StringPool.LIKE, true, descriptions);
134 
135             sql = CustomSQLUtil.replaceAndOperator(sql, andOperator);
136 
137             SQLQuery q = session.createSQLQuery(sql);
138 
139             q.addScalar(HibernateUtil.getCountColumnName(), Hibernate.LONG);
140 
141             QueryPos qPos = QueryPos.getInstance(q);
142 
143             qPos.add(companyId);
144 
145             if (groupId > 0) {
146                 qPos.add(groupId);
147             }
148 
149             qPos.add(templateIds, 2);
150 
151             if (structureIdComparator.equals(StringPool.NOT_EQUAL)) {
152                 if (CustomSQLUtil.isVendorOracle()) {
153                 }
154                 else {
155                     qPos.add(structureId);
156                 }
157             }
158             else {
159                 qPos.add(structureId);
160             }
161 
162             if (structureIdComparator.equals(StringPool.EQUAL)) {
163                 qPos.add(structureId);
164             }
165 
166             qPos.add(names, 2);
167             qPos.add(descriptions, 2);
168 
169             Iterator itr = q.list().iterator();
170 
171             if (itr.hasNext()) {
172                 Long count = (Long)itr.next();
173 
174                 if (count != null) {
175                     return count.intValue();
176                 }
177             }
178 
179             return 0;
180         }
181         catch (Exception e) {
182             throw new SystemException(e);
183         }
184         finally {
185             HibernateUtil.closeSession(session);
186         }
187     }
188 
189     public static List findByKeywords(
190             long companyId, long groupId, String keywords, String structureId,
191             String structureIdComparator, int begin, int end,
192             OrderByComparator obc)
193         throws SystemException {
194 
195         String[] templateIds = null;
196         String[] names = null;
197         String[] descriptions = null;
198         boolean andOperator = false;
199 
200         if (Validator.isNotNull(keywords)) {
201             templateIds = CustomSQLUtil.keywords(keywords, false);
202             names = CustomSQLUtil.keywords(keywords);
203             descriptions = CustomSQLUtil.keywords(keywords);
204         }
205         else {
206             andOperator = true;
207         }
208 
209         return findByC_G_T_S_N_D(
210             companyId, groupId, templateIds, structureId, structureIdComparator,
211             names, descriptions, andOperator, begin, end, obc);
212     }
213 
214     public static List findByC_G_T_S_N_D(
215             long companyId, long groupId, String templateId, String structureId,
216             String structureIdComparator, String name, String description,
217             boolean andOperator, int begin, int end, OrderByComparator obc)
218         throws SystemException {
219 
220         return findByC_G_T_S_N_D(
221             companyId, groupId, new String[] {templateId}, structureId,
222             structureIdComparator, new String[] {name},
223             new String[] {description}, andOperator, begin, end, obc);
224     }
225 
226     public static List findByC_G_T_S_N_D(
227             long companyId, long groupId, String[] templateIds,
228             String structureId, String structureIdComparator, String[] names,
229             String[] descriptions, boolean andOperator, int begin, int end,
230             OrderByComparator obc)
231         throws SystemException {
232 
233         templateIds = CustomSQLUtil.keywords(templateIds, false);
234         names = CustomSQLUtil.keywords(names);
235         descriptions = CustomSQLUtil.keywords(descriptions);
236 
237         Session session = null;
238 
239         try {
240             session = HibernateUtil.openSession();
241 
242             String sql = CustomSQLUtil.get(FIND_BY_C_G_T_S_N_D);
243 
244             if (groupId <= 0) {
245                 sql = StringUtil.replace(sql, "(groupId = ?) AND", "");
246             }
247 
248             sql = CustomSQLUtil.replaceKeywords(
249                 sql, "templateId", StringPool.LIKE, false, templateIds);
250 
251             if (structureIdComparator.equals(StringPool.NOT_EQUAL)) {
252                 String replaceWith =
253                     "structureId != ? AND structureId IS NOT NULL";
254 
255                 if (CustomSQLUtil.isVendorOracle()) {
256                     replaceWith = "structureId IS NOT NULL";
257                 }
258 
259                 sql = StringUtil.replace(
260                     sql, "structureId = ? [$AND_OR_NULL_CHECK$]", replaceWith);
261             }
262 
263             sql = CustomSQLUtil.replaceKeywords(
264                 sql, "lower(name)", StringPool.LIKE, false, names);
265             sql = CustomSQLUtil.replaceKeywords(
266                 sql, "lower(description)", StringPool.LIKE, true, descriptions);
267 
268             sql = CustomSQLUtil.replaceAndOperator(sql, andOperator);
269             sql = CustomSQLUtil.replaceOrderBy(sql, obc);
270 
271             SQLQuery q = session.createSQLQuery(sql);
272 
273             q.addEntity("JournalTemplate", JournalTemplateImpl.class);
274 
275             QueryPos qPos = QueryPos.getInstance(q);
276 
277             qPos.add(companyId);
278 
279             if (groupId > 0) {
280                 qPos.add(groupId);
281             }
282 
283             qPos.add(templateIds, 2);
284 
285             if (structureIdComparator.equals(StringPool.NOT_EQUAL)) {
286                 if (CustomSQLUtil.isVendorOracle()) {
287                 }
288                 else {
289                     qPos.add(structureId);
290                 }
291             }
292             else {
293                 qPos.add(structureId);
294             }
295 
296             if (structureIdComparator.equals(StringPool.EQUAL)) {
297                 qPos.add(structureId);
298             }
299 
300             qPos.add(names, 2);
301             qPos.add(descriptions, 2);
302 
303             return QueryUtil.list(q, HibernateUtil.getDialect(), begin, end);
304         }
305         catch (Exception e) {
306             throw new SystemException(e);
307         }
308         finally {
309             HibernateUtil.closeSession(session);
310         }
311     }
312 
313 }