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