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