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.JournalStructure;
29  import com.liferay.portlet.journal.model.impl.JournalStructureImpl;
30  import com.liferay.util.dao.orm.CustomSQLUtil;
31  
32  import java.util.Iterator;
33  import java.util.List;
34  
35  /**
36   * <a href="JournalStructureFinderImpl.java.html"><b><i>View Source</i></b></a>
37   *
38   * @author Brian Wing Shun Chan
39   */
40  public class JournalStructureFinderImpl
41      extends BasePersistenceImpl<JournalStructure>
42      implements JournalStructureFinder {
43  
44      public static String COUNT_BY_C_G_S_N_D =
45          JournalStructureFinder.class.getName() + ".countByC_G_S_N_D";
46  
47      public static String FIND_BY_C_G_S_N_D =
48          JournalStructureFinder.class.getName() + ".findByC_G_S_N_D";
49  
50      public int countByKeywords(long companyId, long groupId, String keywords)
51          throws SystemException {
52  
53          String[] structureIds = null;
54          String[] names = null;
55          String[] descriptions = null;
56          boolean andOperator = false;
57  
58          if (Validator.isNotNull(keywords)) {
59              structureIds = CustomSQLUtil.keywords(keywords, false);
60              names = CustomSQLUtil.keywords(keywords);
61              descriptions = CustomSQLUtil.keywords(keywords);
62          }
63          else {
64              andOperator = true;
65          }
66  
67          return countByC_G_S_N_D(
68              companyId, groupId, structureIds, names, descriptions, andOperator);
69      }
70  
71      public int countByC_G_S_N_D(
72              long companyId, long groupId, String structureId, String name,
73              String description, boolean andOperator)
74          throws SystemException {
75  
76          return countByC_G_S_N_D(
77              companyId, groupId, new String[] {structureId}, new String[] {name},
78              new String[] {description}, andOperator);
79      }
80  
81      public int countByC_G_S_N_D(
82              long companyId, long groupId, String[] structureIds, String[] names,
83              String[] descriptions, boolean andOperator)
84          throws SystemException {
85  
86          structureIds = CustomSQLUtil.keywords(structureIds, false);
87          names = CustomSQLUtil.keywords(names);
88          descriptions = CustomSQLUtil.keywords(descriptions);
89  
90          Session session = null;
91  
92          try {
93              session = openSession();
94  
95              String sql = CustomSQLUtil.get(COUNT_BY_C_G_S_N_D);
96  
97              if (groupId <= 0) {
98                  sql = StringUtil.replace(sql, "(groupId = ?) AND", "");
99              }
100 
101             sql = CustomSQLUtil.replaceKeywords(
102                 sql, "structureId", StringPool.LIKE, false, structureIds);
103             sql = CustomSQLUtil.replaceKeywords(
104                 sql, "lower(name)", StringPool.LIKE, false, names);
105             sql = CustomSQLUtil.replaceKeywords(
106                 sql, "lower(description)", StringPool.LIKE, true, descriptions);
107 
108             sql = CustomSQLUtil.replaceAndOperator(sql, andOperator);
109 
110             SQLQuery q = session.createSQLQuery(sql);
111 
112             q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
113 
114             QueryPos qPos = QueryPos.getInstance(q);
115 
116             qPos.add(companyId);
117 
118             if (groupId > 0) {
119                 qPos.add(groupId);
120             }
121 
122             qPos.add(structureIds, 2);
123             qPos.add(names, 2);
124             qPos.add(descriptions, 2);
125 
126             Iterator<Long> itr = q.list().iterator();
127 
128             if (itr.hasNext()) {
129                 Long count = itr.next();
130 
131                 if (count != null) {
132                     return count.intValue();
133                 }
134             }
135 
136             return 0;
137         }
138         catch (Exception e) {
139             throw new SystemException(e);
140         }
141         finally {
142             closeSession(session);
143         }
144     }
145 
146     public List<JournalStructure> findByKeywords(
147             long companyId, long groupId, String keywords, int start, int end,
148             OrderByComparator obc)
149         throws SystemException {
150 
151         String[] structureIds = null;
152         String[] names = null;
153         String[] descriptions = null;
154         boolean andOperator = false;
155 
156         if (Validator.isNotNull(keywords)) {
157             structureIds = CustomSQLUtil.keywords(keywords, false);
158             names = CustomSQLUtil.keywords(keywords);
159             descriptions = CustomSQLUtil.keywords(keywords);
160         }
161         else {
162             andOperator = true;
163         }
164 
165         return findByC_G_S_N_D(
166             companyId, groupId, structureIds, names, descriptions, andOperator,
167             start, end, obc);
168     }
169 
170     public List<JournalStructure> findByC_G_S_N_D(
171             long companyId, long groupId, String structureId, String name,
172             String description, boolean andOperator, int start, int end,
173             OrderByComparator obc)
174         throws SystemException {
175 
176         return findByC_G_S_N_D(
177             companyId, groupId, new String[] {structureId}, new String[] {name},
178             new String[] {description}, andOperator, start, end, obc);
179     }
180 
181     public List<JournalStructure> findByC_G_S_N_D(
182             long companyId, long groupId, String[] structureIds, String[] names,
183             String[] descriptions, boolean andOperator, int start, int end,
184             OrderByComparator obc)
185         throws SystemException {
186 
187         structureIds = CustomSQLUtil.keywords(structureIds, false);
188         names = CustomSQLUtil.keywords(names);
189         descriptions = CustomSQLUtil.keywords(descriptions);
190 
191         Session session = null;
192 
193         try {
194             session = openSession();
195 
196             String sql = CustomSQLUtil.get(FIND_BY_C_G_S_N_D);
197 
198             if (groupId <= 0) {
199                 sql = StringUtil.replace(sql, "(groupId = ?) AND", "");
200             }
201 
202             sql = CustomSQLUtil.replaceKeywords(
203                 sql, "structureId", StringPool.LIKE, false, structureIds);
204             sql = CustomSQLUtil.replaceKeywords(
205                 sql, "lower(name)", StringPool.LIKE, false, names);
206             sql = CustomSQLUtil.replaceKeywords(
207                 sql, "lower(description)", StringPool.LIKE, true, descriptions);
208 
209             sql = CustomSQLUtil.replaceAndOperator(sql, andOperator);
210             sql = CustomSQLUtil.replaceOrderBy(sql, obc);
211 
212             SQLQuery q = session.createSQLQuery(sql);
213 
214             q.addEntity("JournalStructure", JournalStructureImpl.class);
215 
216             QueryPos qPos = QueryPos.getInstance(q);
217 
218             qPos.add(companyId);
219 
220             if (groupId > 0) {
221                 qPos.add(groupId);
222             }
223 
224             qPos.add(structureIds, 2);
225             qPos.add(names, 2);
226             qPos.add(descriptions, 2);
227 
228             return (List<JournalStructure>)QueryUtil.list(
229                 q, getDialect(), start, end);
230         }
231         catch (Exception e) {
232             throw new SystemException(e);
233         }
234         finally {
235             closeSession(session);
236         }
237     }
238 
239 }