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