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