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