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