001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.shopping.service.persistence.impl;
016    
017    import com.liferay.portal.kernel.dao.orm.QueryPos;
018    import com.liferay.portal.kernel.dao.orm.QueryUtil;
019    import com.liferay.portal.kernel.dao.orm.SQLQuery;
020    import com.liferay.portal.kernel.dao.orm.Session;
021    import com.liferay.portal.kernel.dao.orm.Type;
022    import com.liferay.portal.kernel.exception.SystemException;
023    import com.liferay.portal.kernel.util.ArrayUtil;
024    import com.liferay.portal.kernel.util.StringBundler;
025    import com.liferay.portal.kernel.util.StringPool;
026    import com.liferay.portal.kernel.util.StringUtil;
027    import com.liferay.portal.security.permission.InlineSQLHelperUtil;
028    import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
029    import com.liferay.portlet.shopping.model.ShoppingItem;
030    import com.liferay.portlet.shopping.model.impl.ShoppingItemImpl;
031    import com.liferay.portlet.shopping.service.persistence.ShoppingItemFinder;
032    import com.liferay.util.dao.orm.CustomSQLUtil;
033    
034    import java.util.Iterator;
035    import java.util.List;
036    
037    /**
038     * @author Brian Wing Shun Chan
039     */
040    public class ShoppingItemFinderImpl
041            extends BasePersistenceImpl<ShoppingItem> implements ShoppingItemFinder {
042    
043            public static final String COUNT_BY_G_C =
044                    ShoppingItemFinder.class.getName() + ".countByG_C";
045    
046            @Override
047            public int countByG_C(long groupId, List<Long> categoryIds) {
048                    return doCountByG_C(groupId, categoryIds, false);
049            }
050    
051            @Override
052            public int countByFeatured(long groupId, long[] categoryIds) {
053                    Session session = null;
054    
055                    try {
056                            session = openSession();
057    
058                            StringBundler query = new StringBundler();
059    
060                            query.append("SELECT COUNT(*) AS COUNT_VALUE FROM ShoppingItem ");
061                            query.append("WHERE ");
062                            query.append("ShoppingItem.groupId = ? AND (");
063    
064                            if (ArrayUtil.isNotEmpty(categoryIds)) {
065                                    query.append(StringPool.OPEN_PARENTHESIS);
066    
067                                    for (int i = 0; i < categoryIds.length; i++) {
068                                            query.append("ShoppingItem.categoryId = ? ");
069    
070                                            if ((i + 1) < categoryIds.length) {
071                                                    query.append("OR ");
072                                            }
073                                    }
074    
075                                    query.append(") AND ");
076                            }
077    
078                            query.append("ShoppingItem.featured = ? AND ");
079                            query.append("ShoppingItem.smallImage = ?");
080    
081                            SQLQuery q = session.createSynchronizedSQLQuery(query.toString());
082    
083                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
084    
085                            QueryPos qPos = QueryPos.getInstance(q);
086    
087                            qPos.add(groupId);
088    
089                            for (long categoryId : categoryIds) {
090                                    qPos.add(categoryId);
091                            }
092    
093                            qPos.add(true);
094                            qPos.add(true);
095    
096                            Iterator<Long> itr = q.iterate();
097    
098                            if (itr.hasNext()) {
099                                    Long count = itr.next();
100    
101                                    if (count != null) {
102                                            return count.intValue();
103                                    }
104                            }
105    
106                            return 0;
107                    }
108                    catch (Exception e) {
109                            throw new SystemException(e);
110                    }
111                    finally {
112                            closeSession(session);
113                    }
114            }
115    
116            @Override
117            public int countByKeywords(
118                    long groupId, long[] categoryIds, String keywords) {
119    
120                    Session session = null;
121    
122                    try {
123                            session = openSession();
124    
125                            StringBundler query = new StringBundler();
126    
127                            query.append("SELECT COUNT(*) AS COUNT_VALUE FROM ShoppingItem ");
128                            query.append("WHERE ");
129                            query.append("ShoppingItem.groupId = ? AND (");
130    
131                            if (ArrayUtil.isNotEmpty(categoryIds)) {
132                                    query.append(StringPool.OPEN_PARENTHESIS);
133    
134                                    for (int i = 0; i < categoryIds.length; i++) {
135                                            query.append("ShoppingItem.categoryId = ? ");
136    
137                                            if ((i + 1) < categoryIds.length) {
138                                                    query.append("OR ");
139                                            }
140                                    }
141    
142                                    query.append(") AND ");
143                            }
144    
145                            query.append("(ShoppingItem.name LIKE ? OR ");
146                            query.append("ShoppingItem.description LIKE ? OR ");
147                            query.append("ShoppingItem.properties LIKE ?))");
148    
149                            keywords = '%' + keywords + '%';
150    
151                            SQLQuery q = session.createSynchronizedSQLQuery(query.toString());
152    
153                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
154    
155                            QueryPos qPos = QueryPos.getInstance(q);
156    
157                            qPos.add(groupId);
158    
159                            for (long categoryId : categoryIds) {
160                                    qPos.add(categoryId);
161                            }
162    
163                            qPos.add(keywords);
164                            qPos.add(keywords);
165                            qPos.add(keywords);
166    
167                            Iterator<Long> itr = q.iterate();
168    
169                            if (itr.hasNext()) {
170                                    Long count = itr.next();
171    
172                                    if (count != null) {
173                                            return count.intValue();
174                                    }
175                            }
176    
177                            return 0;
178                    }
179                    catch (Exception e) {
180                            throw new SystemException(e);
181                    }
182                    finally {
183                            closeSession(session);
184                    }
185            }
186    
187            @Override
188            public int countBySale(long groupId, long[] categoryIds) {
189                    Session session = null;
190    
191                    try {
192                            session = openSession();
193    
194                            StringBundler query = new StringBundler();
195    
196                            query.append("SELECT COUNT(*) AS COUNT_VALUE FROM ShoppingItem ");
197                            query.append("WHERE ");
198                            query.append("ShoppingItem.groupId = ? AND (");
199    
200                            if (ArrayUtil.isNotEmpty(categoryIds)) {
201                                    query.append(StringPool.OPEN_PARENTHESIS);
202    
203                                    for (int i = 0; i < categoryIds.length; i++) {
204                                            query.append("ShoppingItem.categoryId = ? ");
205    
206                                            if ((i + 1) < categoryIds.length) {
207                                                    query.append("OR ");
208                                            }
209                                    }
210    
211                                    query.append(") AND ");
212                            }
213    
214                            query.append("ShoppingItem.sale = ? AND ");
215                            query.append("ShoppingItem.smallImage = ?");
216    
217                            SQLQuery q = session.createSynchronizedSQLQuery(query.toString());
218    
219                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
220    
221                            QueryPos qPos = QueryPos.getInstance(q);
222    
223                            qPos.add(groupId);
224    
225                            for (long categoryId : categoryIds) {
226                                    qPos.add(categoryId);
227                            }
228    
229                            qPos.add(true);
230                            qPos.add(true);
231    
232                            Iterator<Long> itr = q.iterate();
233    
234                            if (itr.hasNext()) {
235                                    Long count = itr.next();
236    
237                                    if (count != null) {
238                                            return count.intValue();
239                                    }
240                            }
241    
242                            return 0;
243                    }
244                    catch (Exception e) {
245                            throw new SystemException(e);
246                    }
247                    finally {
248                            closeSession(session);
249                    }
250            }
251    
252            @Override
253            public int filterCountByG_C(long groupId, List<Long> categoryIds) {
254                    return doCountByG_C(groupId, categoryIds, true);
255            }
256    
257            @Override
258            public List<ShoppingItem> findByFeatured(
259                    long groupId, long[] categoryIds, int numOfItems) {
260    
261                    int countByFeatured = countByFeatured(groupId, categoryIds);
262    
263                    Session session = null;
264    
265                    try {
266                            session = openSession();
267    
268                            StringBundler query = new StringBundler();
269    
270                            query.append("SELECT {ShoppingItem.*} FROM ShoppingItem ");
271                            query.append("WHERE ");
272                            query.append("ShoppingItem.groupId = ? AND (");
273    
274                            if (ArrayUtil.isNotEmpty(categoryIds)) {
275                                    query.append(StringPool.OPEN_PARENTHESIS);
276    
277                                    for (int i = 0; i < categoryIds.length; i++) {
278                                            query.append("ShoppingItem.categoryId = ? ");
279    
280                                            if ((i + 1) < categoryIds.length) {
281                                                    query.append("OR ");
282                                            }
283                                    }
284    
285                                    query.append(") AND ");
286                            }
287    
288                            query.append("ShoppingItem.featured = ? AND ");
289                            query.append("ShoppingItem.smallImage = ?");
290    
291                            SQLQuery q = session.createSynchronizedSQLQuery(query.toString());
292    
293                            q.addEntity("ShoppingItem", ShoppingItemImpl.class);
294    
295                            QueryPos qPos = QueryPos.getInstance(q);
296    
297                            qPos.add(groupId);
298    
299                            for (long categoryId : categoryIds) {
300                                    qPos.add(categoryId);
301                            }
302    
303                            qPos.add(true);
304                            qPos.add(true);
305    
306                            return (List<ShoppingItem>)QueryUtil.randomList(
307                                    q, getDialect(), countByFeatured, numOfItems);
308                    }
309                    catch (Exception e) {
310                            throw new SystemException(e);
311                    }
312                    finally {
313                            closeSession(session);
314                    }
315            }
316    
317            @Override
318            public List<ShoppingItem> findByKeywords(
319                    long groupId, long[] categoryIds, String keywords, int start, int end) {
320    
321                    Session session = null;
322    
323                    try {
324                            session = openSession();
325    
326                            StringBundler query = new StringBundler();
327    
328                            query.append("SELECT {ShoppingItem.*} FROM ShoppingItem ");
329                            query.append("WHERE ");
330                            query.append("ShoppingItem.groupId = ? AND (");
331    
332                            if (ArrayUtil.isNotEmpty(categoryIds)) {
333                                    query.append(StringPool.OPEN_PARENTHESIS);
334    
335                                    for (int i = 0; i < categoryIds.length; i++) {
336                                            query.append("ShoppingItem.categoryId = ? ");
337    
338                                            if ((i + 1) < categoryIds.length) {
339                                                    query.append("OR ");
340                                            }
341                                    }
342    
343                                    query.append(") AND ");
344                            }
345    
346                            query.append("(ShoppingItem.name LIKE ? OR ");
347                            query.append("ShoppingItem.description LIKE ? OR ");
348                            query.append("ShoppingItem.properties LIKE ?))");
349    
350                            keywords = '%' + keywords + '%';
351    
352                            SQLQuery q = session.createSynchronizedSQLQuery(query.toString());
353    
354                            q.addEntity("ShoppingItem", ShoppingItemImpl.class);
355    
356                            QueryPos qPos = QueryPos.getInstance(q);
357    
358                            qPos.add(groupId);
359    
360                            for (long categoryId : categoryIds) {
361                                    qPos.add(categoryId);
362                            }
363    
364                            qPos.add(keywords);
365                            qPos.add(keywords);
366                            qPos.add(keywords);
367    
368                            return (List<ShoppingItem>)QueryUtil.list(
369                                    q, getDialect(), start, end);
370                    }
371                    catch (Exception e) {
372                            throw new SystemException(e);
373                    }
374                    finally {
375                            closeSession(session);
376                    }
377            }
378    
379            @Override
380            public List<ShoppingItem> findBySale(
381                    long groupId, long[] categoryIds, int numOfItems) {
382    
383                    int countBySale = countBySale(groupId, categoryIds);
384    
385                    Session session = null;
386    
387                    try {
388                            session = openSession();
389    
390                            StringBundler query = new StringBundler();
391    
392                            query.append("SELECT {ShoppingItem.*} FROM ShoppingItem ");
393                            query.append("WHERE ");
394                            query.append("ShoppingItem.groupId = ? AND (");
395    
396                            if (ArrayUtil.isNotEmpty(categoryIds)) {
397                                    query.append(StringPool.OPEN_PARENTHESIS);
398    
399                                    for (int i = 0; i < categoryIds.length; i++) {
400                                            query.append("ShoppingItem.categoryId = ? ");
401    
402                                            if ((i + 1) < categoryIds.length) {
403                                                    query.append("OR ");
404                                            }
405                                    }
406    
407                                    query.append(") AND ");
408                            }
409    
410                            query.append("ShoppingItem.sale = ? AND ");
411                            query.append("ShoppingItem.smallImage = ?");
412    
413                            SQLQuery q = session.createSynchronizedSQLQuery(query.toString());
414    
415                            q.addEntity("ShoppingItem", ShoppingItemImpl.class);
416    
417                            QueryPos qPos = QueryPos.getInstance(q);
418    
419                            qPos.add(groupId);
420    
421                            for (long categoryId : categoryIds) {
422                                    qPos.add(categoryId);
423                            }
424    
425                            qPos.add(true);
426                            qPos.add(true);
427    
428                            return (List<ShoppingItem>)QueryUtil.randomList(
429                                    q, getDialect(), countBySale, numOfItems);
430                    }
431                    catch (Exception e) {
432                            throw new SystemException(e);
433                    }
434                    finally {
435                            closeSession(session);
436                    }
437            }
438    
439            protected int doCountByG_C(
440                    long groupId, List<Long> categoryIds, boolean inlineSQLHelper) {
441    
442                    Session session = null;
443    
444                    try {
445                            session = openSession();
446    
447                            String sql = CustomSQLUtil.get(COUNT_BY_G_C);
448    
449                            if (inlineSQLHelper) {
450                                    sql = InlineSQLHelperUtil.replacePermissionCheck(
451                                            sql, ShoppingItem.class.getName(), "ShoppingItem.itemId",
452                                            groupId);
453                            }
454    
455                            sql = StringUtil.replace(
456                                    sql, "[$CATEGORY_ID$]", getCategoryIds(categoryIds));
457    
458                            SQLQuery q = session.createSynchronizedSQLQuery(sql);
459    
460                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
461    
462                            QueryPos qPos = QueryPos.getInstance(q);
463    
464                            qPos.add(groupId);
465    
466                            for (int i = 0; i < categoryIds.size(); i++) {
467                                    Long categoryId = categoryIds.get(i);
468    
469                                    qPos.add(categoryId);
470                            }
471    
472                            Iterator<Long> itr = q.iterate();
473    
474                            if (itr.hasNext()) {
475                                    Long count = itr.next();
476    
477                                    if (count != null) {
478                                            return count.intValue();
479                                    }
480                            }
481    
482                            return 0;
483                    }
484                    catch (Exception e) {
485                            throw new SystemException(e);
486                    }
487                    finally {
488                            closeSession(session);
489                    }
490            }
491    
492            protected String getCategoryIds(List<Long> categoryIds) {
493                    if (categoryIds.isEmpty()) {
494                            return StringPool.BLANK;
495                    }
496    
497                    StringBundler sb = new StringBundler(categoryIds.size() * 2 - 1);
498    
499                    for (int i = 0; i < categoryIds.size(); i++) {
500                            sb.append("categoryId = ? ");
501    
502                            if ((i + 1) != categoryIds.size()) {
503                                    sb.append("OR ");
504                            }
505                    }
506    
507                    return sb.toString();
508            }
509    
510    }