1   /**
2    * Copyright (c) 2000-2008 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.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.OrderByComparator;
32  import com.liferay.portal.kernel.util.StringPool;
33  import com.liferay.portal.kernel.util.StringUtil;
34  import com.liferay.portal.kernel.util.Validator;
35  import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
36  import com.liferay.portlet.journal.model.JournalTemplate;
37  import com.liferay.portlet.journal.model.impl.JournalTemplateImpl;
38  import com.liferay.util.dao.orm.CustomSQLUtil;
39  
40  import java.util.Iterator;
41  import java.util.List;
42  
43  /**
44   * <a href="JournalTemplateFinderImpl.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 JournalTemplateFinderImpl
51      extends BasePersistenceImpl implements JournalTemplateFinder {
52  
53      public static String COUNT_BY_C_G_T_S_N_D =
54          JournalTemplateFinder.class.getName() + ".countByC_G_T_S_N_D";
55  
56      public static String FIND_BY_C_G_T_S_N_D =
57          JournalTemplateFinder.class.getName() + ".findByC_G_T_S_N_D";
58  
59      public int countByKeywords(
60              long companyId, long groupId, String keywords, String structureId,
61              String structureIdComparator)
62          throws SystemException {
63  
64          String[] templateIds = null;
65          String[] names = null;
66          String[] descriptions = null;
67          boolean andOperator = false;
68  
69          if (Validator.isNotNull(keywords)) {
70              templateIds = CustomSQLUtil.keywords(keywords, false);
71              names = CustomSQLUtil.keywords(keywords);
72              descriptions = CustomSQLUtil.keywords(keywords);
73          }
74          else {
75              andOperator = true;
76          }
77  
78          return countByC_G_T_S_N_D(
79              companyId, groupId, templateIds, structureId, structureIdComparator,
80              names, descriptions, andOperator);
81      }
82  
83      public int countByC_G_T_S_N_D(
84              long companyId, long groupId, String templateId, String structureId,
85              String structureIdComparator, String name, String description,
86              boolean andOperator)
87          throws SystemException {
88  
89          return countByC_G_T_S_N_D(
90              companyId, groupId, new String[] {templateId}, structureId,
91              structureIdComparator, new String[] {name},
92              new String[] {description}, andOperator);
93      }
94  
95      public int countByC_G_T_S_N_D(
96              long companyId, long groupId, String[] templateIds,
97              String structureId, String structureIdComparator, String[] names,
98              String[] descriptions, boolean andOperator)
99          throws SystemException {
100 
101         templateIds = CustomSQLUtil.keywords(templateIds, false);
102         names = CustomSQLUtil.keywords(names);
103         descriptions = CustomSQLUtil.keywords(descriptions);
104 
105         Session session = null;
106 
107         try {
108             session = openSession();
109 
110             String sql = CustomSQLUtil.get(COUNT_BY_C_G_T_S_N_D);
111 
112             if (groupId <= 0) {
113                 sql = StringUtil.replace(sql, "(groupId = ?) AND", "");
114             }
115 
116             sql = CustomSQLUtil.replaceKeywords(
117                 sql, "templateId", StringPool.LIKE, false, templateIds);
118 
119             if (structureIdComparator.equals(StringPool.NOT_EQUAL)) {
120                 String replaceWith =
121                     "structureId != ? AND structureId IS NOT NULL";
122 
123                 if (CustomSQLUtil.isVendorOracle()) {
124                     replaceWith = "structureId IS NOT NULL";
125                 }
126 
127                 sql = StringUtil.replace(
128                     sql, "structureId = ? [$AND_OR_NULL_CHECK$]", replaceWith);
129             }
130 
131             sql = CustomSQLUtil.replaceKeywords(
132                 sql, "lower(name)", StringPool.LIKE, false, names);
133             sql = CustomSQLUtil.replaceKeywords(
134                 sql, "lower(description)", StringPool.LIKE, true, descriptions);
135 
136             sql = CustomSQLUtil.replaceAndOperator(sql, andOperator);
137 
138             SQLQuery q = session.createSQLQuery(sql);
139 
140             q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
141 
142             QueryPos qPos = QueryPos.getInstance(q);
143 
144             qPos.add(companyId);
145 
146             if (groupId > 0) {
147                 qPos.add(groupId);
148             }
149 
150             qPos.add(templateIds, 2);
151 
152             if (structureIdComparator.equals(StringPool.NOT_EQUAL)) {
153                 if (CustomSQLUtil.isVendorOracle()) {
154                 }
155                 else {
156                     qPos.add(structureId);
157                 }
158             }
159             else {
160                 qPos.add(structureId);
161             }
162 
163             if (structureIdComparator.equals(StringPool.EQUAL)) {
164                 qPos.add(structureId);
165             }
166 
167             qPos.add(names, 2);
168             qPos.add(descriptions, 2);
169 
170             Iterator<Long> itr = q.list().iterator();
171 
172             if (itr.hasNext()) {
173                 Long count = itr.next();
174 
175                 if (count != null) {
176                     return count.intValue();
177                 }
178             }
179 
180             return 0;
181         }
182         catch (Exception e) {
183             throw new SystemException(e);
184         }
185         finally {
186             closeSession(session);
187         }
188     }
189 
190     public List<JournalTemplate> findByKeywords(
191             long companyId, long groupId, String keywords, String structureId,
192             String structureIdComparator, int start, int end,
193             OrderByComparator obc)
194         throws SystemException {
195 
196         String[] templateIds = null;
197         String[] names = null;
198         String[] descriptions = null;
199         boolean andOperator = false;
200 
201         if (Validator.isNotNull(keywords)) {
202             templateIds = CustomSQLUtil.keywords(keywords, false);
203             names = CustomSQLUtil.keywords(keywords);
204             descriptions = CustomSQLUtil.keywords(keywords);
205         }
206         else {
207             andOperator = true;
208         }
209 
210         return findByC_G_T_S_N_D(
211             companyId, groupId, templateIds, structureId, structureIdComparator,
212             names, descriptions, andOperator, start, end, obc);
213     }
214 
215     public List<JournalTemplate> findByC_G_T_S_N_D(
216             long companyId, long groupId, String templateId, String structureId,
217             String structureIdComparator, String name, String description,
218             boolean andOperator, int start, int end, OrderByComparator obc)
219         throws SystemException {
220 
221         return findByC_G_T_S_N_D(
222             companyId, groupId, new String[] {templateId}, structureId,
223             structureIdComparator, new String[] {name},
224             new String[] {description}, andOperator, start, end, obc);
225     }
226 
227     public List<JournalTemplate> findByC_G_T_S_N_D(
228             long companyId, long groupId, String[] templateIds,
229             String structureId, String structureIdComparator, String[] names,
230             String[] descriptions, boolean andOperator, int start, int end,
231             OrderByComparator obc)
232         throws SystemException {
233 
234         templateIds = CustomSQLUtil.keywords(templateIds, false);
235         names = CustomSQLUtil.keywords(names);
236         descriptions = CustomSQLUtil.keywords(descriptions);
237 
238         Session session = null;
239 
240         try {
241             session = openSession();
242 
243             String sql = CustomSQLUtil.get(FIND_BY_C_G_T_S_N_D);
244 
245             if (groupId <= 0) {
246                 sql = StringUtil.replace(sql, "(groupId = ?) AND", "");
247             }
248 
249             sql = CustomSQLUtil.replaceKeywords(
250                 sql, "templateId", StringPool.LIKE, false, templateIds);
251 
252             if (structureIdComparator.equals(StringPool.NOT_EQUAL)) {
253                 String replaceWith =
254                     "structureId != ? AND structureId IS NOT NULL";
255 
256                 if (CustomSQLUtil.isVendorOracle()) {
257                     replaceWith = "structureId IS NOT NULL";
258                 }
259 
260                 sql = StringUtil.replace(
261                     sql, "structureId = ? [$AND_OR_NULL_CHECK$]", replaceWith);
262             }
263 
264             sql = CustomSQLUtil.replaceKeywords(
265                 sql, "lower(name)", StringPool.LIKE, false, names);
266             sql = CustomSQLUtil.replaceKeywords(
267                 sql, "lower(description)", StringPool.LIKE, true, descriptions);
268 
269             sql = CustomSQLUtil.replaceAndOperator(sql, andOperator);
270             sql = CustomSQLUtil.replaceOrderBy(sql, obc);
271 
272             SQLQuery q = session.createSQLQuery(sql);
273 
274             q.addEntity("JournalTemplate", JournalTemplateImpl.class);
275 
276             QueryPos qPos = QueryPos.getInstance(q);
277 
278             qPos.add(companyId);
279 
280             if (groupId > 0) {
281                 qPos.add(groupId);
282             }
283 
284             qPos.add(templateIds, 2);
285 
286             if (structureIdComparator.equals(StringPool.NOT_EQUAL)) {
287                 if (CustomSQLUtil.isVendorOracle()) {
288                 }
289                 else {
290                     qPos.add(structureId);
291                 }
292             }
293             else {
294                 qPos.add(structureId);
295             }
296 
297             if (structureIdComparator.equals(StringPool.EQUAL)) {
298                 qPos.add(structureId);
299             }
300 
301             qPos.add(names, 2);
302             qPos.add(descriptions, 2);
303 
304             return (List<JournalTemplate>)QueryUtil.list(
305                 q, getDialect(), start, end);
306         }
307         catch (Exception e) {
308             throw new SystemException(e);
309         }
310         finally {
311             closeSession(session);
312         }
313     }
314 
315 }